Skip to content

Commit 466d7e6

Browse files
committed
implemetation updated
1 parent 0235838 commit 466d7e6

File tree

8 files changed

+261
-50
lines changed

8 files changed

+261
-50
lines changed

lib/node_modules/@stdlib/math/base/special/minmax/benchmark/c/native/benchmark.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ static double benchmark( void ) {
9393
double elapsed;
9494
double x1[100];
9595
double x2[100];
96-
double y[] = { 0.0, 0.0 };
96+
double min;
97+
double max;
9798
double t;
9899
int i;
99100

@@ -104,14 +105,14 @@ static double benchmark( void ) {
104105

105106
t = tic();
106107
for ( i = 0; i < ITERATIONS; i++ ) {
107-
stdlib_base_minmax( x1[ i % 100 ], x2[ i % 100 ], &y );
108-
if ( y != y ) {
108+
stdlib_base_minmax( x1[ i % 100 ], x2[ i % 100 ], &min, &max );
109+
if ( min != min && max != max ) {
109110
printf( "should not return NaN\n" );
110111
break;
111112
}
112113
}
113114
elapsed = tic() - t;
114-
if ( y != y ) {
115+
if ( min != min && max != max ) {
115116
printf( "should not return NaN\n" );
116117
}
117118
return elapsed;

lib/node_modules/@stdlib/math/base/special/minmax/examples/c/example.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ int main(void) {
2626

2727
int i;
2828
for ( i = 0; i < 10; i++ ) {
29-
double out[] = { 0.0, 0.0 };
30-
stdlib_base_minmax( x1[ i ], x2[ i ], &out );
31-
printf( "x1[ %d ]: %lf, x2[ %d ]: %lf, minmax( x1[ %d ], x2[ %d ] ): ( %lf, %lf )\n", i, x1[ i ], i, x2[ i ], i, i, out[0], out[1] );
29+
double min;
30+
double max;
31+
stdlib_base_minmax( x1[ i ], x2[ i ], &min, &max );
32+
printf( "x1[ %d ]: %lf, x2[ %d ]: %lf, minmax( x1[ %d ], x2[ %d ] ): ( %lf, %lf )\n", i, x1[ i ], i, x2[ i ], i, i, min, max );
3233
}
3334
}
3435

lib/node_modules/@stdlib/math/base/special/minmax/include/stdlib/math/base/special/minmax.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ extern "C" {
3131
*
3232
* @param x First value
3333
* @param y Second value
34-
* @param out output array
35-
* @return A double containing the minimum and maximum values
34+
* @param min double to store minimum value
35+
* @param max double to store maximum value
3636
*/
37-
double stdlib_base_minmax(const double x, const double y, double* out );
37+
void stdlib_base_minmax(const double x, const double y, double* min, double* max );
3838

3939
#ifdef __cplusplus
4040
}

lib/node_modules/@stdlib/math/base/special/minmax/lib/native.js

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

2121
// MODULES //
2222

23+
var Float64Array = require( '@stdlib/array/float64' );
2324
var addon = require( './../src/addon.node' );
2425

2526

@@ -31,7 +32,7 @@ var addon = require( './../src/addon.node' );
3132
* @private
3233
* @param {number} x - first number
3334
* @param {number} y - second number
34-
* @returns {number} minimum and maximum value
35+
* @returns {Array<number>} minimum and maximum values
3536
*
3637
* @example
3738
* var v = minmax( 3.14, 4.2 );
@@ -46,7 +47,8 @@ var addon = require( './../src/addon.node' );
4647
* // returns [ NaN, NaN ]
4748
*/
4849
function minmax( x, y ) {
49-
var out = addon( x, y );
50+
var out = new Float64Array( 2 );
51+
addon( out, x, y );
5052
return [ out[ 0 ], out[ 1 ] ];
5153
}
5254

lib/node_modules/@stdlib/math/base/special/minmax/manifest.json

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@
3737
"libpath": [],
3838
"dependencies": [
3939
"@stdlib/math/base/napi/binary",
40-
"@stdlib/math/base/assert/is-nan",
41-
"@stdlib/math/base/special/max",
42-
"@stdlib/math/base/special/min"
40+
"@stdlib/math/base/assert/is-nan"
4341
]
4442
},
4543
{
@@ -53,9 +51,7 @@
5351
"libraries": [],
5452
"libpath": [],
5553
"dependencies": [
56-
"@stdlib/math/base/assert/is-nan",
57-
"@stdlib/math/base/special/max",
58-
"@stdlib/math/base/special/min"
54+
"@stdlib/math/base/assert/is-nan"
5955
]
6056
},
6157
{
@@ -69,9 +65,7 @@
6965
"libraries": [],
7066
"libpath": [],
7167
"dependencies": [
72-
"@stdlib/math/base/assert/is-nan",
73-
"@stdlib/math/base/special/max",
74-
"@stdlib/math/base/special/min"
68+
"@stdlib/math/base/assert/is-nan"
7569
]
7670
}
7771
]

lib/node_modules/@stdlib/math/base/special/minmax/src/addon.c

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,105 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/minmax.h"
20-
#include "stdlib/math/base/napi/binary.h"
20+
#include <node_api.h>
21+
#include <stdint.h>
22+
#include <assert.h>
2123

2224
/**
23-
* Evaluates the min and max of two values.
25+
* Receives JavaScript callback invocation data.
2426
*
25-
* @param x First value
26-
* @param y Second value
27-
* @return A double containing the minimum and maximum values
28-
*
29-
* @example
30-
* double y = minmax( 4.0, -5.0 );
31-
* // returns [ -5.0, 4.0 ]
27+
* @private
28+
* @param env environment under which the function is invoked
29+
* @param info callback data
30+
* @return Node-API value
3231
*/
32+
static napi_value addon( napi_env env, napi_callback_info info ) {
33+
napi_status status;
34+
35+
// Get callback arguments:
36+
size_t argc = 3;
37+
napi_value argv[ 3 ];
38+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
39+
assert( status == napi_ok );
40+
41+
// Check whether we were provided the correct number of arguments:
42+
if ( argc < 3 ) {
43+
status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
44+
assert( status == napi_ok );
45+
return NULL;
46+
}
47+
if ( argc > 3 ) {
48+
status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
49+
assert( status == napi_ok );
50+
return NULL;
51+
}
52+
53+
bool res;
54+
status = napi_is_typedarray( env, argv[ 0 ], &res );
55+
assert( status == napi_ok );
56+
if ( res == false ) {
57+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a Float64Array." );
58+
assert( status == napi_ok );
59+
return NULL;
60+
}
61+
62+
napi_valuetype vtype1;
63+
status = napi_typeof( env, argv[ 1 ], &vtype1 );
64+
assert( status == napi_ok );
65+
if ( vtype1 != napi_number ) {
66+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
67+
assert( status == napi_ok );
68+
return NULL;
69+
}
70+
71+
napi_typedarray_type vtype0;
72+
size_t len;
73+
void *Out;
74+
status = napi_get_typedarray_info( env, argv[ 0 ], &vtype0, &len, &Out, NULL, NULL );
75+
assert( status == napi_ok );
76+
if ( vtype0 != napi_float64_array ) {
77+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a Float64Array." );
78+
assert( status == napi_ok );
79+
return NULL;
80+
}
81+
if ( len != 2 ) {
82+
status = napi_throw_range_error( env, NULL, "invalid argument. First argument must have 2 elements." );
83+
assert( status == napi_ok );
84+
return NULL;
85+
}
86+
87+
double value1;
88+
status = napi_get_value_double( env, argv[ 1 ], &value1 );
89+
assert( status == napi_ok );
3390

34-
double minmax( double x, double y ) {
91+
double value2;
92+
status = napi_get_value_double( env, argv[ 2 ], &value2 );
93+
assert( status == napi_ok );
3594

36-
double result[] = { 0.0, 0.0 };
95+
double min;
96+
double max;
97+
stdlib_base_minmax( value1, value2, &min, &max );
3798

38-
stdlib_base_minmax( x, y, result );
39-
40-
return result;
99+
double *op = (double *)Out;
100+
op[ 0 ] = min;
101+
op[ 1 ] = max;
102+
103+
return NULL;
104+
}
105+
106+
/**
107+
* Initializes a Node-API module.
108+
*
109+
* @private
110+
* @param env environment under which the function is invoked
111+
* @param exports exports object
112+
* @return main export
113+
*/
114+
static napi_value init( napi_env env, napi_value exports ) {
115+
napi_value fcn;
116+
napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
117+
assert( status == napi_ok );
118+
return fcn;
41119
}
42120

43-
// cppcheck-suppress shadowFunction
44-
STDLIB_MATH_BASE_NAPI_MODULE_DD_D( minmax )
121+
NAPI_MODULE( NODE_GYP_MODULE_NAME, init )

lib/node_modules/@stdlib/math/base/special/minmax/src/main.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,42 @@
1919
#include <stdlib.h>
2020
#include "stdlib/math/base/special/minmax.h"
2121
#include "stdlib/math/base/assert/is_nan.h"
22-
#include "stdlib/math/base/special/max.h"
23-
#include "stdlib/math/base/special/min.h"
2422

2523
/**
2624
* Evaluates the min and max of two values.
2725
*
2826
* @param x First value
2927
* @param y Second value
30-
* @param out output array
31-
* @return A double containing the minimum and maximum values
28+
* @param min to store minimum value
29+
* @param max to store maximum value
3230
*
3331
* @example
34-
* double y = stdlib_base_minmax( 4.0, -5.0, [ 0.0, 0.0 ] );
35-
* // returns [ -5.0, 4.0 ]
32+
* void y = stdlib_base_minmax( NaN, -1.0, &min, &max );
33+
* // min = NaN, max = NaN
34+
*
35+
* @example
36+
* void y = stdlib_base_minmax( 4.0, -5.0, &min, &max );
37+
* // min = -5.0, max = 4.0
3638
*/
3739

3840

39-
double stdlib_base_minmax( const double x, const double y, double* out ) {
41+
void stdlib_base_minmax( const double x, const double y, double* min, double* max ) {
4042

4143
if( stdlib_base_is_nan( x ) || stdlib_base_is_nan( y ) ){
42-
out[ 0 ] = 0.0 / 0.0;
43-
out[ 1 ] = 0.0 / 0.0;
44+
*min = 0.0 / 0.0; // NaN
45+
*max = 0.0 / 0.0; // NaN
46+
return;
4447
}
45-
else{
46-
out[ 0 ] = stdlib_base_min( x, y );
47-
out[ 1 ] = stdlib_base_max( x, y );
48+
49+
if( x < y ){
50+
*min = x;
51+
*max = y;
52+
return;
4853
}
54+
55+
*min = y;
56+
*max = x;
4957

50-
return *out;
58+
return;
5159

5260
}

0 commit comments

Comments
 (0)