time-to-botec

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

main.c (1921B)


      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 <stdint.h>
     20 #include <stdlib.h>
     21 #include "stdlib/ndarray/base/strides2order.h"
     22 
     23 /**
     24 * Determines the order of a multidimensional array based on a provided stride array.
     25 *
     26 * ## Notes
     27 *
     28 * The function returns one of the following values:
     29 *
     30 * -   `0`: neither row-major nor column-major.
     31 * -   `1`: row-major (C-style).
     32 * -   `2`: column-major (Fortran-style).
     33 * -   `3`: both row-major and column-major.
     34 *
     35 * @param ndims    number of dimensions
     36 * @param strides  array strides
     37 * @return         order
     38 *
     39 * @example
     40 * #include "stdlib/ndarray/base/strides2order.h"
     41 *
     42 * int64_t ndims = 2;
     43 * int64_t strides[] = { 2, 1 };
     44 *
     45 * int8_t o = stdlib_ndarray_strides2order( ndims, strides );
     46 * // returns 1
     47 */
     48 int8_t stdlib_ndarray_strides2order( int64_t ndims, int64_t *strides ) {
     49 	int8_t column;
     50 	int8_t row;
     51 	int64_t s1;
     52 	int64_t s2;
     53 	int64_t i;
     54 
     55 	if ( ndims == 0 ) {
     56 		return 0; // 'none'
     57 	}
     58 	column = 1;
     59 	row = 1;
     60 
     61 	s1 = llabs( strides[ 0 ] );
     62 	for ( i = 1; i < ndims; i++ ) {
     63 		s2 = llabs( strides[ i ] );
     64 		if ( column && s2 < s1 ) {
     65 			column = 0;
     66 		} else if ( row && s2 > s1 ) {
     67 			row = 0;
     68 		}
     69 		if ( row || column ) {
     70 			s1 = s2;
     71 		} else {
     72 			return 0; // 'none'
     73 		}
     74 	}
     75 	if ( row && column ) {
     76 		return 3; // 'both'
     77 	}
     78 	if ( row ) {
     79 		return 1; // 'row-major'
     80 	}
     81 	return 2; // 'column-major'
     82 }