ndarray.js (3970B)
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 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 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 * @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 ] ); 57 * var N = floor( x.length / 2 ); 58 * 59 * var v = ssumpw( N, x, 2, 1 ); 60 * // returns 5.0 61 */ 62 function ssumpw( 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 return x[ offset ]; 82 } 83 ix = offset; 84 if ( N < 8 ) { 85 // Use simple summation... 86 s = 0.0; 87 for ( i = 0; i < N; i++ ) { 88 s = float64ToFloat32( s + x[ ix ] ); 89 ix += stride; 90 } 91 return s; 92 } 93 if ( N <= BLOCKSIZE ) { 94 // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... 95 s0 = x[ ix ]; 96 s1 = x[ ix+stride ]; 97 s2 = x[ ix+(2*stride) ]; 98 s3 = x[ ix+(3*stride) ]; 99 s4 = x[ ix+(4*stride) ]; 100 s5 = x[ ix+(5*stride) ]; 101 s6 = x[ ix+(6*stride) ]; 102 s7 = x[ ix+(7*stride) ]; 103 ix += 8 * stride; 104 105 M = N % 8; 106 for ( i = 8; i < N-M; i += 8 ) { 107 s0 = float64ToFloat32( s0 + x[ ix ] ); 108 s1 = float64ToFloat32( s1 + x[ ix+stride ] ); 109 s2 = float64ToFloat32( s2 + x[ ix+(2*stride) ] ); 110 s3 = float64ToFloat32( s3 + x[ ix+(3*stride) ] ); 111 s4 = float64ToFloat32( s4 + x[ ix+(4*stride) ] ); 112 s5 = float64ToFloat32( s5 + x[ ix+(5*stride) ] ); 113 s6 = float64ToFloat32( s6 + x[ ix+(6*stride) ] ); 114 s7 = float64ToFloat32( s7 + x[ ix+(7*stride) ] ); 115 ix += 8 * stride; 116 } 117 // Pairwise sum the accumulators: 118 s = float64ToFloat32( float64ToFloat32( float64ToFloat32(s0+s1) + float64ToFloat32(s2+s3) ) + float64ToFloat32( float64ToFloat32(s4+s5) + float64ToFloat32(s6+s7) ) ); // eslint-disable-line max-len 119 120 // Clean-up loop... 121 for ( i; i < N; i++ ) { 122 s = float64ToFloat32( s + x[ ix ] ); 123 ix += stride; 124 } 125 return s; 126 } 127 // Recurse by dividing by two, but avoiding non-multiples of unroll factor... 128 n = floor( N/2 ); 129 n -= n % 8; 130 return float64ToFloat32( ssumpw( n, x, stride, ix ) + ssumpw( N-n, x, stride, ix+(n*stride) ) ); // eslint-disable-line max-len 131 } 132 133 134 // EXPORTS // 135 136 module.exports = ssumpw;