time-to-botec

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

main.c (1887B)


      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/ndarray/base/minmax_view_buffer_index.h"
     20 #include <stdint.h>
     21 
     22 /**
     23 * Computes the minimum and maximum linear indices (in bytes) in an underlying data buffer accessible to an array view.
     24 *
     25 * @param ndims    number of dimensions
     26 * @param shape    array shape (dimensions)
     27 * @param strides  array strides (in bytes)
     28 * @param offset   index offset
     29 * @param out      2-element output array
     30 * @return         status code
     31 *
     32 * @example
     33 * #include "stdlib/ndarray/base/minmax_view_buffer_index.h"
     34 * #include <stdint.h>
     35 *
     36 * int64_t ndims = 2;
     37 * int64_t shape[] = { 10, 10 };
     38 * int64_t strides[] = { 10, 1 };
     39 * int64_t offset = 0;
     40 * int64_t out[ 2 ];
     41 *
     42 * stdlib_ndarray_minmax_view_buffer_index( ndims, shape, strides, offset, out );
     43 *
     44 * int64_t min = out[ 0 ];
     45 * // returns 0
     46 *
     47 * int64_t max = out[ 1 ];
     48 * // returns 99
     49 */
     50 int8_t stdlib_ndarray_minmax_view_buffer_index( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *out ) {
     51 	int64_t min;
     52 	int64_t max;
     53 	int64_t s;
     54 	int64_t i;
     55 
     56 	min = offset;
     57 	max = offset;
     58 	for ( i = 0; i < ndims; i++ ) {
     59 		s = strides[ i ];
     60 		if ( s > 0 ) {
     61 			max += s * ( shape[i]-1 );
     62 		} else if ( s < 0 ) {
     63 			min += s * ( shape[i]-1 ); // decrements min
     64 		}
     65 	}
     66 	out[ 0 ] = min;
     67 	out[ 1 ] = max;
     68 
     69 	return 0;
     70 }