Skip to content

Commit f57bee2

Browse files
committed
feat: add wasm/base/dtype2wasm
1 parent 8204371 commit f57bee2

File tree

11 files changed

+602
-0
lines changed

11 files changed

+602
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# dtype2wasm
22+
23+
> Return the WebAssembly data type associated with a provided array [data type][@stdlib/array/dtypes] value.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var dtype2wasm = require( '@stdlib/wasm/base/dtype2wasm' );
41+
```
42+
43+
#### dtype2wasm( dtype )
44+
45+
Returns the WebAssembly data type associated with a provided array [data type][@stdlib/array/dtypes] value.
46+
47+
```javascript
48+
var out = dtype2wasm( 'float64' );
49+
// returns 'float64'
50+
51+
out = dtype2wasm( 'int8' );
52+
// returns 'int8'
53+
```
54+
55+
If provided an unknown or unsupported array data type, the function **assumes** that associated values can be stored as double-precision floating-point numbers and returns `'float64'`.
56+
57+
```javascript
58+
var out = dtype2wasm( 'foobar' );
59+
// returns 'float64'
60+
```
61+
62+
</section>
63+
64+
<!-- /.usage -->
65+
66+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
67+
68+
<section class="notes">
69+
70+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<!-- Package usage examples. -->
75+
76+
<section class="examples">
77+
78+
## Examples
79+
80+
<!-- eslint no-undef: "error" -->
81+
82+
```javascript
83+
var dtype2wasm = require( '@stdlib/wasm/base/dtype2wasm' );
84+
85+
var dtypes = [
86+
'float64',
87+
'float32',
88+
'int8',
89+
'uint8',
90+
'uint8c',
91+
'int16',
92+
'uint16',
93+
'int32',
94+
'uint32',
95+
'generic'
96+
];
97+
98+
var i;
99+
for ( i = 0; i < dtypes.length; i++ ) {
100+
console.log( '%s => %s', dtypes[ i ], dtype2wasm( dtypes[ i ] ) );
101+
}
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
109+
110+
<section class="references">
111+
112+
</section>
113+
114+
<!-- /.references -->
115+
116+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
117+
118+
<section class="related">
119+
120+
</section>
121+
122+
<!-- /.related -->
123+
124+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
125+
126+
<section class="links">
127+
128+
[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes
129+
130+
</section>
131+
132+
<!-- /.links -->
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 bench = require( '@stdlib/bench' );
24+
var pkg = require( './../package.json' ).name;
25+
var dtype2wasm = require( './../lib' );
26+
27+
28+
// MAIN //
29+
30+
bench( pkg, function benchmark( b ) {
31+
var dtypes;
32+
var out;
33+
var i;
34+
35+
dtypes = [
36+
'float64',
37+
'float32',
38+
'int8',
39+
'uint8',
40+
'int16',
41+
'uint16',
42+
'int32',
43+
'uint32',
44+
45+
'generic',
46+
'uint8c'
47+
];
48+
49+
b.tic();
50+
for ( i = 0; i < b.iterations; i++ ) {
51+
out = dtype2wasm( dtypes[ i%dtypes.length ] );
52+
if ( typeof out !== 'string' ) {
53+
b.fail( 'should return a string' );
54+
}
55+
}
56+
b.toc();
57+
if ( typeof out !== 'string' ) {
58+
b.fail( 'should return a string' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( dtype )
3+
Returns the WebAssembly data type associated with a provided array data type
4+
value.
5+
6+
If provided a "generic" or unknown/unsupported array data type, the function
7+
assumes that the values can be stored as double-precision floating-point
8+
numbers.
9+
10+
Parameters
11+
----------
12+
dtype: string
13+
Array data type.
14+
15+
Returns
16+
-------
17+
out: string
18+
WebAssembly data type.
19+
20+
Examples
21+
--------
22+
> var out = {{alias}}( 'float64' )
23+
'float64'
24+
> out = {{alias}}( 'generic' )
25+
'float64'
26+
27+
See Also
28+
--------
29+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Returns the WebAssembly data type associated with a provided array data type value.
23+
*
24+
* ## Notes
25+
*
26+
* - If provided a "generic" or unknown array data type, the function assumes that the values can be stored as double-precision floating-point numbers.
27+
*
28+
* @param dtype - array data type
29+
* @returns WebAssembly data type
30+
*
31+
* @example
32+
* var out = dtype2wasm( 'float64' );
33+
* // returns 'float64'
34+
*
35+
* out = dtype2wasm( 'generic' );
36+
* // returns 'float64'
37+
*/
38+
declare function dtype2wasm( dtype: string ): string;
39+
40+
41+
// EXPORTS //
42+
43+
export = dtype2wasm;
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+
import dtype2wasm = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
dtype2wasm( 'float64' ); // $ExpectType string
27+
dtype2wasm( 'generic' ); // $ExpectType string
28+
}
29+
30+
// The compiler throws an error if the function is provided insufficient arguments...
31+
{
32+
dtype2wasm(); // $ExpectError
33+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
var dtype2wasm = require( './../lib' );
22+
23+
var dtypes = [
24+
'float64',
25+
'float32',
26+
'int8',
27+
'uint8',
28+
'uint8c',
29+
'int16',
30+
'uint16',
31+
'int32',
32+
'uint32',
33+
'generic'
34+
];
35+
36+
var i;
37+
for ( i = 0; i < dtypes.length; i++ ) {
38+
console.log( '%s => %s', dtypes[ i ], dtype2wasm( dtypes[ i ] ) );
39+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
/**
22+
* Return the WebAssembly data type associated with a provided array data type value.
23+
*
24+
* @module @stdlib/wasm/base/dtype2wasm
25+
*
26+
* @example
27+
* var dtype2wasm = require( '@stdlib/wasm/base/dtype2wasm' );
28+
*
29+
* var out = dtype2wasm( 'float64' );
30+
* // returns 'float64'
31+
*
32+
* out = dtype2wasm( 'generic' );
33+
* // returns 'float64'
34+
*/
35+
36+
// MODULES //
37+
38+
var dtype2wasm = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = dtype2wasm;

0 commit comments

Comments
 (0)