addon.cpp (3595B)
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/stats/base/dmeankbn.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_stats_base_dmeankbn { 30 31 /** 32 * Computes the arithmetic mean of a double-precision floating-point strided array using an improved Kahan–Babuška algorithm. 33 * 34 * ## Notes 35 * 36 * - When called from JavaScript, the function expects three arguments: 37 * 38 * - `N`: number of indexed elements 39 * - `X`: input array 40 * - `stride`: stride length 41 */ 42 napi_value node_dmeankbn( napi_env env, napi_callback_info info ) { 43 napi_status status; 44 45 size_t argc = 3; 46 napi_value argv[ 3 ]; 47 status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr ); 48 assert( status == napi_ok ); 49 50 if ( argc < 3 ) { 51 napi_throw_error( env, nullptr, "invalid invocation. Must provide 3 arguments." ); 52 return nullptr; 53 } 54 55 napi_valuetype vtype0; 56 status = napi_typeof( env, argv[ 0 ], &vtype0 ); 57 assert( status == napi_ok ); 58 if ( vtype0 != napi_number ) { 59 napi_throw_type_error( env, nullptr, "invalid argument. First argument must be a number." ); 60 return nullptr; 61 } 62 63 bool res; 64 status = napi_is_typedarray( env, argv[ 1 ], &res ); 65 assert( status == napi_ok ); 66 if ( res == false ) { 67 napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float64Array." ); 68 return nullptr; 69 } 70 71 napi_valuetype vtype2; 72 status = napi_typeof( env, argv[ 2 ], &vtype2 ); 73 assert( status == napi_ok ); 74 if ( vtype2 != napi_number ) { 75 napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a number." ); 76 return nullptr; 77 } 78 79 int64_t N; 80 status = napi_get_value_int64( env, argv[ 0 ], &N ); 81 assert( status == napi_ok ); 82 83 int64_t stride; 84 status = napi_get_value_int64( env, argv[ 2 ], &stride ); 85 assert( status == napi_ok ); 86 87 napi_typedarray_type vtype1; 88 size_t xlen; 89 void *X; 90 status = napi_get_typedarray_info( env, argv[ 1 ], &vtype1, &xlen, &X, nullptr, nullptr ); 91 assert( status == napi_ok ); 92 if ( vtype1 != napi_float64_array ) { 93 napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float64Array." ); 94 return nullptr; 95 } 96 if ( (N-1)*llabs(stride) >= (int64_t)xlen ) { 97 napi_throw_range_error( env, nullptr, "invalid argument. Second argument has insufficient elements based on the associated stride and the number of indexed elements." ); 98 return nullptr; 99 } 100 101 napi_value v; 102 status = napi_create_double( env, stdlib_strided_dmeankbn( N, (double *)X, stride ), &v ); 103 assert( status == napi_ok ); 104 105 return v; 106 } 107 108 napi_value Init( napi_env env, napi_value exports ) { 109 napi_status status; 110 napi_value fcn; 111 status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_dmeankbn, NULL, &fcn ); 112 assert( status == napi_ok ); 113 return fcn; 114 } 115 116 NAPI_MODULE( NODE_GYP_MODULE_NAME, Init ) 117 } // end namespace stdlib_stats_base_dmeankbn