Skip to content

Commit 4c079e0

Browse files
feat: add H_H macro in math/base/napi/unary
PR-URL: #9576 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent abfb9e2 commit 4c079e0

File tree

5 files changed

+227
-1
lines changed

5 files changed

+227
-1
lines changed

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,69 @@ The function accepts the following arguments:
547547
void stdlib_math_base_napi_f_i( napi_env env, napi_callback_info info, int32_t (*fcn)( float ) );
548548
```
549549
550+
#### STDLIB_MATH_BASE_NAPI_MODULE_H_H( fcn )
551+
552+
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting and returning half-precision floating-point numbers.
553+
554+
```c
555+
#include "stdlib/number/float16/ctor.h"
556+
557+
static stdlib_float16_t identity( const stdlib_float16_t x ) {
558+
return x;
559+
}
560+
561+
// ...
562+
563+
// Register a Node-API module:
564+
STDLIB_MATH_BASE_NAPI_MODULE_H_H( identity );
565+
```
566+
567+
The macro expects the following arguments:
568+
569+
- **fcn**: `stdlib_float16_t (*fcn)( stdlib_float16_t )` unary function.
570+
571+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
572+
573+
#### stdlib_math_base_napi_h_h( env, info, fcn )
574+
575+
Invokes a unary function accepting and returning half-precision floating-point numbers.
576+
577+
```c
578+
#include "stdlib/number/float16/ctor.h"
579+
#include <node_api.h>
580+
581+
// ...
582+
583+
static stdlib_float16_t identity( const stdlib_float16_t x ) {
584+
return x;
585+
}
586+
587+
// ...
588+
589+
/**
590+
* Receives JavaScript callback invocation data.
591+
*
592+
* @param env environment under which the function is invoked
593+
* @param info callback data
594+
* @return Node-API value
595+
*/
596+
napi_value addon( napi_env env, napi_callback_info info ) {
597+
return stdlib_math_base_napi_h_h( env, info, identity );
598+
}
599+
600+
// ...
601+
```
602+
603+
The function accepts the following arguments:
604+
605+
- **env**: `[in] napi_env` environment under which the function is invoked.
606+
- **info**: `[in] napi_callback_info` callback data.
607+
- **fcn**: `[in] stdlib_float16_t (*fcn)( stdlib_float16_t )` unary function.
608+
609+
```c
610+
void stdlib_math_base_napi_h_h( napi_env env, napi_callback_info info, stdlib_float16_t (*fcn)( stdlib_float16_t ) );
611+
```
612+
550613
#### STDLIB_MATH_BASE_NAPI_MODULE_I_D( fcn )
551614

552615
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting a signed 32-bit integer and returning a double-precision floating-point number.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "stdlib/math/base/napi/unary/d_f.h"
2828
#include "stdlib/math/base/napi/unary/f_f.h"
2929
#include "stdlib/math/base/napi/unary/f_i.h"
30+
#include "stdlib/math/base/napi/unary/h_h.h"
3031
#include "stdlib/math/base/napi/unary/i_d.h"
3132
#include "stdlib/math/base/napi/unary/i_f.h"
3233
#include "stdlib/math/base/napi/unary/i_i.h"
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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_H_H_H
20+
#define STDLIB_MATH_BASE_NAPI_UNARY_H_H_H
21+
22+
#include "stdlib/number/float16/ctor.h"
23+
#include <node_api.h>
24+
#include <assert.h>
25+
26+
/**
27+
* Macro for registering a Node-API module exporting an interface invoking a unary function accepting and returning half-precision floating-point numbers.
28+
*
29+
* @param fcn unary function
30+
*
31+
* @example
32+
* #include "stdlib/number/float16/ctor.h"
33+
*
34+
* static stdlib_float16_t identity( const stdlib_float16_t x ) {
35+
* return x;
36+
* }
37+
*
38+
* // ...
39+
*
40+
* // Register a Node-API module:
41+
* STDLIB_MATH_BASE_NAPI_MODULE_H_H( identity );
42+
*/
43+
#define STDLIB_MATH_BASE_NAPI_MODULE_H_H( fcn ) \
44+
static napi_value stdlib_math_base_napi_h_h_wrapper( \
45+
napi_env env, \
46+
napi_callback_info info \
47+
) { \
48+
return stdlib_math_base_napi_h_h( env, info, fcn ); \
49+
}; \
50+
static napi_value stdlib_math_base_napi_h_h_init( \
51+
napi_env env, \
52+
napi_value exports \
53+
) { \
54+
napi_value f; \
55+
napi_status status = napi_create_function( \
56+
env, \
57+
"exports", \
58+
NAPI_AUTO_LENGTH, \
59+
stdlib_math_base_napi_h_h_wrapper, \
60+
NULL, \
61+
&f \
62+
); \
63+
assert( status == napi_ok ); \
64+
return f; \
65+
}; \
66+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_h_h_init )
67+
68+
/*
69+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
70+
*/
71+
#ifdef __cplusplus
72+
extern "C" {
73+
#endif
74+
75+
/**
76+
* Invokes a unary function accepting and returning half-precision floating-point numbers.
77+
*/
78+
napi_value stdlib_math_base_napi_h_h( napi_env env, napi_callback_info info, stdlib_float16_t (*fcn)( stdlib_float16_t ) );
79+
80+
#ifdef __cplusplus
81+
}
82+
#endif
83+
84+
#endif // !STDLIB_MATH_BASE_NAPI_UNARY_H_H_H

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"./src/d_f.c",
3333
"./src/f_f.c",
3434
"./src/f_i.c",
35+
"./src/h_h.c",
3536
"./src/i_d.c",
3637
"./src/i_f.c",
3738
"./src/i_i.c",
@@ -51,7 +52,10 @@
5152
"@stdlib/complex/float32/ctor",
5253
"@stdlib/complex/float64/ctor",
5354
"@stdlib/complex/float64/reim",
54-
"@stdlib/complex/float32/reim"
55+
"@stdlib/complex/float32/reim",
56+
"@stdlib/number/float16/ctor",
57+
"@stdlib/number/float64/base/to-float16",
58+
"@stdlib/number/float16/base/to-float64"
5559
]
5660
}
5761
]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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/h_h.h"
20+
#include "stdlib/number/float64/base/to_float16.h"
21+
#include "stdlib/number/float16/base/to_float64.h"
22+
#include "stdlib/number/float16/ctor.h"
23+
#include <node_api.h>
24+
#include <assert.h>
25+
26+
/**
27+
* Invokes a unary function accepting and returning half-precision floating-point numbers.
28+
*
29+
* ## Notes
30+
*
31+
* - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
32+
*
33+
* - `x`: input value.
34+
*
35+
* @param env environment under which the function is invoked
36+
* @param info callback data
37+
* @param fcn unary function
38+
* @return function return value as a Node-API half-precision floating-point number
39+
*/
40+
napi_value stdlib_math_base_napi_h_h( napi_env env, napi_callback_info info, stdlib_float16_t (*fcn)( stdlib_float16_t ) ) {
41+
napi_status status;
42+
43+
size_t argc = 1;
44+
napi_value argv[ 1 ];
45+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
46+
assert( status == napi_ok );
47+
48+
if ( argc < 1 ) {
49+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide a number." );
50+
assert( status == napi_ok );
51+
return NULL;
52+
}
53+
54+
napi_valuetype vtype0;
55+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
56+
assert( status == napi_ok );
57+
if ( vtype0 != napi_number ) {
58+
status = napi_throw_type_error( env, NULL, "invalid argument. Must provide a number." );
59+
assert( status == napi_ok );
60+
return NULL;
61+
}
62+
63+
double x;
64+
status = napi_get_value_double( env, argv[ 0 ], &x );
65+
assert( status == napi_ok );
66+
67+
stdlib_float16_t out = fcn( stdlib_base_float64_to_float16( x ) );
68+
69+
napi_value v;
70+
status = napi_create_double( env, stdlib_base_float16_to_float64( out ), &v );
71+
assert( status == napi_ok );
72+
73+
return v;
74+
}

0 commit comments

Comments
 (0)