time-to-botec

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

dsapxsumpw.c (3712B)


      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/dsapxsumpw.h"
     20 #include <stdint.h>
     21 
     22 /**
     23 * Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation with extended accumulation and returning an extended precision result.
     24 *
     25 * ## Method
     26 *
     27 * -   This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`.
     28 *
     29 * ## References
     30 *
     31 * -   Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050](https://doi.org/10.1137/0914050).
     32 *
     33 * @param N       number of indexed elements
     34 * @param alpha   constant
     35 * @param X       input array
     36 * @param stride  stride length
     37 * @return        output value
     38 */
     39 double stdlib_strided_dsapxsumpw( const int64_t N, const float alpha, const float *X, const int64_t stride ) {
     40 	float *xp1;
     41 	float *xp2;
     42 	double sum;
     43 	int64_t ix;
     44 	int64_t M;
     45 	int64_t n;
     46 	int64_t i;
     47 	double s0;
     48 	double s1;
     49 	double s2;
     50 	double s3;
     51 	double s4;
     52 	double s5;
     53 	double s6;
     54 	double s7;
     55 	double a;
     56 
     57 	if ( N <= 0 ) {
     58 		return 0.0;
     59 	}
     60 	a = (double)alpha;
     61 	if ( N == 1 || stride == 0 ) {
     62 		return a + (double)X[ 0 ];
     63 	}
     64 	if ( stride < 0 ) {
     65 		ix = (1-N) * stride;
     66 	} else {
     67 		ix = 0;
     68 	}
     69 	if ( N < 8 ) {
     70 		// Use simple summation...
     71 		sum = 0.0;
     72 		for ( i = 0; i < N; i++ ) {
     73 			sum += a + (double)X[ ix ];
     74 			ix += stride;
     75 		}
     76 		return sum;
     77 	}
     78 	// Blocksize for pairwise summation: 128 (NOTE: decreasing the blocksize decreases rounding error as more pairs are summed, but also decreases performance. Because the inner loop is unrolled eight times, the blocksize is effectively `16`.)
     79 	if ( N <= 128 ) {
     80 		// Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)...
     81 		s0 = a + (double)X[ ix ];
     82 		s1 = a + (double)X[ ix+stride ];
     83 		s2 = a + (double)X[ ix+(2*stride) ];
     84 		s3 = a + (double)X[ ix+(3*stride) ];
     85 		s4 = a + (double)X[ ix+(4*stride) ];
     86 		s5 = a + (double)X[ ix+(5*stride) ];
     87 		s6 = a + (double)X[ ix+(6*stride) ];
     88 		s7 = a + (double)X[ ix+(7*stride) ];
     89 		ix += 8 * stride;
     90 
     91 		M = N % 8;
     92 		for ( i = 8; i < N-M; i += 8 ) {
     93 			s0 += a + (double)X[ ix ];
     94 			s1 += a + (double)X[ ix+stride ];
     95 			s2 += a + (double)X[ ix+(2*stride) ];
     96 			s3 += a + (double)X[ ix+(3*stride) ];
     97 			s4 += a + (double)X[ ix+(4*stride) ];
     98 			s5 += a + (double)X[ ix+(5*stride) ];
     99 			s6 += a + (double)X[ ix+(6*stride) ];
    100 			s7 += a + (double)X[ ix+(7*stride) ];
    101 			ix += 8 * stride;
    102 		}
    103 		// Pairwise sum the accumulators:
    104 		sum = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7));
    105 
    106 		// Clean-up loop...
    107 		for (; i < N; i++ ) {
    108 			sum += a + (double)X[ ix ];
    109 			ix += stride;
    110 		}
    111 		return sum;
    112 	}
    113 	// Recurse by dividing by two, but avoiding non-multiples of unroll factor...
    114 	n = N / 2;
    115 	n -= n % 8;
    116 	if ( stride < 0 ) {
    117 		xp1 = (float *)X + ( (n-N)*stride );
    118 		xp2 = (float *)X;
    119 	} else {
    120 		xp1 = (float *)X;
    121 		xp2 = (float *)X + ( n*stride );
    122 	}
    123 	return stdlib_strided_dsapxsumpw( n, alpha, xp1, stride ) + stdlib_strided_dsapxsumpw( N-n, alpha, xp2, stride );
    124 }