main.c (1666B)
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/shape2strides.h" 20 #include "stdlib/ndarray/orders.h" 21 #include <stdint.h> 22 23 /** 24 * Generates a stride array from an array shape. 25 * 26 * @param ndims number of dimensions 27 * @param shape array shape (dimensions) 28 * @param order specifies whether an array is row-major (C-style) or column-major (Fortran-style) 29 * @param out output strides array 30 * @return status code 31 * 32 * @example 33 * #include "stdlib/ndarray/base/shape2strides.h" 34 * #include "stdlib/ndarray/orders.h" 35 * 36 * int64_t ndims = 3; 37 * int64_t shape[] = { 2, 3, 10 }; 38 * int64_t out[ 3 ]; 39 * 40 * stdlib_ndarray_shape2strides( ndims, shape, STDLIB_NDARRAY_ROW_MAJOR, out ); 41 */ 42 int8_t stdlib_ndarray_shape2strides( int64_t ndims, int64_t *shape, enum STDLIB_NDARRAY_ORDER order, int64_t *out ) { 43 int64_t i; 44 int64_t s; 45 46 s = 1; 47 if ( order == STDLIB_NDARRAY_COLUMN_MAJOR ) { 48 for ( i = 0; i < ndims; i++ ) { 49 out[ i ] = s; 50 s *= shape[ i ]; 51 } 52 } else { // row-major 53 for ( i = ndims-1; i >= 0; i-- ) { 54 out[ i ] = s; 55 s *= shape[ i ]; 56 } 57 } 58 return 0; 59 }