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