addon.cpp (5059B)
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/blas/ext/base/dnannsumkbn.h" 20 #include <node_api.h> 21 #include <stdint.h> 22 #include <stdlib.h> 23 #include <stdbool.h> 24 #include <assert.h> 25 26 /** 27 * Add-on namespace. 28 */ 29 namespace stdlib_blas_ext_base_dnannsumkbn { 30 31 /** 32 * Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using an improved Kahan–Babuška algorithm. 33 * 34 * ## Notes 35 * 36 * - When called from JavaScript, the function expects five arguments: 37 * 38 * - `N`: number of indexed elements 39 * - `X`: input array 40 * - `strideX`: `X` stride length 41 * - `Out`: output array 42 * - `strideOut`: `Out` stride length 43 */ 44 napi_value node_dnannsumkbn( napi_env env, napi_callback_info info ) { 45 napi_status status; 46 47 size_t argc = 5; 48 napi_value argv[ 5 ]; 49 status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr ); 50 assert( status == napi_ok ); 51 52 if ( argc < 5 ) { 53 napi_throw_error( env, nullptr, "invalid invocation. Must provide 5 arguments." ); 54 return nullptr; 55 } 56 57 napi_valuetype vtype0; 58 status = napi_typeof( env, argv[ 0 ], &vtype0 ); 59 assert( status == napi_ok ); 60 if ( vtype0 != napi_number ) { 61 napi_throw_type_error( env, nullptr, "invalid argument. First argument must be a number." ); 62 return nullptr; 63 } 64 65 bool res1; 66 status = napi_is_typedarray( env, argv[ 1 ], &res1 ); 67 assert( status == napi_ok ); 68 if ( res1 == false ) { 69 napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float64Array." ); 70 return nullptr; 71 } 72 73 napi_valuetype vtype2; 74 status = napi_typeof( env, argv[ 2 ], &vtype2 ); 75 assert( status == napi_ok ); 76 if ( vtype2 != napi_number ) { 77 napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a number." ); 78 return nullptr; 79 } 80 81 bool res3; 82 status = napi_is_typedarray( env, argv[ 3 ], &res3 ); 83 assert( status == napi_ok ); 84 if ( res3 == false ) { 85 napi_throw_type_error( env, nullptr, "invalid argument. Fourth argument must be a Float64Array." ); 86 return nullptr; 87 } 88 89 napi_valuetype vtype4; 90 status = napi_typeof( env, argv[ 4 ], &vtype4 ); 91 assert( status == napi_ok ); 92 if ( vtype4 != napi_number ) { 93 napi_throw_type_error( env, nullptr, "invalid argument. Fifth argument must be a number." ); 94 return nullptr; 95 } 96 97 int64_t N; 98 status = napi_get_value_int64( env, argv[ 0 ], &N ); 99 assert( status == napi_ok ); 100 101 int64_t strideX; 102 status = napi_get_value_int64( env, argv[ 2 ], &strideX ); 103 assert( status == napi_ok ); 104 105 int64_t strideOut; 106 status = napi_get_value_int64( env, argv[ 4 ], &strideOut ); 107 assert( status == napi_ok ); 108 109 napi_typedarray_type vtype1; 110 size_t xlen; 111 void *X; 112 status = napi_get_typedarray_info( env, argv[ 1 ], &vtype1, &xlen, &X, nullptr, nullptr ); 113 assert( status == napi_ok ); 114 if ( vtype1 != napi_float64_array ) { 115 napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float64Array." ); 116 return nullptr; 117 } 118 if ( (N-1)*llabs(strideX) >= (int64_t)xlen ) { 119 napi_throw_range_error( env, nullptr, "invalid argument. Second argument has insufficient elements based on the associated stride and the number of indexed elements." ); 120 return nullptr; 121 } 122 123 napi_typedarray_type vtype3; 124 size_t olen; 125 void *Out; 126 status = napi_get_typedarray_info( env, argv[ 3 ], &vtype3, &olen, &Out, nullptr, nullptr ); 127 assert( status == napi_ok ); 128 if ( vtype3 != napi_float64_array ) { 129 napi_throw_type_error( env, nullptr, "invalid argument. Fourth argument must be a Float64Array." ); 130 return nullptr; 131 } 132 if ( llabs(strideOut) > (int64_t)olen ) { 133 napi_throw_range_error( env, nullptr, "invalid argument. Fourth argument has insufficient elements based on the associated stride and the required number of output elements." ); 134 return nullptr; 135 } 136 137 int64_t io; 138 if ( strideOut < 0 ) { 139 io = -strideOut; 140 } else { 141 io = 0; 142 } 143 144 double *out = (double *)Out; 145 int64_t n; 146 out[ io ] = stdlib_strided_dnannsumkbn( N, (double *)X, strideX, &n ); 147 out[ io+strideOut ] = (double)n; 148 149 return nullptr; 150 151 } 152 153 napi_value Init( napi_env env, napi_value exports ) { 154 napi_status status; 155 napi_value fcn; 156 status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_dnannsumkbn, NULL, &fcn ); 157 assert( status == napi_ok ); 158 return fcn; 159 } 160 161 NAPI_MODULE( NODE_GYP_MODULE_NAME, Init ) 162 } // end namespace stdlib_blas_ext_base_dnannsumkbn