ndarray.js (4191B)
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 var abs = require( '@stdlib/math/base/special/abs' ); 26 27 28 // VARIABLES // 29 30 // 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`.): 31 var BLOCKSIZE = 128; 32 33 34 // MAIN // 35 36 /** 37 * Computes the sum of absolute values (L1 norm) of single-precision floating-point strided array elements using pairwise summation. 38 * 39 * ## Method 40 * 41 * - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`. 42 * 43 * ## References 44 * 45 * - 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). 46 * 47 * @param {PositiveInteger} N - number of indexed elements 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 = sasumpw( N, x, 2, 1 ); 61 * // returns 9.0 62 */ 63 function sasumpw( N, 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 abs( 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 = float64ToFloat32( s + abs( x[ ix ] ) ); 90 ix += stride; 91 } 92 return 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 = abs( x[ ix ] ); 97 s1 = abs( x[ ix+stride ] ); 98 s2 = abs( x[ ix+(2*stride) ] ); 99 s3 = abs( x[ ix+(3*stride) ] ); 100 s4 = abs( x[ ix+(4*stride) ] ); 101 s5 = abs( x[ ix+(5*stride) ] ); 102 s6 = abs( x[ ix+(6*stride) ] ); 103 s7 = abs( x[ ix+(7*stride) ] ); 104 ix += 8 * stride; 105 106 M = N % 8; 107 for ( i = 8; i < N-M; i += 8 ) { 108 s0 = float64ToFloat32( s0 + abs( x[ ix ] ) ); 109 s1 = float64ToFloat32( s1 + abs( x[ ix+stride ] ) ); 110 s2 = float64ToFloat32( s2 + abs( x[ ix+(2*stride) ] ) ); 111 s3 = float64ToFloat32( s3 + abs( x[ ix+(3*stride) ] ) ); 112 s4 = float64ToFloat32( s4 + abs( x[ ix+(4*stride) ] ) ); 113 s5 = float64ToFloat32( s5 + abs( x[ ix+(5*stride) ] ) ); 114 s6 = float64ToFloat32( s6 + abs( x[ ix+(6*stride) ] ) ); 115 s7 = float64ToFloat32( s7 + abs( x[ ix+(7*stride) ] ) ); 116 ix += 8 * stride; 117 } 118 // Pairwise sum the accumulators: 119 s = float64ToFloat32( float64ToFloat32( float64ToFloat32(s0+s1) + float64ToFloat32(s2+s3) ) + float64ToFloat32( float64ToFloat32(s4+s5) + float64ToFloat32(s6+s7) ) ); // eslint-disable-line max-len 120 121 // Clean-up loop... 122 for ( i; i < N; i++ ) { 123 s = float64ToFloat32( s + abs( x[ ix ] ) ); 124 ix += stride; 125 } 126 return 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( sasumpw( n, x, stride, ix ) + sasumpw( N-n, x, stride, ix+(n*stride) ) ); // eslint-disable-line max-len 132 } 133 134 135 // EXPORTS // 136 137 module.exports = sasumpw;