diff --git a/lib/node_modules/@stdlib/math/base/napi/unary/README.md b/lib/node_modules/@stdlib/math/base/napi/unary/README.md index 415cab64d669..cd85e52365e7 100644 --- a/lib/node_modules/@stdlib/math/base/napi/unary/README.md +++ b/lib/node_modules/@stdlib/math/base/napi/unary/README.md @@ -547,6 +547,69 @@ The function accepts the following arguments: void stdlib_math_base_napi_f_i( napi_env env, napi_callback_info info, int32_t (*fcn)( float ) ); ``` +#### STDLIB_MATH_BASE_NAPI_MODULE_H_H( fcn ) + +Macro for registering a Node-API module exporting an interface for invoking a unary function accepting and returning half-precision floating-point numbers. + +```c +#include "stdlib/number/float16/ctor.h" + +static stdlib_float16_t identity( const stdlib_float16_t x ) { + return x; +} + +// ... + +// Register a Node-API module: +STDLIB_MATH_BASE_NAPI_MODULE_H_H( identity ); +``` + +The macro expects the following arguments: + +- **fcn**: `stdlib_float16_t (*fcn)( stdlib_float16_t )` unary 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_h_h( env, info, fcn ) + +Invokes a unary function accepting and returning half-precision floating-point numbers. + +```c +#include "stdlib/number/float16/ctor.h" +#include + +// ... + +static stdlib_float16_t identity( const stdlib_float16_t x ) { + return x; +} + +// ... + +/** +* 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_h_h( env, info, identity ); +} + +// ... +``` + +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 )` unary function. + +```c +void stdlib_math_base_napi_h_h( napi_env env, napi_callback_info info, stdlib_float16_t (*fcn)( stdlib_float16_t ) ); +``` + #### STDLIB_MATH_BASE_NAPI_MODULE_I_D( fcn ) 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. diff --git a/lib/node_modules/@stdlib/math/base/napi/unary/include/stdlib/math/base/napi/unary.h b/lib/node_modules/@stdlib/math/base/napi/unary/include/stdlib/math/base/napi/unary.h index a1532be7bb46..752f0a2c803a 100644 --- a/lib/node_modules/@stdlib/math/base/napi/unary/include/stdlib/math/base/napi/unary.h +++ b/lib/node_modules/@stdlib/math/base/napi/unary/include/stdlib/math/base/napi/unary.h @@ -27,6 +27,7 @@ #include "stdlib/math/base/napi/unary/d_f.h" #include "stdlib/math/base/napi/unary/f_f.h" #include "stdlib/math/base/napi/unary/f_i.h" +#include "stdlib/math/base/napi/unary/h_h.h" #include "stdlib/math/base/napi/unary/i_d.h" #include "stdlib/math/base/napi/unary/i_f.h" #include "stdlib/math/base/napi/unary/i_i.h" diff --git a/lib/node_modules/@stdlib/math/base/napi/unary/include/stdlib/math/base/napi/unary/h_h.h b/lib/node_modules/@stdlib/math/base/napi/unary/include/stdlib/math/base/napi/unary/h_h.h new file mode 100644 index 000000000000..fbe4e898c8db --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/napi/unary/include/stdlib/math/base/napi/unary/h_h.h @@ -0,0 +1,84 @@ +/** +* @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_UNARY_H_H_H +#define STDLIB_MATH_BASE_NAPI_UNARY_H_H_H + +#include "stdlib/number/float16/ctor.h" +#include +#include + +/** +* Macro for registering a Node-API module exporting an interface invoking a unary function accepting and returning half-precision floating-point numbers. +* +* @param fcn unary function +* +* @example +* #include "stdlib/number/float16/ctor.h" +* +* static stdlib_float16_t identity( const stdlib_float16_t x ) { +* return x; +* } +* +* // ... +* +* // Register a Node-API module: +* STDLIB_MATH_BASE_NAPI_MODULE_H_H( identity ); +*/ +#define STDLIB_MATH_BASE_NAPI_MODULE_H_H( fcn ) \ + static napi_value stdlib_math_base_napi_h_h_wrapper( \ + napi_env env, \ + napi_callback_info info \ + ) { \ + return stdlib_math_base_napi_h_h( env, info, fcn ); \ + }; \ + static napi_value stdlib_math_base_napi_h_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_h_h_wrapper, \ + NULL, \ + &f \ + ); \ + assert( status == napi_ok ); \ + return f; \ + }; \ + NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_h_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 unary function accepting and returning half-precision floating-point numbers. +*/ +napi_value stdlib_math_base_napi_h_h( napi_env env, napi_callback_info info, stdlib_float16_t (*fcn)( stdlib_float16_t ) ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_MATH_BASE_NAPI_UNARY_H_H_H diff --git a/lib/node_modules/@stdlib/math/base/napi/unary/manifest.json b/lib/node_modules/@stdlib/math/base/napi/unary/manifest.json index 0276bc332a62..30a32d400a23 100644 --- a/lib/node_modules/@stdlib/math/base/napi/unary/manifest.json +++ b/lib/node_modules/@stdlib/math/base/napi/unary/manifest.json @@ -32,6 +32,7 @@ "./src/d_f.c", "./src/f_f.c", "./src/f_i.c", + "./src/h_h.c", "./src/i_d.c", "./src/i_f.c", "./src/i_i.c", @@ -51,7 +52,10 @@ "@stdlib/complex/float32/ctor", "@stdlib/complex/float64/ctor", "@stdlib/complex/float64/reim", - "@stdlib/complex/float32/reim" + "@stdlib/complex/float32/reim", + "@stdlib/number/float16/ctor", + "@stdlib/number/float64/base/to-float16", + "@stdlib/number/float16/base/to-float64" ] } ] diff --git a/lib/node_modules/@stdlib/math/base/napi/unary/src/h_h.c b/lib/node_modules/@stdlib/math/base/napi/unary/src/h_h.c new file mode 100644 index 000000000000..9a612b04f0d7 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/napi/unary/src/h_h.c @@ -0,0 +1,74 @@ +/** +* @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/unary/h_h.h" +#include "stdlib/number/float64/base/to_float16.h" +#include "stdlib/number/float16/base/to_float64.h" +#include "stdlib/number/float16/ctor.h" +#include +#include + +/** +* Invokes a unary 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. +* +* @param env environment under which the function is invoked +* @param info callback data +* @param fcn unary function +* @return function return value as a Node-API half-precision floating-point number +*/ +napi_value stdlib_math_base_napi_h_h( napi_env env, napi_callback_info info, stdlib_float16_t (*fcn)( stdlib_float16_t ) ) { + napi_status status; + + size_t argc = 1; + napi_value argv[ 1 ]; + status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); + assert( status == napi_ok ); + + if ( argc < 1 ) { + status = napi_throw_error( env, NULL, "invalid invocation. Must provide a number." ); + 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. Must provide a number." ); + assert( status == napi_ok ); + return NULL; + } + + double x; + status = napi_get_value_double( env, argv[ 0 ], &x ); + assert( status == napi_ok ); + + stdlib_float16_t out = fcn( stdlib_base_float64_to_float16( x ) ); + + napi_value v; + status = napi_create_double( env, stdlib_base_float16_to_float64( out ), &v ); + assert( status == napi_ok ); + + return v; +}