Skip to content

Commit 8a2f4ff

Browse files
committed
Add utility to assign callbacks to binary interfaces according to promotion rules
1 parent 0389c38 commit 8a2f4ff

File tree

9 files changed

+1206
-0
lines changed

9 files changed

+1206
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 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+
# Callbacks
22+
23+
> Assign callbacks to binary interfaces according to type [promotion rules][@stdlib/ndarray/promotion-rules].
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 callbacks = require( '@stdlib/strided/base/binary-signature-callbacks' );
41+
```
42+
43+
#### callbacks( table, signatures )
44+
45+
Assigns callbacks to binary interfaces according to type type [promotion rules][@stdlib/ndarray/promotion-rules].
46+
47+
```javascript
48+
var signatures = require( '@stdlib/strided/base/binary-dtype-signatures' );
49+
var add = require( '@stdlib/math/base/ops/add' );
50+
var cadd = require( '@stdlib/math/base/ops/cadd' );
51+
var caddf = require( '@stdlib/math/base/ops/caddf' );
52+
53+
var dtypes = [
54+
'float64',
55+
'float32',
56+
'int32',
57+
'uint8'
58+
];
59+
60+
var sigs = signatures( dtypes, dtypes, dtypes );
61+
// returns [...]
62+
63+
var table = {
64+
'default': add,
65+
'complex64': caddf,
66+
'complex128': cadd
67+
};
68+
69+
var list = callbacks( table, sigs );
70+
// returns [...]
71+
```
72+
73+
The function accepts the following arguments:
74+
75+
- **table**: callback table.
76+
- **signatures**: options.
77+
78+
A callback `table` should have the following properties:
79+
80+
- **default**: default callback.
81+
- **complex64**: callback for single-precision complex floating-point numbers.
82+
- **complex128**: callback for double-precision complex floating-point numbers.
83+
84+
</section>
85+
86+
<!-- /.usage -->
87+
88+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
89+
90+
<section class="notes">
91+
92+
## Notes
93+
94+
- The function assumes that the provided signature array has the following properties:
95+
96+
- a strided array having a stride length of `3` (i.e., every `3` elements define a binary interface signature).
97+
- for each signature (i.e., set of three consecutive non-overlapping strided array elements), the first two elements are the input data types and the third element is the return data type.
98+
- all signatures follow type [promotion rules][@stdlib/ndarray/promotion-rules].
99+
100+
</section>
101+
102+
<!-- /.notes -->
103+
104+
<!-- Package usage examples. -->
105+
106+
<section class="examples">
107+
108+
## Examples
109+
110+
<!-- eslint no-undef: "error" -->
111+
112+
```javascript
113+
var dtypes = require( '@stdlib/strided/dtypes' );
114+
var signatures = require( '@stdlib/strided/base/binary-dtype-signatures' );
115+
var add = require( '@stdlib/math/base/ops/add' );
116+
var cadd = require( '@stdlib/math/base/ops/cadd' );
117+
var caddf = require( '@stdlib/math/base/ops/caddf' );
118+
var callbacks = require( '@stdlib/strided/base/binary-signature-callbacks' );
119+
120+
// Get the list of supported strided array data types:
121+
var dt = dtypes();
122+
123+
// Generate binary interface signatures:
124+
var sigs = signatures( dt, dt, dt );
125+
126+
// Define a callback table:
127+
var table = {
128+
'default': add,
129+
'complex64': caddf,
130+
'complex128': cadd
131+
};
132+
133+
// Generate a list of callbacks according to type promotion rules:
134+
var clbks = callbacks( table, sigs );
135+
// returns [...]
136+
```
137+
138+
</section>
139+
140+
<!-- /.examples -->
141+
142+
<!-- 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. -->
143+
144+
<section class="references">
145+
146+
</section>
147+
148+
<!-- /.references -->
149+
150+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
151+
152+
<section class="related">
153+
154+
</section>
155+
156+
<!-- /.related -->
157+
158+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
159+
160+
<section class="links">
161+
162+
[@stdlib/ndarray/promotion-rules]: https://github.com/stdlib-js/stdlib
163+
164+
</section>
165+
166+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isArray = require( '@stdlib/assert/is-array' );
25+
var dtypes = require( '@stdlib/strided/dtypes' );
26+
var signatures = require( '@stdlib/strided/base/binary-dtype-signatures' );
27+
var cadd = require( '@stdlib/math/base/ops/cadd' );
28+
var caddf = require( '@stdlib/math/base/ops/caddf' );
29+
var add = require( '@stdlib/math/base/ops/add' );
30+
var pkg = require( './../package.json' ).name;
31+
var callbacks = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var TABLE = {
37+
'default': add,
38+
'complex64': caddf,
39+
'complex128': cadd
40+
};
41+
42+
43+
// MAIN //
44+
45+
bench( pkg+'::strings', function benchmark( b ) {
46+
var sigs;
47+
var out;
48+
var v1;
49+
var v2;
50+
var v3;
51+
var i;
52+
53+
v1 = dtypes();
54+
v2 = dtypes();
55+
v3 = dtypes();
56+
sigs = signatures( v1, v2, v3, {
57+
'enums': false
58+
});
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
out = callbacks( TABLE, sigs );
63+
if ( typeof out !== 'object' ) {
64+
b.fail( 'should return an array' );
65+
}
66+
}
67+
b.toc();
68+
if ( !isArray( out ) ) {
69+
b.fail( 'should return an array' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});
74+
75+
bench( pkg+'::enums', function benchmark( b ) {
76+
var sigs;
77+
var out;
78+
var v1;
79+
var v2;
80+
var v3;
81+
var i;
82+
83+
v1 = dtypes();
84+
v2 = dtypes();
85+
v3 = dtypes();
86+
sigs = signatures( v1, v2, v3, {
87+
'enums': true
88+
});
89+
90+
b.tic();
91+
for ( i = 0; i < b.iterations; i++ ) {
92+
out = callbacks( TABLE, sigs );
93+
if ( typeof out !== 'object' ) {
94+
b.fail( 'should return an array' );
95+
}
96+
}
97+
b.toc();
98+
if ( !isArray( out ) ) {
99+
b.fail( 'should return an array' );
100+
}
101+
b.pass( 'benchmark finished' );
102+
b.end();
103+
});

0 commit comments

Comments
 (0)