|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var isnan = require( '@stdlib/math/base/assert/is-nan' ); |
| 24 | +var floor = require( '@stdlib/math/base/special/floor' ); |
| 25 | + |
| 26 | + |
| 27 | +// VARIABLES // |
| 28 | + |
| 29 | +// Blocksize for pairwise summation (NOTE: decreasing the blocksize decreases rounding error as more pairs are summed, but also decreases performance. Because the inner loop is unrolled eight times, the blocksize is effectively `16`.): |
| 30 | +var BLOCKSIZE = 128; |
| 31 | + |
| 32 | + |
| 33 | +// MAIN // |
| 34 | + |
| 35 | +/** |
| 36 | +* Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation. |
| 37 | +* |
| 38 | +* ## Method |
| 39 | +* |
| 40 | +* - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`. |
| 41 | +* |
| 42 | +* ## References |
| 43 | +* |
| 44 | +* - Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050](https://doi.org/10.1137/0914050). |
| 45 | +* |
| 46 | +* @private |
| 47 | +* @param {PositiveInteger} N - number of indexed elements |
| 48 | +* @param {Object} x - input array object |
| 49 | +* @param {Collection} x.data - input array data |
| 50 | +* @param {Array<Function>} x.accessors - array element accessors |
| 51 | +* @param {integer} strideX - stride length for `x` |
| 52 | +* @param {NonNegativeInteger} offsetX - starting index for `x` |
| 53 | +* @returns {number} sum |
| 54 | +* |
| 55 | +* @example |
| 56 | +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); |
| 57 | +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); |
| 58 | +* |
| 59 | +* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); |
| 60 | +* |
| 61 | +* var v = gnansumpw( 4, arraylike2object( x ), 2, 1 ); |
| 62 | +* // returns 5.0 |
| 63 | +*/ |
| 64 | +function gnansumpw( N, x, strideX, offsetX ) { |
| 65 | + var xbuf; |
| 66 | + var xget; |
| 67 | + var ix; |
| 68 | + var s0; |
| 69 | + var s1; |
| 70 | + var s2; |
| 71 | + var s3; |
| 72 | + var s4; |
| 73 | + var s5; |
| 74 | + var s6; |
| 75 | + var s7; |
| 76 | + var M; |
| 77 | + var s; |
| 78 | + var n; |
| 79 | + var v; |
| 80 | + var i; |
| 81 | + |
| 82 | + // Cache reference to array data: |
| 83 | + xbuf = x.data; |
| 84 | + |
| 85 | + // Cache reference to the element accessors: |
| 86 | + xget = x.accessors[ 0 ]; |
| 87 | + |
| 88 | + ix = offsetX; |
| 89 | + if ( strideX === 0 ) { |
| 90 | + v = xget( xbuf, ix ); |
| 91 | + if ( isnan( v ) ) { |
| 92 | + return 0.0; |
| 93 | + } |
| 94 | + return N * v; |
| 95 | + } |
| 96 | + if ( N < 8 ) { |
| 97 | + // Use simple summation... |
| 98 | + s = 0.0; |
| 99 | + for ( i = 0; i < N; i++ ) { |
| 100 | + v = xget( xbuf, ix ); |
| 101 | + if ( isnan( v ) === false ) { |
| 102 | + s += v; |
| 103 | + } |
| 104 | + ix += strideX; |
| 105 | + } |
| 106 | + return s; |
| 107 | + } |
| 108 | + if ( N <= BLOCKSIZE ) { |
| 109 | + // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... |
| 110 | + v = xget( xbuf, ix ); |
| 111 | + s0 = ( isnan( v ) ) ? 0.0 : v; |
| 112 | + ix += strideX; |
| 113 | + v = xget( xbuf, ix ); |
| 114 | + s1 = ( isnan( v ) ) ? 0.0 : v; |
| 115 | + ix += strideX; |
| 116 | + v = xget( xbuf, ix ); |
| 117 | + s2 = ( isnan( v ) ) ? 0.0 : v; |
| 118 | + ix += strideX; |
| 119 | + v = xget( xbuf, ix ); |
| 120 | + s3 = ( isnan( v ) ) ? 0.0 : v; |
| 121 | + ix += strideX; |
| 122 | + v = xget( xbuf, ix ); |
| 123 | + s4 = ( isnan( v ) ) ? 0.0 : v; |
| 124 | + ix += strideX; |
| 125 | + v = xget( xbuf, ix ); |
| 126 | + s5 = ( isnan( v ) ) ? 0.0 : v; |
| 127 | + ix += strideX; |
| 128 | + v = xget( xbuf, ix ); |
| 129 | + s6 = ( isnan( v ) ) ? 0.0 : v; |
| 130 | + ix += strideX; |
| 131 | + v = xget( xbuf, ix ); |
| 132 | + s7 = ( isnan( v ) ) ? 0.0 : v; |
| 133 | + ix += strideX; |
| 134 | + |
| 135 | + M = N % 8; |
| 136 | + for ( i = 8; i < N-M; i += 8 ) { |
| 137 | + v = xget( xbuf, ix ); |
| 138 | + s0 += ( isnan( v ) ) ? 0.0 : v; |
| 139 | + ix += strideX; |
| 140 | + v = xget( xbuf, ix ); |
| 141 | + s1 += ( isnan( v ) ) ? 0.0 : v; |
| 142 | + ix += strideX; |
| 143 | + v = xget( xbuf, ix ); |
| 144 | + s2 += ( isnan( v ) ) ? 0.0 : v; |
| 145 | + ix += strideX; |
| 146 | + v = xget( xbuf, ix ); |
| 147 | + s3 += ( isnan( v ) ) ? 0.0 : v; |
| 148 | + ix += strideX; |
| 149 | + v = xget( xbuf, ix ); |
| 150 | + s4 += ( isnan( v ) ) ? 0.0 : v; |
| 151 | + ix += strideX; |
| 152 | + v = xget( xbuf, ix ); |
| 153 | + s5 += ( isnan( v ) ) ? 0.0 : v; |
| 154 | + ix += strideX; |
| 155 | + v = xget( xbuf, ix ); |
| 156 | + s6 += ( isnan( v ) ) ? 0.0 : v; |
| 157 | + ix += strideX; |
| 158 | + v = xget( xbuf, ix ); |
| 159 | + s7 += ( isnan( v ) ) ? 0.0 : v; |
| 160 | + ix += strideX; |
| 161 | + } |
| 162 | + // Pairwise sum the accumulators: |
| 163 | + s = ( (s0+s1) + (s2+s3) ) + ( (s4+s5) + (s6+s7) ); |
| 164 | + |
| 165 | + // Clean-up loop... |
| 166 | + for ( i; i < N; i++ ) { |
| 167 | + v = xget( xbuf, ix ); |
| 168 | + if ( isnan( v ) === false ) { |
| 169 | + s += v; |
| 170 | + } |
| 171 | + ix += strideX; |
| 172 | + } |
| 173 | + return s; |
| 174 | + } |
| 175 | + // Recurse by dividing by two, but avoiding non-multiples of unroll factor... |
| 176 | + n = floor( N/2 ); |
| 177 | + n -= n % 8; |
| 178 | + return gnansumpw( n, x, strideX, ix ) + gnansumpw( N-n, x, strideX, ix+(n*strideX) ); // eslint-disable-line max-len |
| 179 | +} |
| 180 | + |
| 181 | + |
| 182 | +// EXPORTS // |
| 183 | + |
| 184 | +module.exports = gnansumpw; |
0 commit comments