time-to-botec

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

dnansumkbn2.c (2340B)


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