Skip to content

Commit e21c16f

Browse files
feat: add HH_H macro in math/base/napi/binary
PR-URL: #9577 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent c265420 commit e21c16f

File tree

5 files changed

+248
-1
lines changed

5 files changed

+248
-1
lines changed

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,73 @@ The function accepts the following arguments:
848848
void stdlib_math_base_napi_fi_f( napi_env env, napi_callback_info info, float (*fcn)( float, int32_t ) );
849849
```
850850

851+
#### STDLIB_MATH_BASE_NAPI_MODULE_HH_H( fcn )
852+
853+
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning half-precision floating-point numbers.
854+
855+
```c
856+
#include "stdlib/number/float64/base/to_float16.h"
857+
#include "stdlib/number/float16/base/to_float64.h"
858+
#include "stdlib/number/float16/ctor.h"
859+
860+
static stdlib_float16_t add( const stdlib_float16_t x, const stdlib_float16_t y ) {
861+
double z = stdlib_base_float16_to_float64( x ) + stdlib_base_float16_to_float64( y );
862+
return stdlib_base_float64_to_float16( z );
863+
}
864+
865+
// ...
866+
867+
// Register a Node-API module:
868+
STDLIB_MATH_BASE_NAPI_MODULE_HH_H( add );
869+
```
870+
871+
The macro expects the following arguments:
872+
873+
- **fcn**: `stdlib_float16_t (*fcn)( stdlib_float16_t, stdlib_float16_t )` binary function.
874+
875+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
876+
877+
#### stdlib_math_base_napi_hh_h( env, info, fcn )
878+
879+
Invokes a binary function accepting and returning half-precision floating-point numbers.
880+
881+
```c
882+
#include "stdlib/number/float16/ctor.h"
883+
#include "stdlib/number/float64/base/to_float16.h"
884+
#include "stdlib/number/float16/base/to_float64.h"
885+
#include <node_api.h>
886+
887+
static stdlib_float16_t add( const stdlib_float16_t x, const stdlib_float16_t y ) {
888+
double z = stdlib_base_float16_to_float64( x ) + stdlib_base_float16_to_float64( y );
889+
return stdlib_base_float64_to_float16( z );
890+
}
891+
892+
// ...
893+
894+
/**
895+
* Receives JavaScript callback invocation data.
896+
*
897+
* @param env environment under which the function is invoked
898+
* @param info callback data
899+
* @return Node-API value
900+
*/
901+
napi_value addon( napi_env env, napi_callback_info info ) {
902+
return stdlib_math_base_napi_hh_h( env, info, add );
903+
}
904+
905+
// ...
906+
```
907+
908+
The function accepts the following arguments:
909+
910+
- **env**: `[in] napi_env` environment under which the function is invoked.
911+
- **info**: `[in] napi_callback_info` callback data.
912+
- **fcn**: `[in] stdlib_float16_t (*fcn)( stdlib_float16_t, stdlib_float16_t )` binary function.
913+
914+
```c
915+
void stdlib_math_base_napi_hh_h( napi_env env, napi_callback_info info, stdlib_float16_t (*fcn)( stdlib_float16_t, stdlib_float16_t ) );
916+
```
917+
851918
#### STDLIB_MATH_BASE_NAPI_MODULE_ID_D( fcn )
852919
853920
Macro for registering a Node-API module exporting an interface invoking a binary function accepting a signed 32-bit integer and a double-precision floating-point number and returning a double-precision floating-point number.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "stdlib/math/base/napi/binary/fc_c.h"
3131
#include "stdlib/math/base/napi/binary/ff_f.h"
3232
#include "stdlib/math/base/napi/binary/fi_f.h"
33+
#include "stdlib/math/base/napi/binary/hh_h.h"
3334
#include "stdlib/math/base/napi/binary/id_d.h"
3435
#include "stdlib/math/base/napi/binary/ii_d.h"
3536
#include "stdlib/math/base/napi/binary/ii_f.h"
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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_BINARY_HH_H_H
20+
#define STDLIB_MATH_BASE_NAPI_BINARY_HH_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 binary function accepting and returning half-precision floating-point numbers.
28+
*
29+
* @param fcn binary function
30+
*
31+
* @example
32+
* #include "stdlib/number/float64/base/to_float16.h"
33+
* #include "stdlib/number/float16/base/to_float64.h"
34+
* #include "stdlib/number/float16/ctor.h"
35+
*
36+
* static stdlib_float16_t add( const stdlib_float16_t x, const stdlib_float16_t y ) {
37+
* double z = stdlib_base_float16_to_float64( x ) + stdlib_base_float16_to_float64( y );
38+
* return stdlib_base_float64_to_float16( z );
39+
* }
40+
*
41+
* // ...
42+
*
43+
* // Register a Node-API module:
44+
* STDLIB_MATH_BASE_NAPI_MODULE_HH_H( add );
45+
*/
46+
#define STDLIB_MATH_BASE_NAPI_MODULE_HH_H( fcn ) \
47+
static napi_value stdlib_math_base_napi_hh_h_wrapper( \
48+
napi_env env, \
49+
napi_callback_info info \
50+
) { \
51+
return stdlib_math_base_napi_hh_h( env, info, fcn ); \
52+
}; \
53+
static napi_value stdlib_math_base_napi_hh_h_init( \
54+
napi_env env, \
55+
napi_value exports \
56+
) { \
57+
napi_value f; \
58+
napi_status status = napi_create_function( \
59+
env, \
60+
"exports", \
61+
NAPI_AUTO_LENGTH, \
62+
stdlib_math_base_napi_hh_h_wrapper, \
63+
NULL, \
64+
&f \
65+
); \
66+
assert( status == napi_ok ); \
67+
return f; \
68+
}; \
69+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_hh_h_init )
70+
71+
/*
72+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
73+
*/
74+
#ifdef __cplusplus
75+
extern "C" {
76+
#endif
77+
78+
/**
79+
* Invokes a binary function accepting and returning half-precision floating-point numbers.
80+
*/
81+
napi_value stdlib_math_base_napi_hh_h( napi_env env, napi_callback_info info, stdlib_float16_t (*fcn)( stdlib_float16_t, stdlib_float16_t ) );
82+
83+
#ifdef __cplusplus
84+
}
85+
#endif
86+
87+
#endif // !STDLIB_MATH_BASE_NAPI_BINARY_HH_H_H

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"./src/fc_c.c",
3636
"./src/ff_f.c",
3737
"./src/fi_f.c",
38+
"./src/hh_h.c",
3839
"./src/id_d.c",
3940
"./src/ii_d.c",
4041
"./src/ii_f.c",
@@ -56,7 +57,10 @@
5657
"@stdlib/complex/float32/ctor",
5758
"@stdlib/complex/float64/ctor",
5859
"@stdlib/complex/float64/reim",
59-
"@stdlib/complex/float32/reim"
60+
"@stdlib/complex/float32/reim",
61+
"@stdlib/number/float16/ctor",
62+
"@stdlib/number/float64/base/to-float16",
63+
"@stdlib/number/float16/base/to-float64"
6064
]
6165
}
6266
]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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/binary/hh_h.h"
20+
#include "stdlib/number/float16/ctor.h"
21+
#include "stdlib/number/float64/base/to_float16.h"
22+
#include "stdlib/number/float16/base/to_float64.h"
23+
#include <node_api.h>
24+
#include <assert.h>
25+
26+
/**
27+
* Invokes a binary 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+
* - `y`: input value.
35+
*
36+
* @param env environment under which the function is invoked
37+
* @param info callback data
38+
* @param fcn binary function
39+
* @return function return value as a Node-API double-precision floating-point number
40+
*/
41+
napi_value stdlib_math_base_napi_hh_h( napi_env env, napi_callback_info info, stdlib_float16_t (*fcn)( stdlib_float16_t, stdlib_float16_t ) ) {
42+
napi_status status;
43+
44+
size_t argc = 2;
45+
napi_value argv[ 2 ];
46+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
47+
assert( status == napi_ok );
48+
49+
if ( argc < 2 ) {
50+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide two numbers." );
51+
assert( status == napi_ok );
52+
return NULL;
53+
}
54+
55+
napi_valuetype vtype0;
56+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
57+
assert( status == napi_ok );
58+
if ( vtype0 != napi_number ) {
59+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
60+
assert( status == napi_ok );
61+
return NULL;
62+
}
63+
64+
napi_valuetype vtype1;
65+
status = napi_typeof( env, argv[ 1 ], &vtype1 );
66+
assert( status == napi_ok );
67+
if ( vtype1 != napi_number ) {
68+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
69+
assert( status == napi_ok );
70+
return NULL;
71+
}
72+
73+
double x;
74+
status = napi_get_value_double( env, argv[ 0 ], &x );
75+
assert( status == napi_ok );
76+
77+
double y;
78+
status = napi_get_value_double( env, argv[ 1 ], &y );
79+
assert( status == napi_ok );
80+
81+
stdlib_float16_t out = fcn( stdlib_base_float64_to_float16( x ), stdlib_base_float64_to_float16( y ) );
82+
83+
napi_value v;
84+
status = napi_create_double( env, stdlib_base_float16_to_float64( out ), &v );
85+
assert( status == napi_ok );
86+
87+
return v;
88+
}

0 commit comments

Comments
 (0)