time-to-botec

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

sapxsumkbn2.c (2239B)


      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/sapxsumkbn2.h"
     20 #include <stdint.h>
     21 #include <math.h>
     22 
     23 /**
     24 * 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.
     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 alpha   constant
     36 * @param X       input array
     37 * @param stride  stride length
     38 * @return        output value
     39 */
     40 float stdlib_strided_sapxsumkbn2( const int64_t N, const float alpha, const float *X, const int64_t stride ) {
     41 	int64_t ix;
     42 	int64_t i;
     43 	float sum;
     44 	float ccs;
     45 	float cs;
     46 	float cc;
     47 	float v;
     48 	float t;
     49 	float c;
     50 
     51 	if ( N <= 0 ) {
     52 		return 0.0f;
     53 	}
     54 	if ( N == 1 || stride == 0 ) {
     55 		return alpha + X[ 0 ];
     56 	}
     57 	if ( stride < 0 ) {
     58 		ix = (1-N) * stride;
     59 	} else {
     60 		ix = 0;
     61 	}
     62 	sum = 0.0f;
     63 	ccs = 0.0f; // second order correction term for lost lower order bits
     64 	cs = 0.0f; // first order correction term for lost low order bits
     65 	for ( i = 0; i < N; i++ ) {
     66 		v = alpha + X[ ix ];
     67 		t = sum + v;
     68 		if ( fabsf( sum ) >= fabsf( v ) ) {
     69 			c = (sum-t) + v;
     70 		} else {
     71 			c = (v-t) + sum;
     72 		}
     73 		sum = t;
     74 		t = cs + c;
     75 		if ( fabsf( cs ) >= fabsf( c ) ) {
     76 			cc = (cs-t) + c;
     77 		} else {
     78 			cc = (c-t) + cs;
     79 		}
     80 		cs = t;
     81 		ccs += cc;
     82 		ix += stride;
     83 	}
     84 	return sum + cs + ccs;
     85 }