addon.c (6572B)
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/special/abs2.h" 20 #include "stdlib/math/base/special/abs2f.h" 21 #include "stdlib/strided/dtypes.h" 22 #include "stdlib/strided/base/function_object.h" 23 #include "stdlib/strided/base/unary.h" 24 #include "stdlib/strided/napi/unary.h" 25 #include <stdint.h> 26 27 /** 28 * Computes the squared absolute value for an unsigned 32-bit integer. 29 * 30 * @param x input value 31 * @return input value 32 */ 33 static uint32_t abs2_u( const uint32_t x ) { 34 return x * x; 35 } 36 37 /** 38 * Computes the squared absolute value for an unsigned 16-bit integer. 39 * 40 * @param x input value 41 * @return input value 42 */ 43 static uint16_t abs2_t( const uint16_t x ) { 44 return x * x; 45 } 46 47 /** 48 * Computes the squared absolute value for an unsigned 8-bit integer. 49 * 50 * @param x input value 51 * @return input value 52 */ 53 static uint8_t abs2_b( const uint8_t x ) { 54 return x * x; 55 } 56 57 /** 58 * Computes the squared absolute value of a signed 16-bit integer. 59 * 60 * @param x input value 61 * @return squared absolute value 62 */ 63 static int16_t abs2_k( const int16_t x ) { 64 return x * x; 65 } 66 67 /** 68 * Computes the squared absolute value of a signed 8-bit integer. 69 * 70 * @param x input value 71 * @return squared absolute value 72 */ 73 static int8_t abs2_s( const int8_t x ) { 74 return x * x; 75 } 76 77 // Define an interface name: 78 static const char name[] = "stdlib_strided_abs2"; 79 80 // Define a list of strided array functions: 81 static StridedArrayFcn functions[] = { 82 // NOTE: these are ordered according to likelihood of use (e.g., more likely that `float64` arrays are provided than `uint8`) 83 84 // float64 (1) 85 stdlib_strided_d_d, 86 87 // float32 (2) 88 stdlib_strided_f_f, 89 stdlib_strided_f_d, 90 91 // int32 (3) 92 stdlib_strided_i_i, 93 stdlib_strided_i_u, 94 stdlib_strided_i_d, 95 96 // int16 (6) 97 stdlib_strided_k_k, 98 stdlib_strided_k_i, 99 stdlib_strided_k_t, 100 stdlib_strided_k_u, 101 stdlib_strided_k_f, 102 stdlib_strided_k_d, 103 104 // int8 (8) 105 stdlib_strided_s_s, 106 stdlib_strided_s_k, 107 stdlib_strided_s_i, 108 stdlib_strided_s_b, 109 stdlib_strided_s_t, 110 stdlib_strided_s_u, 111 stdlib_strided_s_f, 112 stdlib_strided_s_d, 113 114 // uint32 (2) 115 stdlib_strided_u_u, 116 stdlib_strided_u_d, 117 118 // uint16 (5) 119 stdlib_strided_t_i, 120 stdlib_strided_t_t, 121 stdlib_strided_t_u, 122 stdlib_strided_t_f, 123 stdlib_strided_t_d, 124 125 // uint8 (7) 126 stdlib_strided_b_k, 127 stdlib_strided_b_i, 128 stdlib_strided_b_b, 129 stdlib_strided_b_t, 130 stdlib_strided_b_u, 131 stdlib_strided_b_f, 132 stdlib_strided_b_d 133 }; 134 135 // Define the **strided array** argument types for each strided array function: 136 static int32_t types[] = { 137 // float64 (1) 138 STDLIB_STRIDED_FLOAT64, STDLIB_STRIDED_FLOAT64, 139 140 // float32 (2) 141 STDLIB_STRIDED_FLOAT32, STDLIB_STRIDED_FLOAT32, 142 STDLIB_STRIDED_FLOAT32, STDLIB_STRIDED_FLOAT64, 143 144 // int32 (3) 145 STDLIB_STRIDED_INT32, STDLIB_STRIDED_INT32, 146 STDLIB_STRIDED_INT32, STDLIB_STRIDED_UINT32, 147 STDLIB_STRIDED_INT32, STDLIB_STRIDED_FLOAT64, 148 149 // int16 (6) 150 STDLIB_STRIDED_INT16, STDLIB_STRIDED_INT16, 151 STDLIB_STRIDED_INT16, STDLIB_STRIDED_INT32, 152 STDLIB_STRIDED_INT16, STDLIB_STRIDED_UINT16, 153 STDLIB_STRIDED_INT16, STDLIB_STRIDED_UINT32, 154 STDLIB_STRIDED_INT16, STDLIB_STRIDED_FLOAT32, 155 STDLIB_STRIDED_INT16, STDLIB_STRIDED_FLOAT64, 156 157 // int8 (8) 158 STDLIB_STRIDED_INT8, STDLIB_STRIDED_INT8, 159 STDLIB_STRIDED_INT8, STDLIB_STRIDED_INT16, 160 STDLIB_STRIDED_INT8, STDLIB_STRIDED_INT32, 161 STDLIB_STRIDED_INT8, STDLIB_STRIDED_UINT8, 162 STDLIB_STRIDED_INT8, STDLIB_STRIDED_UINT16, 163 STDLIB_STRIDED_INT8, STDLIB_STRIDED_UINT32, 164 STDLIB_STRIDED_INT8, STDLIB_STRIDED_FLOAT32, 165 STDLIB_STRIDED_INT8, STDLIB_STRIDED_FLOAT64, 166 167 // uint32 (2) 168 STDLIB_STRIDED_UINT32, STDLIB_STRIDED_UINT32, 169 STDLIB_STRIDED_UINT32, STDLIB_STRIDED_FLOAT64, 170 171 // uint16 (5) 172 STDLIB_STRIDED_UINT16, STDLIB_STRIDED_INT32, 173 STDLIB_STRIDED_UINT16, STDLIB_STRIDED_UINT16, 174 STDLIB_STRIDED_UINT16, STDLIB_STRIDED_UINT32, 175 STDLIB_STRIDED_UINT16, STDLIB_STRIDED_FLOAT32, 176 STDLIB_STRIDED_UINT16, STDLIB_STRIDED_FLOAT64, 177 178 // uint8 (7) 179 STDLIB_STRIDED_UINT8, STDLIB_STRIDED_INT16, 180 STDLIB_STRIDED_UINT8, STDLIB_STRIDED_INT32, 181 STDLIB_STRIDED_UINT8, STDLIB_STRIDED_UINT8, 182 STDLIB_STRIDED_UINT8, STDLIB_STRIDED_UINT16, 183 STDLIB_STRIDED_UINT8, STDLIB_STRIDED_UINT32, 184 STDLIB_STRIDED_UINT8, STDLIB_STRIDED_FLOAT32, 185 STDLIB_STRIDED_UINT8, STDLIB_STRIDED_FLOAT64 186 }; 187 188 // Define a list of strided array function "data" (in this case, callbacks): 189 static void *data[] = { 190 // float64 (1) 191 (void *)stdlib_base_abs2, 192 193 // float32 (2) 194 (void *)stdlib_base_abs2f, 195 (void *)stdlib_base_abs2f, 196 197 // int32 (3) 198 (void *)abs2_u, 199 (void *)abs2_u, 200 (void *)abs2_u, 201 202 // int16 (6) 203 (void *)abs2_k, 204 (void *)abs2_k, 205 (void *)abs2_k, 206 (void *)abs2_k, 207 (void *)abs2_k, 208 (void *)abs2_k, 209 210 // int8 (8) 211 (void *)abs2_s, 212 (void *)abs2_s, 213 (void *)abs2_s, 214 (void *)abs2_s, 215 (void *)abs2_s, 216 (void *)abs2_s, 217 (void *)abs2_s, 218 (void *)abs2_s, 219 220 // uint32 (2) 221 (void *)abs2_u, 222 (void *)abs2_u, 223 224 // uint16 (5) 225 (void *)abs2_t, 226 (void *)abs2_t, 227 (void *)abs2_t, 228 (void *)abs2_t, 229 (void *)abs2_t, 230 231 // uint8 (7) 232 (void *)abs2_b, 233 (void *)abs2_b, 234 (void *)abs2_b, 235 (void *)abs2_b, 236 (void *)abs2_b, 237 (void *)abs2_b, 238 (void *)abs2_b 239 }; 240 241 // Create a strided array function object: 242 static const struct StridedFunctionObject obj = { 243 // Strided array function name: 244 name, 245 246 // Number of input strided arrays: 247 1, 248 249 // Number of output strided arrays: 250 1, 251 252 // Total number of strided array arguments (nin + nout): 253 2, 254 255 // Array containing strided array functions: 256 functions, 257 258 // Number of strided array functions: 259 34, 260 261 // 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 strided array argument types for a corresponding strided array function: 262 types, 263 264 // Array of void pointers corresponding to the "data" (e.g., callbacks) which should be passed to a respective strided array function (note: the number of pointers should match the number of strided array functions): 265 data 266 }; 267 268 STDLIB_STRIDED_NAPI_MODULE_UNARY( obj )