@stdlib/strided-base-binary-addon-dispatch 0.0.1

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/index.js ADDED
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2021 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
+ /**
22
+ * Dispatch to a native add-on applying a binary function to two input strided arrays.
23
+ *
24
+ * @module @stdlib/strided-base-binary-addon-dispatch
25
+ *
26
+ * @example
27
+ * var dispatch = require( '@stdlib/strided-base-binary-addon-dispatch' );
28
+ *
29
+ * function addon( N, dtypeX, x, strideX, dtypeY, y, strideY, dtypeZ, z, strideZ ) {
30
+ * // Call into native add-on...
31
+ * }
32
+ *
33
+ * function fallback( N, dtypeX, x, strideX, dtypeY, y, strideY, dtypeZ, z, strideZ ) {
34
+ * // Fallback JavaScript implementation...
35
+ * }
36
+ *
37
+ * // Create a dispatch function:
38
+ * var f = dispatch( addon, fallback );
39
+ *
40
+ * // ...
41
+ *
42
+ * // Invoke the dispatch function with strided array arguments:
43
+ * f( 2, 'generic', [ 1, 2 ], 1, 'generic', [ 3, 4 ], 1, 'generic', [ 0, 0 ], 1 );
44
+ */
45
+
46
+ // MODULES //
47
+
48
+ var main = require( './main.js' );
49
+
50
+
51
+ // EXPORTS //
52
+
53
+ module.exports = main;
package/lib/main.js ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2020 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 setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
24
+ var binary = require( './binary.js' );
25
+ var ndarray = require( './ndarray.js' );
26
+
27
+
28
+ // MAIN //
29
+
30
+ setReadOnly( binary, 'ndarray', ndarray );
31
+
32
+
33
+ // EXPORTS //
34
+
35
+ module.exports = binary;
package/lib/ndarray.js ADDED
@@ -0,0 +1,228 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2021 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 */
20
+
21
+ 'use strict';
22
+
23
+ // MODULES //
24
+
25
+ var isFunction = require( '@stdlib/assert-is-function' );
26
+ var isTypedArrayLike = require( '@stdlib/assert-is-typed-array-like' );
27
+ var isNonNegativeInteger = require( '@stdlib/assert-is-nonnegative-integer' ).isPrimitive;
28
+ var resolve = require( '@stdlib/strided-base-dtype-resolve-enum' );
29
+ var reinterpretComplex64 = require( '@stdlib/strided-base-reinterpret-complex64' );
30
+ var reinterpretComplex128 = require( '@stdlib/strided-base-reinterpret-complex128' );
31
+ var offsetView = require( '@stdlib/strided-base-offset-view' );
32
+ var minViewBufferIndex = require( '@stdlib/strided-base-min-view-buffer-index' );
33
+
34
+
35
+ // VARIABLES //
36
+
37
+ var COMPLEX64 = resolve( 'complex64' );
38
+ var COMPLEX128 = resolve( 'complex128' );
39
+
40
+
41
+ // MAIN //
42
+
43
+ /**
44
+ * Returns a function which dispatches to a native add-on applying a binary function to two input arrays using alternative indexing semantics.
45
+ *
46
+ * ## Notes
47
+ *
48
+ * - The returned function has the following signature:
49
+ *
50
+ * ```text
51
+ * f( N, dtypeX, x, strideX, offsetX, dtypeY, y, strideY, offsetY, dtypeZ, z, strideZ, offsetZ )
52
+ * ```
53
+ *
54
+ * where
55
+ *
56
+ * - **N**: number of indexed elements.
57
+ * - **dtypeX**: `x` data type.
58
+ * - **x**: input array.
59
+ * - **strideX**: `x` stride length.
60
+ * - **offsetX**: starting `x` index.
61
+ * - **dtypeY**: `y` data type.
62
+ * - **y**: input array.
63
+ * - **strideY**: `y` stride length.
64
+ * - **offsetY**: starting `y` index.
65
+ * - **dtypeZ**: `z` data type.
66
+ * - **z**: output array.
67
+ * - **strideZ**: `z` stride length.
68
+ * - **offsetZ**: starting `z` index.
69
+ *
70
+ * - The add-on function should have the following signature:
71
+ *
72
+ * ```text
73
+ * f( N, dtypeX, x, strideX, dtypeY, y, strideY, dtypeZ, z, strideZ )
74
+ * ```
75
+ *
76
+ * where
77
+ *
78
+ * - **N**: number of indexed elements.
79
+ * - **dtypeX**: `x` data type (enumeration constant).
80
+ * - **x**: input array.
81
+ * - **strideX**: `x` stride length.
82
+ * - **dtypeY**: `y` data type (enumeration constant).
83
+ * - **y**: input array.
84
+ * - **strideY**: `y` stride length.
85
+ * - **dtypeZ**: `z` data type (enumeration constant).
86
+ * - **z**: output array.
87
+ * - **strideZ**: `z` stride length.
88
+ *
89
+ * - The fallback function should have the following signature:
90
+ *
91
+ * ```text
92
+ * f( N, dtypeX, x, strideX, offsetX, dtypeY, y, strideY, offsetY, dtypeZ, z, strideZ, offsetZ )
93
+ * ```
94
+ *
95
+ * where
96
+ *
97
+ * - **N**: number of indexed elements.
98
+ * - **dtypeX**: `x` data type.
99
+ * - **x**: input array.
100
+ * - **strideX**: `x` stride length.
101
+ * - **offsetX**: starting `x` index.
102
+ * - **dtypeY**: `y` data type.
103
+ * - **y**: input array.
104
+ * - **strideY**: `y` stride length.
105
+ * - **offsetY**: starting `y` index.
106
+ * - **dtypeZ**: `z` data type.
107
+ * - **z**: output array.
108
+ * - **strideZ**: `z` stride length.
109
+ * - **offsetZ**: starting `z` index.
110
+ *
111
+ * @param {Function} addon - add-on interface
112
+ * @param {Function} fallback - fallback function
113
+ * @throws {TypeError} first argument must be a function
114
+ * @throws {TypeError} second argument must be a function
115
+ * @returns {Function} dispatch function
116
+ *
117
+ * @example
118
+ * function addon( N, dtypeX, x, strideX, dtypeY, y, strideY, dtypeZ, z, strideZ ) {
119
+ * // Call into native add-on...
120
+ * }
121
+ *
122
+ * function fallback( N, dtypeX, x, strideX, offsetX, dtypeY, y, strideY, offsetY, dtypeZ, z, strideZ, offsetZ ) {
123
+ * // Fallback JavaScript implementation...
124
+ * }
125
+ *
126
+ * // Create a dispatch function:
127
+ * var f = dispatch( addon, fallback );
128
+ *
129
+ * // ...
130
+ *
131
+ * // Invoke the dispatch function with strided array arguments:
132
+ * f( 2, 'generic', [ 1, 2 ], 1, 0, 'generic', [ 3, 4 ], 1, 0, 'generic', [ 0, 0 ], 1, 0 );
133
+ */
134
+ function dispatch( addon, fallback ) {
135
+ if ( !isFunction( addon ) ) {
136
+ throw new TypeError( 'invalid argument. First argument must be a function. Value: `' + addon + '`.' );
137
+ }
138
+ if ( !isFunction( fallback ) ) {
139
+ throw new TypeError( 'invalid argument. Second argument must be a function. Value: `' + fallback + '`.' );
140
+ }
141
+ return dispatcher;
142
+
143
+ /**
144
+ * Dispatches to a native add-on.
145
+ *
146
+ * @private
147
+ * @param {integer} N - number of indexed elements
148
+ * @param {*} dtypeX - `x` data type
149
+ * @param {Collection} x - input array
150
+ * @param {integer} strideX - `x` stride length
151
+ * @param {NonNegativeInteger} offsetX - starting `x` index
152
+ * @param {*} dtypeY - `y` data type
153
+ * @param {Collection} y - input array
154
+ * @param {integer} strideY - `y` stride length
155
+ * @param {NonNegativeInteger} offsetY - starting `y` index
156
+ * @param {*} dtypeZ - `z` data type
157
+ * @param {Collection} z - destination array
158
+ * @param {integer} strideZ - `z` stride length
159
+ * @param {NonNegativeInteger} offsetZ - starting `z` index
160
+ * @throws {TypeError} fifth argument must be a nonnegative integer
161
+ * @throws {TypeError} ninth argument must be a nonnegative integer
162
+ * @throws {TypeError} thirteenth argument must be a nonnegative integer
163
+ * @throws {TypeError} unable to resolve a strided array function supporting the provided array argument data types
164
+ * @returns {Collection} `z`
165
+ */
166
+ function dispatcher( N, dtypeX, x, strideX, offsetX, dtypeY, y, strideY, offsetY, dtypeZ, z, strideZ, offsetZ ) { // eslint-disable-line max-params
167
+ var viewX;
168
+ var viewY;
169
+ var viewZ;
170
+
171
+ // WARNING: we assume that, if we're provided something resembling a typed array, we're provided a typed array; however, this can lead to potential unintended errors as the native add-on may not work with non-typed array objects (e.g., generic arrays)...
172
+ if (
173
+ !isTypedArrayLike( x ) ||
174
+ !isTypedArrayLike( y ) ||
175
+ !isTypedArrayLike( z )
176
+ ) {
177
+ fallback( N, dtypeX, x, strideX, offsetX, dtypeY, y, strideY, offsetY, dtypeZ, z, strideZ, offsetZ );
178
+ return z;
179
+ }
180
+ dtypeX = resolve( dtypeX );
181
+ dtypeY = resolve( dtypeY );
182
+ dtypeZ = resolve( dtypeZ );
183
+ if ( dtypeX === null || dtypeY === null || dtypeZ === null ) {
184
+ throw new TypeError( 'invalid arguments. Unable to resolve a strided array function supporting the provided array argument data types.' );
185
+ }
186
+ if ( !isNonNegativeInteger( offsetX ) ) {
187
+ throw new TypeError( 'invalid argument. First input array offset argument must be a nonnegative integer.' );
188
+ }
189
+ if ( !isNonNegativeInteger( offsetY ) ) {
190
+ throw new TypeError( 'invalid argument. Second input array offset argument must be a nonnegative integer.' );
191
+ }
192
+ if ( !isNonNegativeInteger( offsetZ ) ) {
193
+ throw new TypeError( 'invalid argument. Output array offset argument must be a nonnegative integer.' );
194
+ }
195
+ offsetX = minViewBufferIndex( N, strideX, offsetX );
196
+ offsetY = minViewBufferIndex( N, strideY, offsetY );
197
+ offsetZ = minViewBufferIndex( N, strideZ, offsetZ );
198
+ if ( dtypeX === COMPLEX64 ) {
199
+ viewX = reinterpretComplex64( x, offsetX );
200
+ } else if ( dtypeX === COMPLEX128 ) {
201
+ viewX = reinterpretComplex128( x, offsetX );
202
+ } else {
203
+ viewX = offsetView( x, offsetX );
204
+ }
205
+ if ( dtypeY === COMPLEX64 ) {
206
+ viewY = reinterpretComplex64( y, offsetY );
207
+ } else if ( dtypeY === COMPLEX128 ) {
208
+ viewY = reinterpretComplex128( y, offsetY );
209
+ } else {
210
+ viewY = offsetView( y, offsetY );
211
+ }
212
+ if ( dtypeZ === COMPLEX64 ) {
213
+ viewZ = reinterpretComplex64( z, offsetZ );
214
+ } else if ( dtypeZ === COMPLEX128 ) {
215
+ viewZ = reinterpretComplex128( z, offsetZ );
216
+ } else {
217
+ viewZ = offsetView( z, offsetZ );
218
+ }
219
+
220
+ addon( N, dtypeX, viewX, strideX, dtypeY, viewY, strideY, dtypeZ, viewZ, strideZ );
221
+ return z;
222
+ }
223
+ }
224
+
225
+
226
+ // EXPORTS //
227
+
228
+ module.exports = dispatch;
package/package.json ADDED
@@ -0,0 +1,95 @@
1
+ {
2
+ "name": "@stdlib/strided-base-binary-addon-dispatch",
3
+ "version": "0.0.1",
4
+ "description": "Dispatch to a native add-on applying a binary function to two input strided arrays.",
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
+ "directories": {
18
+ "benchmark": "./benchmark",
19
+ "doc": "./docs",
20
+ "example": "./examples",
21
+ "lib": "./lib",
22
+ "test": "./test"
23
+ },
24
+ "types": "./docs/types",
25
+ "scripts": {
26
+ "test": "make test",
27
+ "test-cov": "make test-cov",
28
+ "examples": "make examples",
29
+ "benchmark": "make benchmark"
30
+ },
31
+ "homepage": "https://stdlib.io",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git://github.com/stdlib-js/strided-base-binary-addon-dispatch.git"
35
+ },
36
+ "bugs": {
37
+ "url": "https://github.com/stdlib-js/stdlib/issues"
38
+ },
39
+ "dependencies": {
40
+ "@stdlib/assert-is-function": "^0.0.x",
41
+ "@stdlib/assert-is-nonnegative-integer": "^0.0.x",
42
+ "@stdlib/assert-is-typed-array-like": "^0.0.x",
43
+ "@stdlib/strided-base-dtype-resolve-enum": "^0.0.x",
44
+ "@stdlib/strided-base-min-view-buffer-index": "^0.0.x",
45
+ "@stdlib/strided-base-offset-view": "^0.0.x",
46
+ "@stdlib/strided-base-reinterpret-complex128": "^0.0.x",
47
+ "@stdlib/strided-base-reinterpret-complex64": "^0.0.x",
48
+ "@stdlib/types": "^0.0.x",
49
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x"
50
+ },
51
+ "devDependencies": {
52
+ "@stdlib/array-complex128": "^0.0.x",
53
+ "@stdlib/array-complex64": "^0.0.x",
54
+ "@stdlib/array-float64": "^0.0.x",
55
+ "@stdlib/assert-has-own-property": "^0.0.x",
56
+ "@stdlib/assert-is-float32array": "^0.0.x",
57
+ "@stdlib/assert-is-float64array": "^0.0.x",
58
+ "@stdlib/bench": "^0.0.x",
59
+ "@stdlib/utils-noop": "^0.0.x",
60
+ "tape": "git+https://github.com/kgryte/tape.git#fix/globby",
61
+ "istanbul": "^0.4.1",
62
+ "tap-spec": "5.x.x"
63
+ },
64
+ "engines": {
65
+ "node": ">=0.10.0",
66
+ "npm": ">2.7.0"
67
+ },
68
+ "os": [
69
+ "aix",
70
+ "darwin",
71
+ "freebsd",
72
+ "linux",
73
+ "macos",
74
+ "openbsd",
75
+ "sunos",
76
+ "win32",
77
+ "windows"
78
+ ],
79
+ "keywords": [
80
+ "stdlib",
81
+ "base",
82
+ "strided",
83
+ "array",
84
+ "dispatch",
85
+ "addon",
86
+ "add-on",
87
+ "native",
88
+ "binary"
89
+ ],
90
+ "__stdlib__": {},
91
+ "funding": {
92
+ "type": "patreon",
93
+ "url": "https://www.patreon.com/athan"
94
+ }
95
+ }