time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

addon.cpp (4100B)


      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/snanvariancech.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_snanvariancech {
     30 
     31 	/**
     32 	* Computes the variance of a single-precision floating-point strided array ignoring `NaN` values and using a one-pass trial mean algorithm.
     33 	*
     34 	* ## Notes
     35 	*
     36 	* -   When called from JavaScript, the function expects four arguments:
     37 	*
     38 	*     -   `N`: number of indexed elements
     39 	*     -   `correction`: degrees of freedom adjustment
     40 	*     -   `X`: input array
     41 	*     -   `stride`: stride length
     42 	*/
     43 	napi_value node_snanvariancech( napi_env env, napi_callback_info info ) {
     44 		napi_status status;
     45 
     46 		size_t argc = 4;
     47 		napi_value argv[ 4 ];
     48 		status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr );
     49 		assert( status == napi_ok );
     50 
     51 		if ( argc < 4 ) {
     52 			napi_throw_error( env, nullptr, "invalid invocation. Must provide 4 arguments." );
     53 			return nullptr;
     54 		}
     55 
     56 		napi_valuetype vtype0;
     57 		status = napi_typeof( env, argv[ 0 ], &vtype0 );
     58 		assert( status == napi_ok );
     59 		if ( vtype0 != napi_number ) {
     60 			napi_throw_type_error( env, nullptr, "invalid argument. First argument must be a number." );
     61 			return nullptr;
     62 		}
     63 
     64 		napi_valuetype vtype1;
     65 		status = napi_typeof( env, argv[ 1 ], &vtype1 );
     66 		assert( status == napi_ok );
     67 		if ( vtype1 != napi_number ) {
     68 			napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a number." );
     69 			return nullptr;
     70 		}
     71 
     72 		bool res;
     73 		status = napi_is_typedarray( env, argv[ 2 ], &res );
     74 		assert( status == napi_ok );
     75 		if ( res == false ) {
     76 			napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a Float32Array." );
     77 			return nullptr;
     78 		}
     79 
     80 		napi_valuetype vtype3;
     81 		status = napi_typeof( env, argv[ 3 ], &vtype3 );
     82 		assert( status == napi_ok );
     83 		if ( vtype3 != napi_number ) {
     84 			napi_throw_type_error( env, nullptr, "invalid argument. Fourth argument must be a number." );
     85 			return nullptr;
     86 		}
     87 
     88 		int64_t N;
     89 		status = napi_get_value_int64( env, argv[ 0 ], &N );
     90 		assert( status == napi_ok );
     91 
     92 		double correction;
     93 		status = napi_get_value_double( env, argv[ 1 ], &correction );
     94 		assert( status == napi_ok );
     95 
     96 		int64_t stride;
     97 		status = napi_get_value_int64( env, argv[ 3 ], &stride );
     98 		assert( status == napi_ok );
     99 
    100 		napi_typedarray_type vtype2;
    101 		size_t xlen;
    102 		void *X;
    103 		status = napi_get_typedarray_info( env, argv[ 2 ], &vtype2, &xlen, &X, nullptr, nullptr );
    104 		assert( status == napi_ok );
    105 		if ( vtype2 != napi_float32_array ) {
    106 			napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a Float32Array." );
    107 			return nullptr;
    108 		}
    109 		if ( (N-1)*llabs(stride) >= (int64_t)xlen ) {
    110 			napi_throw_range_error( env, nullptr, "invalid argument. Third argument has insufficient elements based on the associated stride and the number of indexed elements." );
    111 			return nullptr;
    112 		}
    113 
    114 		napi_value v;
    115 		status = napi_create_double( env, (double)stdlib_strided_snanvariancech( N, (float)correction, (float *)X, stride ), &v );
    116 		assert( status == napi_ok );
    117 
    118 		return v;
    119 	}
    120 
    121 	napi_value Init( napi_env env, napi_value exports ) {
    122 		napi_status status;
    123 		napi_value fcn;
    124 		status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_snanvariancech, NULL, &fcn );
    125 		assert( status == napi_ok );
    126 		return fcn;
    127 	}
    128 
    129 	NAPI_MODULE( NODE_GYP_MODULE_NAME, Init )
    130 } // end namespace stdlib_stats_base_snanvariancech