addon.cpp (5273B)
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/dmeanstdev.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_dmeanstdev { 30 31 /** 32 * Computes the mean and standard deviation of a double-precision floating-point strided array. 33 * 34 * ## Notes 35 * 36 * - When called from JavaScript, the function expects six arguments: 37 * 38 * - `N`: number of indexed elements 39 * - `correction`: degrees of freedom adjustment 40 * - `X`: input array 41 * - `strideX`: `X` stride length 42 * - `Out`: output array 43 * - `strideOut`: `Out` stride length 44 */ 45 napi_value node_dmeanstdev( napi_env env, napi_callback_info info ) { 46 napi_status status; 47 48 size_t argc = 6; 49 napi_value argv[ 6 ]; 50 status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr ); 51 assert( status == napi_ok ); 52 53 if ( argc < 6 ) { 54 napi_throw_error( env, nullptr, "invalid invocation. Must provide 6 arguments." ); 55 return nullptr; 56 } 57 58 napi_valuetype vtype0; 59 status = napi_typeof( env, argv[ 0 ], &vtype0 ); 60 assert( status == napi_ok ); 61 if ( vtype0 != napi_number ) { 62 napi_throw_type_error( env, nullptr, "invalid argument. First argument must be a number." ); 63 return nullptr; 64 } 65 66 napi_valuetype vtype1; 67 status = napi_typeof( env, argv[ 1 ], &vtype1 ); 68 assert( status == napi_ok ); 69 if ( vtype1 != napi_number ) { 70 napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a number." ); 71 return nullptr; 72 } 73 74 bool res2; 75 status = napi_is_typedarray( env, argv[ 2 ], &res2 ); 76 assert( status == napi_ok ); 77 if ( res2 == false ) { 78 napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a Float64Array." ); 79 return nullptr; 80 } 81 82 napi_valuetype vtype3; 83 status = napi_typeof( env, argv[ 3 ], &vtype3 ); 84 assert( status == napi_ok ); 85 if ( vtype3 != napi_number ) { 86 napi_throw_type_error( env, nullptr, "invalid argument. Fourth argument must be a number." ); 87 return nullptr; 88 } 89 90 bool res4; 91 status = napi_is_typedarray( env, argv[ 4 ], &res4 ); 92 assert( status == napi_ok ); 93 if ( res4 == false ) { 94 napi_throw_type_error( env, nullptr, "invalid argument. Fifth argument must be a Float64Array." ); 95 return nullptr; 96 } 97 98 napi_valuetype vtype5; 99 status = napi_typeof( env, argv[ 5 ], &vtype5 ); 100 assert( status == napi_ok ); 101 if ( vtype5 != napi_number ) { 102 napi_throw_type_error( env, nullptr, "invalid argument. Sixth argument must be a number." ); 103 return nullptr; 104 } 105 106 int64_t N; 107 status = napi_get_value_int64( env, argv[ 0 ], &N ); 108 assert( status == napi_ok ); 109 110 double correction; 111 status = napi_get_value_double( env, argv[ 1 ], &correction ); 112 assert( status == napi_ok ); 113 114 int64_t strideX; 115 status = napi_get_value_int64( env, argv[ 3 ], &strideX ); 116 assert( status == napi_ok ); 117 118 int64_t strideOut; 119 status = napi_get_value_int64( env, argv[ 5 ], &strideOut ); 120 assert( status == napi_ok ); 121 122 napi_typedarray_type vtype2; 123 size_t xlen; 124 void *X; 125 status = napi_get_typedarray_info( env, argv[ 2 ], &vtype2, &xlen, &X, nullptr, nullptr ); 126 assert( status == napi_ok ); 127 if ( vtype2 != napi_float64_array ) { 128 napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a Float64Array." ); 129 return nullptr; 130 } 131 if ( (N-1)*llabs(strideX) >= (int64_t)xlen ) { 132 napi_throw_range_error( env, nullptr, "invalid argument. Third argument has insufficient elements based on the associated stride and the number of indexed elements." ); 133 return nullptr; 134 } 135 136 napi_typedarray_type vtype4; 137 size_t olen; 138 void *Out; 139 status = napi_get_typedarray_info( env, argv[ 4 ], &vtype4, &olen, &Out, nullptr, nullptr ); 140 assert( status == napi_ok ); 141 if ( vtype4 != napi_float64_array ) { 142 napi_throw_type_error( env, nullptr, "invalid argument. Fifth argument must be a Float64Array." ); 143 return nullptr; 144 } 145 if ( llabs(strideOut) > (int64_t)olen ) { 146 napi_throw_range_error( env, nullptr, "invalid argument. Fifth argument has insufficient elements based on the associated stride and the required number of output elements." ); 147 return nullptr; 148 } 149 150 stdlib_strided_dmeanstdev( N, correction, (double *)X, strideX, (double *)Out, strideOut ); 151 152 return nullptr; 153 } 154 155 napi_value Init( napi_env env, napi_value exports ) { 156 napi_status status; 157 napi_value fcn; 158 status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_dmeanstdev, NULL, &fcn ); 159 assert( status == napi_ok ); 160 return fcn; 161 } 162 163 NAPI_MODULE( NODE_GYP_MODULE_NAME, Init ) 164 } // end namespace stdlib_stats_base_dmeanstdev