addon.c (6602B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2021 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/special/abs.h" 20 #include "stdlib/math/base/special/absf.h" 21 #include "stdlib/math/base/special/labs.h" 22 #include "stdlib/ndarray/base/function_object.h" 23 #include "stdlib/ndarray/base/napi/unary.h" 24 #include "stdlib/ndarray/base/unary.h" 25 #include "stdlib/ndarray/dtypes.h" 26 #include <stdint.h> 27 28 /** 29 * Evaluates the identity function for an unsigned 32-bit integer. 30 * 31 * @param x input value 32 * @return input value 33 */ 34 static uint32_t identity_u( const uint32_t x ) { 35 return x; 36 } 37 38 /** 39 * Evaluates the identity function for an unsigned 16-bit integer. 40 * 41 * @param x input value 42 * @return input value 43 */ 44 static uint16_t identity_t( const uint16_t x ) { 45 return x; 46 } 47 48 /** 49 * Evaluates the identity function for an unsigned 8-bit integer. 50 * 51 * @param x input value 52 * @return input value 53 */ 54 static uint8_t identity_b( const uint8_t x ) { 55 return x; 56 } 57 58 /** 59 * Computes the absolute value of a signed 16-bit integer. 60 * 61 * @param x input value 62 * @return absolute value 63 */ 64 static int16_t abs_k( const int16_t x ) { 65 if ( x < 0 ) { 66 return -x; 67 } 68 return x; 69 } 70 71 /** 72 * Computes the absolute value of a signed 8-bit integer. 73 * 74 * @param x input value 75 * @return absolute value 76 */ 77 static int8_t abs_s( const int8_t x ) { 78 if ( x < 0 ) { 79 return -x; 80 } 81 return x; 82 } 83 84 // Define an interface name: 85 static const char name[] = "stdlib_ndarray_abs"; 86 87 // Define a list of ndarray functions: 88 static ndarrayFcn functions[] = { 89 // NOTE: these are ordered according to likelihood of use (e.g., more likely that `float64` arrays are provided than `uint8`) 90 91 // float64 (1) 92 stdlib_ndarray_d_d, 93 94 // float32 (2) 95 stdlib_ndarray_f_f, 96 stdlib_ndarray_f_d, 97 98 // int32 (3) 99 stdlib_ndarray_i_i, 100 stdlib_ndarray_i_u, 101 stdlib_ndarray_i_d, 102 103 // int16 (6) 104 stdlib_ndarray_k_k, 105 stdlib_ndarray_k_i, 106 stdlib_ndarray_k_t, 107 stdlib_ndarray_k_u, 108 stdlib_ndarray_k_f, 109 stdlib_ndarray_k_d, 110 111 // int8 (8) 112 stdlib_ndarray_s_s, 113 stdlib_ndarray_s_k, 114 stdlib_ndarray_s_i, 115 stdlib_ndarray_s_b, 116 stdlib_ndarray_s_t, 117 stdlib_ndarray_s_u, 118 stdlib_ndarray_s_f, 119 stdlib_ndarray_s_d, 120 121 // uint32 (2) 122 stdlib_ndarray_u_u, 123 stdlib_ndarray_u_d, 124 125 // uint16 (5) 126 stdlib_ndarray_t_i, 127 stdlib_ndarray_t_t, 128 stdlib_ndarray_t_u, 129 stdlib_ndarray_t_f, 130 stdlib_ndarray_t_d, 131 132 // uint8 (7) 133 stdlib_ndarray_b_k, 134 stdlib_ndarray_b_i, 135 stdlib_ndarray_b_b, 136 stdlib_ndarray_b_t, 137 stdlib_ndarray_b_u, 138 stdlib_ndarray_b_f, 139 stdlib_ndarray_b_d 140 }; 141 142 // Define the **ndarray** argument types for each ndarray function: 143 static int32_t types[] = { 144 // float64 (1) 145 STDLIB_NDARRAY_FLOAT64, STDLIB_NDARRAY_FLOAT64, 146 147 // float32 (2) 148 STDLIB_NDARRAY_FLOAT32, STDLIB_NDARRAY_FLOAT32, 149 STDLIB_NDARRAY_FLOAT32, STDLIB_NDARRAY_FLOAT64, 150 151 // int32 (3) 152 STDLIB_NDARRAY_INT32, STDLIB_NDARRAY_INT32, 153 STDLIB_NDARRAY_INT32, STDLIB_NDARRAY_UINT32, 154 STDLIB_NDARRAY_INT32, STDLIB_NDARRAY_FLOAT64, 155 156 // int16 (6) 157 STDLIB_NDARRAY_INT16, STDLIB_NDARRAY_INT16, 158 STDLIB_NDARRAY_INT16, STDLIB_NDARRAY_INT32, 159 STDLIB_NDARRAY_INT16, STDLIB_NDARRAY_UINT16, 160 STDLIB_NDARRAY_INT16, STDLIB_NDARRAY_UINT32, 161 STDLIB_NDARRAY_INT16, STDLIB_NDARRAY_FLOAT32, 162 STDLIB_NDARRAY_INT16, STDLIB_NDARRAY_FLOAT64, 163 164 // int8 (8) 165 STDLIB_NDARRAY_INT8, STDLIB_NDARRAY_INT8, 166 STDLIB_NDARRAY_INT8, STDLIB_NDARRAY_INT16, 167 STDLIB_NDARRAY_INT8, STDLIB_NDARRAY_INT32, 168 STDLIB_NDARRAY_INT8, STDLIB_NDARRAY_UINT8, 169 STDLIB_NDARRAY_INT8, STDLIB_NDARRAY_UINT16, 170 STDLIB_NDARRAY_INT8, STDLIB_NDARRAY_UINT32, 171 STDLIB_NDARRAY_INT8, STDLIB_NDARRAY_FLOAT32, 172 STDLIB_NDARRAY_INT8, STDLIB_NDARRAY_FLOAT64, 173 174 // uint32 (2) 175 STDLIB_NDARRAY_UINT32, STDLIB_NDARRAY_UINT32, 176 STDLIB_NDARRAY_UINT32, STDLIB_NDARRAY_FLOAT64, 177 178 // uint16 (5) 179 STDLIB_NDARRAY_UINT16, STDLIB_NDARRAY_INT32, 180 STDLIB_NDARRAY_UINT16, STDLIB_NDARRAY_UINT16, 181 STDLIB_NDARRAY_UINT16, STDLIB_NDARRAY_UINT32, 182 STDLIB_NDARRAY_UINT16, STDLIB_NDARRAY_FLOAT32, 183 STDLIB_NDARRAY_UINT16, STDLIB_NDARRAY_FLOAT64, 184 185 // uint8 (7) 186 STDLIB_NDARRAY_UINT8, STDLIB_NDARRAY_INT16, 187 STDLIB_NDARRAY_UINT8, STDLIB_NDARRAY_INT32, 188 STDLIB_NDARRAY_UINT8, STDLIB_NDARRAY_UINT8, 189 STDLIB_NDARRAY_UINT8, STDLIB_NDARRAY_UINT16, 190 STDLIB_NDARRAY_UINT8, STDLIB_NDARRAY_UINT32, 191 STDLIB_NDARRAY_UINT8, STDLIB_NDARRAY_FLOAT32, 192 STDLIB_NDARRAY_UINT8, STDLIB_NDARRAY_FLOAT64 193 }; 194 195 // Define a list of ndarray function "data" (in this case, callbacks): 196 static void *data[] = { 197 // float64 (1) 198 (void *)stdlib_base_abs, 199 200 // float32 (2) 201 (void *)stdlib_base_absf, 202 (void *)stdlib_base_absf, 203 204 // int32 (3) 205 (void *)stdlib_base_labs, 206 (void *)stdlib_base_labs, 207 (void *)stdlib_base_labs, 208 209 // int16 (6) 210 (void *)abs_k, 211 (void *)abs_k, 212 (void *)abs_k, 213 (void *)abs_k, 214 (void *)abs_k, 215 (void *)abs_k, 216 217 // int8 (8) 218 (void *)abs_s, 219 (void *)abs_s, 220 (void *)abs_s, 221 (void *)abs_s, 222 (void *)abs_s, 223 (void *)abs_s, 224 (void *)abs_s, 225 (void *)abs_s, 226 227 // uint32 (2) 228 (void *)identity_u, 229 (void *)identity_u, 230 231 // uint16 (5) 232 (void *)identity_t, 233 (void *)identity_t, 234 (void *)identity_t, 235 (void *)identity_t, 236 (void *)identity_t, 237 238 // uint8 (7) 239 (void *)identity_b, 240 (void *)identity_b, 241 (void *)identity_b, 242 (void *)identity_b, 243 (void *)identity_b, 244 (void *)identity_b, 245 (void *)identity_b 246 }; 247 248 // Create an ndarray function object: 249 static const struct ndarrayFunctionObject obj = { 250 // ndarray function name: 251 name, 252 253 // Number of input ndarrays: 254 1, 255 256 // Number of output ndarrays: 257 1, 258 259 // Total number of ndarray arguments (nin + nout): 260 2, 261 262 // Array containing ndarray functions: 263 functions, 264 265 // Number of ndarray functions: 266 34, 267 268 // Array of type "numbers" (as enumerated elsewhere), where the total number of types equals `narrays * nfunctions` and where each set of `narrays` consecutive types (non-overlapping) corresponds to the set of ndarray argument types for a corresponding ndarray function: 269 types, 270 271 // Array of void pointers corresponding to the "data" (e.g., callbacks) which should be passed to a respective ndarray function (note: the number of pointers should match the number of ndarray functions): 272 data 273 }; 274 275 STDLIB_NDARRAY_NAPI_MODULE_UNARY( obj )