Skip to content

Commit 1bbd885

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 67a91d6 commit 1bbd885

File tree

4 files changed

+60
-19
lines changed

4 files changed

+60
-19
lines changed

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

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

2121
// MODULES //
2222

23-
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
23+
var f32 = require( '@stdlib/number/float64/base/to-float32' );
2424
var absf = require( '@stdlib/math/base/special/absf' );
2525

2626

@@ -54,6 +54,7 @@ var absf = require( '@stdlib/math/base/special/absf' );
5454
function ssumkbn2( N, x, strideX, offsetX ) {
5555
var sum;
5656
var ccs;
57+
var flg;
5758
var ix;
5859
var cs;
5960
var cc;
@@ -67,31 +68,49 @@ function ssumkbn2( N, x, strideX, offsetX ) {
6768
}
6869
ix = offsetX;
6970
if ( strideX === 0 ) {
70-
return float64ToFloat32( N * x[ ix ] );
71+
return f32( N * x[ ix ] );
72+
}
73+
v = x[ ix ];
74+
sum = v;
75+
76+
// In order to preserve the sign of zero which can be lost during compensated summation below, find the first non-zero element...
77+
if ( sum === 0.0 ) {
78+
for ( i = 1; i < N; i++ ) {
79+
v = x[ ix ];
80+
if ( v !== 0.0 ) {
81+
flg = true;
82+
break;
83+
}
84+
sum = f32( sum + v );
85+
ix += strideX;
86+
}
87+
} else {
88+
flg = true;
89+
ix += strideX;
90+
i = 1;
7191
}
72-
sum = 0.0;
7392
ccs = 0.0; // second order correction term for lost low order bits
7493
cs = 0.0; // first order correction term for lost low order bits
75-
for ( i = 0; i < N; i++ ) {
94+
for ( ; i < N; i++ ) {
7695
v = x[ ix ];
77-
t = float64ToFloat32( sum+v );
96+
t = f32( sum+v );
7897
if ( absf( sum ) >= absf( v ) ) {
79-
c = float64ToFloat32( float64ToFloat32( sum-t ) + v );
98+
c = f32( f32( sum-t ) + v );
8099
} else {
81-
c = float64ToFloat32( float64ToFloat32( v-t ) + sum );
100+
c = f32( f32( v-t ) + sum );
82101
}
83102
sum = t;
84-
t = float64ToFloat32( cs+c );
103+
t = f32( cs+c );
85104
if ( absf( cs ) >= absf( c ) ) {
86-
cc = float64ToFloat32( float64ToFloat32( cs-t ) + c );
105+
cc = f32( f32( cs-t ) + c );
87106
} else {
88-
cc = float64ToFloat32( float64ToFloat32( c-t ) + cs );
107+
cc = f32( f32( c-t ) + cs );
89108
}
90109
cs = t;
91-
ccs = float64ToFloat32( ccs+cc );
110+
ccs = f32( ccs+cc );
92111
ix += strideX;
93112
}
94-
return float64ToFloat32( sum + float64ToFloat32( cs+ccs ) );
113+
return ( flg ) ? f32( sum + f32( cs+ccs ) ) : sum;
95114
}
96115

97116

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ float API_SUFFIX(stdlib_strided_ssumkbn2_ndarray)( const CBLAS_INT N, const floa
6969
float v;
7070
float t;
7171
float c;
72+
int flg;
7273

7374
if ( N <= 0 ) {
7475
return 0.0f;
@@ -77,10 +78,29 @@ float API_SUFFIX(stdlib_strided_ssumkbn2_ndarray)( const CBLAS_INT N, const floa
7778
if ( strideX == 0 ) {
7879
return N * X[ ix ];
7980
}
80-
sum = 0.0f;
81+
v = X[ ix ];
82+
sum = v;
83+
flg = 0;
84+
85+
// In order to preserve the sign of zero which can be lost during compensated summation below, find the first non-zero element...
86+
if ( sum == 0.0f ) {
87+
for ( i = 1; i < N; i++ ) {
88+
v = X[ ix ];
89+
if ( v != 0.0f ) {
90+
flg = 1;
91+
break;
92+
}
93+
sum += v;
94+
ix += strideX;
95+
}
96+
} else {
97+
flg = 1;
98+
ix += strideX;
99+
i = 1;
100+
}
81101
ccs = 0.0f; // second order correction term for lost lower order bits
82102
cs = 0.0f; // first order correction term for lost low order bits
83-
for ( i = 0; i < N; i++ ) {
103+
for ( ; i < N; i++ ) {
84104
v = X[ ix ];
85105
t = sum + v;
86106
if ( stdlib_base_absf( sum ) >= stdlib_base_absf( v ) ) {
@@ -99,5 +119,5 @@ float API_SUFFIX(stdlib_strided_ssumkbn2_ndarray)( const CBLAS_INT N, const floa
99119
ccs += cc;
100120
ix += strideX;
101121
}
102-
return sum + cs + ccs;
122+
return ( flg == 1 ) ? sum+cs+ccs : sum;
103123
}

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

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

2323
var tape = require( 'tape' );
2424
var isPositiveZerof = require( '@stdlib/math/base/assert/is-positive-zerof' );
25+
var isNegativeZerof = require( '@stdlib/math/base/assert/is-negative-zerof' );
2526
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2627
var Float32Array = require( '@stdlib/array/float32' );
2728
var ssumkbn2 = require( './../lib/ndarray.js' );
@@ -67,14 +68,14 @@ tape( 'the function calculates the sum of all strided array elements', function
6768
t.end();
6869
});
6970

70-
tape( 'the function does not preserve the sign of zero', function test( t ) {
71+
tape( 'the function preserves the sign of zero', function test( t ) {
7172
var x;
7273
var v;
7374

7475
x = new Float32Array( [ -0.0, -0.0, -0.0 ] );
7576

7677
v = ssumkbn2( x.length, x, 1, 0 );
77-
t.strictEqual( isPositiveZerof( v ), true, 'returns expected value' );
78+
t.strictEqual( isNegativeZerof( v ), true, 'returns expected value' );
7879

7980
x = new Float32Array( [ 0.0, -0.0, -0.0 ] );
8081

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var resolve = require( 'path' ).resolve;
2424
var tape = require( 'tape' );
2525
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var isPositiveZerof = require( '@stdlib/math/base/assert/is-positive-zerof' );
27+
var isNegativeZerof = require( '@stdlib/math/base/assert/is-negative-zerof' );
2728
var Float32Array = require( '@stdlib/array/float32' );
2829
var tryRequire = require( '@stdlib/utils/try-require' );
2930

@@ -76,14 +77,14 @@ tape( 'the function calculates the sum of all strided array elements', opts, fun
7677
t.end();
7778
});
7879

79-
tape( 'the function does not preserve the sign of zero', opts, function test( t ) {
80+
tape( 'the function preserves the sign of zero', opts, function test( t ) {
8081
var x;
8182
var v;
8283

8384
x = new Float32Array( [ -0.0, -0.0, -0.0 ] );
8485

8586
v = ssumkbn2( x.length, x, 1, 0 );
86-
t.strictEqual( isPositiveZerof( v ), true, 'returns expected value' );
87+
t.strictEqual( isNegativeZerof( v ), true, 'returns expected value' );
8788

8889
x = new Float32Array( [ 0.0, -0.0, -0.0 ] );
8990

0 commit comments

Comments
 (0)