time-to-botec

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

ndarray.js (2921B)


      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 abs = require( '@stdlib/math/base/special/abs' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Adds a constant to each single-precision floating-point strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm.
     31 *
     32 * ## Method
     33 *
     34 * -   This implementation uses a second-order iterative Kahan–Babuška algorithm, as described by Klein (2005).
     35 *
     36 * ## References
     37 *
     38 * -   Klein, Andreas. 2005. "A Generalized Kahan-Babuška-Summation-Algorithm." _Computing_ 76 (3): 279–93. doi:[10.1007/s00607-005-0139-x](https://doi.org/10.1007/s00607-005-0139-x).
     39 *
     40 * @param {PositiveInteger} N - number of indexed elements
     41 * @param {number} alpha - constant
     42 * @param {Float32Array} x - input array
     43 * @param {integer} stride - stride length
     44 * @param {NonNegativeInteger} offset - starting index
     45 * @returns {number} sum
     46 *
     47 * @example
     48 * var Float32Array = require( '@stdlib/array/float32' );
     49 * var floor = require( '@stdlib/math/base/special/floor' );
     50 *
     51 * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
     52 * var N = floor( x.length / 2 );
     53 *
     54 * var v = sapxsumkbn2( N, 5.0, x, 2, 1 );
     55 * // returns 25.0
     56 */
     57 function sapxsumkbn2( N, alpha, x, stride, offset ) {
     58 	var sum;
     59 	var ccs;
     60 	var ix;
     61 	var cs;
     62 	var cc;
     63 	var v;
     64 	var t;
     65 	var c;
     66 	var i;
     67 
     68 	if ( N <= 0 ) {
     69 		return 0.0;
     70 	}
     71 	if ( N === 1 || stride === 0 ) {
     72 		return float64ToFloat32( alpha + x[ offset ] );
     73 	}
     74 	ix = offset;
     75 	sum = 0.0;
     76 	ccs = 0.0; // second order correction term for lost low order bits
     77 	cs = 0.0; // first order correction term for lost low order bits
     78 	for ( i = 0; i < N; i++ ) {
     79 		v = float64ToFloat32( alpha + x[ ix ] );
     80 		t = float64ToFloat32( sum+v );
     81 		if ( abs( sum ) >= abs( v ) ) {
     82 			c = float64ToFloat32( float64ToFloat32( sum-t ) + v );
     83 		} else {
     84 			c = float64ToFloat32( float64ToFloat32( v-t ) + sum );
     85 		}
     86 		sum = t;
     87 		t = float64ToFloat32( cs+c );
     88 		if ( abs( cs ) >= abs( c ) ) {
     89 			cc = float64ToFloat32( float64ToFloat32( cs-t ) + c );
     90 		} else {
     91 			cc = float64ToFloat32( float64ToFloat32( c-t ) + cs );
     92 		}
     93 		cs = t;
     94 		ccs = float64ToFloat32( ccs+cc );
     95 		ix += stride;
     96 	}
     97 	return float64ToFloat32( sum + float64ToFloat32( cs+ccs ) );
     98 }
     99 
    100 
    101 // EXPORTS //
    102 
    103 module.exports = sapxsumkbn2;