Skip to content

Commit 5a2afad

Browse files
feat: add D_F macro in math/base/napi/unary
PR-URL: #6253 Ref: https://github.com/freebsd/freebsd-src/blob/6f6c07813b38ab04d8b1b2bb87c0291dbae25a25/lib/msun/src/k_cosf.c#L31-L33 Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]> Co-authored-by: Athan Reines <[email protected]>
1 parent 6ff59dd commit 5a2afad

File tree

5 files changed

+212
-0
lines changed

5 files changed

+212
-0
lines changed

lib/node_modules/@stdlib/math/base/napi/unary/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,66 @@ The function accepts the following arguments:
301301
void stdlib_math_base_napi_d_d( napi_env env, napi_callback_info info, double (*fcn)( double ) );
302302
```
303303
304+
#### STDLIB_MATH_BASE_NAPI_MODULE_D_F( fcn )
305+
306+
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting a double-precision floating-point number and returning a single-precision floating-point number.
307+
308+
```c
309+
static float fcn( const double x ) {
310+
return (float)x;
311+
}
312+
313+
// ...
314+
315+
// Register a Node-API module:
316+
STDLIB_MATH_BASE_NAPI_MODULE_D_F( fcn );
317+
```
318+
319+
The macro expects the following arguments:
320+
321+
- **fcn**: `float (*fcn)( double )` unary function.
322+
323+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
324+
325+
#### stdlib_math_base_napi_d_f( env, info, fcn )
326+
327+
Invokes a unary function accepting a double-precision floating-point number and returning a single-precision floating-point number.
328+
329+
```c
330+
#include <node_api.h>
331+
332+
// ...
333+
334+
static float fcn( const double x ) {
335+
return (float)x;
336+
}
337+
338+
// ...
339+
340+
/**
341+
* Receives JavaScript callback invocation data.
342+
*
343+
* @param env environment under which the function is invoked
344+
* @param info callback data
345+
* @return Node-API value
346+
*/
347+
napi_value addon( napi_env env, napi_callback_info info ) {
348+
return stdlib_math_base_napi_d_f( env, info, fcn );
349+
}
350+
351+
// ...
352+
```
353+
354+
The function accepts the following arguments:
355+
356+
- **env**: `[in] napi_env` environment under which the function is invoked.
357+
- **info**: `[in] napi_callback_info` callback data.
358+
- **fcn**: `[in] float (*fcn)( double )` unary function.
359+
360+
```c
361+
void stdlib_math_base_napi_d_f( napi_env env, napi_callback_info info, float (*fcn)( double ) );
362+
```
363+
304364
#### STDLIB_MATH_BASE_NAPI_MODULE_F_F( fcn )
305365

306366
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting and returning single-precision floating-point numbers.

lib/node_modules/@stdlib/math/base/napi/unary/include/stdlib/math/base/napi/unary.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "stdlib/math/base/napi/unary/c_c.h"
2424
#include "stdlib/math/base/napi/unary/c_f.h"
2525
#include "stdlib/math/base/napi/unary/d_d.h"
26+
#include "stdlib/math/base/napi/unary/d_f.h"
2627
#include "stdlib/math/base/napi/unary/f_f.h"
2728
#include "stdlib/math/base/napi/unary/f_i.h"
2829
#include "stdlib/math/base/napi/unary/i_d.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#ifndef STDLIB_MATH_BASE_NAPI_UNARY_D_F_H
20+
#define STDLIB_MATH_BASE_NAPI_UNARY_D_F_H
21+
22+
#include <node_api.h>
23+
#include <assert.h>
24+
25+
/**
26+
* Macro for registering a Node-API module exporting an interface invoking a unary function accepting a double-precision floating-point number and returning a single-precision floating-point number.
27+
*
28+
* @param fcn unary function
29+
*
30+
* @example
31+
* static float fcn( const double x ) {
32+
* return (float)x;
33+
* }
34+
*
35+
* // ...
36+
*
37+
* // Register a Node-API module:
38+
* STDLIB_MATH_BASE_NAPI_MODULE_D_F( fcn );
39+
*/
40+
#define STDLIB_MATH_BASE_NAPI_MODULE_D_F( fcn ) \
41+
static napi_value stdlib_math_base_napi_d_f_wrapper( \
42+
napi_env env, \
43+
napi_callback_info info \
44+
) { \
45+
return stdlib_math_base_napi_d_f( env, info, fcn ); \
46+
}; \
47+
static napi_value stdlib_math_base_napi_d_f_init( \
48+
napi_env env, \
49+
napi_value exports \
50+
) { \
51+
napi_value fcn; \
52+
napi_status status = napi_create_function( \
53+
env, \
54+
"exports", \
55+
NAPI_AUTO_LENGTH, \
56+
stdlib_math_base_napi_d_f_wrapper, \
57+
NULL, \
58+
&fcn \
59+
); \
60+
assert( status == napi_ok ); \
61+
return fcn; \
62+
}; \
63+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_d_f_init )
64+
65+
/*
66+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
67+
*/
68+
#ifdef __cplusplus
69+
extern "C" {
70+
#endif
71+
72+
/**
73+
* Invokes a unary function accepting a double-precision floating-point number and returning a single-precision floating-point number.
74+
*/
75+
napi_value stdlib_math_base_napi_d_f( napi_env env, napi_callback_info info, float (*fcn)( double ) );
76+
77+
#ifdef __cplusplus
78+
}
79+
#endif
80+
81+
#endif // !STDLIB_MATH_BASE_NAPI_UNARY_D_F_H

lib/node_modules/@stdlib/math/base/napi/unary/manifest.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"./src/c_c.c",
2929
"./src/c_f.c",
3030
"./src/d_d.c",
31+
"./src/d_f.c",
3132
"./src/f_f.c",
3233
"./src/f_i.c",
3334
"./src/i_d.c",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "stdlib/math/base/napi/unary/d_f.h"
20+
#include <node_api.h>
21+
#include <assert.h>
22+
23+
/**
24+
* Invokes a unary function accepting a double-precision floating-point number and returning a single-precision floating-point number.
25+
*
26+
* ## Notes
27+
*
28+
* - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
29+
*
30+
* - `x`: input value.
31+
*
32+
* @param env environment under which the function is invoked
33+
* @param info callback data
34+
* @param fcn unary function
35+
* @return function return value as a Node-API double-precision floating-point number
36+
*/
37+
napi_value stdlib_math_base_napi_d_f( napi_env env, napi_callback_info info, float (*fcn)( double ) ) {
38+
napi_status status;
39+
40+
size_t argc = 1;
41+
napi_value argv[ 1 ];
42+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
43+
assert( status == napi_ok );
44+
45+
if ( argc < 1 ) {
46+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide a number." );
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. Must provide a number." );
56+
assert( status == napi_ok );
57+
return NULL;
58+
}
59+
60+
double x;
61+
status = napi_get_value_double( env, argv[ 0 ], &x );
62+
assert( status == napi_ok );
63+
64+
napi_value v;
65+
status = napi_create_double( env, (double)fcn( x ), &v );
66+
assert( status == napi_ok );
67+
68+
return v;
69+
}

0 commit comments

Comments
 (0)