Skip to content

Commit fd83184

Browse files
gururaj1512kgryte
authored andcommitted
feat: add number/int32/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 b688f77 commit fd83184

File tree

16 files changed

+1579
-0
lines changed

16 files changed

+1579
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
# imuldw
22+
23+
> Compute the double word product 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 imuldw = require( '@stdlib/number/int32/base/muldw' );
37+
```
38+
39+
#### imuldw( a, b )
40+
41+
Multiplies two signed 32-bit integers and returns an `array` of two signed 32-bit integers which represents the signed 64-bit integer product.
42+
43+
```javascript
44+
var v = imuldw( 1, 10 );
45+
// returns [ 0, 10 ]
46+
47+
v = imuldw( 0x80000000|0, 0x40000000|0 ); // -(2^31) * 2^30 = -2305843009213694000 => 32-bit integer overflow
48+
// returns [ -536870912, 0 ]
49+
```
50+
51+
#### imuldw.assign( a, b, out, stride, offset )
52+
53+
Multiplies two signed 32-bit integers and assigns results representing the signed 64-bit integer product to a provided output array.
54+
55+
```javascript
56+
var out = [ 0, 0 ];
57+
58+
var v = imuldw.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 imuldw = require( '@stdlib/number/int32/base/muldw' );
88+
89+
var i;
90+
var j;
91+
var y;
92+
93+
for ( i = 0x7FFFFFF0; i < 0x7FFFFFFF; i++ ) {
94+
for ( j = i; j < 0x7FFFFFFF; j++) {
95+
y = imuldw( i|0, j|0 );
96+
console.log( '%d x %d = 0x%s%s', i|0, j|0, lpad( ( y[0] >>> 0 ).toString( 16 ), 8, '0'), lpad( ( y[1] >>> 0 ).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/mul`][@stdlib/number/int32/base/mul]</span><span class="delimiter">: </span><span class="description">perform C-like multiplication of two signed 32-bit integers.</span>
114+
115+
</section>
116+
117+
<!-- /.related -->
118+
119+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="links">
122+
123+
[@stdlib/constants/float64/max-safe-integer]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/constants/float64/max-safe-integer
124+
125+
<!-- <related-links> -->
126+
127+
[@stdlib/number/int32/base/mul]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/number/int32/base/mul
128+
129+
<!-- </related-links> -->
130+
131+
</section>
132+
133+
<!-- /.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 imuldw = 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 = imuldw( 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 = imuldw.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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
{{alias}}( a, b )
3+
Multiplies two signed 32-bit integers and returns an array of two signed 32-
4+
bit integers which represents the signed 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+
Signed 32-bit integer.
15+
16+
b: integer
17+
Signed 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 signed 32-bit integers and assigns results representing the
34+
signed 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+
Signed 32-bit integer.
45+
46+
b: integer
47+
Signed 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+
--------
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 `imuldw`.
27+
*/
28+
interface Imuldw {
29+
/**
30+
* Performs multiplication of two signed 32-bit integers and returns an array of two signed 32-bit integers which represents the signed 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 output array
39+
*
40+
* @example
41+
* var v = imuldw( 0xAAAAAAAA, 0x55555555 );
42+
* // returns [ -477218589, 1908874354 ]
43+
*/
44+
( a: number, b: number ): Array<number>;
45+
46+
/**
47+
* Performs multiplication of two signed 32-bit integers and returns an array of two signed 32-bit integers which represents the signed 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 a - integer
54+
* @param b - integer
55+
* @param out - output array
56+
* @param stride - output array stride
57+
* @param offset - output array index offset
58+
* @returns output array
59+
*
60+
* @example
61+
* var v = imuldw( 0xAAAAAAAA, 0x55555555 );
62+
* // returns [ -477218589, 1908874354 ]
63+
*/
64+
assign<T = unknown>( a: number, b: number, out: Collection<T>, stride: number, offset: number ): Collection<T | number>;
65+
}
66+
67+
/**
68+
* Performs multiplication of two signed 32-bit integers and returns an array of two signed 32-bit integers which represents the signed 64-bit integer product.
69+
*
70+
* ## Notes
71+
*
72+
* - 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.
73+
*
74+
* @param a - integer
75+
* @param b - integer
76+
* @returns output array
77+
*
78+
* @example
79+
* var v = imuldw( 0xAAAAAAAA, 0x55555555 );
80+
* // returns [ -477218589, 1908874354 ]
81+
*/
82+
declare var imuldw: Imuldw;
83+
84+
85+
// EXPORTS //
86+
87+
export = imuldw;

0 commit comments

Comments
 (0)