From ab0a354aa3ac3984b6cca43862c1c0b46cd60f36 Mon Sep 17 00:00:00 2001 From: AtharvaPatange <164712914+AtharvaPatange@users.noreply.github.com> Date: Thu, 22 Oct 2026 11:27:02 +0530 Subject: [PATCH 1/2] feat: add C implementation for blas/ext/base/ndarray/ssort --- .../blas/ext/base/ndarray/ssort/README.md | 176 ++++++++++++++- .../base/ndarray/ssort/benchmark/benchmark.js | 2 +- .../ssort/benchmark/benchmark.native.js | 113 ++++++++++ .../base/ndarray/ssort/benchmark/c/Makefile | 146 +++++++++++++ .../ssort/benchmark/c/benchmark.length.c | 195 +++++++++++++++++ .../blas/ext/base/ndarray/ssort/binding.gyp | 170 +++++++++++++++ .../base/ndarray/ssort/docs/types/index.d.ts | 2 +- .../ext/base/ndarray/ssort/docs/types/test.ts | 4 +- .../base/ndarray/ssort/examples/c/Makefile | 146 +++++++++++++ .../base/ndarray/ssort/examples/c/example.c | 87 ++++++++ .../ext/base/ndarray/ssort/examples/index.js | 4 +- .../blas/ext/base/ndarray/ssort/include.gypi | 53 +++++ .../stdlib/blas/ext/base/ndarray/ssort.h | 40 ++++ .../blas/ext/base/ndarray/ssort/lib/index.js | 18 +- .../blas/ext/base/ndarray/ssort/lib/main.js | 2 +- .../blas/ext/base/ndarray/ssort/lib/native.js | 76 +++++++ .../blas/ext/base/ndarray/ssort/manifest.json | 109 ++++++++++ .../blas/ext/base/ndarray/ssort/package.json | 4 + .../blas/ext/base/ndarray/ssort/src/Makefile | 70 ++++++ .../blas/ext/base/ndarray/ssort/src/addon.c | 64 ++++++ .../blas/ext/base/ndarray/ssort/src/main.c | 50 +++++ .../blas/ext/base/ndarray/ssort/test/test.js | 72 +++--- .../ext/base/ndarray/ssort/test/test.main.js | 196 +++++++++++++++++ .../base/ndarray/ssort/test/test.native.js | 205 ++++++++++++++++++ 24 files changed, 1953 insertions(+), 51 deletions(-) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/benchmark/c/benchmark.length.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/binding.gyp create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/include.gypi create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/include/stdlib/blas/ext/base/ndarray/ssort.h create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/lib/native.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/manifest.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/src/Makefile create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/src/addon.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/src/main.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/test/test.main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/test/test.native.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/README.md index 52ab80db6ed0..9cadde0f4137 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ssort/README.md @@ -47,7 +47,7 @@ var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var x = new Float32Vector( [ 1.0, -2.0, 3.0, -4.0 ] ); var order = scalar2ndarray( 1.0, { - 'dtype': 'generic' + 'dtype': 'float32' }); var out = ssort( [ x, order ] ); @@ -97,14 +97,14 @@ var x = uniform( [ 10 ], -100, 100, { console.log( ndarray2array( x ) ); var order = scalar2ndarray( 1.0, { - 'dtype': 'generic' + 'dtype': 'float32' }); ssort( [ x, order ] ); console.log( ndarray2array( x ) ); order = scalar2ndarray( -1.0, { - 'dtype': 'generic' + 'dtype': 'float32' }); ssort( [ x, order ] ); @@ -115,6 +115,176 @@ console.log( ndarray2array( x ) ); + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/ext/base/ndarray/ssort.h" +``` + +#### stdlib_blas_ext_ssort( arrays ) + +Sorts a one-dimensional single-precision floating-point ndarray. + +```c +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/dtypes.h" +#include "stdlib/ndarray/index_modes.h" +#include "stdlib/ndarray/orders.h" +#include "stdlib/ndarray/base/bytes_per_element.h" +#include + +// Create an ndarray: +float data[] = { 1.0f, -2.0f, 3.0f, -4.0f }; +int64_t shape[] = { 4 }; +int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT }; +int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; + +struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)data, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes ); + +// Create an ndarray specifying the sort order: +const float odata[] = { 1.0f }; +int64_t ostrides[] = { 0 }; + +struct ndarray *order = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)odata, 0, NULL, ostrides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes ); + +// Sort the ndarray: +const struct ndarray *arrays[] = { x, order }; +stdlib_blas_ext_ssort( arrays ); + +// Free allocated memory: +stdlib_ndarray_free( x ); +stdlib_ndarray_free( order ); +``` + +The function accepts the following arguments: + +- **arrays**: `[in] struct ndarray**` list containing the following ndarrays: + + - `[inout] struct ndarray*` a one-dimensional input ndarray. + - `[in] struct ndarray*` a zero-dimensional ndarray specifying the sort order. + +```c +void stdlib_blas_ext_ssort( const struct ndarray *arrays[] ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/ext/base/ndarray/ssort.h" +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/dtypes.h" +#include "stdlib/ndarray/index_modes.h" +#include "stdlib/ndarray/orders.h" +#include "stdlib/ndarray/base/bytes_per_element.h" +#include +#include +#include + +int main( void ) { + // Create a data buffer: + float data[] = { 1.0f, -2.0f, 3.0f, -4.0f }; + + // Specify the number of array dimensions: + const int64_t ndims = 1; + + // Specify the array shape: + int64_t shape[] = { 4 }; + + // Specify the array strides: + int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT }; + + // Specify the byte offset: + const int64_t offset = 0; + + // Specify the array order: + const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR; + + // Specify the index mode: + const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR; + + // Specify the subscript index modes: + int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; + const int64_t nsubmodes = 1; + + // Create an ndarray: + // cppcheck-suppress invalidPointerCast + struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + + // Create a data buffer for an ndarray specifying the sort order: + const float odata[] = { 1.0f }; + + // Specify the array strides for a zero-dimensional ndarray: + int64_t ostrides[] = { 0 }; + + // Create an ndarray specifying the sort order: + // cppcheck-suppress invalidPointerCast + struct ndarray *ord = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)odata, 0, NULL, ostrides, 0, order, imode, nsubmodes, submodes ); + if ( x == NULL || ord == NULL ) { + fprintf( stderr, "Error allocating memory.\n" ); + exit( 1 ); + } + + // Define a list of ndarrays: + const struct ndarray *arrays[] = { x, ord }; + + // Sort the ndarray: + stdlib_blas_ext_ssort( arrays ); + + // Print the result: + for ( int i = 0; i < 4; i++ ) { + printf( "x[ %i ] = %f\n", i, data[ i ] ); + } + + // Free allocated memory: + stdlib_ndarray_free( x ); + stdlib_ndarray_free( ord ); +} +``` + +
+ + + +
+ + +