Skip to content

Commit c528ca7

Browse files
committed
feat: add number/int32/base/mul
Ref: #2261 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: missing_dependencies - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 8a80a81 commit c528ca7

File tree

15 files changed

+1249
-0
lines changed

15 files changed

+1249
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# imul
22+
23+
> Perform C-like multiplication of two signed 32-bit integers.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var imul = require( '@stdlib/number/int32/base/mul' );
37+
```
38+
39+
#### imul( a, b )
40+
41+
Performs C-like multiplication of two signed 32-bit integers.
42+
43+
```javascript
44+
var v = imul( -10|0, 4|0 );
45+
// returns -40
46+
47+
v = imul( 1073741824|0, -5|0 ); // 2^30 * -5 = -5368709120 => 32-bit integer overflow
48+
// returns -1073741824
49+
```
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
56+
57+
<section class="notes">
58+
59+
## Notes
60+
61+
- The function emulates C-like multiplication of two signed 32-bit integers.
62+
63+
</section>
64+
65+
<!-- /.notes -->
66+
67+
<section class="examples">
68+
69+
## Examples
70+
71+
<!-- eslint no-undef: "error" -->
72+
73+
```javascript
74+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
75+
var INT32_MIN = require( '@stdlib/constants/int32/min' );
76+
var INT32_MAX = require( '@stdlib/constants/int32/max' );
77+
var imul = require( '@stdlib/number/int32/base/mul' );
78+
79+
var randi;
80+
var a;
81+
var b;
82+
var y;
83+
var i;
84+
85+
randi = discreteUniform( INT32_MIN, INT32_MAX );
86+
87+
for ( i = 0; i < 100; i++ ) {
88+
a = randi()|0;
89+
b = randi()|0;
90+
y = imul( a, b );
91+
console.log( '%d x %d = %d', a, b, y );
92+
}
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
100+
101+
<section class="related">
102+
103+
* * *
104+
105+
## See Also
106+
107+
- <span class="package-name">[`@stdlib/math/base/ops/imuldw`][@stdlib/math/base/ops/imuldw]</span><span class="delimiter">: </span><span class="description">compute the double word product of two signed 32-bit integers.</span>
108+
109+
</section>
110+
111+
<!-- /.related -->
112+
113+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
114+
115+
<section class="links">
116+
117+
<!-- <related-links> -->
118+
119+
[@stdlib/math/base/ops/imuldw]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/ops/imuldw
120+
121+
<!-- </related-links> -->
122+
123+
</section>
124+
125+
<!-- /.links -->
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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 minstd = require( '@stdlib/random/base/minstd' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var imul = require( './../lib' );
28+
var polyfill = require( './../lib/polyfill.js' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var x;
35+
var y;
36+
var i;
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
x = minstd();
41+
y = imul( x|0, x|0 );
42+
if ( isnan( y ) ) {
43+
b.fail( 'should not return NaN' );
44+
}
45+
}
46+
b.toc();
47+
if ( isnan( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
53+
54+
bench( pkg+'::polyfill', function benchmark( b ) {
55+
var x;
56+
var y;
57+
var i;
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
x = minstd();
62+
y = polyfill( x|0, x|0 );
63+
if ( isnan( y ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
}
67+
b.toc();
68+
if ( isnan( y ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});
74+
75+
bench( pkg+'::naive_multiplication', function benchmark( b ) {
76+
var x;
77+
var y;
78+
var i;
79+
80+
b.tic();
81+
for ( i = 0; i < b.iterations; i++ ) {
82+
x = minstd();
83+
y = ( x|0 ) * ( x|0 );
84+
if ( isnan( y ) ) {
85+
b.fail( 'should not return NaN' );
86+
}
87+
}
88+
b.toc();
89+
if ( isnan( y ) ) {
90+
b.fail( 'should not return NaN' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( a, b )
3+
Performs C-like multiplication of two signed 32-bit integers.
4+
5+
Parameters
6+
----------
7+
a: integer
8+
Signed 32-bit integer.
9+
10+
b: integer
11+
Signed 32-bit integer.
12+
13+
Returns
14+
-------
15+
out: integer
16+
Product.
17+
18+
Examples
19+
--------
20+
> var v = {{alias}}( -10|0, 4|0 )
21+
-40
22+
23+
See Also
24+
--------
25+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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+
* Performs C-like multiplication of two signed 32-bit integers.
23+
*
24+
* @param a - signed 32-bit integer
25+
* @param b - signed 32-bit integer
26+
* @returns product
27+
*
28+
* @example
29+
* var v = imul( -10|0, 4|0 );
30+
* // returns -40
31+
*/
32+
declare function imul( a: number, b: number ): number;
33+
34+
35+
// EXPORTS //
36+
37+
export = imul;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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 imul = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
imul( -10, 4 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided values other than two numbers...
30+
{
31+
imul( true, 3 ); // $ExpectError
32+
imul( false, 2 ); // $ExpectError
33+
imul( '5', 1 ); // $ExpectError
34+
imul( [], 1 ); // $ExpectError
35+
imul( {}, 2 ); // $ExpectError
36+
imul( ( x: number ): number => x, 2 ); // $ExpectError
37+
38+
imul( 9, true ); // $ExpectError
39+
imul( 9, false ); // $ExpectError
40+
imul( 5, '5' ); // $ExpectError
41+
imul( 8, [] ); // $ExpectError
42+
imul( 9, {} ); // $ExpectError
43+
imul( 8, ( x: number ): number => x ); // $ExpectError
44+
45+
imul( [], true ); // $ExpectError
46+
imul( {}, false ); // $ExpectError
47+
imul( false, '5' ); // $ExpectError
48+
imul( {}, [] ); // $ExpectError
49+
imul( '5', ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided insufficient arguments...
53+
{
54+
imul(); // $ExpectError
55+
imul( 3 ); // $ExpectError
56+
}
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) 2018 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 discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
22+
var INT32_MIN = require( '@stdlib/constants/int32/min' );
23+
var INT32_MAX = require( '@stdlib/constants/int32/max' );
24+
var imul = require( './../lib' );
25+
26+
var randi;
27+
var a;
28+
var b;
29+
var y;
30+
var i;
31+
32+
randi = discreteUniform( INT32_MIN, INT32_MAX );
33+
34+
for ( i = 0; i < 100; i++ ) {
35+
a = randi()|0;
36+
b = randi()|0;
37+
y = imul( a, b );
38+
console.log( '%d x %d = %d', a, b, y );
39+
}

0 commit comments

Comments
 (0)