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