Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions lib/node_modules/@stdlib/math/base/napi/binary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <node_api.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 );
}

// ...

/**
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <node_api.h>
#include <assert.h>

/**
* 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
6 changes: 5 additions & 1 deletion lib/node_modules/@stdlib/math/base/napi/binary/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -56,7 +57,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"
]
}
]
Expand Down
88 changes: 88 additions & 0 deletions lib/node_modules/@stdlib/math/base/napi/binary/src/hh_h.c
Original file line number Diff line number Diff line change
@@ -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 <node_api.h>
#include <assert.h>

/**
* 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;
}