Skip to content

Commit 8a85314

Browse files
refactor!: remove NaN checks and optimize implementation for better performance
PR-URL: #11699 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 33bbad7 commit 8a85314

4 files changed

Lines changed: 17 additions & 59 deletions

File tree

lib/node_modules/@stdlib/number/int32/base/muldw/benchmark/benchmark.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var minstd = require( '@stdlib/random/base/minstd' );
24+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var format = require( '@stdlib/string/format' );
2727
var pkg = require( './../package.json' ).name;
@@ -33,12 +33,17 @@ var imuldw = require( './../lib' );
3333
bench( pkg, function benchmark( b ) {
3434
var x;
3535
var y;
36+
var z;
3637
var i;
3738

39+
x = discreteUniform( 100, 0x10000, 0x10000000, {
40+
'dtype': 'int32'
41+
});
42+
3843
b.tic();
3944
for ( i = 0; i < b.iterations; i++ ) {
40-
x = minstd();
41-
y = imuldw( x, x );
45+
z = x[ i%x.length ];
46+
y = imuldw( z, z );
4247
if ( isnan( y[ 0 ] ) ) {
4348
b.fail( 'should not return NaN' );
4449
}
@@ -55,14 +60,19 @@ bench( format( '%s:assign', pkg ), function benchmark( b ) {
5560
var out;
5661
var x;
5762
var y;
63+
var z;
5864
var i;
5965

66+
x = discreteUniform( 100, 0x10000, 0x10000000, {
67+
'dtype': 'int32'
68+
});
69+
6070
out = [ 0.0, 0.0 ];
6171

6272
b.tic();
6373
for ( i = 0; i < b.iterations; i++ ) {
64-
x = minstd();
65-
y = imuldw.assign( x, x, out, 1, 0 );
74+
z = x[ i%x.length ];
75+
y = imuldw.assign( z, z, out, 1, 0 );
6676
if ( isnan( y[ 0 ] ) ) {
6777
b.fail( 'should not return NaN' );
6878
}

lib/node_modules/@stdlib/number/int32/base/muldw/lib/assign.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
// MODULES //
2222

23-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
23+
var imul = require( '@stdlib/number/int32/base/mul' );
2424

2525

2626
// VARIABLES //
@@ -48,19 +48,13 @@ var LOW_WORD_MASK = 0x0000ffff>>>0; // asm type annotation
4848
function imuldw( a, b, out, stride, offset ) {
4949
var w1;
5050
var w2;
51-
var w3;
5251
var ha;
5352
var hb;
5453
var la;
5554
var lb;
5655
var t;
5756
var k;
5857

59-
if ( isnan( a ) || isnan( b ) ) {
60-
out[ offset ] = NaN;
61-
out[ offset + stride ] = NaN;
62-
return out;
63-
}
6458
a |= 0; // asm type annotation
6559
b |= 0; // asm type annotation
6660

@@ -71,7 +65,6 @@ function imuldw( a, b, out, stride, offset ) {
7165
lb = ( b & LOW_WORD_MASK ) >>> 0;
7266

7367
t = ( la*lb ) >>> 0;
74-
w3 = ( t & LOW_WORD_MASK ) >>> 0;
7568
k = ( t >>> 16 ) >>> 0;
7669

7770
t = ( ( ha*lb ) + k ) >>> 0;
@@ -82,7 +75,7 @@ function imuldw( a, b, out, stride, offset ) {
8275
k = ( t >> 16 ) >>> 0;
8376

8477
out[ offset ] = ( ( ha*hb ) + w1 + k ) | 0; // compute the higher 32 bits and cast to a signed 32-bit integer
85-
out[ offset + stride ] = ( ( t << 16 ) + w3 ) | 0; // compute the lower 32 bits and cast to a signed 32-bit integer
78+
out[ offset + stride ] = imul( a, b ) | 0; // compute the lower 32 bits and cast to a signed 32-bit integer
8679

8780
return out;
8881
}

lib/node_modules/@stdlib/number/int32/base/muldw/test/test.assign.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2524
var Float64Array = require( '@stdlib/array/float64' );
2625
var imuldw = require( './../lib/assign.js' );
2726

@@ -39,31 +38,6 @@ tape( 'main export is a function', function test( t ) {
3938
t.end();
4039
});
4140

42-
tape( 'the function returns `NaN` if provided `NaN`', function test( t ) {
43-
var out;
44-
var v;
45-
46-
out = [ 0, 0 ];
47-
v = imuldw( NaN, 1, out, 1, 0 );
48-
t.strictEqual( v, out, 'returns output array' );
49-
t.strictEqual( isnan( v[0] ), true, 'returns expected value' );
50-
t.strictEqual( isnan( v[1] ), true, 'returns expected value' );
51-
52-
out = [ 0, 0 ];
53-
v = imuldw( 1, NaN, out, 1, 0 );
54-
t.strictEqual( v, out, 'returns output array' );
55-
t.strictEqual( isnan( v[0] ), true, 'returns expected value' );
56-
t.strictEqual( isnan( v[1] ), true, 'returns expected value' );
57-
58-
out = [ 0, 0 ];
59-
v = imuldw( NaN, NaN, out, 1, 0 );
60-
t.strictEqual( v, out, 'returns output array' );
61-
t.strictEqual( isnan( v[0] ), true, 'returns expected value' );
62-
t.strictEqual( isnan( v[1] ), true, 'returns expected value' );
63-
64-
t.end();
65-
});
66-
6741
tape( 'the function computes the double word product of two (signed) words', function test( t ) {
6842
var expected;
6943
var actual;

lib/node_modules/@stdlib/number/int32/base/muldw/test/test.main.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2524
var imuldw = require( './../lib/main.js' );
2625

2726

@@ -38,24 +37,6 @@ tape( 'main export is a function', function test( t ) {
3837
t.end();
3938
});
4039

41-
tape( 'the function returns `NaN` if provided `NaN`', function test( t ) {
42-
var v;
43-
44-
v = imuldw( NaN, 1 );
45-
t.strictEqual( isnan( v[0] ), true, 'returns expected value' );
46-
t.strictEqual( isnan( v[1] ), true, 'returns expected value' );
47-
48-
v = imuldw( 1, NaN );
49-
t.strictEqual( isnan( v[0] ), true, 'returns expected value' );
50-
t.strictEqual( isnan( v[1] ), true, 'returns expected value' );
51-
52-
v = imuldw( NaN, NaN );
53-
t.strictEqual( isnan( v[0] ), true, 'returns expected value' );
54-
t.strictEqual( isnan( v[1] ), true, 'returns expected value' );
55-
56-
t.end();
57-
});
58-
5940
tape( 'the function computes the double word product of two (signed) words', function test( t ) {
6041
var expected;
6142
var actual;

0 commit comments

Comments
 (0)