smskrange.c (2001B)
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/stats/base/smskrange.h" 20 #include "stdlib/math/base/assert/is_nanf.h" 21 #include <stdint.h> 22 23 /** 24 * Computes the range of a single-precision floating-point strided array according to a mask. 25 * 26 * @param N number of indexed elements 27 * @param X input array 28 * @param strideX X stride length 29 * @param Mask mask array 30 * @param strideMask Mask stride length 31 * @return output value 32 */ 33 float stdlib_strided_smskrange( const int64_t N, const float *X, const int64_t strideX, const uint8_t *Mask, const int64_t strideMask ) { 34 int64_t ix; 35 int64_t im; 36 int64_t i; 37 float max; 38 float min; 39 float v; 40 41 if ( N <= 0 ) { 42 return 0.0f / 0.0f; // NaN 43 } 44 if ( strideX < 0 ) { 45 ix = (1-N) * strideX; 46 } else { 47 ix = 0; 48 } 49 if ( strideMask < 0 ) { 50 im = (1-N) * strideMask; 51 } else { 52 im = 0; 53 } 54 for ( i = 0; i < N; i++ ) { 55 if ( Mask[ im ] == 0 ) { 56 break; 57 } 58 ix += strideX; 59 im += strideMask; 60 } 61 if ( i == N ) { 62 return 0.0f / 0.0f; // NaN 63 } 64 min = X[ ix ]; 65 if ( stdlib_base_is_nanf( min ) ) { 66 return 0.0f / 0.0f; // NaN 67 } 68 max = min; 69 i += 1; 70 for (; i < N; i++ ) { 71 ix += strideX; 72 im += strideMask; 73 if ( Mask[ im ] ) { 74 continue; 75 } 76 v = X[ ix ]; 77 if ( stdlib_base_is_nanf( v ) ) { 78 return 0.0f / 0.0f; // NaN 79 } 80 if ( v < min ) { 81 min = v; 82 } else if ( v > max ) { 83 max = v; 84 } 85 } 86 return max - min; 87 }