ndarray.js (4571B)
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 /* eslint-disable max-len */ 20 21 'use strict'; 22 23 // MODULES // 24 25 var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 26 var floor = require( '@stdlib/math/base/special/floor' ); 27 28 29 // VARIABLES // 30 31 // 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`.): 32 var BLOCKSIZE = 128; 33 34 35 // MAIN // 36 37 /** 38 * Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. 39 * 40 * ## Method 41 * 42 * - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`. 43 * 44 * ## References 45 * 46 * - 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). 47 * 48 * @param {PositiveInteger} N - number of indexed elements 49 * @param {number} alpha - constant 50 * @param {Float32Array} x - input array 51 * @param {integer} stride - stride length 52 * @param {NonNegativeInteger} offset - starting index 53 * @returns {number} sum 54 * 55 * @example 56 * var Float32Array = require( '@stdlib/array/float32' ); 57 * var floor = require( '@stdlib/math/base/special/floor' ); 58 * 59 * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 60 * var N = floor( x.length / 2 ); 61 * 62 * var v = sapxsumpw( N, 5.0, x, 2, 1 ); 63 * // returns 25.0 64 */ 65 function sapxsumpw( N, alpha, 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 i; 79 80 if ( N <= 0 ) { 81 return 0.0; 82 } 83 if ( N === 1 || stride === 0 ) { 84 return float64ToFloat32( alpha + 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 s = float64ToFloat32( s + float64ToFloat32( alpha + x[ ix ] ) ); 92 ix += stride; 93 } 94 return s; 95 } 96 if ( N <= BLOCKSIZE ) { 97 // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... 98 s0 = float64ToFloat32( alpha + x[ ix ] ); 99 s1 = float64ToFloat32( alpha + x[ ix+stride ] ); 100 s2 = float64ToFloat32( alpha + x[ ix+(2*stride) ] ); 101 s3 = float64ToFloat32( alpha + x[ ix+(3*stride) ] ); 102 s4 = float64ToFloat32( alpha + x[ ix+(4*stride) ] ); 103 s5 = float64ToFloat32( alpha + x[ ix+(5*stride) ] ); 104 s6 = float64ToFloat32( alpha + x[ ix+(6*stride) ] ); 105 s7 = float64ToFloat32( alpha + x[ ix+(7*stride) ] ); 106 ix += 8 * stride; 107 108 M = N % 8; 109 for ( i = 8; i < N-M; i += 8 ) { 110 s0 = float64ToFloat32( s0 + float64ToFloat32( alpha + x[ ix ] ) ); 111 s1 = float64ToFloat32( s1 + float64ToFloat32( alpha + x[ ix+stride ] ) ); 112 s2 = float64ToFloat32( s2 + float64ToFloat32( alpha + x[ ix+(2*stride) ] ) ); 113 s3 = float64ToFloat32( s3 + float64ToFloat32( alpha + x[ ix+(3*stride) ] ) ); 114 s4 = float64ToFloat32( s4 + float64ToFloat32( alpha + x[ ix+(4*stride) ] ) ); 115 s5 = float64ToFloat32( s5 + float64ToFloat32( alpha + x[ ix+(5*stride) ] ) ); 116 s6 = float64ToFloat32( s6 + float64ToFloat32( alpha + x[ ix+(6*stride) ] ) ); 117 s7 = float64ToFloat32( s7 + float64ToFloat32( alpha + x[ ix+(7*stride) ] ) ); 118 ix += 8 * stride; 119 } 120 // Pairwise sum the accumulators: 121 s = float64ToFloat32( float64ToFloat32( float64ToFloat32(s0+s1) + float64ToFloat32(s2+s3) ) + float64ToFloat32( float64ToFloat32(s4+s5) + float64ToFloat32(s6+s7) ) ); 122 123 // Clean-up loop... 124 for ( i; i < N; i++ ) { 125 s = float64ToFloat32( s + float64ToFloat32( alpha + x[ ix ] ) ); 126 ix += stride; 127 } 128 return s; 129 } 130 // Recurse by dividing by two, but avoiding non-multiples of unroll factor... 131 n = floor( N/2 ); 132 n -= n % 8; 133 return float64ToFloat32( sapxsumpw( n, alpha, x, stride, ix ) + sapxsumpw( N-n, alpha, x, stride, ix+(n*stride) ) ); 134 } 135 136 137 // EXPORTS // 138 139 module.exports = sapxsumpw;