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