@stdlib/blas-base-wasm-idamax 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 idamax = new Module( mem );
55
+ * // returns <Module>
56
+ *
57
+ * // Initialize the routine:
58
+ * idamax.initializeSync();
59
+ *
60
+ * // Define a vector data type:
61
+ * var dtype = 'float64';
62
+ *
63
+ * // Specify a vector length:
64
+ * var N = 5;
65
+ *
66
+ * // Define a pointer (i.e., byte offset) to the first vector element:
67
+ * var xptr = 0;
68
+ *
69
+ * // Write vector values to module memory:
70
+ * idamax.write( xptr, oneTo( N, dtype ) );
71
+ *
72
+ * // Perform computation:
73
+ * var idx = idamax.main( N, xptr, 1 );
74
+ * // returns 4
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
+ * Finds the index of the first element having the maximum absolute value.
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 {integer} index value
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 idamax = new Module( mem );
120
+ * // returns <Module>
121
+ *
122
+ * // Initialize the routine:
123
+ * idamax.initializeSync();
124
+ *
125
+ * // Define a vector data type:
126
+ * var dtype = 'float64';
127
+ *
128
+ * // Specify a vector length:
129
+ * var N = 5;
130
+ *
131
+ * // Define a pointer (i.e., byte offset) to the first vector element:
132
+ * var xptr = 0;
133
+ *
134
+ * // Write vector values to module memory:
135
+ * idamax.write( xptr, oneTo( N, dtype ) );
136
+ *
137
+ * // Perform computation:
138
+ * var idx = idamax.main( N, xptr, 1 );
139
+ * // returns 4
140
+ */
141
+ setReadOnly( Module.prototype, 'main', function idamax( N, xptr, strideX ) {
142
+ return this._instance.exports.c_idamax( N, xptr, strideX );
143
+ });
144
+
145
+ /**
146
+ * Finds the index of the first element having the maximum absolute value 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 {integer} index value
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 idamax = new Module( mem );
170
+ * // returns <Module>
171
+ *
172
+ * // Initialize the routine:
173
+ * idamax.initializeSync();
174
+ *
175
+ * // Define a vector data type:
176
+ * var dtype = 'float64';
177
+ *
178
+ * // Specify a vector length:
179
+ * var N = 5;
180
+ *
181
+ * // Define a pointer (i.e., byte offset) to the first vector element:
182
+ * var xptr = 0;
183
+ *
184
+ * // Write vector values to module memory:
185
+ * idamax.write( xptr, oneTo( N, dtype ) );
186
+ *
187
+ * // Perform computation:
188
+ * var idx = idamax.ndarray( N, xptr, 1, 0 );
189
+ * // returns 4
190
+ */
191
+ setReadOnly( Module.prototype, 'ndarray', function idamax( N, xptr, strideX, offsetX ) {
192
+ return this._instance.exports.c_idamax_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 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 Float64Array = require( '@stdlib/array-float64' );
45
+ *
46
+ * // Create a new routine:
47
+ * var idamax = new Routine();
48
+ *
49
+ * // Initialize the module:
50
+ * idamax.initializeSync();
51
+ *
52
+ * // Define a strided array:
53
+ * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, -5.0 ] );
54
+ *
55
+ * // Perform operation:
56
+ * var idx = idamax.main( x.length, x, 1 );
57
+ * // returns 4
58
+ *
59
+ * @example
60
+ * var Float64Array = require( '@stdlib/array-float64' );
61
+ *
62
+ * // Create a new routine:
63
+ * var idamax = new Routine();
64
+ *
65
+ * // Initialize the module:
66
+ * idamax.initializeSync();
67
+ *
68
+ * // Define a strided array:
69
+ * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, -5.0 ] );
70
+ *
71
+ * // Perform operation:
72
+ * var idx = idamax.ndarray( x.length, x, 1, 0 );
73
+ * // returns 4
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
+ * Finds the index of the first element having the maximum absolute value.
90
+ *
91
+ * @name main
92
+ * @memberof Routine.prototype
93
+ * @readonly
94
+ * @type {Function}
95
+ * @param {PositiveInteger} N - number of indexed elements
96
+ * @param {Float64Array} x - input array
97
+ * @param {integer} strideX - `x` stride length
98
+ * @returns {integer} index value
99
+ *
100
+ * @example
101
+ * var Float64Array = require( '@stdlib/array-float64' );
102
+ *
103
+ * // Create a new routine:
104
+ * var idamax = new Routine();
105
+ *
106
+ * // Initialize the module:
107
+ * idamax.initializeSync();
108
+ *
109
+ * // Define a strided array:
110
+ * var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, -5.0 ] );
111
+ *
112
+ * // Perform operation:
113
+ * var idx = idamax.main( x.length, x, 1 );
114
+ * // returns 4
115
+ */
116
+ setReadOnly( Routine.prototype, 'main', function idamax( N, x, strideX ) {
117
+ return this.ndarray( N, x, strideX, stride2offset( N, strideX ) );
118
+ });
119
+
120
+ /**
121
+ * Finds the index of the first element having the maximum absolute value 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 {Float64Array} x - input array
129
+ * @param {integer} strideX - `x` stride length
130
+ * @param {NonNegativeInteger} offsetX - starting index for `x`
131
+ * @returns {integer} index value
132
+ *
133
+ * @example
134
+ * var Float64Array = require( '@stdlib/array-float64' );
135
+ *
136
+ * // Create a new routine:
137
+ * var idamax = new Routine();
138
+ *
139
+ * // Initialize the module:
140
+ * idamax.initializeSync();
141
+ *
142
+ * // Define a strided array:
143
+ * var x = new Float64Array( [ 1.0, 2.0, -3.0, 4.0, -5.0 ] );
144
+ *
145
+ * // Perform operation:
146
+ * var idx = idamax.ndarray( x.length, x, 1, 0 );
147
+ * // returns 4
148
+ */
149
+ setReadOnly( Routine.prototype, 'ndarray', function idamax( N, x, strideX, offsetX ) {
150
+ var ptrs;
151
+ var p0;
152
+
153
+ // Convert the input array 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 ); // eslint-disable-line max-len
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-idamax"
33
+ ]
34
+ }
35
+ ]
36
+ }
package/package.json ADDED
@@ -0,0 +1,96 @@
1
+ {
2
+ "name": "@stdlib/blas-base-wasm-idamax",
3
+ "version": "0.1.0",
4
+ "description": "Find the index of the first element having the maximum absolute value.",
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-idamax.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-idamax": "^0.1.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
+ "idamax",
75
+ "maximum",
76
+ "abs",
77
+ "absolute",
78
+ "find",
79
+ "index",
80
+ "linear",
81
+ "algebra",
82
+ "subroutines",
83
+ "vector",
84
+ "array",
85
+ "ndarray",
86
+ "float64",
87
+ "double",
88
+ "float64array",
89
+ "webassembly",
90
+ "wasm"
91
+ ],
92
+ "funding": {
93
+ "type": "opencollective",
94
+ "url": "https://opencollective.com/stdlib"
95
+ }
96
+ }
@@ -0,0 +1,4 @@
1
+ [
2
+ "_c_idamax",
3
+ "_c_idamax_ndarray"
4
+ ]
package/src/main.wasm ADDED
Binary file
package/src/main.wat ADDED
@@ -0,0 +1,107 @@
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 i32)))
20
+ (type (;2;) (func (param i32 i32 i32 i32) (result i32)))
21
+ (import "env" "memory" (memory (;0;) 0))
22
+ (func (;0;) (type 0)
23
+ nop)
24
+ (func (;1;) (type 1) (param i32 i32 i32) (result i32)
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 i32)
40
+ (local i32 i32 i32 f64 f64)
41
+ block ;; label = @1
42
+ local.get 0
43
+ i32.const 0
44
+ i32.le_s
45
+ if ;; label = @2
46
+ i32.const -1
47
+ local.set 4
48
+ br 1 (;@1;)
49
+ end
50
+ local.get 0
51
+ i32.const 1
52
+ i32.eq
53
+ if ;; label = @2
54
+ i32.const 0
55
+ return
56
+ end
57
+ local.get 1
58
+ local.get 3
59
+ i32.const 3
60
+ i32.shl
61
+ i32.add
62
+ f64.load
63
+ f64.abs
64
+ local.set 7
65
+ i32.const 1
66
+ local.set 5
67
+ loop ;; label = @2
68
+ local.get 0
69
+ local.get 5
70
+ i32.eq
71
+ br_if 1 (;@1;)
72
+ local.get 1
73
+ local.get 2
74
+ local.get 3
75
+ i32.add
76
+ local.tee 3
77
+ i32.const 3
78
+ i32.shl
79
+ i32.add
80
+ f64.load
81
+ f64.abs
82
+ local.tee 8
83
+ local.get 7
84
+ local.get 7
85
+ local.get 8
86
+ f64.lt
87
+ local.tee 6
88
+ select
89
+ local.set 7
90
+ local.get 5
91
+ local.get 4
92
+ local.get 6
93
+ select
94
+ local.set 4
95
+ local.get 5
96
+ i32.const 1
97
+ i32.add
98
+ local.set 5
99
+ br 0 (;@2;)
100
+ end
101
+ unreachable
102
+ end
103
+ local.get 4)
104
+ (export "__wasm_call_ctors" (func 0))
105
+ (export "__wasm_apply_data_relocs" (func 0))
106
+ (export "c_idamax" (func 1))
107
+ (export "c_idamax_ndarray" (func 2)))