Skip to content

Commit

Permalink
refactor: use stdlib fmod and DDD_D napi function
Browse files Browse the repository at this point in the history
  • Loading branch information
gunjjoshi committed Aug 20, 2024
1 parent 60b7b1e commit b345bfe
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 103 deletions.
3 changes: 2 additions & 1 deletion lib/node_modules/@stdlib/math/base/special/wrap/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

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' );


// MAIN //
Expand Down Expand Up @@ -91,7 +92,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 ) );
}


Expand Down
44 changes: 42 additions & 2 deletions lib/node_modules/@stdlib/math/base/special/wrap/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"options": {},
"options": {
"task": "build"
},
"fields": [
{
"field": "src",
Expand All @@ -24,6 +26,7 @@
],
"confs": [
{
"task": "build",
"src": [
"./src/main.c"
],
Expand All @@ -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"
]
}
]
Expand Down
96 changes: 3 additions & 93 deletions lib/node_modules/@stdlib/math/base/special/wrap/src/addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,97 +17,7 @@
*/

#include "stdlib/math/base/special/wrap.h"
#include <node_api.h>
#include <assert.h>
#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 )
13 changes: 6 additions & 7 deletions lib/node_modules/@stdlib/math/base/special/wrap/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <math.h>

// TODO: remove <math.h> 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).
Expand Down Expand Up @@ -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...
Expand All @@ -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 ) );
}

0 comments on commit b345bfe

Please sign in to comment.