Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 4x 4x 68x 68x 72x 2x 2x 2x 2x 2x 66x 72x 70x 70x 62x 62x 8x 8x 66x 72x 4x 4x 62x 62x 62x 62x 62x 62x 72x 4x 8x 8x 8x 8x 8x 8x 8x 8x 4x 62x 72x 10x 10x 10x 10x 52x 52x 72x 12x 12x 48x 48x 36x 36x 36x 48x 48x 12x 12x 12x 12x 72x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 230x 230x 230x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 22x 22x 22x 22x 22x 22x 22x 22x 22x 126x 126x 124x 124x 124x 126x 126x 22x 22x 22x 22x 18x 18x 18x 18x 18x 18x 18x 18x 18x 72x 3x 3x 3x 3x 3x | /** * @license Apache-2.0 * * Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 'use strict'; // MODULES // var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); var floor = require( '@stdlib/math/base/special/floor' ); // VARIABLES // // 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`.): var BLOCKSIZE = 128; // MAIN // /** * Computes the sum of a single-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation. * * ## Method * * - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`. * * ## References * * - 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). * * @private * @param {PositiveInteger} N - number of indexed elements * @param {Float32Array} x - input array * @param {integer} strideX - stride length for `x` * @param {NonNegativeInteger} offsetX - starting index for `x` * @param {Float32Array} out - two-element output array whose first element is the accumulated sum and whose second element is the accumulated number of summed values * @param {integer} strideOut - stride length for `out` * @param {NonNegativeInteger} offsetOut - starting index for `out` * @returns {Float32Array} output array * * @example * var Float32Array = require( '@stdlib/array/float32' ); * * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); * var out = new Float32Array( [ 0.0, 0 ] ); * * var v = sumpw( 5.0, x, 2, 1, out, 1, 0 ); * // returns <Float32Array>[ 5.0, 4 ] */ function sumpw( N, x, strideX, offsetX, out, strideOut, offsetOut ) { // eslint-disable-line max-statements var ix; var io; var s0; var s1; var s2; var s3; var s4; var s5; var s6; var s7; var t0; var t1; var t2; var t3; var M; var s; var n; var v; var i; if ( N <= 0 ) { return out; } io = offsetOut; ix = offsetX; if ( strideX === 0 ) { if ( isnanf( x[ ix ] ) ) { return out; } out[ io ] += float64ToFloat32( float64ToFloat32( x[ ix ] ) * N ); out[ io+strideOut ] += N; return out; } // Find the first non-NaN element... for ( i = 0; i < N; i++ ) { v = x[ ix ]; if ( isnanf( v ) === false ) { break; } ix += strideX; } // If every element was NaN, we are done... if ( i === N ) { return out; } n = 1; s = float64ToFloat32( v ); ix += strideX; i += 1; // In order to preserve the sign of zero which can be lost during pairwise summation below, find the first non-zero element... if ( s === 0.0 ) { for ( ; i < N; i++ ) { v = x[ ix ]; if ( isnanf( v ) === false ) { if ( v !== 0.0 ) { break; } s += float64ToFloat32( v ); n += 1; } ix += strideX; } } // If every subsequent element was either NaN or zero, we are done... if ( i === N ) { out[ io ] += float64ToFloat32( s ); out[ io+strideOut ] += n; return out; } // If we are here, then we found a non-zero element and we no longer have to be concerned about preserving zero's sign... if ( (N-i) < 8 ) { // Use simple summation... for ( ; i < N; i++ ) { v = x[ ix ]; if ( isnanf( v ) === false ) { s += float64ToFloat32( v ); n += 1; } ix += strideX; } out[ io ] += float64ToFloat32( s ); out[ io+strideOut ] += n; return out; } if ( (N-i) <= BLOCKSIZE ) { // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... s0 = -0.0; // note: negative zero in order to ensure sign preservation if all elements are negative zero s1 = -0.0; s2 = -0.0; s3 = -0.0; s4 = -0.0; s5 = -0.0; s6 = -0.0; s7 = -0.0; M = (N-i) % 8; for ( ; i < N-M; i += 8 ) { v = x[ ix ]; if ( isnanf( v ) === false ) { s0 += float64ToFloat32( v ); n += 1; } ix += strideX; v = x[ ix ]; if ( isnanf( v ) === false ) { s1 += float64ToFloat32( v ); n += 1; } ix += strideX; v = x[ ix ]; if ( isnanf( v ) === false ) { s2 += float64ToFloat32( v ); n += 1; } ix += strideX; v = x[ ix ]; if ( isnanf( v ) === false ) { s3 += float64ToFloat32( v ); n += 1; } ix += strideX; v = x[ ix ]; if ( isnanf( v ) === false ) { s4 += float64ToFloat32( v ); n += 1; } ix += strideX; v = x[ ix ]; if ( isnanf( v ) === false ) { s5 += float64ToFloat32( v ); n += 1; } ix += strideX; v = x[ ix ]; if ( isnanf( v ) === false ) { s6 += float64ToFloat32( v ); n += 1; } ix += strideX; v = x[ ix ]; if ( isnanf( v ) === false ) { s7 += float64ToFloat32( v ); n += 1; } ix += strideX; } // Pairwise sum the accumulators: t0 = float64ToFloat32( s0 ) + float64ToFloat32( s1 ); t1 = float64ToFloat32( s2 ) + float64ToFloat32( s3 ); t2 = float64ToFloat32( s4 ) + float64ToFloat32( s5 ); t3 = float64ToFloat32( s6 ) + float64ToFloat32( s7 ); s += float64ToFloat32( float64ToFloat32( t0 + t1 ) + float64ToFloat32( t2 + t3 ) ); // eslint-disable-line max-len // Clean-up loop... for ( ; i < N; i++ ) { v = float64ToFloat32( x[ ix ] ); if ( isnanf( v ) === false ) { s += float64ToFloat32( v ); n += 1; } ix += strideX; } out[ io ] += float64ToFloat32( s ); out[ io+strideOut ] += n; return out; } out[ io ] += float64ToFloat32( s ); out[ io+strideOut ] += n; // Recurse by dividing by two, but avoiding non-multiples of unroll factor... n = floor( (N-i)/2 ); n -= n % 8; sumpw( n, x, strideX, ix, out, strideOut, io ); sumpw( N-i-n, x, strideX, ix+(n*strideX), out, strideOut, io ); return out; } // EXPORTS // module.exports = sumpw; |