main.c (6472B)
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/strided/napi/smskmap.h" 20 #include "stdlib/strided/base/smskmap.h" 21 #include <node_api.h> 22 #include <stdint.h> 23 #include <stdlib.h> 24 #include <assert.h> 25 26 /** 27 * Invokes a strided array interface which applies a unary callback accepting and returning single-precision floating-point numbers to each element in a single-precision floating-point strided input array according to a corresponding element in a strided mask array and assigns results to elements in a single-precision floating-point strided output array. 28 * 29 * ## Notes 30 * 31 * - This function expects that the callback `info` argument provides access to the following JavaScript arguments: 32 * 33 * - `N`: number of indexed elements 34 * - `X`: input array 35 * - `strideX`: `X` stride length 36 * - `Mask`: mask array 37 * - `strideMask`: `Mask` stride length 38 * - `Y`: destination array 39 * - `strideY`: `Y` stride length 40 * 41 * @param env environment under which the function is invoked 42 * @param info callback data 43 * @param fcn unary callback 44 */ 45 void stdlib_strided_napi_smskmap( napi_env env, napi_callback_info info, float (*fcn)( float ) ) { 46 napi_status status; 47 48 size_t argc = 7; 49 napi_value argv[ 7 ]; 50 status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); 51 assert( status == napi_ok ); 52 53 if ( argc != 7 ) { 54 status = napi_throw_error( env, NULL, "invalid invocation. Must provide 7 arguments." ); 55 assert( status == napi_ok ); 56 return; 57 } 58 59 napi_valuetype vtype0; 60 status = napi_typeof( env, argv[ 0 ], &vtype0 ); 61 assert( status == napi_ok ); 62 if ( vtype0 != napi_number ) { 63 status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." ); 64 assert( status == napi_ok ); 65 return; 66 } 67 68 bool res1; 69 status = napi_is_typedarray( env, argv[ 1 ], &res1 ); 70 assert( status == napi_ok ); 71 if ( res1 == false ) { 72 status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a Float32Array." ); 73 assert( status == napi_ok ); 74 return; 75 } 76 77 napi_valuetype vtype2; 78 status = napi_typeof( env, argv[ 2 ], &vtype2 ); 79 assert( status == napi_ok ); 80 if ( vtype2 != napi_number ) { 81 status = napi_throw_type_error( env, NULL, "invalid argument. Third argument must be a number." ); 82 assert( status == napi_ok ); 83 return; 84 } 85 86 bool res3; 87 status = napi_is_typedarray( env, argv[ 3 ], &res3 ); 88 assert( status == napi_ok ); 89 if ( res3 == false ) { 90 status = napi_throw_type_error( env, NULL, "invalid argument. Fourth argument must be a Uint8Array." ); 91 assert( status == napi_ok ); 92 return; 93 } 94 95 napi_valuetype vtype4; 96 status = napi_typeof( env, argv[ 4 ], &vtype4 ); 97 assert( status == napi_ok ); 98 if ( vtype4 != napi_number ) { 99 status = napi_throw_type_error( env, NULL, "invalid argument. Fifth argument must be a number." ); 100 assert( status == napi_ok ); 101 return; 102 } 103 104 bool res5; 105 status = napi_is_typedarray( env, argv[ 5 ], &res5 ); 106 assert( status == napi_ok ); 107 if ( res5 == false ) { 108 status = napi_throw_type_error( env, NULL, "invalid argument. Sixth argument must be a Float32Array." ); 109 assert( status == napi_ok ); 110 return; 111 } 112 113 napi_valuetype vtype6; 114 status = napi_typeof( env, argv[ 6 ], &vtype6 ); 115 assert( status == napi_ok ); 116 if ( vtype6 != napi_number ) { 117 status = napi_throw_type_error( env, NULL, "invalid argument. Seventh argument must be a number." ); 118 assert( status == napi_ok ); 119 return; 120 } 121 122 int64_t N; 123 status = napi_get_value_int64( env, argv[ 0 ], &N ); 124 assert( status == napi_ok ); 125 126 int64_t strideX; 127 status = napi_get_value_int64( env, argv[ 2 ], &strideX ); 128 assert( status == napi_ok ); 129 130 int64_t strideMask; 131 status = napi_get_value_int64( env, argv[ 4 ], &strideMask ); 132 assert( status == napi_ok ); 133 134 int64_t strideY; 135 status = napi_get_value_int64( env, argv[ 6 ], &strideY ); 136 assert( status == napi_ok ); 137 138 napi_typedarray_type vtype1; 139 size_t xlen; 140 void *X; 141 status = napi_get_typedarray_info( env, argv[ 1 ], &vtype1, &xlen, &X, NULL, NULL ); 142 assert( status == napi_ok ); 143 if ( vtype1 != napi_float32_array ) { 144 status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a Float32Array." ); 145 assert( status == napi_ok ); 146 return; 147 } 148 if ( (N-1)*llabs(strideX) >= (int64_t)xlen ) { 149 status = napi_throw_range_error( env, NULL, "invalid argument. Second argument has insufficient elements based on the associated stride and the number of indexed elements." ); 150 assert( status == napi_ok ); 151 return; 152 } 153 154 napi_typedarray_type vtype3; 155 size_t mlen; 156 void *Mask; 157 status = napi_get_typedarray_info( env, argv[ 3 ], &vtype3, &mlen, &Mask, NULL, NULL ); 158 assert( status == napi_ok ); 159 if ( vtype3 != napi_uint8_array ) { 160 status = napi_throw_type_error( env, NULL, "invalid argument. Fourth argument must be a Uint8Array." ); 161 assert( status == napi_ok ); 162 return; 163 } 164 if ( (N-1)*llabs(strideMask) >= (int64_t)mlen ) { 165 status = napi_throw_range_error( env, NULL, "invalid argument. Fourth argument has insufficient elements based on the associated stride and the number of indexed elements." ); 166 assert( status == napi_ok ); 167 return; 168 } 169 170 napi_typedarray_type vtype5; 171 size_t ylen; 172 void *Y; 173 status = napi_get_typedarray_info( env, argv[ 5 ], &vtype5, &ylen, &Y, NULL, NULL ); 174 assert( status == napi_ok ); 175 if ( vtype5 != napi_float32_array ) { 176 status = napi_throw_type_error( env, NULL, "invalid argument. Sixth argument must be a Float32Array." ); 177 assert( status == napi_ok ); 178 return; 179 } 180 if ( (N-1)*llabs(strideY) >= (int64_t)ylen ) { 181 status = napi_throw_range_error( env, NULL, "invalid argument. Sixth argument has insufficient elements based on the associated stride and the number of indexed elements." ); 182 assert( status == napi_ok ); 183 return; 184 } 185 186 stdlib_strided_smskmap( N, (float *)X, strideX, (uint8_t *)Mask, strideMask, (float *)Y, strideY, fcn ); 187 188 return; 189 }