Skip to content

Commit 64a15d3

Browse files
committed
Revert "refactor: use stdlib fmod and DDD_D napi function"
This reverts commit b345bfe.
1 parent 4fb46d2 commit 64a15d3

File tree

4 files changed

+103
-53
lines changed

4 files changed

+103
-53
lines changed

lib/node_modules/@stdlib/math/base/special/wrap/lib/main.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2424
var trunc = require( '@stdlib/math/base/special/trunc' );
25-
var fmod = require( '@stdlib/math/base/special/fmod' );
2625

2726

2827
// MAIN //
@@ -92,7 +91,7 @@ function wrap( v, min, max ) {
9291
if ( v < min ) {
9392
v += delta * ( trunc( (min-v)/delta ) + 1.0 );
9493
}
95-
return min + ( fmod( ( v - min ), delta ) );
94+
return min + ( (v-min) % delta );
9695
}
9796

9897

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

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
2-
"options": {
3-
"task": "build"
4-
},
2+
"options": {},
53
"fields": [
64
{
75
"field": "src",
@@ -26,7 +24,6 @@
2624
],
2725
"confs": [
2826
{
29-
"task": "build",
3027
"src": [
3128
"./src/main.c"
3229
],
@@ -36,45 +33,8 @@
3633
"libraries": [],
3734
"libpath": [],
3835
"dependencies": [
39-
"@stdlib/math/base/napi/ternary",
4036
"@stdlib/math/base/assert/is-nan",
41-
"@stdlib/math/base/special/trunc",
42-
"@stdlib/math/base/special/fmod",
43-
"@stdlib/math/base/special/abs"
44-
]
45-
},
46-
{
47-
"task": "benchmark",
48-
"src": [
49-
"./src/main.c"
50-
],
51-
"include": [
52-
"./include"
53-
],
54-
"libraries": [],
55-
"libpath": [],
56-
"dependencies": [
57-
"@stdlib/math/base/assert/is-nan",
58-
"@stdlib/math/base/special/trunc",
59-
"@stdlib/math/base/special/fmod",
60-
"@stdlib/math/base/special/abs"
61-
]
62-
},
63-
{
64-
"task": "examples",
65-
"src": [
66-
"./src/main.c"
67-
],
68-
"include": [
69-
"./include"
70-
],
71-
"libraries": [],
72-
"libpath": [],
73-
"dependencies": [
74-
"@stdlib/math/base/assert/is-nan",
75-
"@stdlib/math/base/special/trunc",
76-
"@stdlib/math/base/special/fmod",
77-
"@stdlib/math/base/special/abs"
37+
"@stdlib/math/base/special/trunc"
7838
]
7939
}
8040
]

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

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,97 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/wrap.h"
20-
#include "stdlib/math/base/napi/ternary.h"
20+
#include <node_api.h>
21+
#include <assert.h>
2122

22-
// cppcheck-suppress shadowFunction
23-
STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_wrap )
23+
/**
24+
* Receives JavaScript callback invocation data.
25+
*
26+
* @param env environment under which the function is invoked
27+
* @param info callback data
28+
* @return Node-API value
29+
*/
30+
static napi_value addon( napi_env env, napi_callback_info info ) {
31+
napi_status status;
32+
33+
// Get callback arguments:
34+
size_t argc = 3;
35+
napi_value argv[ 3 ];
36+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
37+
assert( status == napi_ok );
38+
39+
// Check whether we were provided the correct number of arguments:
40+
if ( argc < 3 ) {
41+
status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
42+
assert( status == napi_ok );
43+
return NULL;
44+
}
45+
if ( argc > 3 ) {
46+
status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
47+
assert( status == napi_ok );
48+
return NULL;
49+
}
50+
51+
napi_valuetype vtype0;
52+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
53+
assert( status == napi_ok );
54+
if ( vtype0 != napi_number ) {
55+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
56+
assert( status == napi_ok );
57+
return NULL;
58+
}
59+
60+
napi_valuetype vtype1;
61+
status = napi_typeof( env, argv[ 1 ], &vtype1 );
62+
assert( status == napi_ok );
63+
if ( vtype0 != napi_number ) {
64+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
65+
assert( status == napi_ok );
66+
return NULL;
67+
}
68+
69+
napi_valuetype vtype2;
70+
status = napi_typeof( env, argv[ 2 ], &vtype2 );
71+
assert( status == napi_ok );
72+
if ( vtype0 != napi_number ) {
73+
status = napi_throw_type_error( env, NULL, "invalid argument. Third argument must be a number." );
74+
assert( status == napi_ok );
75+
return NULL;
76+
}
77+
78+
double v;
79+
status = napi_get_value_double( env, argv[ 0 ], &v );
80+
assert( status == napi_ok );
81+
82+
double min;
83+
status = napi_get_value_double( env, argv[ 1 ], &min );
84+
assert( status == napi_ok );
85+
86+
double max;
87+
status = napi_get_value_double( env, argv[ 2 ], &max );
88+
assert( status == napi_ok );
89+
90+
double out = stdlib_base_wrap( v, min, max );
91+
92+
napi_value w;
93+
status = napi_create_double( env, out, &w );
94+
assert( status == napi_ok );
95+
96+
return w;
97+
}
98+
99+
/**
100+
* Initializes a Node-API module.
101+
*
102+
* @param env environment under which the function is invoked
103+
* @param exports exports object
104+
* @return main export
105+
*/
106+
static napi_value init( napi_env env, napi_value exports ) {
107+
napi_value fcn;
108+
napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
109+
assert( status == napi_ok );
110+
return fcn;
111+
}
112+
113+
NAPI_MODULE( NODE_GYP_MODULE_NAME, init )

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
#include "stdlib/math/base/special/wrap.h"
2020
#include "stdlib/math/base/assert/is_nan.h"
2121
#include "stdlib/math/base/special/trunc.h"
22-
#include "stdlib/math/base/special/fmod.h"
23-
#include "stdlib/math/base/special/abs.h"
22+
#include <math.h>
23+
24+
// TODO: remove <math.h> header and fmod once we have stdlib equivalent
2425

2526
/**
2627
* Wraps a value on the half-open interval [min,max).
@@ -48,13 +49,13 @@ double stdlib_base_wrap( const double v, const double min, const double max ) {
4849
vc = v;
4950

5051
// Normalize +-0 to +0...
51-
if ( stdlib_base_abs( vc ) == 0.0 ) {
52+
if ( vc == 0.0 ) {
5253
vc = 0.0;
5354
}
54-
if ( stdlib_base_abs( minc ) == 0.0 ) {
55+
if ( minc == 0.0 ) {
5556
minc = 0.0;
5657
}
57-
if ( stdlib_base_abs( maxc ) == 0.0 ) {
58+
if ( maxc == 0.0 ) {
5859
maxc = 0.0;
5960
}
6061
// Simple case where value is already within range...
@@ -66,5 +67,5 @@ double stdlib_base_wrap( const double v, const double min, const double max ) {
6667
if ( vc < minc ) {
6768
vc += delta * ( stdlib_base_trunc( ( minc - vc ) / delta ) + 1.0 );
6869
}
69-
return minc + ( stdlib_base_fmod( vc - minc, delta ) );
70+
return minc + ( fmod( vc - minc, delta ) );
7071
}

0 commit comments

Comments
 (0)