From f72c5f605d6f4d8bf70faefd0c439fcbfade6836 Mon Sep 17 00:00:00 2001 From: Neeraj Pathak Date: Tue, 6 Jan 2026 12:33:22 +0530 Subject: [PATCH 1/3] feat: add initial setup --- .../@stdlib/math/base/napi/binary/README.md | 67 ++++++++++++++ .../include/stdlib/math/base/napi/binary.h | 1 + .../stdlib/math/base/napi/binary/hh_h.h | 87 ++++++++++++++++++ .../math/base/napi/binary/manifest.json | 1 + .../@stdlib/math/base/napi/binary/src/hh_h.c | 88 +++++++++++++++++++ 5 files changed, 244 insertions(+) create mode 100644 lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary/hh_h.h create mode 100644 lib/node_modules/@stdlib/math/base/napi/binary/src/hh_h.c diff --git a/lib/node_modules/@stdlib/math/base/napi/binary/README.md b/lib/node_modules/@stdlib/math/base/napi/binary/README.md index ea5c36c5e42e..ce4a7654792a 100644 --- a/lib/node_modules/@stdlib/math/base/napi/binary/README.md +++ b/lib/node_modules/@stdlib/math/base/napi/binary/README.md @@ -848,6 +848,73 @@ The function accepts the following arguments: void stdlib_math_base_napi_fi_f( napi_env env, napi_callback_info info, float (*fcn)( float, int32_t ) ); ``` +#### STDLIB_MATH_BASE_NAPI_MODULE_HH_H( fcn ) + +Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning half-precision floating-point numbers. + +```c +#include "stdlib/number/float64/base/to_float16.h" +#include "stdlib/number/float16/base/to_float64.h" +#include "stdlib/number/float16/ctor.h" + +static stdlib_float16_t add( const stdlib_float16_t x, const stdlib_float16_t y ) { + double z = stdlib_base_float16_to_float64( x ) + stdlib_base_float16_to_float64( y ); + return stdlib_base_float64_to_float16( z ); +} + +// ... + +// Register a Node-API module: +STDLIB_MATH_BASE_NAPI_MODULE_HH_H( add ); +``` + +The macro expects the following arguments: + +- **fcn**: `stdlib_float16_t (*fcn)( stdlib_float16_t, stdlib_float16_t )` binary function. + +When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration. + +#### stdlib_math_base_napi_hh_h( env, info, fcn ) + +Invokes a binary function accepting and returning half-precision floating-point numbers. + +```c +#include "stdlib/number/float16/ctor.h" +#include "stdlib/number/float64/base/to_float16.h" +#include "stdlib/number/float16/base/to_float64.h" +#include + +static stdlib_float16_t add( const stdlib_float16_t x, const stdlib_float16_t y ) { + double z = stdlib_base_float16_to_float64( x ) + stdlib_base_float16_to_float64( y ); + return stdlib_base_float64_to_float16( z ); +} + +// ... + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +napi_value addon( napi_env env, napi_callback_info info ) { + return stdlib_math_base_napi_hh_h( env, info, add ); +} + +// ... +``` + +The function accepts the following arguments: + +- **env**: `[in] napi_env` environment under which the function is invoked. +- **info**: `[in] napi_callback_info` callback data. +- **fcn**: `[in] stdlib_float16_t (*fcn)( stdlib_float16_t, stdlib_float16_t )` binary function. + +```c +void stdlib_math_base_napi_hh_h( napi_env env, napi_callback_info info, stdlib_float16_t (*fcn)( stdlib_float16_t, stdlib_float16_t ) ); +``` + #### STDLIB_MATH_BASE_NAPI_MODULE_ID_D( fcn ) 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. diff --git a/lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary.h b/lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary.h index f99f8dc6cb56..44b8598f9130 100644 --- a/lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary.h +++ b/lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary.h @@ -30,6 +30,7 @@ #include "stdlib/math/base/napi/binary/fc_c.h" #include "stdlib/math/base/napi/binary/ff_f.h" #include "stdlib/math/base/napi/binary/fi_f.h" +#include "stdlib/math/base/napi/binary/hh_h.h" #include "stdlib/math/base/napi/binary/id_d.h" #include "stdlib/math/base/napi/binary/ii_d.h" #include "stdlib/math/base/napi/binary/ii_f.h" diff --git a/lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary/hh_h.h b/lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary/hh_h.h new file mode 100644 index 000000000000..f8c8902edd60 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/napi/binary/include/stdlib/math/base/napi/binary/hh_h.h @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef STDLIB_MATH_BASE_NAPI_BINARY_HH_H_H +#define STDLIB_MATH_BASE_NAPI_BINARY_HH_H_H + +#include "stdlib/number/float16/ctor.h" +#include +#include + +/** +* Macro for registering a Node-API module exporting an interface invoking a binary function accepting and returning half-precision floating-point numbers. +* +* @param fcn binary function +* +* @example +* #include "stdlib/number/float64/base/to_float16.h" +* #include "stdlib/number/float16/base/to_float64.h" +* #include "stdlib/number/float16/ctor.h" +* +* static stdlib_float16_t add( const stdlib_float16_t x, const stdlib_float16_t y ) { +* double z = stdlib_base_float16_to_float64( x ) + stdlib_base_float16_to_float64( y ); +* return stdlib_base_float64_to_float16( z ); +* } +* +* // ... +* +* // Register a Node-API module: +* STDLIB_MATH_BASE_NAPI_MODULE_HH_H( add ); +*/ +#define STDLIB_MATH_BASE_NAPI_MODULE_HH_H( fcn ) \ + static napi_value stdlib_math_base_napi_hh_h_wrapper( \ + napi_env env, \ + napi_callback_info info \ + ) { \ + return stdlib_math_base_napi_hh_h( env, info, fcn ); \ + }; \ + static napi_value stdlib_math_base_napi_hh_h_init( \ + napi_env env, \ + napi_value exports \ + ) { \ + napi_value f; \ + napi_status status = napi_create_function( \ + env, \ + "exports", \ + NAPI_AUTO_LENGTH, \ + stdlib_math_base_napi_hh_h_wrapper, \ + NULL, \ + &f \ + ); \ + assert( status == napi_ok ); \ + return f; \ + }; \ + NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_hh_h_init ) + +/* +* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. +*/ +#ifdef __cplusplus +extern "C" { +#endif + +/** +* Invokes a binary function accepting and returning half-precision floating-point numbers. +*/ +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 ) ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_MATH_BASE_NAPI_BINARY_HH_H_H diff --git a/lib/node_modules/@stdlib/math/base/napi/binary/manifest.json b/lib/node_modules/@stdlib/math/base/napi/binary/manifest.json index 51370c67e555..d76ec5211141 100644 --- a/lib/node_modules/@stdlib/math/base/napi/binary/manifest.json +++ b/lib/node_modules/@stdlib/math/base/napi/binary/manifest.json @@ -35,6 +35,7 @@ "./src/fc_c.c", "./src/ff_f.c", "./src/fi_f.c", + "./src/hh_h.c", "./src/id_d.c", "./src/ii_d.c", "./src/ii_f.c", diff --git a/lib/node_modules/@stdlib/math/base/napi/binary/src/hh_h.c b/lib/node_modules/@stdlib/math/base/napi/binary/src/hh_h.c new file mode 100644 index 000000000000..0893abbfd591 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/napi/binary/src/hh_h.c @@ -0,0 +1,88 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/math/base/napi/binary/hh_h.h" +#include "stdlib/number/float16/ctor.h" +#include "stdlib/number/float64/base/to_float16.h" +#include "stdlib/number/float16/base/to_float64.h" +#include +#include + +/** +* Invokes a binary function accepting and returning half-precision floating-point numbers. +* +* ## Notes +* +* - This function expects that the callback `info` argument provides access to the following JavaScript arguments: +* +* - `x`: input value. +* - `y`: input value. +* +* @param env environment under which the function is invoked +* @param info callback data +* @param fcn binary function +* @return function return value as a Node-API half-precision floating-point number +*/ +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 ) ) { + napi_status status; + + size_t argc = 2; + napi_value argv[ 2 ]; + status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); + assert( status == napi_ok ); + + if ( argc < 2 ) { + status = napi_throw_error( env, NULL, "invalid invocation. Must provide two numbers." ); + assert( status == napi_ok ); + return NULL; + } + + napi_valuetype vtype0; + status = napi_typeof( env, argv[ 0 ], &vtype0 ); + assert( status == napi_ok ); + if ( vtype0 != napi_number ) { + status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." ); + assert( status == napi_ok ); + return NULL; + } + + napi_valuetype vtype1; + status = napi_typeof( env, argv[ 1 ], &vtype1 ); + assert( status == napi_ok ); + if ( vtype1 != napi_number ) { + status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." ); + assert( status == napi_ok ); + return NULL; + } + + double x; + status = napi_get_value_double( env, argv[ 0 ], &x ); + assert( status == napi_ok ); + + double y; + status = napi_get_value_double( env, argv[ 1 ], &y ); + assert( status == napi_ok ); + + stdlib_float16_t out = fcn( stdlib_base_float64_to_float16( x ), stdlib_base_float64_to_float16( y ) ); + + napi_value v; + status = napi_create_double( env, stdlib_base_float16_to_float64( out ), &v ); + assert( status == napi_ok ); + + return v; +} From fd299775e9d908792696bd9519412e6b476ef34f Mon Sep 17 00:00:00 2001 From: Neeraj Pathak Date: Fri, 9 Jan 2026 13:50:45 +0530 Subject: [PATCH 2/3] chore: clean up --- lib/node_modules/@stdlib/math/base/napi/binary/manifest.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/math/base/napi/binary/manifest.json b/lib/node_modules/@stdlib/math/base/napi/binary/manifest.json index d76ec5211141..a6ec65a43bd0 100644 --- a/lib/node_modules/@stdlib/math/base/napi/binary/manifest.json +++ b/lib/node_modules/@stdlib/math/base/napi/binary/manifest.json @@ -57,7 +57,8 @@ "@stdlib/complex/float32/ctor", "@stdlib/complex/float64/ctor", "@stdlib/complex/float64/reim", - "@stdlib/complex/float32/reim" + "@stdlib/complex/float32/reim", + "@stdlib/complex/float16/ctor" ] } ] From 3ff289abdbdeee506f230c84d0047d18ba55c6f1 Mon Sep 17 00:00:00 2001 From: Neeraj Pathak Date: Fri, 9 Jan 2026 14:03:27 +0530 Subject: [PATCH 3/3] chore: update manifest --- lib/node_modules/@stdlib/math/base/napi/binary/manifest.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/math/base/napi/binary/manifest.json b/lib/node_modules/@stdlib/math/base/napi/binary/manifest.json index a6ec65a43bd0..6c070b83703e 100644 --- a/lib/node_modules/@stdlib/math/base/napi/binary/manifest.json +++ b/lib/node_modules/@stdlib/math/base/napi/binary/manifest.json @@ -58,7 +58,9 @@ "@stdlib/complex/float64/ctor", "@stdlib/complex/float64/reim", "@stdlib/complex/float32/reim", - "@stdlib/complex/float16/ctor" + "@stdlib/number/float16/ctor", + "@stdlib/number/float64/base/to-float16", + "@stdlib/number/float16/base/to-float64" ] } ]