Skip to content

Commit 8dbf11d

Browse files
refactor!: remove NaN checks and optimize performance
PR-URL: #11702 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> BREAKING CHANGE: remove `NaN` checks To migrate, users should explicitly perform `NaN` checks before invoking this API and its `assign` method.
1 parent 8a85314 commit 8dbf11d

4 files changed

Lines changed: 18 additions & 60 deletions

File tree

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

Lines changed: 16 additions & 6 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 umuldw = 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': 'uint32'
41+
});
42+
3843
b.tic();
3944
for ( i = 0; i < b.iterations; i++ ) {
40-
x = minstd();
41-
y = umuldw( x, x );
45+
z = x[ i%x.length ];
46+
y = umuldw( 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

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

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

lib/node_modules/@stdlib/number/uint32/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 umul = require( '@stdlib/number/uint32/base/mul' );
2424

2525

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

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

@@ -72,7 +66,6 @@ function umuldw(a, b, out, stride, offset ) {
7266
lb = ( b & LOW_WORD_MASK ) >>> 0;
7367

7468
t = ( la*lb ) >>> 0;
75-
w3 = ( t & LOW_WORD_MASK ) >>> 0;
7669
k = ( t >>> 16 ) >>> 0;
7770

7871
t = ( ( ha*lb ) + k ) >>> 0;
@@ -83,7 +76,7 @@ function umuldw(a, b, out, stride, offset ) {
8376
k = ( t >>> 16 ) >>> 0;
8477

8578
out[ offset ] = ( ( ha*hb ) + w1 + k ) >>> 0; // compute the higher 32 bits and cast to an unsigned 32-bit integer
86-
out[ offset + stride ] = ( ( t << 16 ) + w3) >>> 0; // compute the lower 32 bits and cast to an unsigned 32-bit integer
79+
out[ offset + stride ] = umul( a, b ) >>> 0; // compute the lower 32 bits and cast to an unsigned 32-bit integer
8780

8881
return out;
8982
}

lib/node_modules/@stdlib/number/uint32/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 umuldw = 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 = umuldw( 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 = umuldw( 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 = umuldw( 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 (unsigned) words', function test( t ) {
6842
var expected;
6943
var actual;

lib/node_modules/@stdlib/number/uint32/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 umuldw = 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 = umuldw( 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 = umuldw( 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 = umuldw( 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 (unsigned) words', function test( t ) {
6041
var expected;
6142
var actual;

0 commit comments

Comments
 (0)