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