time-to-botec

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

main.c (2337B)


      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/strided/base/smskmap.h"
     20 #include <stdint.h>
     21 
     22 /**
     23 * Applies a unary function accepting and returning single-precision floating-point numbers to each element in a single-precision floating-point strided input array according to a corresponding element in a strided mask array and assigns each result to an element in a single-precision floating-point strided output array.
     24 *
     25 * @param N           number of indexed elements
     26 * @param X           input array
     27 * @param strideX     X stride length
     28 * @param Mask        mask array
     29 * @param strideMask  Mask stride length
     30 * @param Y           destination array
     31 * @param strideY     Y stride length
     32 * @param fcn         unary function to apply
     33 *
     34 * @example
     35 * #include "stdlib/strided/base/smskmap.h"
     36 * #include <stdint.h>
     37 *
     38 * static float scale( const float x ) {
     39 *     return x * 10.0f;
     40 * }
     41 *
     42 * float X[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f };
     43 * uint8_t M[] = { 0, 0, 1, 0, 0, 1 };
     44 * float Y[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
     45 *
     46 * int64_t N = 6;
     47 *
     48 * stdlib_strided_smskmap( N, X, 1, M, 1, Y, 1, scale );
     49 */
     50 void stdlib_strided_smskmap( const int64_t N, const float *X, const int64_t strideX, const uint8_t *Mask, const int64_t strideMask, float *Y, const int64_t strideY, float (*fcn)( float ) ) {
     51 	int64_t ix;
     52 	int64_t im;
     53 	int64_t iy;
     54 	int64_t i;
     55 	if ( N <= 0 ) {
     56 		return;
     57 	}
     58 	if ( strideX < 0 ) {
     59 		ix = (1-N) * strideX;
     60 	} else {
     61 		ix = 0;
     62 	}
     63 	if ( strideMask < 0 ) {
     64 		im = (1-N) * strideMask;
     65 	} else {
     66 		im = 0;
     67 	}
     68 	if ( strideY < 0 ) {
     69 		iy = (1-N) * strideY;
     70 	} else {
     71 		iy = 0;
     72 	}
     73 	for ( i = 0; i < N; i++ ) {
     74 		if ( Mask[ im ] == 0 ) {
     75 			Y[ iy ] = fcn( X[ ix ] );
     76 		}
     77 		ix += strideX;
     78 		im += strideMask;
     79 		iy += strideY;
     80 	}
     81 	return;
     82 }