Skip to content

Commit 5014579

Browse files
committed
feat: add number/uint32/base/muldw
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 6fa8a1b commit 5014579

File tree

16 files changed

+1570
-0
lines changed

16 files changed

+1570
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
# umuldw
22+
23+
> Compute the double word product 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 umuldw = require( '@stdlib/number/uint32/base/muldw' );
37+
```
38+
39+
#### umuldw( a, b )
40+
41+
Multiplies two unsigned 32-bit integers and returns an `array` of two unsigned 32-bit integers (in big endian order) which represents the unsigned 64-bit integer product.
42+
43+
```javascript
44+
var v = umuldw( 1, 10 );
45+
// returns [ 0, 10 ]
46+
47+
v = umuldw( 0x80000000, 0x80000000 ); // 2^31 * 2^31 = 4611686018427388000 => 32-bit integer overflow
48+
// returns [ 1073741824, 0 ]
49+
```
50+
51+
#### umuldw.assign( a, b, out, stride, offset )
52+
53+
Multiplies two unsigned 32-bit integers and assigns results representing the unsigned 64-bit integer product (in big endian order) to a provided output array.
54+
55+
```javascript
56+
var out = [ 0, 0 ];
57+
58+
var v = umuldw.assign( 1, 10, out, 1, 0 );
59+
// returns [ 0, 10 ]
60+
61+
var bool = ( v === out );
62+
// returns true
63+
```
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<section class="notes">
70+
71+
## Notes
72+
73+
- When computing the product of 32-bit integer values in double-precision floating-point format (the default JavaScript numeric data type), computing the double word product is necessary in order to **avoid** exceeding the [maximum safe double-precision floating-point integer value][@stdlib/constants/float64/max-safe-integer].
74+
75+
</section>
76+
77+
<!-- /.notes -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
<!-- eslint no-undef: "error" -->
84+
85+
```javascript
86+
var lpad = require( '@stdlib/string/left-pad' );
87+
var umuldw = require( '@stdlib/number/uint32/base/muldw' );
88+
89+
var i;
90+
var j;
91+
var y;
92+
93+
for ( i = 0xFFFFFFF0; i < 0xFFFFFFFF; i++ ) {
94+
for ( j = i; j < 0xFFFFFFFF; j++) {
95+
y = umuldw( i, j );
96+
console.log( '%d x %d = 0x%s%s', i, j, lpad( y[0].toString( 16 ), 8, '0' ), lpad( y[1].toString( 16 ), 8, '0' ) );
97+
}
98+
}
99+
```
100+
101+
</section>
102+
103+
<!-- /.examples -->
104+
105+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
106+
107+
<section class="related">
108+
109+
* * *
110+
111+
## See Also
112+
113+
- <span class="package-name">[`@stdlib/number/int32/base/muldw`][@stdlib/number/int32/base/muldw]</span><span class="delimiter">: </span><span class="description">compute the double word product of two signed 32-bit integers.</span>
114+
- <span class="package-name">[`@stdlib/number/uint32/base/mul`][@stdlib/number/uint32/base/mul]</span><span class="delimiter">: </span><span class="description">perform C-like multiplication of two unsigned 32-bit integers.</span>
115+
116+
</section>
117+
118+
<!-- /.related -->
119+
120+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
121+
122+
<section class="links">
123+
124+
[@stdlib/constants/float64/max-safe-integer]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/constants/float64/max-safe-integer
125+
126+
<!-- <related-links> -->
127+
128+
[@stdlib/number/int32/base/muldw]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/number/int32/base/muldw
129+
130+
[@stdlib/number/uint32/base/mul]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/number/uint32/base/mul
131+
132+
<!-- </related-links> -->
133+
134+
</section>
135+
136+
<!-- /.links -->
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 umuldw = 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 = umuldw( x, x );
41+
if ( isnan( y[0] ) ) {
42+
b.fail( 'should not return NaN' );
43+
}
44+
}
45+
b.toc();
46+
if ( isnan( y[1] ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
52+
53+
bench( pkg+':assign', function benchmark( b ) {
54+
var out;
55+
var x;
56+
var y;
57+
var i;
58+
59+
out = [ 0.0, 0.0];
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
x = minstd();
64+
y = umuldw.assign( x, x, out, 1, 0 );
65+
if ( isnan( y[0] ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( y[1] ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
{{alias}}( a, b )
3+
Multiplies two unsigned 32-bit integers and returns an array of two unsigned
4+
32-bit integers which represents the unsigned 64-bit integer product.
5+
6+
When computing the product of 32-bit integer values in double-precision
7+
floating-point format (the default JavaScript numeric data type), computing
8+
the double word product is necessary in order to avoid exceeding the maximum
9+
safe double-precision floating-point integer value.
10+
11+
Parameters
12+
----------
13+
a: integer
14+
Unsigned 32-bit integer.
15+
16+
b: integer
17+
Unsigned 32-bit integer.
18+
19+
Returns
20+
-------
21+
out: Array<number>
22+
Double word product (in big endian order; i.e., the first element
23+
corresponds to the most significant bits and the second element to the
24+
least significant bits).
25+
26+
Examples
27+
--------
28+
> var v = {{alias}}( 1, 10 )
29+
[ 0, 10 ]
30+
31+
32+
{{alias}}.assign( a, b, out, stride, offset )
33+
Multiplies two unsigned 32-bit integers and assigns results representing
34+
the unsigned 64-bit integer product to a provided output array.
35+
36+
When computing the product of 32-bit integer values in double-precision
37+
floating-point format (the default JavaScript numeric data type), computing
38+
the double word product is necessary in order to avoid exceeding the maximum
39+
safe double-precision floating-point integer value.
40+
41+
Parameters
42+
----------
43+
a: integer
44+
Unsigned 32-bit integer.
45+
46+
b: integer
47+
Unsigned 32-bit integer.
48+
49+
out: Array|TypedArray|Object
50+
Output array.
51+
52+
stride: integer
53+
Output array stride.
54+
55+
offset: integer
56+
Output array index offset.
57+
58+
Returns
59+
-------
60+
out: Array|TypedArray|Object
61+
Double word product (in big endian order; i.e., the first element
62+
corresponds to the most significant bits and the second element to the
63+
least significant bits).
64+
65+
Examples
66+
--------
67+
> var out = [ 0, 0 ];
68+
> var v = {{alias}}.assign( 1, 10, out, 1, 0 )
69+
[ 0, 10 ]
70+
> var bool = ( v === out )
71+
true
72+
73+
See Also
74+
--------
75+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/array';
24+
25+
/**
26+
* Interface describing `umuldw`
27+
*/
28+
interface Umuldw {
29+
/**
30+
* Performs multiplication of two unsigned 32-bit integers and returns an array of two unsigned 32-bit integers which represents the unsigned 64-bit integer product.
31+
*
32+
* ## Notes
33+
*
34+
* - When computing the product of 32-bit integer values in double-precision floating-point format (the default JavaScript numeric data type), computing the double word product is necessary in order to avoid exceeding the maximum safe double-precision floating-point integer value.
35+
*
36+
* @param a - integer
37+
* @param b - integer
38+
* @returns double word product (in big endian order; i.e., the first element corresponds to the most significant bits and the second element to the least significant bits)
39+
*
40+
* @example
41+
* var v = umuldw( 0xAAAAAAAA, 0x55555555 );
42+
* // returns [ 954437176, 1908874354 ]
43+
*/
44+
( a: number, b: number ): Array<number>;
45+
46+
/**
47+
* Performs multiplication of two unsigned 32-bit integers and returns an array of two unsigned 32-bit integers which represents the unsigned 64-bit integer product.
48+
*
49+
* ## Notes
50+
*
51+
* - When computing the product of 32-bit integer values in double-precision floating-point format (the default JavaScript numeric data type), computing the double word product is necessary in order to avoid exceeding the maximum safe double-precision floating-point integer value.
52+
*
53+
* @param out - output array
54+
* @param a - integer
55+
* @param b - integer
56+
* @returns output array
57+
*
58+
* @example
59+
* var v = umuldw( 0xAAAAAAAA, 0x55555555 );
60+
* // returns [ 954437176, 1908874354 ]
61+
*/
62+
assign<T = unknown>( a: number, b: number, out: Collection<T>, stride: number, offset: number ): Collection<T | number>;
63+
}
64+
65+
/**
66+
* Performs multiplication of two unsigned 32-bit integers and returns an array of two unsigned 32-bit integers which represents the unsigned 64-bit integer product.
67+
*
68+
* ## Notes
69+
*
70+
* - When computing the product of 32-bit integer values in double-precision floating-point format (the default JavaScript numeric data type), computing the double word product is necessary in order to avoid exceeding the maximum safe double-precision floating-point integer value.
71+
*
72+
* @param a - integer
73+
* @param b - integer
74+
* @returns double word product (in big endian order; i.e., the first element corresponds to the most significant bits and the second element to the least significant bits)
75+
*
76+
* @example
77+
* var v = umuldw( 0xAAAAAAAA, 0x55555555 );
78+
* // returns [ 954437176, 1908874354 ]
79+
*/
80+
declare var umuldw: Umuldw;
81+
82+
83+
// EXPORTS //
84+
85+
export = umuldw;

0 commit comments

Comments
 (0)