Skip to content

Commit 348d584

Browse files
gururaj1512kgryte
authored andcommitted
feat: add number/uint32/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 8dc22d7 commit 348d584

File tree

13 files changed

+1111
-0
lines changed

13 files changed

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

0 commit comments

Comments
 (0)