Skip to content

Commit f65c856

Browse files
authored
refactor: use stdlib fmod and DDD_D napi function in math/base/special/wrap
PR-URL: #2814 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent d01e0f3 commit f65c856

File tree

3 files changed

+51
-102
lines changed

3 files changed

+51
-102
lines changed

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

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
2-
"options": {},
2+
"options": {
3+
"task": "build"
4+
},
35
"fields": [
46
{
57
"field": "src",
@@ -24,6 +26,7 @@
2426
],
2527
"confs": [
2628
{
29+
"task": "build",
2730
"src": [
2831
"./src/main.c"
2932
],
@@ -33,8 +36,45 @@
3336
"libraries": [],
3437
"libpath": [],
3538
"dependencies": [
39+
"@stdlib/math/base/napi/ternary",
3640
"@stdlib/math/base/assert/is-nan",
37-
"@stdlib/math/base/special/trunc"
41+
"@stdlib/math/base/special/trunc",
42+
"@stdlib/math/base/special/fmod",
43+
"@stdlib/math/base/assert/is-negative-zero"
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/assert/is-negative-zero"
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/assert/is-negative-zero"
3878
]
3979
}
4080
]

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

+3-93
Original file line numberDiff line numberDiff line change
@@ -17,97 +17,7 @@
1717
*/
1818

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

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 )
22+
// cppcheck-suppress shadowFunction
23+
STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_wrap )

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
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 <math.h>
23-
24-
// TODO: remove <math.h> header and fmod once we have stdlib equivalent
22+
#include "stdlib/math/base/special/fmod.h"
23+
#include "stdlib/math/base/assert/is_negative_zero.h"
2524

2625
/**
2726
* 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 ) {
4948
vc = v;
5049

5150
// Normalize +-0 to +0...
52-
if ( vc == 0.0 ) {
51+
if ( stdlib_base_is_negative_zero( vc ) ) {
5352
vc = 0.0;
5453
}
55-
if ( minc == 0.0 ) {
54+
if ( stdlib_base_is_negative_zero( minc ) ) {
5655
minc = 0.0;
5756
}
58-
if ( maxc == 0.0 ) {
57+
if ( stdlib_base_is_negative_zero( maxc ) ) {
5958
maxc = 0.0;
6059
}
6160
// 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 ) {
6766
if ( vc < minc ) {
6867
vc += delta * ( stdlib_base_trunc( ( minc - vc ) / delta ) + 1.0 );
6968
}
70-
return minc + ( fmod( vc - minc, delta ) );
69+
return minc + ( stdlib_base_fmod( vc - minc, delta ) );
7170
}

0 commit comments

Comments
 (0)