Skip to content

Commit 894885f

Browse files
committed
feat: add FC_C macro
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 89df5ae commit 894885f

File tree

5 files changed

+318
-0
lines changed

5 files changed

+318
-0
lines changed

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,91 @@ The function accepts the following arguments:
577577
void stdlib_math_base_napi_dz_z( napi_env env, napi_callback_info info, stdlib_complex128_t (*fcn)( double, stdlib_complex128_t ) );
578578
```
579579

580+
#### STDLIB_MATH_BASE_NAPI_MODULE_FC_C( fcn )
581+
582+
Macro for registering a Node-API module exporting an interface invoking a binary function accepting a single-precision complex floating-point number and a single-precision floating-point number and returning a single-precision complex floating-point number.
583+
584+
```c
585+
#include "stdlib/complex/float32/ctor.h"
586+
#include "stdlib/complex/float32/reim.h"
587+
588+
static stdlib_complex64_t mul( const float y, const stdlib_complex64_t x ) {
589+
float xre;
590+
float xim;
591+
float re;
592+
float im;
593+
594+
stdlib_complex64_reim( x, &xre, &xim );
595+
596+
re = xre * y;
597+
im = xim * y;
598+
599+
return stdlib_complex64( re, im );
600+
}
601+
602+
// ...
603+
604+
// Register a Node-API module:
605+
STDLIB_MATH_BASE_NAPI_MODULE_FC_C( mul );
606+
```
607+
608+
The macro expects the following arguments:
609+
610+
- **fcn**: `stdlib_complex64_t (*fcn)( float, stdlib_complex64_t )` binary function.
611+
612+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
613+
614+
#### stdlib_math_base_napi_fc_c( env, info, fcn )
615+
616+
Invokes a binary function accepting a single-precision floating-point number and a single-precision complex floating-point number and returning a single-precision complex floating-point number.
617+
618+
```c
619+
#include "stdlib/complex/float32/ctor.h"
620+
#include "stdlib/complex/float32/reim.h"
621+
#include <node_api.h>
622+
623+
// ...
624+
625+
static stdlib_complex64_t mul( const float y, const stdlib_complex64_t x ) {
626+
float xre;
627+
float xim;
628+
float re;
629+
float im;
630+
631+
stdlib_complex64_reim( x, &xre, &xim );
632+
633+
re = xre * y;
634+
im = xim * y;
635+
636+
return stdlib_complex64( re, im );
637+
}
638+
639+
// ...
640+
641+
/**
642+
* Receives JavaScript callback invocation data.
643+
*
644+
* @param env environment under which the function is invoked
645+
* @param info callback data
646+
* @return Node-API value
647+
*/
648+
napi_value addon( napi_env env, napi_callback_info info ) {
649+
return stdlib_math_base_napi_fc_c( env, info, mul );
650+
}
651+
652+
// ...
653+
```
654+
655+
The function accepts the following arguments:
656+
657+
- **env**: `[in] napi_env` environment under which the function is invoked.
658+
- **info**: `[in] napi_callback_info` callback data.
659+
- **fcn**: `[in] stdlib_complex64_t (*fcn)( float, stdlib_complex64_t )` binary function.
660+
661+
```c
662+
void stdlib_math_base_napi_fc_c( napi_env env, napi_callback_info info, stdlib_complex64_t (*fcn)( float, stdlib_complex64_t ) );
663+
```
664+
580665
#### STDLIB_MATH_BASE_NAPI_MODULE_FF_F( fcn )
581666
582667
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning single-precision floating-point numbers.

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
@@ -26,6 +26,7 @@
2626
#include "stdlib/math/base/napi/binary/dd_d.h"
2727
#include "stdlib/math/base/napi/binary/di_d.h"
2828
#include "stdlib/math/base/napi/binary/dz_z.h"
29+
#include "stdlib/math/base/napi/binary/fc_c.h"
2930
#include "stdlib/math/base/napi/binary/ff_f.h"
3031
#include "stdlib/math/base/napi/binary/fi_f.h"
3132
#include "stdlib/math/base/napi/binary/id_d.h"
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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_FC_C_H
20+
#define STDLIB_MATH_BASE_NAPI_BINARY_FC_C_H
21+
22+
#include "stdlib/complex/float32/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 a single-precision floating-point number and a single-precision complex floating-point number and returning a single-precision complex floating-point number.
28+
*
29+
* @param fcn binary function
30+
*
31+
* @example
32+
* #include "stdlib/complex/float32/ctor.h"
33+
* #include "stdlib/complex/float32/reim.h"
34+
*
35+
* static stdlib_complex64_t mul( const float n, const stdlib_complex64_t x ) {
36+
* float re;
37+
* float im;
38+
*
39+
* stdlib_complex64_reim( x, &re, &im );
40+
* return stdlib_complex64( re*n, im*n );
41+
* }
42+
*
43+
* // ...
44+
*
45+
* // Register a Node-API module:
46+
* STDLIB_MATH_BASE_NAPI_MODULE_FC_C( mul );
47+
*/
48+
#define STDLIB_MATH_BASE_NAPI_MODULE_FC_C( fcn ) \
49+
static napi_value stdlib_math_base_napi_fc_c_wrapper( \
50+
napi_env env, \
51+
napi_callback_info info \
52+
) { \
53+
return stdlib_math_base_napi_fc_c( env, info, fcn ); \
54+
}; \
55+
static napi_value stdlib_math_base_napi_fc_c_init( \
56+
napi_env env, \
57+
napi_value exports \
58+
) { \
59+
napi_value fcn; \
60+
napi_status status = napi_create_function( \
61+
env, \
62+
"exports", \
63+
NAPI_AUTO_LENGTH, \
64+
stdlib_math_base_napi_fc_c_wrapper, \
65+
NULL, \
66+
&fcn \
67+
); \
68+
assert( status == napi_ok ); \
69+
return fcn; \
70+
}; \
71+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_fc_c_init )
72+
73+
/*
74+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
75+
*/
76+
#ifdef __cplusplus
77+
extern "C" {
78+
#endif
79+
80+
/**
81+
* Invokes a binary function accepting a single-precision floating-point number and a single-precision complex floating-point number and returning a single-precision complex floating-point number.
82+
*/
83+
napi_value stdlib_math_base_napi_fc_c( napi_env env, napi_callback_info info, stdlib_complex64_t (*fcn)( float, stdlib_complex64_t ) );
84+
85+
#ifdef __cplusplus
86+
}
87+
#endif
88+
89+
#endif // !STDLIB_MATH_BASE_NAPI_BINARY_FC_C_H

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"./src/dd_d.c",
3232
"./src/di_d.c",
3333
"./src/dz_z.c",
34+
"./src/fc_c.c",
3435
"./src/ff_f.c",
3536
"./src/fi_f.c",
3637
"./src/id_d.c",
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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/fc_c.h"
20+
#include "stdlib/complex/float32/ctor.h"
21+
#include "stdlib/complex/float32/reim.h"
22+
#include <node_api.h>
23+
#include <assert.h>
24+
25+
/**
26+
* Invokes a binary function accepting a single-precision floating-point number and a single-precision complex floating-point number and returning a single-precision complex floating-point number.
27+
*
28+
* ## Notes
29+
*
30+
* - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
31+
*
32+
* - `x`: input value.
33+
* - `y`: input value.
34+
*
35+
* @param env environment under which the function is invoked
36+
* @param info callback data
37+
* @param fcn binary function
38+
* @return function return value as a Node-API complex-like object
39+
*/
40+
napi_value stdlib_math_base_napi_fc_c( napi_env env, napi_callback_info info, stdlib_complex64_t (*fcn)( float, stdlib_complex64_t ) ) {
41+
napi_status status;
42+
43+
size_t argc = 2;
44+
napi_value argv[ 2 ];
45+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
46+
assert( status == napi_ok );
47+
48+
if ( argc < 2 ) {
49+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide two arguments." );
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. First argument must be a number." );
59+
assert( status == napi_ok );
60+
return NULL;
61+
}
62+
63+
bool hprop;
64+
status = napi_has_named_property( env, argv[ 1 ], "re", &hprop );
65+
assert( status == napi_ok );
66+
if ( !hprop ) {
67+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must have a real component." );
68+
assert( status == napi_ok );
69+
return NULL;
70+
}
71+
72+
napi_value xre;
73+
status = napi_get_named_property( env, argv[ 1 ], "re", &xre );
74+
assert( status == napi_ok );
75+
76+
napi_valuetype xretype;
77+
status = napi_typeof( env, xre, &xretype );
78+
assert( status == napi_ok );
79+
if ( xretype != napi_number ) {
80+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must have a real component which is a number." );
81+
assert( status == napi_ok );
82+
return NULL;
83+
}
84+
85+
status = napi_has_named_property( env, argv[ 1 ], "im", &hprop );
86+
assert( status == napi_ok );
87+
if ( !hprop ) {
88+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must have an imaginary component." );
89+
assert( status == napi_ok );
90+
return NULL;
91+
}
92+
93+
napi_value xim;
94+
status = napi_get_named_property( env, argv[ 1 ], "im", &xim );
95+
assert( status == napi_ok );
96+
97+
napi_valuetype ximtype;
98+
status = napi_typeof( env, xim, &ximtype );
99+
assert( status == napi_ok );
100+
if ( ximtype != napi_number ) {
101+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must have an imaginary component which a number." );
102+
assert( status == napi_ok );
103+
return NULL;
104+
}
105+
106+
double y;
107+
status = napi_get_value_double( env, argv[ 0 ], &y );
108+
assert( status == napi_ok );
109+
110+
double re0;
111+
status = napi_get_value_double( env, xre, &re0 );
112+
assert( status == napi_ok );
113+
114+
double im0;
115+
status = napi_get_value_double( env, xim, &im0 );
116+
assert( status == napi_ok );
117+
118+
stdlib_complex64_t v = fcn( y, stdlib_complex64( (float)re0, (float)im0 ) );
119+
float re;
120+
float im;
121+
stdlib_complex64_reim( v, &re, &im );
122+
123+
napi_value obj;
124+
status = napi_create_object( env, &obj );
125+
assert( status == napi_ok );
126+
127+
napi_value vre;
128+
status = napi_create_double( env, (double)re, &vre );
129+
assert( status == napi_ok );
130+
131+
status = napi_set_named_property( env, obj, "re", vre );
132+
assert( status == napi_ok );
133+
134+
napi_value vim;
135+
status = napi_create_double( env, (double)im, &vim );
136+
assert( status == napi_ok );
137+
138+
status = napi_set_named_property( env, obj, "im", vim );
139+
assert( status == napi_ok );
140+
141+
return obj;
142+
}

0 commit comments

Comments
 (0)