Skip to content

Commit b00ba84

Browse files
committed
feat: add function for returning the number of bytes per ndarray element
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent cf38d87 commit b00ba84

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

lib/node_modules/@stdlib/ndarray/ctor/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,44 @@ The function accepts the following arguments:
920920
int64_t stdlib_ndarray_byte_length( const struct ndarray *arr );
921921
```
922922

923+
#### stdlib_ndarray_byte_length_per_element( \*arr )
924+
925+
Returns the number of bytes per ndarray element.
926+
927+
```c
928+
#include "stdlib/ndarray/ctor.h"
929+
#include <stdint.h>
930+
#include <stdlib.h>
931+
#include <stdio.h>
932+
933+
// ...
934+
935+
// Create an ndarray:
936+
struct ndarray *x = stdlib_ndarray_allocate( ... );
937+
if ( x == NULL ) {
938+
fprintf( stderr, "Error allocating memory.\n" );
939+
exit( 1 );
940+
}
941+
942+
// ...
943+
944+
// Retrieve the number of bytes per element:
945+
int64_t nb = stdlib_ndarray_byte_length_per_element( x );
946+
947+
// ...
948+
949+
// Free allocated memory:
950+
stdlib_ndarray_free( x );
951+
```
952+
953+
The function accepts the following arguments:
954+
955+
- **arr**: `[in] struct ndarray*` input ndarray.
956+
957+
```c
958+
int64_t stdlib_ndarray_byte_length_per_element( const struct ndarray *arr );
959+
```
960+
923961
#### stdlib_ndarray_data( \*arr )
924962

925963
Returns a pointer to an ndarray's underlying byte array.

lib/node_modules/@stdlib/ndarray/ctor/benchmark/c/benchmark.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3174,6 +3174,50 @@ static double benchmark64( void ) {
31743174
return elapsed;
31753175
}
31763176

3177+
/**
3178+
* Runs a benchmark.
3179+
*
3180+
* @return elapsed time in seconds
3181+
*/
3182+
static double benchmark65( void ) {
3183+
double elapsed;
3184+
int64_t v;
3185+
double t;
3186+
int i;
3187+
3188+
uint8_t buffer[] = { 0, 0, 0, 0, 0, 0 };
3189+
int64_t ndims = 2;
3190+
int64_t shape[] = { 3, 2 };
3191+
int64_t strides[] = { 2, 1 };
3192+
int64_t offset = 0;
3193+
int64_t nsubmodes = 1;
3194+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
3195+
3196+
struct ndarray *arr = stdlib_ndarray_allocate( STDLIB_NDARRAY_UINT8, buffer, ndims, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, nsubmodes, submodes );
3197+
if ( arr == NULL ) {
3198+
printf( "unable to allocate memory\n" );
3199+
exit( 1 );
3200+
}
3201+
3202+
t = tic();
3203+
for ( i = 0; i < ITERATIONS; i++ ) {
3204+
// NOTE: this is likely to be optimized away by a modern compiler, making this benchmark meaningless.
3205+
v = stdlib_ndarray_byte_length_per_element( arr );
3206+
if ( v != 1 ) {
3207+
printf( "unexpected result\n" );
3208+
break;
3209+
}
3210+
}
3211+
elapsed = tic() - t;
3212+
3213+
if ( v != 1 ) {
3214+
printf( "unexpected result\n" );
3215+
}
3216+
stdlib_ndarray_free( arr );
3217+
3218+
return elapsed;
3219+
}
3220+
31773221
/**
31783222
* Main execution sequence.
31793223
*/
@@ -3636,5 +3680,12 @@ int main( void ) {
36363680
print_results( elapsed );
36373681
printf( "ok %d benchmark finished\n", count );
36383682
}
3683+
for ( i = 0; i < REPEATS; i++ ) {
3684+
count += 1;
3685+
printf( "# c::native::%s::get:byte_length_per_element\n", NAME );
3686+
elapsed = benchmark65();
3687+
print_results( elapsed );
3688+
printf( "ok %d benchmark finished\n", count );
3689+
}
36393690
print_summary( count, count );
36403691
}

lib/node_modules/@stdlib/ndarray/ctor/include/stdlib/ndarray/ctor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ struct ndarray * stdlib_ndarray_allocate( int16_t dtype, uint8_t *data, int64_t
4848
*/
4949
int64_t stdlib_ndarray_byte_length( const struct ndarray *arr );
5050

51+
/**
52+
* Returns the number of bytes per ndarray element.
53+
*/
54+
int64_t stdlib_ndarray_byte_length_per_element( const struct ndarray *arr );
55+
5156
/**
5257
* Returns a pointer to an ndarray's underlying byte array.
5358
*/

lib/node_modules/@stdlib/ndarray/ctor/src/main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ int64_t stdlib_ndarray_byte_length( const struct ndarray *arr ) {
162162
return arr->byteLength;
163163
}
164164

165+
/**
166+
* Returns the number of bytes per ndarray element.
167+
*
168+
* @param arr input ndarray
169+
* @return bytes per element
170+
*/
171+
int64_t stdlib_ndarray_byte_length_per_element( const struct ndarray *arr ) {
172+
return arr->BYTES_PER_ELEMENT;
173+
}
174+
165175
/**
166176
* Returns a pointer to an ndarray's underlying byte array.
167177
*

0 commit comments

Comments
 (0)