From fcb3d0efdd301365d256c2673478e990a9d961ca Mon Sep 17 00:00:00 2001 From: GUNJ JOSHI Date: Tue, 20 Aug 2024 19:18:04 +0530 Subject: [PATCH 1/2] refactor: use stdlib fmod and DDD_D napi function --- .../math/base/special/wrap/lib/main.js | 10 +- .../math/base/special/wrap/manifest.json | 44 ++++++++- .../math/base/special/wrap/src/addon.c | 96 +------------------ .../@stdlib/math/base/special/wrap/src/main.c | 13 ++- 4 files changed, 57 insertions(+), 106 deletions(-) diff --git a/lib/node_modules/@stdlib/math/base/special/wrap/lib/main.js b/lib/node_modules/@stdlib/math/base/special/wrap/lib/main.js index b38447fe4896..035be858433a 100644 --- a/lib/node_modules/@stdlib/math/base/special/wrap/lib/main.js +++ b/lib/node_modules/@stdlib/math/base/special/wrap/lib/main.js @@ -22,6 +22,8 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); var trunc = require( '@stdlib/math/base/special/trunc' ); +var fmod = require( '@stdlib/math/base/special/fmod' ); +var abs = require( '@stdlib/math/base/special/abs' ); // MAIN // @@ -73,13 +75,13 @@ function wrap( v, min, max ) { return NaN; } // Normalize +-0 to +0... - if ( v === 0.0 ) { + if ( abs( v ) === 0.0 ) { v = 0.0; } - if ( min === 0.0 ) { + if ( abs( min ) === 0.0 ) { min = 0.0; } - if ( max === 0.0 ) { + if ( abs( max ) === 0.0 ) { max = 0.0; } // Simple case where value is already within range... @@ -91,7 +93,7 @@ function wrap( v, min, max ) { if ( v < min ) { v += delta * ( trunc( (min-v)/delta ) + 1.0 ); } - return min + ( (v-min) % delta ); + return min + ( fmod( ( v - min ), delta ) ); } diff --git a/lib/node_modules/@stdlib/math/base/special/wrap/manifest.json b/lib/node_modules/@stdlib/math/base/special/wrap/manifest.json index d89e21f11198..618251f2c7d3 100644 --- a/lib/node_modules/@stdlib/math/base/special/wrap/manifest.json +++ b/lib/node_modules/@stdlib/math/base/special/wrap/manifest.json @@ -1,5 +1,7 @@ { - "options": {}, + "options": { + "task": "build" + }, "fields": [ { "field": "src", @@ -24,6 +26,7 @@ ], "confs": [ { + "task": "build", "src": [ "./src/main.c" ], @@ -33,8 +36,45 @@ "libraries": [], "libpath": [], "dependencies": [ + "@stdlib/math/base/napi/ternary", "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/special/trunc" + "@stdlib/math/base/special/trunc", + "@stdlib/math/base/special/fmod", + "@stdlib/math/base/special/abs" + ] + }, + { + "task": "benchmark", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/assert/is-nan", + "@stdlib/math/base/special/trunc", + "@stdlib/math/base/special/fmod", + "@stdlib/math/base/special/abs" + ] + }, + { + "task": "examples", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/assert/is-nan", + "@stdlib/math/base/special/trunc", + "@stdlib/math/base/special/fmod", + "@stdlib/math/base/special/abs" ] } ] diff --git a/lib/node_modules/@stdlib/math/base/special/wrap/src/addon.c b/lib/node_modules/@stdlib/math/base/special/wrap/src/addon.c index da1c76652da1..a2404d55ee2c 100644 --- a/lib/node_modules/@stdlib/math/base/special/wrap/src/addon.c +++ b/lib/node_modules/@stdlib/math/base/special/wrap/src/addon.c @@ -17,97 +17,7 @@ */ #include "stdlib/math/base/special/wrap.h" -#include -#include +#include "stdlib/math/base/napi/ternary.h" -/** -* Receives JavaScript callback invocation data. -* -* @param env environment under which the function is invoked -* @param info callback data -* @return Node-API value -*/ -static napi_value addon( napi_env env, napi_callback_info info ) { - napi_status status; - - // Get callback arguments: - size_t argc = 3; - napi_value argv[ 3 ]; - status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); - assert( status == napi_ok ); - - // Check whether we were provided the correct number of arguments: - if ( argc < 3 ) { - status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." ); - assert( status == napi_ok ); - return NULL; - } - if ( argc > 3 ) { - status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." ); - assert( status == napi_ok ); - return NULL; - } - - napi_valuetype vtype0; - status = napi_typeof( env, argv[ 0 ], &vtype0 ); - assert( status == napi_ok ); - if ( vtype0 != napi_number ) { - status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." ); - assert( status == napi_ok ); - return NULL; - } - - napi_valuetype vtype1; - status = napi_typeof( env, argv[ 1 ], &vtype1 ); - assert( status == napi_ok ); - if ( vtype0 != napi_number ) { - status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." ); - assert( status == napi_ok ); - return NULL; - } - - napi_valuetype vtype2; - status = napi_typeof( env, argv[ 2 ], &vtype2 ); - assert( status == napi_ok ); - if ( vtype0 != napi_number ) { - status = napi_throw_type_error( env, NULL, "invalid argument. Third argument must be a number." ); - assert( status == napi_ok ); - return NULL; - } - - double v; - status = napi_get_value_double( env, argv[ 0 ], &v ); - assert( status == napi_ok ); - - double min; - status = napi_get_value_double( env, argv[ 1 ], &min ); - assert( status == napi_ok ); - - double max; - status = napi_get_value_double( env, argv[ 2 ], &max ); - assert( status == napi_ok ); - - double out = stdlib_base_wrap( v, min, max ); - - napi_value w; - status = napi_create_double( env, out, &w ); - assert( status == napi_ok ); - - return w; -} - -/** -* Initializes a Node-API module. -* -* @param env environment under which the function is invoked -* @param exports exports object -* @return main export -*/ -static napi_value init( napi_env env, napi_value exports ) { - napi_value fcn; - napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn ); - assert( status == napi_ok ); - return fcn; -} - -NAPI_MODULE( NODE_GYP_MODULE_NAME, init ) +// cppcheck-suppress shadowFunction +STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_wrap ) diff --git a/lib/node_modules/@stdlib/math/base/special/wrap/src/main.c b/lib/node_modules/@stdlib/math/base/special/wrap/src/main.c index df46f5f9f30c..83cfde819a72 100644 --- a/lib/node_modules/@stdlib/math/base/special/wrap/src/main.c +++ b/lib/node_modules/@stdlib/math/base/special/wrap/src/main.c @@ -19,9 +19,8 @@ #include "stdlib/math/base/special/wrap.h" #include "stdlib/math/base/assert/is_nan.h" #include "stdlib/math/base/special/trunc.h" -#include - -// TODO: remove header and fmod once we have stdlib equivalent +#include "stdlib/math/base/special/fmod.h" +#include "stdlib/math/base/special/abs.h" /** * Wraps a value on the half-open interval [min,max). @@ -49,13 +48,13 @@ double stdlib_base_wrap( const double v, const double min, const double max ) { vc = v; // Normalize +-0 to +0... - if ( vc == 0.0 ) { + if ( stdlib_base_abs( vc ) == 0.0 ) { vc = 0.0; } - if ( minc == 0.0 ) { + if ( stdlib_base_abs( minc ) == 0.0 ) { minc = 0.0; } - if ( maxc == 0.0 ) { + if ( stdlib_base_abs( maxc ) == 0.0 ) { maxc = 0.0; } // Simple case where value is already within range... @@ -67,5 +66,5 @@ double stdlib_base_wrap( const double v, const double min, const double max ) { if ( vc < minc ) { vc += delta * ( stdlib_base_trunc( ( minc - vc ) / delta ) + 1.0 ); } - return minc + ( fmod( vc - minc, delta ) ); + return minc + ( stdlib_base_fmod( vc - minc, delta ) ); } From b66cc7c909662db26ab2c4070a763e8eec704f48 Mon Sep 17 00:00:00 2001 From: GUNJ JOSHI Date: Wed, 21 Aug 2024 05:28:19 +0530 Subject: [PATCH 2/2] refactor: use is_negative_zero, revert js changes --- .../@stdlib/math/base/special/wrap/lib/main.js | 10 ++++------ .../@stdlib/math/base/special/wrap/manifest.json | 6 +++--- .../@stdlib/math/base/special/wrap/src/main.c | 8 ++++---- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/node_modules/@stdlib/math/base/special/wrap/lib/main.js b/lib/node_modules/@stdlib/math/base/special/wrap/lib/main.js index 035be858433a..b38447fe4896 100644 --- a/lib/node_modules/@stdlib/math/base/special/wrap/lib/main.js +++ b/lib/node_modules/@stdlib/math/base/special/wrap/lib/main.js @@ -22,8 +22,6 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); var trunc = require( '@stdlib/math/base/special/trunc' ); -var fmod = require( '@stdlib/math/base/special/fmod' ); -var abs = require( '@stdlib/math/base/special/abs' ); // MAIN // @@ -75,13 +73,13 @@ function wrap( v, min, max ) { return NaN; } // Normalize +-0 to +0... - if ( abs( v ) === 0.0 ) { + if ( v === 0.0 ) { v = 0.0; } - if ( abs( min ) === 0.0 ) { + if ( min === 0.0 ) { min = 0.0; } - if ( abs( max ) === 0.0 ) { + if ( max === 0.0 ) { max = 0.0; } // Simple case where value is already within range... @@ -93,7 +91,7 @@ function wrap( v, min, max ) { if ( v < min ) { v += delta * ( trunc( (min-v)/delta ) + 1.0 ); } - return min + ( fmod( ( v - min ), delta ) ); + return min + ( (v-min) % delta ); } diff --git a/lib/node_modules/@stdlib/math/base/special/wrap/manifest.json b/lib/node_modules/@stdlib/math/base/special/wrap/manifest.json index 618251f2c7d3..b2f19cbfa4f3 100644 --- a/lib/node_modules/@stdlib/math/base/special/wrap/manifest.json +++ b/lib/node_modules/@stdlib/math/base/special/wrap/manifest.json @@ -40,7 +40,7 @@ "@stdlib/math/base/assert/is-nan", "@stdlib/math/base/special/trunc", "@stdlib/math/base/special/fmod", - "@stdlib/math/base/special/abs" + "@stdlib/math/base/assert/is-negative-zero" ] }, { @@ -57,7 +57,7 @@ "@stdlib/math/base/assert/is-nan", "@stdlib/math/base/special/trunc", "@stdlib/math/base/special/fmod", - "@stdlib/math/base/special/abs" + "@stdlib/math/base/assert/is-negative-zero" ] }, { @@ -74,7 +74,7 @@ "@stdlib/math/base/assert/is-nan", "@stdlib/math/base/special/trunc", "@stdlib/math/base/special/fmod", - "@stdlib/math/base/special/abs" + "@stdlib/math/base/assert/is-negative-zero" ] } ] diff --git a/lib/node_modules/@stdlib/math/base/special/wrap/src/main.c b/lib/node_modules/@stdlib/math/base/special/wrap/src/main.c index 83cfde819a72..59fccb638f69 100644 --- a/lib/node_modules/@stdlib/math/base/special/wrap/src/main.c +++ b/lib/node_modules/@stdlib/math/base/special/wrap/src/main.c @@ -20,7 +20,7 @@ #include "stdlib/math/base/assert/is_nan.h" #include "stdlib/math/base/special/trunc.h" #include "stdlib/math/base/special/fmod.h" -#include "stdlib/math/base/special/abs.h" +#include "stdlib/math/base/assert/is_negative_zero.h" /** * Wraps a value on the half-open interval [min,max). @@ -48,13 +48,13 @@ double stdlib_base_wrap( const double v, const double min, const double max ) { vc = v; // Normalize +-0 to +0... - if ( stdlib_base_abs( vc ) == 0.0 ) { + if ( stdlib_base_is_negative_zero( vc ) ) { vc = 0.0; } - if ( stdlib_base_abs( minc ) == 0.0 ) { + if ( stdlib_base_is_negative_zero( minc ) ) { minc = 0.0; } - if ( stdlib_base_abs( maxc ) == 0.0 ) { + if ( stdlib_base_is_negative_zero( maxc ) ) { maxc = 0.0; } // Simple case where value is already within range...