Skip to content

Commit 3d21748

Browse files
committed
feat: add function to return the index offset in units of elements
--- 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 dd45a8f commit 3d21748

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,48 @@ The function accepts the following arguments:
14691469
int64_t stdlib_ndarray_offset( const struct ndarray *arr );
14701470
```
14711471

1472+
#### stdlib_ndarray_offset_elements( \*arr )
1473+
1474+
Returns an ndarray index offset (in elements).
1475+
1476+
```c
1477+
#include "stdlib/ndarray/ctor.h"
1478+
#include <stdint.h>
1479+
#include <stdlib.h>
1480+
#include <stdio.h>
1481+
1482+
// ...
1483+
1484+
// Create an ndarray:
1485+
struct ndarray *x = stdlib_ndarray_allocate( ... );
1486+
if ( x == NULL ) {
1487+
fprintf( stderr, "Error allocating memory.\n" );
1488+
exit( 1 );
1489+
}
1490+
1491+
// ...
1492+
1493+
// Retrieve the index offset:
1494+
int64_t offset = stdlib_ndarray_offset_elements( x );
1495+
1496+
// ...
1497+
1498+
// Free allocated memory:
1499+
stdlib_ndarray_free( x );
1500+
```
1501+
1502+
The function accepts the following arguments:
1503+
1504+
- **arr**: `[in] struct ndarray*` input ndarray.
1505+
1506+
```c
1507+
int64_t stdlib_ndarray_offset_elements( const struct ndarray *arr );
1508+
```
1509+
1510+
Notes:
1511+
1512+
- The function assumes that the ndarray is **aligned** (i.e., the index offset in bytes is a multiple of the number of bytes per ndarray element).
1513+
14721514
#### stdlib_ndarray_order( \*arr )
14731515

14741516
Returns the order of an ndarray.

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3263,6 +3263,51 @@ static double benchmark66( void ) {
32633263
return elapsed;
32643264
}
32653265

3266+
/**
3267+
* Runs a benchmark.
3268+
*
3269+
* @return elapsed time in seconds
3270+
*/
3271+
static double benchmark67( void ) {
3272+
double elapsed;
3273+
int64_t v;
3274+
int64_t j;
3275+
double t;
3276+
int i;
3277+
3278+
uint8_t buffer[] = { 0, 0, 0, 0, 0, 0 };
3279+
int64_t ndims = 2;
3280+
int64_t shape[] = { 3, 2 };
3281+
int64_t strides[] = { -2, -1 };
3282+
int64_t offset = 5;
3283+
int64_t nsubmodes = 1;
3284+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
3285+
3286+
struct ndarray *arr = stdlib_ndarray_allocate( STDLIB_NDARRAY_UINT8, buffer, ndims, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, nsubmodes, submodes );
3287+
if ( arr == NULL ) {
3288+
printf( "unable to allocate memory\n" );
3289+
exit( 1 );
3290+
}
3291+
3292+
t = tic();
3293+
for ( i = 0; i < ITERATIONS; i++ ) {
3294+
j = (int64_t)( rand_double()*ndims );
3295+
v = stdlib_ndarray_offset_elements( arr, j );
3296+
if ( v != offset ) {
3297+
printf( "unexpected result\n" );
3298+
break;
3299+
}
3300+
}
3301+
elapsed = tic() - t;
3302+
3303+
if ( v != offset ) {
3304+
printf( "unexpected result\n" );
3305+
}
3306+
stdlib_ndarray_free( arr );
3307+
3308+
return elapsed;
3309+
}
3310+
32663311
/**
32673312
* Main execution sequence.
32683313
*/
@@ -3739,5 +3784,12 @@ int main( void ) {
37393784
print_results( elapsed );
37403785
printf( "ok %d benchmark finished\n", count );
37413786
}
3787+
for ( i = 0; i < REPEATS; i++ ) {
3788+
count += 1;
3789+
printf( "# c::native::%s::get:offset_elements\n", NAME );
3790+
elapsed = benchmark67();
3791+
print_results( elapsed );
3792+
printf( "ok %d benchmark finished\n", count );
3793+
}
37423794
print_summary( count, count );
37433795
}

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
@@ -113,6 +113,11 @@ int64_t stdlib_ndarray_ndims( const struct ndarray *arr );
113113
*/
114114
int64_t stdlib_ndarray_offset( const struct ndarray *arr );
115115

116+
/**
117+
* Returns an ndarray index offset (in elements).
118+
*/
119+
int64_t stdlib_ndarray_offset_elements( const struct ndarray *arr );
120+
116121
/**
117122
* Returns the order of an ndarray.
118123
*/

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,26 @@ int64_t stdlib_ndarray_nsubmodes( const struct ndarray *arr ) {
368368
* Returns an ndarray index offset (in bytes).
369369
*
370370
* @param arr input ndarray
371-
* @return array strides
371+
* @return index offset
372372
*/
373373
int64_t stdlib_ndarray_offset( const struct ndarray *arr ) {
374374
return arr->offset;
375375
}
376376

377+
/**
378+
* Returns an ndarray index offset (in elements).
379+
*
380+
* ## Notes
381+
*
382+
* - The function assumes that an ndarray is aligned (i.e., offset is a multiple of the number of bytes per ndarray element).
383+
*
384+
* @param arr input ndarray
385+
* @return index offset
386+
*/
387+
int64_t stdlib_ndarray_offset_elements( const struct ndarray *arr ) {
388+
return arr->offset / arr->BYTES_PER_ELEMENT;
389+
}
390+
377391
/**
378392
* Returns the order of an ndarray.
379393
*

0 commit comments

Comments
 (0)