Skip to content

Commit 0045365

Browse files
committed
feat: add C ndarray interface and refactor implementation
1 parent c365bf2 commit 0045365

File tree

11 files changed

+189
-208
lines changed

11 files changed

+189
-208
lines changed

lib/node_modules/@stdlib/stats/base/smax/include.gypi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
# Source files:
3838
'src_files': [
39-
'<(src_dir)/addon.cpp',
39+
'<(src_dir)/addon.c',
4040
'<!@(node -e "var arr = require(\'@stdlib/utils/library-manifest\')(\'./manifest.json\',{},{\'basedir\':process.cwd(),\'paths\':\'posix\'}).src; for ( var i = 0; i < arr.length; i++ ) { console.log( arr[ i ] ); }")',
4141
],
4242

lib/node_modules/@stdlib/stats/base/smax/include/stdlib/stats/base/smax.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#ifndef STDLIB_STATS_BASE_SMAX_H
2020
#define STDLIB_STATS_BASE_SMAX_H
2121

22-
#include <stdint.h>
22+
#include "stdlib/blas/base/shared.h"
2323

2424
/*
2525
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
@@ -31,7 +31,12 @@ extern "C" {
3131
/**
3232
* Computes the maximum value of a single-precision floating-point strided array.
3333
*/
34-
float stdlib_strided_smax( const int64_t N, const float *X, const int64_t stride );
34+
float API_SUFFIX(stdlib_strided_smax)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
35+
36+
/**
37+
* Computes the maximum value of a single-precision floating-point strided array using alternative indexing semantics.
38+
*/
39+
float API_SUFFIX(stdlib_strided_smax_ndarray)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
3540

3641
#ifdef __cplusplus
3742
}

lib/node_modules/@stdlib/stats/base/smax/lib/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,17 @@
2828
* var smax = require( '@stdlib/stats/base/smax' );
2929
*
3030
* var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
31-
* var N = x.length;
3231
*
33-
* var v = smax( N, x, 1 );
32+
* var v = smax( x.length, x, 1 );
3433
* // returns 2.0
3534
*
3635
* @example
3736
* var Float32Array = require( '@stdlib/array/float32' );
38-
* var floor = require( '@stdlib/math/base/special/floor' );
3937
* var smax = require( '@stdlib/stats/base/smax' );
4038
*
4139
* var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
42-
* var N = floor( x.length / 2 );
4340
*
44-
* var v = smax.ndarray( N, x, 2, 1 );
41+
* var v = smax.ndarray( 4, x, 2, 1 );
4542
* // returns 4.0
4643
*/
4744

lib/node_modules/@stdlib/stats/base/smax/lib/ndarray.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,19 @@ var isPositiveZerof = require( '@stdlib/math/base/assert/is-positive-zerof' );
3131
*
3232
* @param {PositiveInteger} N - number of indexed elements
3333
* @param {Float32Array} x - input array
34-
* @param {integer} stride - stride length
35-
* @param {NonNegativeInteger} offset - starting index
34+
* @param {integer} strideX - stride length
35+
* @param {NonNegativeInteger} offsetX - starting index
3636
* @returns {number} maximum value
3737
*
3838
* @example
3939
* var Float32Array = require( '@stdlib/array/float32' );
40-
* var floor = require( '@stdlib/math/base/special/floor' );
4140
*
4241
* var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
43-
* var N = floor( x.length / 2 );
4442
*
45-
* var v = smax( N, x, 2, 1 );
43+
* var v = smax( 4, x, 2, 1 );
4644
* // returns 4.0
4745
*/
48-
function smax( N, x, stride, offset ) {
46+
function smax( N, x, strideX, offsetX ) {
4947
var max;
5048
var ix;
5149
var v;
@@ -54,13 +52,13 @@ function smax( N, x, stride, offset ) {
5452
if ( N <= 0 ) {
5553
return NaN;
5654
}
57-
if ( N === 1 || stride === 0 ) {
58-
return x[ offset ];
55+
if ( N === 1 || strideX === 0 ) {
56+
return x[ offsetX ];
5957
}
60-
ix = offset;
58+
ix = offsetX;
6159
max = x[ ix ];
6260
for ( i = 1; i < N; i++ ) {
63-
ix += stride;
61+
ix += strideX;
6462
v = x[ ix ];
6563
if ( isnanf( v ) ) {
6664
return v;

lib/node_modules/@stdlib/stats/base/smax/lib/ndarray.native.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
// MODULES //
2222

23-
var Float32Array = require( '@stdlib/array/float32' );
24-
var addon = require( './smax.native.js' );
23+
var addon = require( './../src/addon.node' );
2524

2625

2726
// MAIN //
@@ -31,27 +30,20 @@ var addon = require( './smax.native.js' );
3130
*
3231
* @param {PositiveInteger} N - number of indexed elements
3332
* @param {Float32Array} x - input array
34-
* @param {integer} stride - stride length
35-
* @param {NonNegativeInteger} offset - starting index
33+
* @param {integer} strideX - stride length
34+
* @param {NonNegativeInteger} offsetX - starting index
3635
* @returns {number} maximum value
3736
*
3837
* @example
3938
* var Float32Array = require( '@stdlib/array/float32' );
40-
* var floor = require( '@stdlib/math/base/special/floor' );
4139
*
4240
* var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
43-
* var N = floor( x.length / 2 );
4441
*
45-
* var v = smax( N, x, 2, 1 );
42+
* var v = smax( 4, x, 2, 1 );
4643
* // returns 4.0
4744
*/
48-
function smax( N, x, stride, offset ) {
49-
var view;
50-
if ( stride < 0 ) {
51-
offset += (N-1) * stride;
52-
}
53-
view = new Float32Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offset), x.length-offset ); // eslint-disable-line max-len
54-
return addon( N, view, stride );
45+
function smax( N, x, strideX, offsetX ) {
46+
return addon.ndarray( N, x, strideX, offsetX );
5547
}
5648

5749

lib/node_modules/@stdlib/stats/base/smax/lib/smax.js

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
// MODULES //
2222

23-
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
24-
var isPositiveZerof = require( '@stdlib/math/base/assert/is-positive-zerof' );
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './ndarray.js' );
2525

2626

2727
// MAIN //
@@ -31,47 +31,19 @@ var isPositiveZerof = require( '@stdlib/math/base/assert/is-positive-zerof' );
3131
*
3232
* @param {PositiveInteger} N - number of indexed elements
3333
* @param {Float32Array} x - input array
34-
* @param {integer} stride - stride length
34+
* @param {integer} strideX - stride length
3535
* @returns {number} maximum value
3636
*
3737
* @example
3838
* var Float32Array = require( '@stdlib/array/float32' );
3939
*
4040
* var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
41-
* var N = x.length;
4241
*
43-
* var v = smax( N, x, 1 );
42+
* var v = smax( x.length, x, 1 );
4443
* // returns 2.0
4544
*/
46-
function smax( N, x, stride ) {
47-
var max;
48-
var ix;
49-
var v;
50-
var i;
51-
52-
if ( N <= 0 ) {
53-
return NaN;
54-
}
55-
if ( N === 1 || stride === 0 ) {
56-
return x[ 0 ];
57-
}
58-
if ( stride < 0 ) {
59-
ix = (1-N) * stride;
60-
} else {
61-
ix = 0;
62-
}
63-
max = x[ ix ];
64-
for ( i = 1; i < N; i++ ) {
65-
ix += stride;
66-
v = x[ ix ];
67-
if ( isnanf( v ) ) {
68-
return v;
69-
}
70-
if ( v > max || ( v === max && isPositiveZerof( v ) ) ) {
71-
max = v;
72-
}
73-
}
74-
return max;
45+
function smax( N, x, strideX ) {
46+
return ndarray( N, x, strideX, stride2offset( N, strideX ) );
7547
}
7648

7749

lib/node_modules/@stdlib/stats/base/smax/lib/smax.native.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,19 @@ var addon = require( './../src/addon.node' );
3030
*
3131
* @param {PositiveInteger} N - number of indexed elements
3232
* @param {Float32Array} x - input array
33-
* @param {integer} stride - stride length
33+
* @param {integer} strideX - stride length
3434
* @returns {number} maximum value
3535
*
3636
* @example
3737
* var Float32Array = require( '@stdlib/array/float32' );
3838
*
3939
* var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
40-
* var N = x.length;
4140
*
42-
* var v = smax( N, x, 1 );
41+
* var v = smax( x.length, x, 1 );
4342
* // returns 2.0
4443
*/
45-
function smax( N, x, stride ) {
46-
return addon( N, x, stride );
44+
function smax( N, x, strideX ) {
45+
return addon( N, x, strideX );
4746
}
4847

4948

lib/node_modules/@stdlib/stats/base/smax/manifest.json

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
2-
"options": {},
2+
"options": {
3+
"task": "build",
4+
"wasm": false
5+
},
36
"fields": [
47
{
58
"field": "src",
@@ -24,17 +27,78 @@
2427
],
2528
"confs": [
2629
{
30+
"task": "build",
31+
"wasm": false,
2732
"src": [
28-
"./src/smax.c"
33+
"./src/main.c"
2934
],
3035
"include": [
3136
"./include"
3237
],
33-
"libraries": [
34-
"-lm"
38+
"libraries": [],
39+
"libpath": [],
40+
"dependencies": [
41+
"@stdlib/blas/base/shared",
42+
"@stdlib/strided/base/stride2offset",
43+
"@stdlib/math/base/assert/is-positive-zerof",
44+
"@stdlib/math/base/assert/is-nanf",
45+
"@stdlib/napi/export",
46+
"@stdlib/napi/argv",
47+
"@stdlib/napi/argv-int64",
48+
"@stdlib/napi/argv-strided-float32array",
49+
"@stdlib/napi/create-double"
50+
]
51+
},
52+
{
53+
"task": "benchmark",
54+
"wasm": false,
55+
"src": [
56+
"./src/main.c"
57+
],
58+
"include": [
59+
"./include"
60+
],
61+
"libraries": [],
62+
"libpath": [],
63+
"dependencies": [
64+
"@stdlib/blas/base/shared",
65+
"@stdlib/strided/base/stride2offset",
66+
"@stdlib/math/base/assert/is-nanf",
67+
"@stdlib/math/base/assert/is-positive-zerof"
68+
]
69+
},
70+
{
71+
"task": "examples",
72+
"wasm": false,
73+
"src": [
74+
"./src/main.c"
75+
],
76+
"include": [
77+
"./include"
78+
],
79+
"libraries": [],
80+
"libpath": [],
81+
"dependencies": [
82+
"@stdlib/blas/base/shared",
83+
"@stdlib/strided/base/stride2offset",
84+
"@stdlib/math/base/assert/is-nanf",
85+
"@stdlib/math/base/assert/is-positive-zerof"
86+
]
87+
},
88+
{
89+
"task": "",
90+
"wasm": true,
91+
"src": [
92+
"./src/main.c"
93+
],
94+
"include": [
95+
"./include"
3596
],
97+
"libraries": [],
3698
"libpath": [],
3799
"dependencies": [
100+
"@stdlib/blas/base/shared",
101+
"@stdlib/strided/base/stride2offset",
38102
"@stdlib/math/base/assert/is-nanf",
39103
"@stdlib/math/base/assert/is-positive-zerof"
40104
]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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/stats/base/smax.h"
20+
#include "stdlib/napi/export.h"
21+
#include "stdlib/napi/argv.h"
22+
#include "stdlib/napi/argv_int64.h"
23+
#include "stdlib/napi/argv_strided_float32array.h"
24+
#include "stdlib/napi/create_double.h"
25+
#include <node_api.h>
26+
27+
/**
28+
* Receives JavaScript callback invocation data.
29+
*
30+
* @param env environment under which the function is invoked
31+
* @param info callback data
32+
* @return Node-API value
33+
*/
34+
static napi_value addon( napi_env env, napi_callback_info info ) {
35+
STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
36+
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
37+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 );
38+
STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 1 );
39+
STDLIB_NAPI_CREATE_DOUBLE( env, (double)stdlib_strided_smax( N, X, strideX ), v );
40+
return v;
41+
}
42+
43+
/**
44+
* Receives JavaScript callback invocation data.
45+
*
46+
* @param env environment under which the function is invoked
47+
* @param info callback data
48+
* @return Node-API value
49+
*/
50+
static napi_value addon_method( napi_env env, napi_callback_info info ) {
51+
STDLIB_NAPI_ARGV( env, info, argv, argc, 4 );
52+
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
53+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 );
54+
STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 3 );
55+
STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 1 );
56+
STDLIB_NAPI_CREATE_DOUBLE( env, (double)stdlib_strided_smax_ndarray( N, X, strideX, offsetX ), v );
57+
return v;
58+
}
59+
60+
STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method )

0 commit comments

Comments
 (0)