Skip to content

Commit

Permalink
build: add support for wasm for snrm2
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-095 committed Oct 22, 2024
1 parent 637273a commit db761b5
Show file tree
Hide file tree
Showing 7 changed files with 424 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var base64ToUint8Array = require( '@stdlib/string/base/base64-to-uint8array' );


// MAIN //

var wasm = base64ToUint8Array( 'AGFzbQEAAAAADwhkeWxpbmsuMAEEAAAAAAETA2AAAGADf39/AX1gBH9/f38BfQIPAQNlbnYGbWVtb3J5AgAAAwQDAAECB0wEEV9fd2FzbV9jYWxsX2N0b3JzAAAYX193YXNtX2FwcGx5X2RhdGFfcmVsb2NzAAAHY19zbnJtMgABD2Nfc25ybTJfbmRhcnJheQACCogDAwMAAQshAQF+IAAgASACIAKsIgNCASAArH1+QgAgA0IAVxunEAIL3wICBX0Df0EBIQkgAEEASgR9A0ACQCABIANBAnRqKgIAiyIHQwAAgFleBEAgBiAHQwAAgBmUIgYgBpSSIQZBACEJDAELIAdDAAAAIF0EQCAJIQpBACEJIApFDQEgBSAHQwAAAGWUIgUgBZSSIQVBASEJDAELIAQgByAHlJIhBAsgAiADaiEDIAtBAWoiCyAARw0ACwJAIAZDAAAAAF4EQCAEQwAAgBmUQwAAgBmUIAaSIgUgBiAEQ///f39eGyAFIARDAAAAAF8bIQRDAACAZSEGDAELQwAAgD8hBiAFQwAAAABeRQ0AAkAgBCAEXA0AIARDAAAAAF4NACAEQ///f39eDQAgBSEEQwAAABohBgwBCyAFkUMAAAAalCIFIASRIgQgBCAFXSIAGyIHIAeUIQggBCAFIAAbIAeVIgQgBJS7RAAAAAAAAPA/oCAIu6K2IQQLIAYgBJGUBUMAAAAACws=' );


// EXPORTS //

module.exports = wasm;
Binary file not shown.
220 changes: 220 additions & 0 deletions lib/node_modules/@stdlib/blas/base/snrm2-wasm/src/main.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
;; @license Apache-2.0
;;
;; Copyright (c) 2024 The Stdlib Authors.
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

(module
(type (;0;) (func))
(type (;1;) (func (param i32 i32 i32) (result f32)))
(type (;2;) (func (param i32 i32 i32 i32) (result f32)))
(import "env" "memory" (memory (;0;) 0))
(func (;0;) (type 0)
nop)
(func (;1;) (type 1) (param i32 i32 i32) (result f32)
(local i64)
local.get 0
local.get 1
local.get 2
local.get 2
i64.extend_i32_s
local.tee 3
i64.const 1
local.get 0
i64.extend_i32_s
i64.sub
i64.mul
i64.const 0
local.get 3
i64.const 0
i64.le_s
select
i32.wrap_i64
call 2)
(func (;2;) (type 2) (param i32 i32 i32 i32) (result f32)
(local f32 f32 f32 f32 f32 i32 i32 i32)
i32.const 1
local.set 9
local.get 0
i32.const 0
i32.gt_s
if (result f32) ;; label = @1
loop ;; label = @2
block ;; label = @3
local.get 1
local.get 3
i32.const 2
i32.shl
i32.add
f32.load
f32.abs
local.tee 7
f32.const 0x1p+52 (;=4.5036e+15;)
f32.gt
if ;; label = @4
local.get 6
local.get 7
f32.const 0x1p-76 (;=1.32349e-23;)
f32.mul
local.tee 6
local.get 6
f32.mul
f32.add
local.set 6
i32.const 0
local.set 9
br 1 (;@3;)
end
local.get 7
f32.const 0x1p-63 (;=1.0842e-19;)
f32.lt
if ;; label = @4
local.get 9
local.set 10
i32.const 0
local.set 9
local.get 10
i32.eqz
br_if 1 (;@3;)
local.get 5
local.get 7
f32.const 0x1p+75 (;=3.77789e+22;)
f32.mul
local.tee 5
local.get 5
f32.mul
f32.add
local.set 5
i32.const 1
local.set 9
br 1 (;@3;)
end
local.get 4
local.get 7
local.get 7
f32.mul
f32.add
local.set 4
end
local.get 2
local.get 3
i32.add
local.set 3
local.get 11
i32.const 1
i32.add
local.tee 11
local.get 0
i32.ne
br_if 0 (;@2;)
end
block ;; label = @2
local.get 6
f32.const 0x0p+0 (;=0;)
f32.gt
if ;; label = @3
local.get 4
f32.const 0x1p-76 (;=1.32349e-23;)
f32.mul
f32.const 0x1p-76 (;=1.32349e-23;)
f32.mul
local.get 6
f32.add
local.tee 5
local.get 6
local.get 4
f32.const 0x1.fffffep+127 (;=3.40282e+38;)
f32.gt
select
local.get 5
local.get 4
f32.const 0x0p+0 (;=0;)
f32.le
select
local.set 4
f32.const 0x1p+76 (;=7.55579e+22;)
local.set 6
br 1 (;@2;)
end
f32.const 0x1p+0 (;=1;)
local.set 6
local.get 5
f32.const 0x0p+0 (;=0;)
f32.gt
i32.eqz
br_if 0 (;@2;)
block ;; label = @3
local.get 4
local.get 4
f32.ne
br_if 0 (;@3;)
local.get 4
f32.const 0x0p+0 (;=0;)
f32.gt
br_if 0 (;@3;)
local.get 4
f32.const 0x1.fffffep+127 (;=3.40282e+38;)
f32.gt
br_if 0 (;@3;)
local.get 5
local.set 4
f32.const 0x1p-75 (;=2.64698e-23;)
local.set 6
br 1 (;@2;)
end
local.get 5
f32.sqrt
f32.const 0x1p-75 (;=2.64698e-23;)
f32.mul
local.tee 5
local.get 4
f32.sqrt
local.tee 4
local.get 4
local.get 5
f32.lt
local.tee 0
select
local.tee 7
local.get 7
f32.mul
local.set 8
local.get 4
local.get 5
local.get 0
select
local.get 7
f32.div
local.tee 4
local.get 4
f32.mul
f64.promote_f32
f64.const 0x1p+0 (;=1;)
f64.add
local.get 8
f64.promote_f32
f64.mul
f32.demote_f64
local.set 4
end
local.get 6
local.get 4
f32.sqrt
f32.mul
else
f32.const 0x0p+0 (;=0;)
end)
(export "__wasm_call_ctors" (func 0))
(export "__wasm_apply_data_relocs" (func 0))
(export "c_snrm2" (func 1))
(export "c_snrm2_ndarray" (func 2)))
70 changes: 50 additions & 20 deletions lib/node_modules/@stdlib/blas/base/snrm2-wasm/test/test.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,41 @@
// MODULES //

var tape = require( 'tape' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var Float32Array = require( '@stdlib/array/float32' );
var EPS = require( '@stdlib/constants/float32/eps' );
var abs = require( '@stdlib/math/base/special/abs' );
var snrm2 = require( './../lib' );


// FUNCTIONS //

/**
* Tests for element-wise approximate equality.
*
* @private
* @param {Object} t - test object
* @param {Collection} actual - actual values
* @param {Collection} expected - expected values
* @param {number} rtol - relative tolerance
*/
function isApprox( t, actual, expected, rtol ) {
var delta;
var tol;
var i;

t.strictEqual( actual.length, expected.length, 'returns expected value' );
for ( i = 0; i < expected.length; i++ ) {
if ( actual[ i ] === expected[ i ] ) {
t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' );
} else {
delta = abs( actual[ i ] - expected[ i ] );
tol = rtol * EPS * abs( expected[ i ] );
t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' );
}
}
}


// TESTS //

tape( 'main export is an object', function test( t ) {
Expand All @@ -40,25 +70,25 @@ tape( 'the `main` method has an arity of 3', function test( t ) {
});

tape( 'the `main` method calculates the L2-norm of a vector', function test( t ) {
var nrm2;
var x;
var z;

x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] );

z = snrm2.main( x.length, x, 1 );
t.strictEqual( z, sqrt( 55.0 ), 'returns expected value' );
nrm2 = snrm2.main( x.length, x, 1 );
isApprox( t, nrm2, 7.4, 2.0 );

x = new Float32Array( [ -4.0 ] );

z = snrm2.main( x.length, x, 1 );
t.strictEqual( z, 4.0, 'returns expected value' );
nrm2 = snrm2.main( x.length, x, 1 );
isApprox( t, nrm2, 4.0, 1.0 );

t.end();
});

tape( 'the `main` method supports a `stride` parameter', function test( t ) {
var nrm2;
var x;
var z;

x = new Float32Array([
1.0, // 0
Expand All @@ -71,15 +101,15 @@ tape( 'the `main` method supports a `stride` parameter', function test( t ) {
2.0
]);

z = snrm2.main( 4, x, 2 );
nrm2 = snrm2.main( 4, x, 2 );
isApprox( t, nrm2, 5.0, 1.0 );

t.strictEqual( z, 5.0, 'returns expected value' );
t.end();
});

tape( 'the `main` method supports a negative `stride` parameter', function test( t ) {
var nrm2;
var x;
var z;

x = new Float32Array([
1.0, // 3
Expand All @@ -92,31 +122,31 @@ tape( 'the `main` method supports a negative `stride` parameter', function test(
2.0
]);

z = snrm2.main( 4, x, -2 );
nrm2 = snrm2.main( 4, x, -2 );
isApprox( t, nrm2, 5.0, 1.0 );

t.strictEqual( z, 5.0, 'returns expected value' );
t.end();
});

tape( 'if provided an `N` parameter less than or equal to `0`, the `main` method returns `0`', function test( t ) {
var nrm2;
var x;
var z;

x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] );

z = snrm2.main( 0, x, 1 );
t.strictEqual( z, 0.0, 'returns expected value' );
nrm2 = snrm2.main( 0, x, 1 );
t.strictEqual( nrm2, 0.0, 'returns expected value' );

z = snrm2.main( -1, x, 1 );
t.strictEqual( z, 0.0, 'returns expected value' );
nrm2 = snrm2.main( -1, x, 1 );
t.strictEqual( nrm2, 0.0, 'returns expected value' );

t.end();
});

tape( 'the `main` method supports view offsets', function test( t ) {
var nrm2;
var x0;
var x1;
var z;

x0 = new Float32Array([
2.0,
Expand All @@ -132,8 +162,8 @@ tape( 'the `main` method supports view offsets', function test( t ) {

x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

z = snrm2.main( 4, x1, 2 );
t.strictEqual( z, 5.0, 'returns expected value' );
nrm2 = snrm2.main( 4, x1, 2 );
isApprox( t, nrm2, 5.0, 1.0 );

t.end();
});
Loading

0 comments on commit db761b5

Please sign in to comment.