main.c (3923B)
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/bind2vind.h" 20 #include "stdlib/ndarray/orders.h" 21 #include "stdlib/ndarray/index_modes.h" 22 #include <stdint.h> 23 #include <stdlib.h> 24 25 /** 26 * Converts a linear index in an underlying data buffer to a linear index in an array view. 27 * 28 * ## Notes 29 * 30 * - In "error" mode, the function returns `-1` if an index is out-of-bounds. 31 * 32 * @param ndims number of dimensions 33 * @param shape array shape (dimensions) 34 * @param strides array strides 35 * @param offset location of the first indexed value **based** on the stride array 36 * @param order specifies whether an array is row-major (C-style) or column-major (Fortran-style) 37 * @param idx linear index in an array view 38 * @param mode specifies how to handle a linear index which exceeds array dimensions 39 * @return index 40 * 41 * @example 42 * #include "stdlib/ndarray/index_modes.h" 43 * #include "stdlib/ndarray/orders.h" 44 * #include "stdlib/ndarray/base/bind2vind.h" 45 * 46 * int64_t ndims = 2; 47 * int64_t shape[] = { 3, 3 }; 48 * int64_t strides[] = { -3, 1 }; 49 * int64_t offset = 6; 50 * 51 * int64_t idx = stdlib_ndarray_bind2vind( ndims, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, 7, STDLIB_NDARRAY_INDEX_ERROR ); 52 * // returns 1 53 */ 54 int64_t stdlib_ndarray_bind2vind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, enum STDLIB_NDARRAY_ORDER order, int64_t idx, enum STDLIB_NDARRAY_INDEX_MODE mode ) { 55 int64_t len; 56 int64_t ind; 57 int64_t s; 58 int64_t k; 59 int64_t i; 60 61 len = 1; 62 for ( i = 0; i < ndims; i++ ) { 63 len *= shape[ i ]; 64 } 65 if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { 66 if ( idx < 0 ) { 67 idx = 0; 68 } else if ( idx >= len ) { 69 idx = len - 1; 70 } 71 } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { 72 if ( idx < 0 ) { 73 idx += len; // slight optimization to avoid modulo arithmetic when |idx| <= len 74 if ( idx < 0 ) { 75 idx -= len*( (int64_t)( idx/len ) ); // this is equivalent to `idx mod len`, where the result has same sign as dividend (i.e., `idx`); cannot use `%` as the sign of the result is implementation defined in C 76 if ( idx != 0 ) { 77 idx += len; 78 } 79 } 80 } else if ( idx >= len ) { 81 idx -= len; // slight optimization to avoid modulo arithmetic when len < idx <= 2*len 82 if ( idx >= len ) { 83 idx %= len; 84 } 85 } 86 } else if ( idx < 0 || idx >= len ) { 87 return -1; 88 } 89 // Trivial case where the offset into the underlying data buffer is 0... 90 if ( offset == 0 ) { 91 return idx; 92 } 93 // The approach which follows is to resolve a buffer index to its subscripts and then plug the subscripts into the standard formula for computing the linear index in the array view (i.e., where all strides are positive and offset is 0)... 94 ind = 0; 95 if ( order == STDLIB_NDARRAY_COLUMN_MAJOR ) { 96 for ( i = ndims-1; i >= 0; i-- ) { 97 s = strides[ i ]; 98 if ( s < 0 ) { 99 k = idx / s; // truncates 100 idx -= k * s; 101 k += shape[ i ] - 1; 102 } else { 103 k = idx / s; // cppcheck-suppress zerodivcond // truncates 104 idx -= k * s; 105 } 106 ind += k * llabs( s ); 107 } 108 return ind; 109 } 110 // Case: row-major 111 for ( i = 0; i < ndims; i++ ) { 112 s = strides[ i ]; 113 if ( s < 0 ) { 114 k = idx / s; // truncates 115 idx -= k * s; 116 k += shape[ i ] - 1; 117 } else { 118 k = idx / s; // cppcheck-suppress zerodivcond // truncates 119 idx -= k * s; 120 } 121 ind += k * llabs( s ); 122 } 123 return ind; 124 }