time-to-botec

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

dnannsumkbn2.c (2454B)


      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/dnannsumkbn2.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 * @param n       pointer for storing the number of non-NaN elements
     39 * @return        output value
     40 */
     41 double stdlib_strided_dnannsumkbn2( const int64_t N, const double *X, const int64_t stride, int64_t *n ) {
     42 	double sum;
     43 	double ccs;
     44 	int64_t ix;
     45 	int64_t i;
     46 	double cs;
     47 	double cc;
     48 	double v;
     49 	double t;
     50 	double c;
     51 
     52 	sum = 0.0;
     53 	*n = 0;
     54 	if ( N <= 0 ) {
     55 		return sum;
     56 	}
     57 	if ( N == 1 || stride == 0 ) {
     58 		if ( stdlib_base_is_nan( X[ 0 ] ) ) {
     59 			return sum;
     60 		}
     61 		*n += 1;
     62 		return X[ 0 ];
     63 	}
     64 	if ( stride < 0 ) {
     65 		ix = (1-N) * stride;
     66 	} else {
     67 		ix = 0;
     68 	}
     69 	ccs = 0.0; // second order correction term for lost lower order bits
     70 	cs = 0.0; // first order correction term for lost low order bits
     71 	for ( i = 0; i < N; i++ ) {
     72 		v = X[ ix ];
     73 		if ( !stdlib_base_is_nan( v ) ) {
     74 			t = sum + v;
     75 			if ( fabs( sum ) >= fabs( v ) ) {
     76 				c = (sum-t) + v;
     77 			} else {
     78 				c = (v-t) + sum;
     79 			}
     80 			sum = t;
     81 			t = cs + c;
     82 			if ( fabs( cs ) >= fabs( c ) ) {
     83 				cc = (cs-t) + c;
     84 			} else {
     85 				cc = (c-t) + cs;
     86 			}
     87 			cs = t;
     88 			ccs += cc;
     89 			*n += 1;
     90 		}
     91 		ix += stride;
     92 	}
     93 	return sum + cs + ccs;
     94 }