ndarray.js (4471B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 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 float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 24 var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); 25 var floor = require( '@stdlib/math/base/special/floor' ); 26 27 28 // VARIABLES // 29 30 // 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`.): 31 var BLOCKSIZE = 128; 32 33 34 // MAIN // 35 36 /** 37 * Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation with extended accumulation. 38 * 39 * ## Method 40 * 41 * - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`. 42 * 43 * ## References 44 * 45 * - 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). 46 * 47 * @param {PositiveInteger} N - number of indexed elements 48 * @param {Float32Array} x - input array 49 * @param {integer} stride - stride length 50 * @param {NonNegativeInteger} offset - starting index 51 * @returns {number} sum 52 * 53 * @example 54 * var Float32Array = require( '@stdlib/array/float32' ); 55 * var floor = require( '@stdlib/math/base/special/floor' ); 56 * 57 * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); 58 * var N = floor( x.length / 2 ); 59 * 60 * var v = sdsnansumpw( N, x, 2, 1 ); 61 * // returns 5.0 62 */ 63 function sdsnansumpw( N, x, stride, offset ) { 64 var ix; 65 var s0; 66 var s1; 67 var s2; 68 var s3; 69 var s4; 70 var s5; 71 var s6; 72 var s7; 73 var M; 74 var s; 75 var n; 76 var i; 77 78 if ( N <= 0 ) { 79 return 0.0; 80 } 81 if ( N === 1 || stride === 0 ) { 82 if ( isnanf( x[ offset ] ) ) { 83 return 0.0; 84 } 85 return x[ offset ]; 86 } 87 ix = offset; 88 if ( N < 8 ) { 89 // Use simple summation... 90 s = 0.0; 91 for ( i = 0; i < N; i++ ) { 92 if ( isnanf( x[ ix ] ) === false ) { 93 s += x[ ix ]; 94 } 95 ix += stride; 96 } 97 return float64ToFloat32( s ); 98 } 99 if ( N <= BLOCKSIZE ) { 100 // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... 101 s0 = ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 102 ix += stride; 103 s1 = ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 104 ix += stride; 105 s2 = ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 106 ix += stride; 107 s3 = ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 108 ix += stride; 109 s4 = ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 110 ix += stride; 111 s5 = ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 112 ix += stride; 113 s6 = ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 114 ix += stride; 115 s7 = ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 116 ix += stride; 117 118 M = N % 8; 119 for ( i = 8; i < N-M; i += 8 ) { 120 s0 += ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 121 ix += stride; 122 s1 += ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 123 ix += stride; 124 s2 += ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 125 ix += stride; 126 s3 += ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 127 ix += stride; 128 s4 += ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 129 ix += stride; 130 s5 += ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 131 ix += stride; 132 s6 += ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 133 ix += stride; 134 s7 += ( isnanf( x[ ix ] ) ) ? 0.0 : x[ ix ]; 135 ix += stride; 136 } 137 // Pairwise sum the accumulators: 138 s = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7)); 139 140 // Clean-up loop... 141 for ( i; i < N; i++ ) { 142 if ( isnanf( x[ ix ] ) === false ) { 143 s += x[ ix ]; 144 } 145 ix += stride; 146 } 147 return float64ToFloat32( s ); 148 } 149 // Recurse by dividing by two, but avoiding non-multiples of unroll factor... 150 n = floor( N/2 ); 151 n -= n % 8; 152 return float64ToFloat32( sdsnansumpw( n, x, stride, ix ) + sdsnansumpw( N-n, x, stride, ix+(n*stride) ) ); // eslint-disable-line max-len 153 } 154 155 156 // EXPORTS // 157 158 module.exports = sdsnansumpw;