time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

snanmskrange.c (1963B)


      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/snanmskrange.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, ignoring `NaN` values.
     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_snanmskrange( 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 		v = X[ ix ];
     56 		if ( v == v && Mask[ im ] == 0 ) {
     57 			break;
     58 		}
     59 		ix += strideX;
     60 		im += strideMask;
     61 	}
     62 	if ( i == N ) {
     63 		return 0.0f / 0.0f; // NaN
     64 	}
     65 	min = v;
     66 	max = min;
     67 	i += 1;
     68 	for (; i < N; i++ ) {
     69 		ix += strideX;
     70 		im += strideMask;
     71 		if ( Mask[ im ] ) {
     72 			continue;
     73 		}
     74 		v = X[ ix ];
     75 		if ( stdlib_base_is_nanf( v ) ) {
     76 			continue;
     77 		}
     78 		if ( v < min ) {
     79 			min = v;
     80 		} else if ( v > max ) {
     81 			max = v;
     82 		}
     83 	}
     84 	return max - min;
     85 }