Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/imuldw/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<!--

@license Apache-2.0

Copyright (c) 2018 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# imuldw

> Compute the double word product of two signed 32-bit integers.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var imuldw = require( '@stdlib/math/base/special/imuldw' );
```

#### imuldw( \[out,] a, b )

Multiplies two signed 32-bit integers and returns an array of two signed 32-bit integers which represents the 64-bit signed integer product.

```javascript
var v = imuldw( 1, 10 );
// returns [ 0, 10 ]

v = imuldw( 0x80000000, 0x40000000 ); // -2^31 * 2^30 = -2305843009213694000 => 32-bit integer overflow
// returns [ -536870912, 0 ]
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var lpad = require( '@stdlib/string/left-pad' );
var imuldw = require( '@stdlib/math/base/special/imuldw' );

var i;
var j;
var y;

for ( i = 0xFFFFFFF0; i < 0xFFFFFFFF; i++ ) {
for ( j = i; j < 0xFFFFFFFF; j++) {
y = imuldw( i, j );
console.log( '%d x %d = 0x%s%s', i, j, lpad( ( y[0] >>> 0 ).toString( 16 ), 8, '0'), lpad( ( y[1] >>> 0 ).toString( 16 ), 8, '0' ) );
}
}
```

</section>

<!-- /.examples -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var minstd = require( '@stdlib/random/base/minstd' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var imuldw = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var x;
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = minstd();
y = imuldw( x, x );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
29 changes: 29 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/imuldw/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

{{alias}}( [out,] a, b )
Multiplies two signed 32-bit integers and returns an array of two signed
32-bit integers which represents the 64-bit signed integer product.

Parameters
----------
out: ArrayLikeObject (optional)
The output array.

a: integer
Signed 32-bit integer.

b: integer
Signed 32-bit integer.

Returns
-------
out: ArrayLikeObject
Double word product.

Examples
--------
> var v = {{alias}}( 1, 10 )
[ 0, 10 ]

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var lpad = require( '@stdlib/string/left-pad' );
var imuldw = require( './../lib' );

var i;
var j;
var y;

for ( i = 0xFFFFFFF0; i < 0xFFFFFFFF; i++ ) {
for ( j = i; j < 0xFFFFFFFF; j++) {
y = imuldw( i, j );
console.log( '%d x %d = 0x%s%s', i, j, lpad( ( y[0] >>> 0 ).toString( 16 ), 8, '0' ), lpad( ( y[1] >>> 0 ).toString( 16 ), 8, '0' ) );
}
}
40 changes: 40 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/imuldw/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* Perform multiplication of two signed 32-bit integers and return an array of two signed 32-bit integers which represents the 64-bit signed integer product.
*
* @module @stdlib/math/base/special/imuldw
*
* @example
* var imuldw = require( '@stdlib/math/base/special/imuldw' );
*
* var v = imuldw( 0xAAAAAAAA, 0x55555555 );
* // returns [ -477218589, 1908874354 ]
*/

// MODULES //

var imuldw = require( './main.js' );


// EXPORTS //

module.exports = imuldw;
101 changes: 101 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/imuldw/lib/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var isnan = require( '@stdlib/math/base/assert/is-nan' );


// VARIABLES //

// Define a mask for the least significant 16 bits (low word): 65535 => 0x0000ffff => 00000000000000001111111111111111
var LOW_WORD_MASK = 0x0000ffff>>>0; // asm type annotation


// MAIN //

/**
* Performs multiplication of two signed 32-bit integers and returns an array of two signed 32-bit integers which represents the 64-bit signed integer product.
*
* @param {ArrayLikeObject} [out] - output array
* @param {uinteger32} a - integer
* @param {uinteger32} b - integer
* @returns {ArrayLikeObject} output array
*
* @example
* var v = imuldw( 0xAAAAAAAA, 0x55555555 );
* // returns [ -477218589, 1908874354 ]
*/
function imuldw() {
var out;
var w1;
var w2;
var w3;
var ha;
var hb;
var la;
var lb;
var a;
var b;
var t;
var k;

if ( arguments.length === 3 ) {
out = arguments[ 0 ];
a = arguments[ 1 ];
b = arguments[ 2 ];
} else {
out = [ 0, 0 ];
a = arguments[ 0 ];
b = arguments[ 1 ];
}
if ( isnan( a ) || isnan( b ) ) {
return NaN;
}
a |= 0; // asm type annotation
b |= 0; // asm type annotation

ha = ( a >> 16 ) | 0;
la = ( a & LOW_WORD_MASK ) >>> 0;

hb = ( b >> 16 ) | 0;
lb = ( b & LOW_WORD_MASK ) >>> 0;

t = ( la * lb ) >>> 0;
w3 = ( t & LOW_WORD_MASK ) >>> 0;
k = ( t >>> 16 ) >>> 0;

t = ( ( ha * lb ) + k ) >>> 0;
w2 = ( t & LOW_WORD_MASK ) >>> 0;
w1 = ( t >> 16 ) >>> 0;

t = ( ( la * hb ) + w2 ) >>> 0;
k = ( t >> 16 ) >>> 0;

out[ 0 ] = ( ( ha * hb ) + w1 + k ) | 0; // the higher 32 bits; convert to signed 32 bit
out[ 1 ] = ( ( t << 16 ) + w3 ) | 0; // the lower 32 bits; convert to signed 32 bit

return out;
}


// EXPORTS //

module.exports = imuldw;
Loading