time-to-botec

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

main.c (2471B)


      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/assert/is_buffer_length_compatible.h"
     20 #include "stdlib/ndarray/base/minmax_view_buffer_index.h"
     21 #include "stdlib/ndarray/base/bytes_per_element.h"
     22 #include "stdlib/ndarray/dtypes.h"
     23 #include <stdint.h>
     24 
     25 /**
     26 * Determines if a buffer length is compatible with provided ndarray meta data.
     27 *
     28 * ## Notes
     29 *
     30 * -   The function returns `1` if the buffer length is compatible and `0` otherwise.
     31 *
     32 * @param dtype    buffer data type
     33 * @param len      number of elements in data buffer
     34 * @param ndims    number of dimensions
     35 * @param shape    array shape (dimensions)
     36 * @param strides  array strides
     37 * @param offset   index offset
     38 * @return         value indicating if a buffer length is compatible
     39 *
     40 * @example
     41 * #include "stdlib/ndarray/base/assert/is_buffer_length_compatible.h"
     42 * #include "stdlib/ndarray/dtypes.h"
     43 *
     44 * int64_t ndims = 2;
     45 * int64_t shape[] = { 10, 10 };
     46 * int64_t strides[] = { 10, 1 };
     47 * int64_t offset = 0;
     48 *
     49 * int8_t b = stdlib_ndarray_is_buffer_length_compatible( STDLIB_NDARRAY_UINT8, 1000, ndims, shape, strides, offset );
     50 * // returns 1
     51 *
     52 * int8_t b = stdlib_ndarray_is_buffer_length_compatible( STDLIB_NDARRAY_UINT8, 10, ndims, shape, strides, offset );
     53 * // returns 0
     54 */
     55 int8_t stdlib_ndarray_is_buffer_length_compatible( enum STDLIB_NDARRAY_DTYPE dtype, int64_t len, int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset ) {
     56 	int64_t tmp[ 2 ];
     57 	int64_t nbytes;
     58 
     59 	// Determine the number of bytes per element:
     60 	nbytes = stdlib_ndarray_bytes_per_element( dtype );
     61 
     62 	// Determine the minimum and maximum linear indices which are accessible by the array view:
     63 	stdlib_ndarray_minmax_view_buffer_index( ndims, shape, strides, offset, tmp );
     64 
     65 	// If the indices are "inbounds", then the buffer length is compatible:
     66 	if ( (tmp[0]/nbytes) >= 0 && (tmp[1]/nbytes) < len ) {
     67 		return 1;
     68 	}
     69 	return 0;
     70 }