time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

snansumpw.js (4725B)


      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 a double-precision floating-point strided array elements, ignoring `NaN` values and 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 * @private
     47 * @param {PositiveInteger} N - number of indexed elements
     48 * @param {NumericArray} out - two-element output array whose first element is the accumulated sum and whose second element is the accumulated number of summed values
     49 * @param {Float32Array} x - input array
     50 * @param {integer} stride - stride length
     51 * @param {NonNegativeInteger} offset - starting index
     52 * @returns {NumericArray} output array
     53 *
     54 * @example
     55 * var Float32Array = require( '@stdlib/array/float32' );
     56 * var floor = require( '@stdlib/math/base/special/floor' );
     57 *
     58 * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
     59 * var N = floor( x.length / 2 );
     60 *
     61 * var out = [ 0.0, 0 ];
     62 * var v = snansumpw( N, out, x, 2, 1 );
     63 * // returns [ 5.0, 4 ]
     64 */
     65 function snansumpw( N, out, 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 v;
     79 	var i;
     80 
     81 	ix = offset;
     82 	if ( N < 8 ) {
     83 		// Use simple summation...
     84 		s = 0.0;
     85 		n = 0;
     86 		for ( i = 0; i < N; i++ ) {
     87 			v = x[ ix ];
     88 			if ( v === v ) {
     89 				s = float64ToFloat32( s + v );
     90 				n += 1;
     91 			}
     92 			ix += stride;
     93 		}
     94 		out[ 0 ] = float64ToFloat32( out[ 0 ] + s );
     95 		out[ 1 ] += n;
     96 		return out;
     97 	}
     98 	if ( N <= BLOCKSIZE ) {
     99 		// Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)...
    100 		s0 = 0.0;
    101 		s1 = 0.0;
    102 		s2 = 0.0;
    103 		s3 = 0.0;
    104 		s4 = 0.0;
    105 		s5 = 0.0;
    106 		s6 = 0.0;
    107 		s7 = 0.0;
    108 		n = 0;
    109 
    110 		M = N % 8;
    111 		for ( i = 0; i < N-M; i += 8 ) {
    112 			v = x[ ix ];
    113 			if ( v === v ) {
    114 				s0 = float64ToFloat32( s0 + v );
    115 				n += 1;
    116 			}
    117 			ix += stride;
    118 			v = x[ ix ];
    119 			if ( v === v ) {
    120 				s1 = float64ToFloat32( s1 + v );
    121 				n += 1;
    122 			}
    123 			ix += stride;
    124 			v = x[ ix ];
    125 			if ( v === v ) {
    126 				s2 = float64ToFloat32( s2 + v );
    127 				n += 1;
    128 			}
    129 			ix += stride;
    130 			v = x[ ix ];
    131 			if ( v === v ) {
    132 				s3 = float64ToFloat32( s3 + v );
    133 				n += 1;
    134 			}
    135 			ix += stride;
    136 			v = x[ ix ];
    137 			if ( v === v ) {
    138 				s4 = float64ToFloat32( s4 + v );
    139 				n += 1;
    140 			}
    141 			ix += stride;
    142 			v = x[ ix ];
    143 			if ( v === v ) {
    144 				s5 = float64ToFloat32( s5 + v );
    145 				n += 1;
    146 			}
    147 			ix += stride;
    148 			v = x[ ix ];
    149 			if ( v === v ) {
    150 				s6 = float64ToFloat32( s6 + v );
    151 				n += 1;
    152 			}
    153 			ix += stride;
    154 			v = x[ ix ];
    155 			if ( v === v ) {
    156 				s7 = float64ToFloat32( s7 + v );
    157 				n += 1;
    158 			}
    159 			ix += stride;
    160 		}
    161 		// Pairwise sum the accumulators:
    162 		s = float64ToFloat32( float64ToFloat32(float64ToFloat32(s0+s1) + float64ToFloat32(s2+s3)) + float64ToFloat32(float64ToFloat32(s4+s5) + float64ToFloat32(s6+s7)) ); // eslint-disable-line max-len
    163 
    164 		// Clean-up loop...
    165 		for ( i; i < N; i++ ) {
    166 			v = x[ ix ];
    167 			if ( v === v ) {
    168 				s = float64ToFloat32( s + v );
    169 				n += 1;
    170 			}
    171 			ix += stride;
    172 		}
    173 		out[ 0 ] = float64ToFloat32( out[ 0 ] + s );
    174 		out[ 1 ] += n;
    175 		return out;
    176 	}
    177 	// Recurse by dividing by two, but avoiding non-multiples of unroll factor...
    178 	n = floor( N/2 );
    179 	n -= n % 8;
    180 	return float64ToFloat32( snansumpw( n, out, x, stride, ix ) + snansumpw( N-n, out, x, stride, ix+(n*stride) ) ); // eslint-disable-line max-len
    181 }
    182 
    183 
    184 // EXPORTS //
    185 
    186 module.exports = snansumpw;