dnannsumpw.c (4336B)
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/dnannsumpw.h" 20 #include <stdint.h> 21 22 /** 23 * Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation. 24 * 25 * @private 26 * @param N number of indexed elements 27 * @param X input array 28 * @param stride stride length 29 * @param n pointer for storing the number of non-NaN elements 30 * @return output value 31 */ 32 static double sumpw( const int64_t N, const double *X, const int64_t stride, int64_t *n ) { 33 double *xp1; 34 double *xp2; 35 double sum; 36 int64_t ix; 37 int64_t M; 38 int64_t m; 39 int64_t i; 40 double s0; 41 double s1; 42 double s2; 43 double s3; 44 double s4; 45 double s5; 46 double s6; 47 double s7; 48 double v; 49 50 if ( N <= 0 ) { 51 return 0.0; 52 } 53 if ( N == 1 || stride == 0 ) { 54 v = X[ 0 ]; 55 if ( v == v ) { 56 *n += 1; 57 return v; 58 } 59 return 0.0; 60 } 61 if ( stride < 0 ) { 62 ix = (1-N) * stride; 63 } else { 64 ix = 0; 65 } 66 if ( N < 8 ) { 67 // Use simple summation... 68 sum = 0.0; 69 for ( i = 0; i < N; i++ ) { 70 v = X[ ix ]; 71 if ( v == v ) { 72 sum += v; 73 *n += 1; 74 } 75 ix += stride; 76 } 77 return sum; 78 } 79 // 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`.) 80 if ( N <= 128 ) { 81 // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... 82 s0 = 0.0; 83 s1 = 0.0; 84 s2 = 0.0; 85 s3 = 0.0; 86 s4 = 0.0; 87 s5 = 0.0; 88 s6 = 0.0; 89 s7 = 0.0; 90 91 M = N % 8; 92 for ( i = 0; i < N-M; i += 8 ) { 93 v = X[ ix ]; 94 if ( v == v ) { 95 s0 += v; 96 *n += 1; 97 } 98 ix += stride; 99 v = X[ ix ]; 100 if ( v == v ) { 101 s1 += v; 102 *n += 1; 103 } 104 ix += stride; 105 v = X[ ix ]; 106 if ( v == v ) { 107 s2 += v; 108 *n += 1; 109 } 110 ix += stride; 111 v = X[ ix ]; 112 if ( v == v ) { 113 s3 += v; 114 *n += 1; 115 } 116 ix += stride; 117 v = X[ ix ]; 118 if ( v == v ) { 119 s4 += v; 120 *n += 1; 121 } 122 ix += stride; 123 v = X[ ix ]; 124 if ( v == v ) { 125 s5 += v; 126 *n += 1; 127 } 128 ix += stride; 129 v = X[ ix ]; 130 if ( v == v ) { 131 s6 += v; 132 *n += 1; 133 } 134 ix += stride; 135 v = X[ ix ]; 136 if ( v == v ) { 137 s7 += v; 138 *n += 1; 139 } 140 ix += stride; 141 } 142 // Pairwise sum the accumulators: 143 sum = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7)); 144 145 // Clean-up loop... 146 for (; i < N; i++ ) { 147 v = X[ ix ]; 148 if ( v == v ) { 149 sum += v; 150 *n += 1; 151 } 152 ix += stride; 153 } 154 return sum; 155 } 156 // Recurse by dividing by two, but avoiding non-multiples of unroll factor... 157 m = N / 2; 158 m -= m % 8; 159 if ( stride < 0 ) { 160 xp1 = (double *)X + ( (m-N)*stride ); 161 xp2 = (double *)X; 162 } else { 163 xp1 = (double *)X; 164 xp2 = (double *)X + ( m*stride ); 165 } 166 return sumpw( m, xp1, stride, n ) + sumpw( N-m, xp2, stride, n ); 167 } 168 169 /** 170 * Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation. 171 * 172 * ## Method 173 * 174 * - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`. 175 * 176 * ## References 177 * 178 * - 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). 179 * 180 * @param N number of indexed elements 181 * @param X input array 182 * @param stride stride length 183 * @param n pointer for storing the number of non-NaN elements 184 * @return output value 185 */ 186 double stdlib_strided_dnannsumpw( const int64_t N, const double *X, const int64_t stride, int64_t *n ) { 187 *n = 0; 188 return sumpw( N, X, stride, n ); 189 }