time-to-botec

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

dcusumkbn2.c (2329B)


      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 #include "stdlib/blas/ext/base/dcusumkbn2.h"
     20 #include <stdint.h>
     21 #include <math.h>
     22 
     23 /**
     24 * Computes the cumulative sum of double-precision floating-point strided array elements using a second-order iterative Kahan–Babuška algorithm.
     25 *
     26 * ## Method
     27 *
     28 * -   This implementation uses a second-order iterative Kahan–Babuška algorithm, as described by Klein (2005).
     29 *
     30 * ## References
     31 *
     32 * -   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).
     33 *
     34 * @param N        number of indexed elements
     35 * @param sum      initial sum
     36 * @param X        input array
     37 * @param strideX  X stride length
     38 * @param Y        output array
     39 * @param strideY  Y stride length
     40 */
     41 void stdlib_strided_dcusumkbn2( const int64_t N, const double sum, const double *X, const int64_t strideX, double *Y, const int64_t strideY ) {
     42 	double ccs;
     43 	int64_t ix;
     44 	int64_t iy;
     45 	int64_t i;
     46 	double cs;
     47 	double cc;
     48 	double v;
     49 	double t;
     50 	double c;
     51 	double s;
     52 
     53 	if ( N <= 0 ) {
     54 		return;
     55 	}
     56 	if ( strideX < 0 ) {
     57 		ix = (1-N) * strideX;
     58 	} else {
     59 		ix = 0;
     60 	}
     61 	if ( strideY < 0 ) {
     62 		iy = (1-N) * strideY;
     63 	} else {
     64 		iy = 0;
     65 	}
     66 	s = sum;
     67 	ccs = 0.0; // second order correction term for lost lower order bits
     68 	cs = 0.0; // first order correction term for lost low order bits
     69 	for ( i = 0; i < N; i++ ) {
     70 		v = X[ ix ];
     71 		t = s + v;
     72 		if ( fabs( s ) >= fabs( v ) ) {
     73 			c = (s-t) + v;
     74 		} else {
     75 			c = (v-t) + s;
     76 		}
     77 		s = t;
     78 		t = cs + c;
     79 		if ( fabs( cs ) >= fabs( c ) ) {
     80 			cc = (cs-t) + c;
     81 		} else {
     82 			cc = (c-t) + cs;
     83 		}
     84 		cs = t;
     85 		ccs += cc;
     86 
     87 		Y[ iy ] = s + cs + ccs;
     88 		ix += strideX;
     89 		iy += strideY;
     90 	}
     91 	return;
     92 }