Skip to content

Commit e20b7f5

Browse files
committed
fix: update implementation to preserve signed zeros
--- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent da339ee commit e20b7f5

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

lib/node_modules/@stdlib/blas/ext/base/dsumors/lib/ndarray.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,19 @@ function dsumors( N, x, strideX, offsetX ) {
4848
var m;
4949
var i;
5050

51-
sum = 0.0;
5251
if ( N <= 0 ) {
53-
return sum;
52+
return 0.0;
5453
}
5554
ix = offsetX;
5655
if ( strideX === 0 ) {
5756
return N * x[ ix ];
5857
}
58+
sum = x[ ix ];
59+
ix += strideX;
5960

6061
// If the stride is equal to `1`, use unrolled loops...
6162
if ( strideX === 1 ) {
62-
m = N % M;
63+
m = (N-1) % M;
6364

6465
// If we have a remainder, run a clean-up loop...
6566
if ( m > 0 ) {
@@ -71,13 +72,13 @@ function dsumors( N, x, strideX, offsetX ) {
7172
if ( N < M ) {
7273
return sum;
7374
}
74-
for ( i = m; i < N; i += M ) {
75+
for ( i = m; i < N-1; i += M ) {
7576
sum += x[ix] + x[ix+1] + x[ix+2] + x[ix+3] + x[ix+4] + x[ix+5];
7677
ix += M;
7778
}
7879
return sum;
7980
}
80-
for ( i = 0; i < N; i++ ) {
81+
for ( i = 1; i < N; i++ ) {
8182
sum += x[ ix ];
8283
ix += strideX;
8384
}

lib/node_modules/@stdlib/blas/ext/base/dsumors/src/main.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,19 @@ double API_SUFFIX(stdlib_strided_dsumors_ndarray)( const CBLAS_INT N, const doub
4848
CBLAS_INT i;
4949
double sum;
5050

51-
sum = 0.0;
5251
if ( N <= 0 ) {
53-
return sum;
52+
return 0.0;
5453
}
5554
ix = offsetX;
5655
if ( strideX == 0 ) {
5756
return N * X[ ix ];
5857
}
58+
sum = X[ ix ];
59+
ix += strideX;
60+
5961
// If the stride is equal to `1`, use unrolled loops...
6062
if ( strideX == 1 ) {
61-
m = N % 6;
63+
m = (N-1) % 6;
6264

6365
// If we have a remainder, run a clean-up loop...
6466
if ( m > 0 ) {
@@ -69,12 +71,12 @@ double API_SUFFIX(stdlib_strided_dsumors_ndarray)( const CBLAS_INT N, const doub
6971
if ( N < 6 ) {
7072
return sum;
7173
}
72-
for ( i = m; i < N; i += 6 ) {
74+
for ( i = m; i < N-1; i += 6 ) {
7375
sum += X[i] + X[i+1] + X[i+2] + X[i+3] + X[i+4] + X[i+5];
7476
}
7577
return sum;
7678
}
77-
for ( i = 0; i < N; i++ ) {
79+
for ( i = 1; i < N; i++ ) {
7880
sum += X[ ix ];
7981
ix += strideX;
8082
}

lib/node_modules/@stdlib/blas/ext/base/dsumors/test/test.ndarray.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var tape = require( 'tape' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
2526
var Float64Array = require( '@stdlib/array/float64' );
2627
var dsumors = require( './../lib/ndarray.js' );
2728

@@ -66,6 +67,17 @@ tape( 'the function calculates the sum of all strided array elements', function
6667
t.end();
6768
});
6869

70+
tape( 'the function preserves the sign of zero', function test( t ) {
71+
var x;
72+
var v;
73+
74+
x = new Float64Array( [ -0.0, -0.0, -0.0, -0.0, -0.0 ] );
75+
v = dsumors( x.length, x, 1, 0 );
76+
t.strictEqual( isNegativeZero( v ), true, 'returns expected value' );
77+
78+
t.end();
79+
});
80+
6981
tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0.0`', function test( t ) {
7082
var x;
7183
var v;

lib/node_modules/@stdlib/blas/ext/base/dsumors/test/test.ndarray.native.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var resolve = require( 'path' ).resolve;
2424
var tape = require( 'tape' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
2627
var Float64Array = require( '@stdlib/array/float64' );
2728
var tryRequire = require( '@stdlib/utils/try-require' );
2829

@@ -75,6 +76,17 @@ tape( 'the function calculates the sum of all strided array elements', opts, fun
7576
t.end();
7677
});
7778

79+
tape( 'the function preserves the sign of zero', opts, function test( t ) {
80+
var x;
81+
var v;
82+
83+
x = new Float64Array( [ -0.0, -0.0, -0.0, -0.0, -0.0 ] );
84+
v = dsumors( x.length, x, 1, 0 );
85+
t.strictEqual( isNegativeZero( v ), true, 'returns expected value' );
86+
87+
t.end();
88+
});
89+
7890
tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0.0`', opts, function test( t ) {
7991
var x;
8092
var v;

0 commit comments

Comments
 (0)