time-to-botec

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

main.c (1684B)


      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/iteration_order.h"
     20 #include <stdint.h>
     21 
     22 /**
     23 * Determines array iteration order given a stride array.
     24 *
     25 * ## Notes
     26 *
     27 * The function returns one of the following values:
     28 *
     29 * -   `1`: left-to-right iteration order (strides are all nonnegative).
     30 * -   `-1`: right-to-left iteration order (strides are all negative).
     31 * -   `0`: unordered (strides are of mixed sign).
     32 *
     33 * @param ndims    number of dimensions
     34 * @param strides  array strides
     35 * @return         iteration order
     36 *
     37 * @example
     38 * #include "stdlib/ndarray/base/iteration_order.h"
     39 *
     40 * uint64_t ndims = 2;
     41 * int64_t strides[] = { 2, 1 };
     42 *
     43 * int8_t o = stdlib_ndarray_iteration_order( ndims, strides );
     44 * // returns 1
     45 */
     46 int8_t stdlib_ndarray_iteration_order( int64_t ndims, int64_t *strides ) {
     47 	int64_t cnt;
     48 	int64_t i;
     49 
     50 	cnt = 0;
     51 	for ( i = 0; i < ndims; i++ ) {
     52 		if ( strides[ i ] < 0 ) {
     53 			cnt += 1;
     54 		}
     55 	}
     56 	if ( cnt == 0 ) {
     57 		// All nonnegative strides:
     58 		return 1;
     59 	}
     60 	if ( cnt == ndims ) {
     61 		// All negative strides:
     62 		return -1;
     63 	}
     64 	// Strides of mixed signs:
     65 	return 0;
     66 }