sdsnansumpw.c (4329B)
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/sdsnansumpw.h" 20 #include "stdlib/math/base/assert/is_nanf.h" 21 #include <stdint.h> 22 23 /** 24 * Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation with extended accumulation. 25 * 26 * ## Method 27 * 28 * - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`. 29 * 30 * ## References 31 * 32 * - 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). 33 * 34 * @param N number of indexed elements 35 * @param X input array 36 * @param stride stride length 37 * @return output value 38 */ 39 float stdlib_strided_sdsnansumpw( const int64_t N, 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 56 if ( N <= 0 ) { 57 return 0.0f; 58 } 59 if ( N == 1 || stride == 0 ) { 60 if ( stdlib_base_is_nanf( X[ 0 ] ) ) { 61 return 0.0f; 62 } 63 return X[ 0 ]; 64 } 65 if ( stride < 0 ) { 66 ix = (1-N) * stride; 67 } else { 68 ix = 0; 69 } 70 if ( N < 8 ) { 71 // Use simple summation... 72 sum = 0.0; 73 for ( i = 0; i < N; i++ ) { 74 if ( !stdlib_base_is_nanf( X[ ix ] ) ) { 75 sum += X[ ix ]; 76 } 77 ix += stride; 78 } 79 return sum; 80 } 81 // 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`.) 82 if ( N <= 128 ) { 83 // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... 84 s0 = ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 85 ix += stride; 86 s1 = ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 87 ix += stride; 88 s2 = ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 89 ix += stride; 90 s3 = ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 91 ix += stride; 92 s4 = ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 93 ix += stride; 94 s5 = ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 95 ix += stride; 96 s6 = ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 97 ix += stride; 98 s7 = ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 99 ix += stride; 100 101 M = N % 8; 102 for ( i = 8; i < N-M; i += 8 ) { 103 s0 += ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 104 ix += stride; 105 s1 += ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 106 ix += stride; 107 s2 += ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 108 ix += stride; 109 s3 += ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 110 ix += stride; 111 s4 += ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 112 ix += stride; 113 s5 += ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 114 ix += stride; 115 s6 += ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 116 ix += stride; 117 s7 += ( stdlib_base_is_nanf( X[ ix ] ) ) ? 0.0 : X[ ix ]; 118 ix += stride; 119 } 120 // Pairwise sum the accumulators: 121 sum = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7)); 122 123 // Clean-up loop... 124 for (; i < N; i++ ) { 125 if ( !stdlib_base_is_nanf( X[ ix ] ) ) { 126 sum += X[ ix ]; 127 } 128 ix += stride; 129 } 130 return sum; 131 } 132 // Recurse by dividing by two, but avoiding non-multiples of unroll factor... 133 n = N / 2; 134 n -= n % 8; 135 if ( stride < 0 ) { 136 xp1 = (float *)X + ( (n-N)*stride ); 137 xp2 = (float *)X; 138 } else { 139 xp1 = (float *)X; 140 xp2 = (float *)X + ( n*stride ); 141 } 142 return stdlib_strided_sdsnansumpw( n, xp1, stride ) + stdlib_strided_sdsnansumpw( N-n, xp2, stride ); 143 }