sapxsumpw.c (3523B)
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/sapxsumpw.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. 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 float stdlib_strided_sapxsumpw( const int64_t N, const float alpha, const float *X, const int64_t stride ) { 40 float *xp1; 41 float *xp2; 42 int64_t ix; 43 int64_t M; 44 int64_t n; 45 int64_t i; 46 float sum; 47 float s0; 48 float s1; 49 float s2; 50 float s3; 51 float s4; 52 float s5; 53 float s6; 54 float s7; 55 56 if ( N <= 0 ) { 57 return 0.0f; 58 } 59 if ( N == 1 || stride == 0 ) { 60 return alpha + X[ 0 ]; 61 } 62 if ( stride < 0 ) { 63 ix = (1-N) * stride; 64 } else { 65 ix = 0; 66 } 67 if ( N < 8 ) { 68 // Use simple summation... 69 sum = 0.0f; 70 for ( i = 0; i < N; i++ ) { 71 sum += alpha + X[ ix ]; 72 ix += stride; 73 } 74 return sum; 75 } 76 // 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`.) 77 if ( N <= 128 ) { 78 // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... 79 s0 = alpha + X[ ix ]; 80 s1 = alpha + X[ ix+stride ]; 81 s2 = alpha + X[ ix+(2*stride) ]; 82 s3 = alpha + X[ ix+(3*stride) ]; 83 s4 = alpha + X[ ix+(4*stride) ]; 84 s5 = alpha + X[ ix+(5*stride) ]; 85 s6 = alpha + X[ ix+(6*stride) ]; 86 s7 = alpha + X[ ix+(7*stride) ]; 87 ix += 8 * stride; 88 89 M = N % 8; 90 for ( i = 8; i < N-M; i += 8 ) { 91 s0 += alpha + X[ ix ]; 92 s1 += alpha + X[ ix+stride ]; 93 s2 += alpha + X[ ix+(2*stride) ]; 94 s3 += alpha + X[ ix+(3*stride) ]; 95 s4 += alpha + X[ ix+(4*stride) ]; 96 s5 += alpha + X[ ix+(5*stride) ]; 97 s6 += alpha + X[ ix+(6*stride) ]; 98 s7 += alpha + X[ ix+(7*stride) ]; 99 ix += 8 * stride; 100 } 101 // Pairwise sum the accumulators: 102 sum = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7)); 103 104 // Clean-up loop... 105 for (; i < N; i++ ) { 106 sum += alpha + X[ ix ]; 107 ix += stride; 108 } 109 return sum; 110 } 111 // Recurse by dividing by two, but avoiding non-multiples of unroll factor... 112 n = N / 2; 113 n -= n % 8; 114 if ( stride < 0 ) { 115 xp1 = (float *)X + ( (n-N)*stride ); 116 xp2 = (float *)X; 117 } else { 118 xp1 = (float *)X; 119 xp2 = (float *)X + ( n*stride ); 120 } 121 return stdlib_strided_sapxsumpw( n, alpha, xp1, stride ) + stdlib_strided_sapxsumpw( N-n, alpha, xp2, stride ); 122 }