@stdlib/blas-base-wasm-sasum 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/module.js ADDED
@@ -0,0 +1,198 @@
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
+ /* eslint-disable no-restricted-syntax, no-invalid-this */
20
+
21
+ 'use strict';
22
+
23
+ // MODULES //
24
+
25
+ var isWebAssemblyMemory = require( '@stdlib/assert-is-wasm-memory' );
26
+ var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
27
+ var inherits = require( '@stdlib/utils-inherit' );
28
+ var WasmModule = require( '@stdlib/wasm-module-wrapper' );
29
+ var format = require( '@stdlib/string-format' );
30
+ var wasmBinary = require( './binary.js' );
31
+
32
+
33
+ // MAIN //
34
+
35
+ /**
36
+ * BLAS routine WebAssembly module wrapper constructor.
37
+ *
38
+ * @constructor
39
+ * @param {Object} memory - WebAssembly memory instance
40
+ * @throws {TypeError} must provide a WebAssembly memory instance
41
+ * @returns {Module} module instance
42
+ *
43
+ * @example
44
+ * var Memory = require( '@stdlib/wasm-memory' );
45
+ * var oneTo = require( '@stdlib/array-one-to' );
46
+ *
47
+ * // Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
48
+ * var mem = new Memory({
49
+ * 'initial': 10,
50
+ * 'maximum': 100
51
+ * });
52
+ *
53
+ * // Create a BLAS routine:
54
+ * var sasum = new Module( mem );
55
+ * // returns <Module>
56
+ *
57
+ * // Initialize the routine:
58
+ * sasum.initializeSync();
59
+ *
60
+ * // Define a vector data type:
61
+ * var dtype = 'float32';
62
+ *
63
+ * // Specify a vector length:
64
+ * var N = 5;
65
+ *
66
+ * // Define a pointer (i.e., byte offset) for storing the input vector:
67
+ * var xptr = 0;
68
+ *
69
+ * // Write vector values to module memory:
70
+ * sasum.write( xptr, oneTo( N, dtype ) );
71
+ *
72
+ * // Perform computation:
73
+ * var sum = sasum.main( N, xptr, 1 );
74
+ * // returns 15.0
75
+ */
76
+ function Module( memory ) {
77
+ if ( !( this instanceof Module ) ) {
78
+ return new Module( memory );
79
+ }
80
+ if ( !isWebAssemblyMemory( memory ) ) {
81
+ throw new TypeError( format( 'invalid argument. Must provide a WebAssembly memory instance. Value: `%s`.', memory ) );
82
+ }
83
+ // Call the parent constructor:
84
+ WasmModule.call( this, wasmBinary, memory, {
85
+ 'env': {
86
+ 'memory': memory
87
+ }
88
+ });
89
+
90
+ return this;
91
+ }
92
+
93
+ // Inherit from the parent constructor:
94
+ inherits( Module, WasmModule );
95
+
96
+ /**
97
+ * Computes the sum of absolute values.
98
+ *
99
+ * @name main
100
+ * @memberof Module.prototype
101
+ * @readonly
102
+ * @type {Function}
103
+ * @param {PositiveInteger} N - number of indexed elements
104
+ * @param {NonNegativeInteger} xptr - input array pointer (i.e., byte offset)
105
+ * @param {integer} strideX - `x` stride length
106
+ * @returns {number} sum of absolute values
107
+ *
108
+ * @example
109
+ * var Memory = require( '@stdlib/wasm-memory' );
110
+ * var oneTo = require( '@stdlib/array-one-to' );
111
+ *
112
+ * // Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
113
+ * var mem = new Memory({
114
+ * 'initial': 10,
115
+ * 'maximum': 100
116
+ * });
117
+ *
118
+ * // Create a BLAS routine:
119
+ * var sasum = new Module( mem );
120
+ * // returns <Module>
121
+ *
122
+ * // Initialize the routine:
123
+ * sasum.initializeSync();
124
+ *
125
+ * // Define a vector data type:
126
+ * var dtype = 'float32';
127
+ *
128
+ * // Specify a vector length:
129
+ * var N = 5;
130
+ *
131
+ * // Define a pointer (i.e., byte offset) for storing the input vector:
132
+ * var xptr = 0;
133
+ *
134
+ * // Write vector values to module memory:
135
+ * sasum.write( xptr, oneTo( N, dtype ) );
136
+ *
137
+ * // Perform computation:
138
+ * var sum = sasum.main( N, xptr, 1 );
139
+ * // returns 15.0
140
+ */
141
+ setReadOnly( Module.prototype, 'main', function sasum( N, xptr, strideX ) {
142
+ return this._instance.exports.c_sasum( N, xptr, strideX );
143
+ });
144
+
145
+ /**
146
+ * Computes the sum of absolute values using alternative indexing semantics.
147
+ *
148
+ * @name ndarray
149
+ * @memberof Module.prototype
150
+ * @readonly
151
+ * @type {Function}
152
+ * @param {PositiveInteger} N - number of indexed elements
153
+ * @param {NonNegativeInteger} xptr - input array pointer (i.e., byte offset)
154
+ * @param {integer} strideX - `x` stride length
155
+ * @param {NonNegativeInteger} offsetX - starting index for `x`
156
+ * @returns {number} sum of absolute values
157
+ *
158
+ * @example
159
+ * var Memory = require( '@stdlib/wasm-memory' );
160
+ * var oneTo = require( '@stdlib/array-one-to' );
161
+ *
162
+ * // Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
163
+ * var mem = new Memory({
164
+ * 'initial': 10,
165
+ * 'maximum': 100
166
+ * });
167
+ *
168
+ * // Create a BLAS routine:
169
+ * var sasum = new Module( mem );
170
+ * // returns <Module>
171
+ *
172
+ * // Initialize the routine:
173
+ * sasum.initializeSync();
174
+ *
175
+ * // Define a vector data type:
176
+ * var dtype = 'float32';
177
+ *
178
+ * // Specify a vector length:
179
+ * var N = 5;
180
+ *
181
+ * // Define a pointer (i.e., byte offset) for storing the input vector:
182
+ * var xptr = 0;
183
+ *
184
+ * // Write vector values to module memory:
185
+ * sasum.write( xptr, oneTo( N, dtype ) );
186
+ *
187
+ * // Perform computation:
188
+ * var sum = sasum.ndarray( N, xptr, 1, 0 );
189
+ * // returns 15.0
190
+ */
191
+ setReadOnly( Module.prototype, 'ndarray', function sasum( N, xptr, strideX, offsetX ) {
192
+ return this._instance.exports.c_sasum_ndarray( N, xptr, strideX, offsetX );
193
+ });
194
+
195
+
196
+ // EXPORTS //
197
+
198
+ module.exports = Module;
package/lib/routine.js ADDED
@@ -0,0 +1,166 @@
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
+ /* eslint-disable max-len, no-restricted-syntax, no-invalid-this */
20
+
21
+ 'use strict';
22
+
23
+ // MODULES //
24
+
25
+ var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
26
+ var inherits = require( '@stdlib/utils-inherit' );
27
+ var stride2offset = require( '@stdlib/strided-base-stride2offset' );
28
+ var Memory = require( '@stdlib/wasm-memory' );
29
+ var arrays2ptrs = require( '@stdlib/wasm-base-arrays2ptrs' );
30
+ var strided2object = require( '@stdlib/wasm-base-strided2object' );
31
+ var Module = require( './module.js' );
32
+
33
+
34
+ // MAIN //
35
+
36
+ /**
37
+ * Routine constructor.
38
+ *
39
+ * @private
40
+ * @constructor
41
+ * @returns {Routine} routine instance
42
+ *
43
+ * @example
44
+ * var Float32Array = require( '@stdlib/array-float32' );
45
+ *
46
+ * // Create a new routine:
47
+ * var sasum = new Routine();
48
+ *
49
+ * // Initialize the module:
50
+ * sasum.initializeSync();
51
+ *
52
+ * // Define a strided array:
53
+ * var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
54
+ *
55
+ * // Perform operation:
56
+ * var sum = sasum.main( x.length, x, 1 );
57
+ * // returns 15.0
58
+ *
59
+ * @example
60
+ * var Float32Array = require( '@stdlib/array-float32' );
61
+ *
62
+ * // Create a new routine:
63
+ * var sasum = new Routine();
64
+ *
65
+ * // Initialize the module:
66
+ * sasum.initializeSync();
67
+ *
68
+ * // Define a strided array:
69
+ * var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
70
+ *
71
+ * // Perform operation:
72
+ * var sum = sasum.ndarray( x.length, x, 1, 0 );
73
+ * // returns 15.0
74
+ */
75
+ function Routine() {
76
+ if ( !( this instanceof Routine ) ) {
77
+ return new Routine();
78
+ }
79
+ Module.call( this, new Memory({
80
+ 'initial': 0
81
+ }));
82
+ return this;
83
+ }
84
+
85
+ // Inherit from the parent constructor:
86
+ inherits( Routine, Module );
87
+
88
+ /**
89
+ * Computes the sum of absolute values.
90
+ *
91
+ * @name main
92
+ * @memberof Routine.prototype
93
+ * @readonly
94
+ * @type {Function}
95
+ * @param {PositiveInteger} N - number of indexed elements
96
+ * @param {Float32Array} x - input array
97
+ * @param {integer} strideX - `x` stride length
98
+ * @returns {number} sum of absolute values
99
+ *
100
+ * @example
101
+ * var Float32Array = require( '@stdlib/array-float32' );
102
+ *
103
+ * // Create a new routine:
104
+ * var sasum = new Routine();
105
+ *
106
+ * // Initialize the module:
107
+ * sasum.initializeSync();
108
+ *
109
+ * // Define a strided array:
110
+ * var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
111
+ *
112
+ * // Perform operation:
113
+ * var sum = sasum.main( x.length, x, 1 );
114
+ * // returns 15.0
115
+ */
116
+ setReadOnly( Routine.prototype, 'main', function sasum( N, x, strideX ) {
117
+ return this.ndarray( N, x, strideX, stride2offset( N, strideX ) );
118
+ });
119
+
120
+ /**
121
+ * Computes the sum of absolute values using alternative indexing semantics.
122
+ *
123
+ * @name ndarray
124
+ * @memberof Routine.prototype
125
+ * @readonly
126
+ * @type {Function}
127
+ * @param {PositiveInteger} N - number of indexed elements
128
+ * @param {Float32Array} x - input array
129
+ * @param {integer} strideX - `x` stride length
130
+ * @param {NonNegativeInteger} offsetX - starting `x` index
131
+ * @returns {number} sum of absolute values
132
+ *
133
+ * @example
134
+ * var Float32Array = require( '@stdlib/array-float32' );
135
+ *
136
+ * // Create a new routine:
137
+ * var sasum = new Routine();
138
+ *
139
+ * // Initialize the module:
140
+ * sasum.initializeSync();
141
+ *
142
+ * // Define a strided array:
143
+ * var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
144
+ *
145
+ * // Perform operation:
146
+ * var sum = sasum.ndarray( x.length, x, 1, 0 );
147
+ * // returns 15.0
148
+ */
149
+ setReadOnly( Routine.prototype, 'ndarray', function sasum( N, x, strideX, offsetX ) {
150
+ var ptrs;
151
+ var p0;
152
+
153
+ // Convert the input arrays to "pointers" in the module's memory:
154
+ ptrs = arrays2ptrs( this, [
155
+ strided2object( N, x, strideX, offsetX )
156
+ ]);
157
+ p0 = ptrs[ 0 ];
158
+
159
+ // Perform computation by calling the corresponding parent method:
160
+ return Module.prototype.ndarray.call( this, N, p0.ptr, p0.stride, p0.offset );
161
+ });
162
+
163
+
164
+ // EXPORTS //
165
+
166
+ module.exports = Routine;
package/manifest.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "options": {},
3
+ "fields": [
4
+ {
5
+ "field": "src",
6
+ "resolve": true,
7
+ "relative": true
8
+ },
9
+ {
10
+ "field": "include",
11
+ "resolve": true,
12
+ "relative": true
13
+ },
14
+ {
15
+ "field": "libraries",
16
+ "resolve": false,
17
+ "relative": false
18
+ },
19
+ {
20
+ "field": "libpath",
21
+ "resolve": true,
22
+ "relative": false
23
+ }
24
+ ],
25
+ "confs": [
26
+ {
27
+ "src": [],
28
+ "include": [],
29
+ "libraries": [],
30
+ "libpath": [],
31
+ "dependencies": [
32
+ "@stdlib/blas-base-sasum"
33
+ ]
34
+ }
35
+ ]
36
+ }
package/package.json ADDED
@@ -0,0 +1,92 @@
1
+ {
2
+ "name": "@stdlib/blas-base-wasm-sasum",
3
+ "version": "0.1.0",
4
+ "description": "Compute the sum of absolute values.",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "main": "./lib",
17
+ "browser": {
18
+ "./lib/binary.js": "./lib/binary.browser.js"
19
+ },
20
+ "directories": {
21
+ "doc": "./docs",
22
+ "lib": "./lib",
23
+ "scripts": "./scripts",
24
+ "src": "./src",
25
+ "dist": "./dist"
26
+ },
27
+ "types": "./docs/types",
28
+ "scripts": {},
29
+ "homepage": "https://stdlib.io",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git://github.com/stdlib-js/blas-base-wasm-sasum.git"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/stdlib-js/stdlib/issues"
36
+ },
37
+ "dependencies": {
38
+ "@stdlib/assert-is-wasm-memory": "^0.1.0",
39
+ "@stdlib/blas-base-sasum": "^0.3.0",
40
+ "@stdlib/fs-read-wasm": "^0.2.2",
41
+ "@stdlib/strided-base-stride2offset": "^0.1.0",
42
+ "@stdlib/string-format": "^0.2.2",
43
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
44
+ "@stdlib/utils-inherit": "^0.2.2",
45
+ "@stdlib/wasm-base-arrays2ptrs": "github:stdlib-js/wasm-base-arrays2ptrs#main",
46
+ "@stdlib/wasm-base-strided2object": "github:stdlib-js/wasm-base-strided2object#main",
47
+ "@stdlib/wasm-memory": "^0.1.0",
48
+ "@stdlib/wasm-module-wrapper": "github:stdlib-js/wasm-module-wrapper#main",
49
+ "@stdlib/error-tools-fmtprodmsg": "^0.2.2"
50
+ },
51
+ "devDependencies": {},
52
+ "engines": {
53
+ "node": ">=0.10.0",
54
+ "npm": ">2.7.0"
55
+ },
56
+ "os": [
57
+ "aix",
58
+ "darwin",
59
+ "freebsd",
60
+ "linux",
61
+ "macos",
62
+ "openbsd",
63
+ "sunos",
64
+ "win32",
65
+ "windows"
66
+ ],
67
+ "keywords": [
68
+ "stdlib",
69
+ "stdmath",
70
+ "mathematics",
71
+ "math",
72
+ "blas",
73
+ "level 1",
74
+ "sasum",
75
+ "linear",
76
+ "algebra",
77
+ "subroutines",
78
+ "sum",
79
+ "vector",
80
+ "array",
81
+ "ndarray",
82
+ "float32",
83
+ "float",
84
+ "float32array",
85
+ "webassembly",
86
+ "wasm"
87
+ ],
88
+ "funding": {
89
+ "type": "opencollective",
90
+ "url": "https://opencollective.com/stdlib"
91
+ }
92
+ }
@@ -0,0 +1,4 @@
1
+ [
2
+ "_c_sasum",
3
+ "_c_sasum_ndarray"
4
+ ]
package/src/main.wasm ADDED
Binary file
package/src/main.wat ADDED
@@ -0,0 +1,169 @@
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.get 0
26
+ local.get 1
27
+ local.get 2
28
+ i32.const 1
29
+ local.get 0
30
+ i32.sub
31
+ local.get 2
32
+ i32.mul
33
+ i32.const 0
34
+ local.get 2
35
+ i32.const 0
36
+ i32.le_s
37
+ select
38
+ call 2)
39
+ (func (;2;) (type 2) (param i32 i32 i32 i32) (result f32)
40
+ (local i32 f32)
41
+ block ;; label = @1
42
+ local.get 0
43
+ i32.const 0
44
+ i32.le_s
45
+ if ;; label = @2
46
+ br 1 (;@1;)
47
+ end
48
+ local.get 2
49
+ i32.const 1
50
+ i32.ne
51
+ if ;; label = @2
52
+ loop ;; label = @3
53
+ local.get 0
54
+ local.get 4
55
+ i32.eq
56
+ br_if 2 (;@1;)
57
+ local.get 4
58
+ i32.const 1
59
+ i32.add
60
+ local.set 4
61
+ local.get 5
62
+ local.get 1
63
+ local.get 3
64
+ i32.const 2
65
+ i32.shl
66
+ i32.add
67
+ f32.load
68
+ f32.abs
69
+ f32.add
70
+ local.set 5
71
+ local.get 2
72
+ local.get 3
73
+ i32.add
74
+ local.set 3
75
+ br 0 (;@3;)
76
+ end
77
+ unreachable
78
+ end
79
+ block ;; label = @2
80
+ local.get 0
81
+ i32.const 6
82
+ i32.rem_u
83
+ local.tee 2
84
+ i32.eqz
85
+ br_if 0 (;@2;)
86
+ loop ;; label = @3
87
+ local.get 2
88
+ local.get 4
89
+ i32.eq
90
+ br_if 1 (;@2;)
91
+ local.get 4
92
+ i32.const 1
93
+ i32.add
94
+ local.set 4
95
+ local.get 5
96
+ local.get 1
97
+ local.get 3
98
+ i32.const 2
99
+ i32.shl
100
+ i32.add
101
+ f32.load
102
+ f32.abs
103
+ f32.add
104
+ local.set 5
105
+ local.get 3
106
+ i32.const 1
107
+ i32.add
108
+ local.set 3
109
+ br 0 (;@3;)
110
+ end
111
+ unreachable
112
+ end
113
+ local.get 0
114
+ i32.const 6
115
+ i32.lt_s
116
+ br_if 0 (;@1;)
117
+ loop ;; label = @2
118
+ local.get 0
119
+ local.get 2
120
+ i32.le_s
121
+ br_if 1 (;@1;)
122
+ local.get 5
123
+ local.get 1
124
+ local.get 3
125
+ i32.const 2
126
+ i32.shl
127
+ i32.add
128
+ local.tee 4
129
+ f32.load
130
+ f32.abs
131
+ local.get 4
132
+ f32.load offset=4
133
+ f32.abs
134
+ f32.add
135
+ local.get 4
136
+ f32.load offset=8
137
+ f32.abs
138
+ f32.add
139
+ local.get 4
140
+ f32.load offset=12
141
+ f32.abs
142
+ f32.add
143
+ local.get 4
144
+ f32.load offset=16
145
+ f32.abs
146
+ f32.add
147
+ local.get 4
148
+ f32.load offset=20
149
+ f32.abs
150
+ f32.add
151
+ f32.add
152
+ local.set 5
153
+ local.get 2
154
+ i32.const 6
155
+ i32.add
156
+ local.set 2
157
+ local.get 3
158
+ i32.const 6
159
+ i32.add
160
+ local.set 3
161
+ br 0 (;@2;)
162
+ end
163
+ unreachable
164
+ end
165
+ local.get 5)
166
+ (export "__wasm_call_ctors" (func 0))
167
+ (export "__wasm_apply_data_relocs" (func 0))
168
+ (export "c_sasum" (func 1))
169
+ (export "c_sasum_ndarray" (func 2)))