@stdlib/math-base-napi-quinary 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/main.c ADDED
@@ -0,0 +1,230 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2023 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/quinary.h"
20
+ #include <node_api.h>
21
+ #include <stdint.h>
22
+ #include <assert.h>
23
+
24
+ /**
25
+ * Invokes a quinary function accepting and returning double-precision floating-point numbers.
26
+ *
27
+ * ## Notes
28
+ *
29
+ * - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
30
+ *
31
+ * - `x`: input value.
32
+ * - `y`: input value.
33
+ * - `z`: input value.
34
+ * - `w`: input value.
35
+ * - `u`: input value.
36
+ *
37
+ * @param env environment under which the function is invoked
38
+ * @param info callback data
39
+ * @param fcn quinary function
40
+ * @return function return value as a Node-API double-precision floating-point number
41
+ */
42
+ napi_value stdlib_math_base_napi_ddddd_d( napi_env env, napi_callback_info info, double (*fcn)( double, double, double, double, double ) ) {
43
+ napi_status status;
44
+
45
+ size_t argc = 5;
46
+ napi_value argv[ 5 ];
47
+ status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
48
+ assert( status == napi_ok );
49
+
50
+ if ( argc < 5 ) {
51
+ status = napi_throw_error( env, NULL, "invalid invocation. Must provide five numbers." );
52
+ assert( status == napi_ok );
53
+ return NULL;
54
+ }
55
+
56
+ napi_valuetype vtype0;
57
+ status = napi_typeof( env, argv[ 0 ], &vtype0 );
58
+ assert( status == napi_ok );
59
+ if ( vtype0 != napi_number ) {
60
+ status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
61
+ assert( status == napi_ok );
62
+ return NULL;
63
+ }
64
+
65
+ napi_valuetype vtype1;
66
+ status = napi_typeof( env, argv[ 1 ], &vtype1 );
67
+ assert( status == napi_ok );
68
+ if ( vtype1 != napi_number ) {
69
+ status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
70
+ assert( status == napi_ok );
71
+ return NULL;
72
+ }
73
+
74
+ napi_valuetype vtype2;
75
+ status = napi_typeof( env, argv[ 2 ], &vtype2 );
76
+ assert( status == napi_ok );
77
+ if ( vtype2 != napi_number ) {
78
+ status = napi_throw_type_error( env, NULL, "invalid argument. Third argument must be a number." );
79
+ assert( status == napi_ok );
80
+ return NULL;
81
+ }
82
+
83
+ napi_valuetype vtype3;
84
+ status = napi_typeof( env, argv[ 3 ], &vtype3 );
85
+ assert( status == napi_ok );
86
+ if ( vtype3 != napi_number ) {
87
+ status = napi_throw_type_error( env, NULL, "invalid argument. Fourth argument must be a number." );
88
+ assert( status == napi_ok );
89
+ return NULL;
90
+ }
91
+
92
+ napi_valuetype vtype4;
93
+ status = napi_typeof( env, argv[ 4 ], &vtype4 );
94
+ assert( status == napi_ok );
95
+ if ( vtype4 != napi_number ) {
96
+ status = napi_throw_type_error( env, NULL, "invalid argument. Fifth argument must be a number." );
97
+ assert( status == napi_ok );
98
+ return NULL;
99
+ }
100
+
101
+ double x;
102
+ status = napi_get_value_double( env, argv[ 0 ], &x );
103
+ assert( status == napi_ok );
104
+
105
+ double y;
106
+ status = napi_get_value_double( env, argv[ 1 ], &y );
107
+ assert( status == napi_ok );
108
+
109
+ double z;
110
+ status = napi_get_value_double( env, argv[ 2 ], &z );
111
+ assert( status == napi_ok );
112
+
113
+ double w;
114
+ status = napi_get_value_double( env, argv[ 3 ], &w );
115
+ assert( status == napi_ok );
116
+
117
+ double u;
118
+ status = napi_get_value_double( env, argv[ 4 ], &u );
119
+ assert( status == napi_ok );
120
+
121
+ napi_value v;
122
+ status = napi_create_double( env, fcn( x, y, z, w, u ), &v );
123
+ assert( status == napi_ok );
124
+
125
+ return v;
126
+ }
127
+
128
+ /**
129
+ * Invokes a quinary function accepting and returning single-precision floating-point numbers.
130
+ *
131
+ * ## Notes
132
+ *
133
+ * - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
134
+ *
135
+ * - `x`: input value.
136
+ * - `y`: input value.
137
+ * - `z`: input value.
138
+ * - `w`: input value.
139
+ * - `u`: input value.
140
+ *
141
+ * @param env environment under which the function is invoked
142
+ * @param info callback data
143
+ * @param fcn quinary function
144
+ * @return function return value as a Node-API double-precision floating-point number
145
+ */
146
+ napi_value stdlib_math_base_napi_fffff_f( napi_env env, napi_callback_info info, float (*fcn)( float, float, float, float, float ) ) {
147
+ napi_status status;
148
+
149
+ size_t argc = 5;
150
+ napi_value argv[ 5 ];
151
+ status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
152
+ assert( status == napi_ok );
153
+
154
+ if ( argc < 5 ) {
155
+ status = napi_throw_error( env, NULL, "invalid invocation. Must provide five numbers." );
156
+ assert( status == napi_ok );
157
+ return NULL;
158
+ }
159
+
160
+ napi_valuetype vtype0;
161
+ status = napi_typeof( env, argv[ 0 ], &vtype0 );
162
+ assert( status == napi_ok );
163
+ if ( vtype0 != napi_number ) {
164
+ status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
165
+ assert( status == napi_ok );
166
+ return NULL;
167
+ }
168
+
169
+ napi_valuetype vtype1;
170
+ status = napi_typeof( env, argv[ 1 ], &vtype1 );
171
+ assert( status == napi_ok );
172
+ if ( vtype1 != napi_number ) {
173
+ status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
174
+ assert( status == napi_ok );
175
+ return NULL;
176
+ }
177
+
178
+ napi_valuetype vtype2;
179
+ status = napi_typeof( env, argv[ 2 ], &vtype2 );
180
+ assert( status == napi_ok );
181
+ if ( vtype2 != napi_number ) {
182
+ status = napi_throw_type_error( env, NULL, "invalid argument. Third argument must be a number." );
183
+ assert( status == napi_ok );
184
+ return NULL;
185
+ }
186
+
187
+ napi_valuetype vtype3;
188
+ status = napi_typeof( env, argv[ 3 ], &vtype3 );
189
+ assert( status == napi_ok );
190
+ if ( vtype3 != napi_number ) {
191
+ status = napi_throw_type_error( env, NULL, "invalid argument. Fourth argument must be a number." );
192
+ assert( status == napi_ok );
193
+ return NULL;
194
+ }
195
+
196
+ napi_valuetype vtype4;
197
+ status = napi_typeof( env, argv[ 4 ], &vtype4 );
198
+ assert( status == napi_ok );
199
+ if ( vtype4 != napi_number ) {
200
+ status = napi_throw_type_error( env, NULL, "invalid argument. Fifth argument must be a number." );
201
+ assert( status == napi_ok );
202
+ return NULL;
203
+ }
204
+
205
+ double x;
206
+ status = napi_get_value_double( env, argv[ 0 ], &x );
207
+ assert( status == napi_ok );
208
+
209
+ double y;
210
+ status = napi_get_value_double( env, argv[ 1 ], &y );
211
+ assert( status == napi_ok );
212
+
213
+ double z;
214
+ status = napi_get_value_double( env, argv[ 2 ], &z );
215
+ assert( status == napi_ok );
216
+
217
+ double w;
218
+ status = napi_get_value_double( env, argv[ 3 ], &w );
219
+ assert( status == napi_ok );
220
+
221
+ double u;
222
+ status = napi_get_value_double( env, argv[ 4 ], &u );
223
+ assert( status == napi_ok );
224
+
225
+ napi_value v;
226
+ status = napi_create_double( env, (double)fcn( (float)x, (float)y, (float)z, (float)w, (float)u ), &v );
227
+ assert( status == napi_ok );
228
+
229
+ return v;
230
+ }