Skip to content

Commit db761b5

Browse files
committed
build: add support for wasm for snrm2
1 parent 637273a commit db761b5

File tree

7 files changed

+424
-51
lines changed

7 files changed

+424
-51
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var base64ToUint8Array = require( '@stdlib/string/base/base64-to-uint8array' );
24+
25+
26+
// MAIN //
27+
28+
var wasm = base64ToUint8Array( 'AGFzbQEAAAAADwhkeWxpbmsuMAEEAAAAAAETA2AAAGADf39/AX1gBH9/f38BfQIPAQNlbnYGbWVtb3J5AgAAAwQDAAECB0wEEV9fd2FzbV9jYWxsX2N0b3JzAAAYX193YXNtX2FwcGx5X2RhdGFfcmVsb2NzAAAHY19zbnJtMgABD2Nfc25ybTJfbmRhcnJheQACCogDAwMAAQshAQF+IAAgASACIAKsIgNCASAArH1+QgAgA0IAVxunEAIL3wICBX0Df0EBIQkgAEEASgR9A0ACQCABIANBAnRqKgIAiyIHQwAAgFleBEAgBiAHQwAAgBmUIgYgBpSSIQZBACEJDAELIAdDAAAAIF0EQCAJIQpBACEJIApFDQEgBSAHQwAAAGWUIgUgBZSSIQVBASEJDAELIAQgByAHlJIhBAsgAiADaiEDIAtBAWoiCyAARw0ACwJAIAZDAAAAAF4EQCAEQwAAgBmUQwAAgBmUIAaSIgUgBiAEQ///f39eGyAFIARDAAAAAF8bIQRDAACAZSEGDAELQwAAgD8hBiAFQwAAAABeRQ0AAkAgBCAEXA0AIARDAAAAAF4NACAEQ///f39eDQAgBSEEQwAAABohBgwBCyAFkUMAAAAalCIFIASRIgQgBCAFXSIAGyIHIAeUIQggBCAFIAAbIAeVIgQgBJS7RAAAAAAAAPA/oCAIu6K2IQQLIAYgBJGUBUMAAAAACws=' );
29+
30+
31+
// EXPORTS //
32+
33+
module.exports = wasm;
542 Bytes
Binary file not shown.
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
;; @license Apache-2.0
2+
;;
3+
;; Copyright (c) 2024 The Stdlib Authors.
4+
;;
5+
;; Licensed under the Apache License, Version 2.0 (the "License");
6+
;; you may not use this file except in compliance with the License.
7+
;; You may obtain a copy of the License at
8+
;;
9+
;; http://www.apache.org/licenses/LICENSE-2.0
10+
;;
11+
;; Unless required by applicable law or agreed to in writing, software
12+
;; distributed under the License is distributed on an "AS IS" BASIS,
13+
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
;; See the License for the specific language governing permissions and
15+
;; limitations under the License.
16+
17+
(module
18+
(type (;0;) (func))
19+
(type (;1;) (func (param i32 i32 i32) (result f32)))
20+
(type (;2;) (func (param i32 i32 i32 i32) (result f32)))
21+
(import "env" "memory" (memory (;0;) 0))
22+
(func (;0;) (type 0)
23+
nop)
24+
(func (;1;) (type 1) (param i32 i32 i32) (result f32)
25+
(local i64)
26+
local.get 0
27+
local.get 1
28+
local.get 2
29+
local.get 2
30+
i64.extend_i32_s
31+
local.tee 3
32+
i64.const 1
33+
local.get 0
34+
i64.extend_i32_s
35+
i64.sub
36+
i64.mul
37+
i64.const 0
38+
local.get 3
39+
i64.const 0
40+
i64.le_s
41+
select
42+
i32.wrap_i64
43+
call 2)
44+
(func (;2;) (type 2) (param i32 i32 i32 i32) (result f32)
45+
(local f32 f32 f32 f32 f32 i32 i32 i32)
46+
i32.const 1
47+
local.set 9
48+
local.get 0
49+
i32.const 0
50+
i32.gt_s
51+
if (result f32) ;; label = @1
52+
loop ;; label = @2
53+
block ;; label = @3
54+
local.get 1
55+
local.get 3
56+
i32.const 2
57+
i32.shl
58+
i32.add
59+
f32.load
60+
f32.abs
61+
local.tee 7
62+
f32.const 0x1p+52 (;=4.5036e+15;)
63+
f32.gt
64+
if ;; label = @4
65+
local.get 6
66+
local.get 7
67+
f32.const 0x1p-76 (;=1.32349e-23;)
68+
f32.mul
69+
local.tee 6
70+
local.get 6
71+
f32.mul
72+
f32.add
73+
local.set 6
74+
i32.const 0
75+
local.set 9
76+
br 1 (;@3;)
77+
end
78+
local.get 7
79+
f32.const 0x1p-63 (;=1.0842e-19;)
80+
f32.lt
81+
if ;; label = @4
82+
local.get 9
83+
local.set 10
84+
i32.const 0
85+
local.set 9
86+
local.get 10
87+
i32.eqz
88+
br_if 1 (;@3;)
89+
local.get 5
90+
local.get 7
91+
f32.const 0x1p+75 (;=3.77789e+22;)
92+
f32.mul
93+
local.tee 5
94+
local.get 5
95+
f32.mul
96+
f32.add
97+
local.set 5
98+
i32.const 1
99+
local.set 9
100+
br 1 (;@3;)
101+
end
102+
local.get 4
103+
local.get 7
104+
local.get 7
105+
f32.mul
106+
f32.add
107+
local.set 4
108+
end
109+
local.get 2
110+
local.get 3
111+
i32.add
112+
local.set 3
113+
local.get 11
114+
i32.const 1
115+
i32.add
116+
local.tee 11
117+
local.get 0
118+
i32.ne
119+
br_if 0 (;@2;)
120+
end
121+
block ;; label = @2
122+
local.get 6
123+
f32.const 0x0p+0 (;=0;)
124+
f32.gt
125+
if ;; label = @3
126+
local.get 4
127+
f32.const 0x1p-76 (;=1.32349e-23;)
128+
f32.mul
129+
f32.const 0x1p-76 (;=1.32349e-23;)
130+
f32.mul
131+
local.get 6
132+
f32.add
133+
local.tee 5
134+
local.get 6
135+
local.get 4
136+
f32.const 0x1.fffffep+127 (;=3.40282e+38;)
137+
f32.gt
138+
select
139+
local.get 5
140+
local.get 4
141+
f32.const 0x0p+0 (;=0;)
142+
f32.le
143+
select
144+
local.set 4
145+
f32.const 0x1p+76 (;=7.55579e+22;)
146+
local.set 6
147+
br 1 (;@2;)
148+
end
149+
f32.const 0x1p+0 (;=1;)
150+
local.set 6
151+
local.get 5
152+
f32.const 0x0p+0 (;=0;)
153+
f32.gt
154+
i32.eqz
155+
br_if 0 (;@2;)
156+
block ;; label = @3
157+
local.get 4
158+
local.get 4
159+
f32.ne
160+
br_if 0 (;@3;)
161+
local.get 4
162+
f32.const 0x0p+0 (;=0;)
163+
f32.gt
164+
br_if 0 (;@3;)
165+
local.get 4
166+
f32.const 0x1.fffffep+127 (;=3.40282e+38;)
167+
f32.gt
168+
br_if 0 (;@3;)
169+
local.get 5
170+
local.set 4
171+
f32.const 0x1p-75 (;=2.64698e-23;)
172+
local.set 6
173+
br 1 (;@2;)
174+
end
175+
local.get 5
176+
f32.sqrt
177+
f32.const 0x1p-75 (;=2.64698e-23;)
178+
f32.mul
179+
local.tee 5
180+
local.get 4
181+
f32.sqrt
182+
local.tee 4
183+
local.get 4
184+
local.get 5
185+
f32.lt
186+
local.tee 0
187+
select
188+
local.tee 7
189+
local.get 7
190+
f32.mul
191+
local.set 8
192+
local.get 4
193+
local.get 5
194+
local.get 0
195+
select
196+
local.get 7
197+
f32.div
198+
local.tee 4
199+
local.get 4
200+
f32.mul
201+
f64.promote_f32
202+
f64.const 0x1p+0 (;=1;)
203+
f64.add
204+
local.get 8
205+
f64.promote_f32
206+
f64.mul
207+
f32.demote_f64
208+
local.set 4
209+
end
210+
local.get 6
211+
local.get 4
212+
f32.sqrt
213+
f32.mul
214+
else
215+
f32.const 0x0p+0 (;=0;)
216+
end)
217+
(export "__wasm_call_ctors" (func 0))
218+
(export "__wasm_apply_data_relocs" (func 0))
219+
(export "c_snrm2" (func 1))
220+
(export "c_snrm2_ndarray" (func 2)))

lib/node_modules/@stdlib/blas/base/snrm2-wasm/test/test.main.js

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,41 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24-
var sqrt = require( '@stdlib/math/base/special/sqrt' );
2524
var Float32Array = require( '@stdlib/array/float32' );
25+
var EPS = require( '@stdlib/constants/float32/eps' );
26+
var abs = require( '@stdlib/math/base/special/abs' );
2627
var snrm2 = require( './../lib' );
2728

2829

30+
// FUNCTIONS //
31+
32+
/**
33+
* Tests for element-wise approximate equality.
34+
*
35+
* @private
36+
* @param {Object} t - test object
37+
* @param {Collection} actual - actual values
38+
* @param {Collection} expected - expected values
39+
* @param {number} rtol - relative tolerance
40+
*/
41+
function isApprox( t, actual, expected, rtol ) {
42+
var delta;
43+
var tol;
44+
var i;
45+
46+
t.strictEqual( actual.length, expected.length, 'returns expected value' );
47+
for ( i = 0; i < expected.length; i++ ) {
48+
if ( actual[ i ] === expected[ i ] ) {
49+
t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' );
50+
} else {
51+
delta = abs( actual[ i ] - expected[ i ] );
52+
tol = rtol * EPS * abs( expected[ i ] );
53+
t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' );
54+
}
55+
}
56+
}
57+
58+
2959
// TESTS //
3060

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

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

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

48-
z = snrm2.main( x.length, x, 1 );
49-
t.strictEqual( z, sqrt( 55.0 ), 'returns expected value' );
78+
nrm2 = snrm2.main( x.length, x, 1 );
79+
isApprox( t, nrm2, 7.4, 2.0 );
5080

5181
x = new Float32Array( [ -4.0 ] );
5282

53-
z = snrm2.main( x.length, x, 1 );
54-
t.strictEqual( z, 4.0, 'returns expected value' );
83+
nrm2 = snrm2.main( x.length, x, 1 );
84+
isApprox( t, nrm2, 4.0, 1.0 );
5585

5686
t.end();
5787
});
5888

5989
tape( 'the `main` method supports a `stride` parameter', function test( t ) {
90+
var nrm2;
6091
var x;
61-
var z;
6292

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

74-
z = snrm2.main( 4, x, 2 );
104+
nrm2 = snrm2.main( 4, x, 2 );
105+
isApprox( t, nrm2, 5.0, 1.0 );
75106

76-
t.strictEqual( z, 5.0, 'returns expected value' );
77107
t.end();
78108
});
79109

80110
tape( 'the `main` method supports a negative `stride` parameter', function test( t ) {
111+
var nrm2;
81112
var x;
82-
var z;
83113

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

95-
z = snrm2.main( 4, x, -2 );
125+
nrm2 = snrm2.main( 4, x, -2 );
126+
isApprox( t, nrm2, 5.0, 1.0 );
96127

97-
t.strictEqual( z, 5.0, 'returns expected value' );
98128
t.end();
99129
});
100130

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

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

107-
z = snrm2.main( 0, x, 1 );
108-
t.strictEqual( z, 0.0, 'returns expected value' );
137+
nrm2 = snrm2.main( 0, x, 1 );
138+
t.strictEqual( nrm2, 0.0, 'returns expected value' );
109139

110-
z = snrm2.main( -1, x, 1 );
111-
t.strictEqual( z, 0.0, 'returns expected value' );
140+
nrm2 = snrm2.main( -1, x, 1 );
141+
t.strictEqual( nrm2, 0.0, 'returns expected value' );
112142

113143
t.end();
114144
});
115145

116146
tape( 'the `main` method supports view offsets', function test( t ) {
147+
var nrm2;
117148
var x0;
118149
var x1;
119-
var z;
120150

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

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

135-
z = snrm2.main( 4, x1, 2 );
136-
t.strictEqual( z, 5.0, 'returns expected value' );
165+
nrm2 = snrm2.main( 4, x1, 2 );
166+
isApprox( t, nrm2, 5.0, 1.0 );
137167

138168
t.end();
139169
});

0 commit comments

Comments
 (0)