time-to-botec

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

scusumkbn.c (2062B)


      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/scusumkbn.h"
     20 #include <stdint.h>
     21 #include <math.h>
     22 
     23 /**
     24 * Computes the cumulative sum of single-precision floating-point strided array elements using an improved Kahan–Babuška algorithm.
     25 *
     26 * ## Method
     27 *
     28 * -   This implementation uses an "improved Kahan–Babuška algorithm", as described by Neumaier (1974).
     29 *
     30 * ## References
     31 *
     32 * -   Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106](https://doi.org/10.1002/zamm.19740540106).
     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_scusumkbn( const int64_t N, const float sum, const float *X, const int64_t strideX, float *Y, const int64_t strideY ) {
     42 	int64_t ix;
     43 	int64_t iy;
     44 	int64_t i;
     45 	float s;
     46 	float v;
     47 	float t;
     48 	float c;
     49 
     50 	if ( N <= 0 ) {
     51 		return;
     52 	}
     53 	if ( strideX < 0 ) {
     54 		ix = (1-N) * strideX;
     55 	} else {
     56 		ix = 0;
     57 	}
     58 	if ( strideY < 0 ) {
     59 		iy = (1-N) * strideY;
     60 	} else {
     61 		iy = 0;
     62 	}
     63 	s = sum;
     64 	c = 0.0f;
     65 	for ( i = 0; i < N; i++ ) {
     66 		v = X[ ix ];
     67 		t = s + v;
     68 		if ( fabsf( s ) >= fabsf( v ) ) {
     69 			c += (s-t) + v;
     70 		} else {
     71 			c += (v-t) + s;
     72 		}
     73 		s = t;
     74 		Y[ iy ] = s + c;
     75 		ix += strideX;
     76 		iy += strideY;
     77 	}
     78 	return;
     79 }