dnansumpw.js (4422B)
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 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 a double-precision floating-point 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 {NumericArray} out - two-element output array whose first element is the accumulated sum and whose second element is the accumulated number of summed values 49 * @param {Float64Array} x - input array 50 * @param {integer} stride - stride length 51 * @param {NonNegativeInteger} offset - starting index 52 * @returns {NumericArray} output array 53 * 54 * @example 55 * var Float64Array = require( '@stdlib/array/float64' ); 56 * var floor = require( '@stdlib/math/base/special/floor' ); 57 * 58 * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); 59 * var N = floor( x.length / 2 ); 60 * 61 * var out = [ 0.0, 0 ]; 62 * var v = dnansumpw( N, out, x, 2, 1 ); 63 * // returns [ 5.0, 4 ] 64 */ 65 function dnansumpw( N, out, x, stride, offset ) { 66 var ix; 67 var s0; 68 var s1; 69 var s2; 70 var s3; 71 var s4; 72 var s5; 73 var s6; 74 var s7; 75 var M; 76 var s; 77 var n; 78 var v; 79 var i; 80 81 if ( N <= 0 ) { 82 return out; 83 } 84 if ( N === 1 || stride === 0 ) { 85 if ( isnan( x[ offset ] ) ) { 86 return out; 87 } 88 out[ 0 ] += x[ offset ]; 89 out[ 1 ] += 1; 90 return out; 91 } 92 ix = offset; 93 if ( N < 8 ) { 94 // Use simple summation... 95 s = 0.0; 96 n = 0; 97 for ( i = 0; i < N; i++ ) { 98 v = x[ ix ]; 99 if ( v === v ) { 100 s += v; 101 n += 1; 102 } 103 ix += stride; 104 } 105 out[ 0 ] += s; 106 out[ 1 ] += n; 107 return out; 108 } 109 if ( N <= BLOCKSIZE ) { 110 // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... 111 s0 = 0.0; 112 s1 = 0.0; 113 s2 = 0.0; 114 s3 = 0.0; 115 s4 = 0.0; 116 s5 = 0.0; 117 s6 = 0.0; 118 s7 = 0.0; 119 n = 0; 120 121 M = N % 8; 122 for ( i = 0; i < N-M; i += 8 ) { 123 v = x[ ix ]; 124 if ( v === v ) { 125 s0 += v; 126 n += 1; 127 } 128 ix += stride; 129 v = x[ ix ]; 130 if ( v === v ) { 131 s1 += v; 132 n += 1; 133 } 134 ix += stride; 135 v = x[ ix ]; 136 if ( v === v ) { 137 s2 += v; 138 n += 1; 139 } 140 ix += stride; 141 v = x[ ix ]; 142 if ( v === v ) { 143 s3 += v; 144 n += 1; 145 } 146 ix += stride; 147 v = x[ ix ]; 148 if ( v === v ) { 149 s4 += v; 150 n += 1; 151 } 152 ix += stride; 153 v = x[ ix ]; 154 if ( v === v ) { 155 s5 += v; 156 n += 1; 157 } 158 ix += stride; 159 v = x[ ix ]; 160 if ( v === v ) { 161 s6 += v; 162 n += 1; 163 } 164 ix += stride; 165 v = x[ ix ]; 166 if ( v === v ) { 167 s7 += v; 168 n += 1; 169 } 170 ix += stride; 171 } 172 // Pairwise sum the accumulators: 173 s = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7)); 174 175 // Clean-up loop... 176 for ( i; i < N; i++ ) { 177 v = x[ ix ]; 178 if ( v === v ) { 179 s += v; 180 n += 1; 181 } 182 ix += stride; 183 } 184 out[ 0 ] += s; 185 out[ 1 ] += n; 186 return out; 187 } 188 // Recurse by dividing by two, but avoiding non-multiples of unroll factor... 189 n = floor( N/2 ); 190 n -= n % 8; 191 return dnansumpw( n, out, x, stride, ix ) + dnansumpw( N-n, out, x, stride, ix+(n*stride) ); // eslint-disable-line max-len 192 } 193 194 195 // EXPORTS // 196 197 module.exports = dnansumpw;