dasum.c (1533B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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/base/dasum.h" 20 #include <math.h> 21 22 /** 23 * Computes the sum of absolute values. 24 * 25 * @param N number of elements to sum 26 * @param X input array 27 * @param stride stride length 28 * @return sum of absolute values 29 */ 30 double c_dasum( const int N, const double *X, const int stride ) { 31 double sum; 32 int m; 33 int i; 34 35 sum = 0.0; 36 if ( N <= 0 || stride <= 0 ) { 37 return sum; 38 } 39 // If the stride is equal to `1`, use unrolled loops... 40 if ( stride == 1 ) { 41 m = N % 6; 42 43 // If we have a remainder, run a clean-up loop... 44 if ( m > 0 ) { 45 for ( i = 0; i < m; i++ ) { 46 sum += fabs( X[i] ); 47 } 48 } 49 if ( N < 6 ) { 50 return sum; 51 } 52 for ( i = m; i < N; i += 6 ) { 53 sum += fabs( X[i] ) + fabs( X[i+1] ) + fabs( X[i+2] ) + fabs( X[i+3] ) + fabs( X[i+4] ) + fabs( X[i+5] ); 54 } 55 return sum; 56 } 57 for ( i = 0; i < N*stride; i += stride ) { 58 sum += fabs( X[i] ); 59 } 60 return sum; 61 }