time-to-botec

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

addon.cpp (2178B)


      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/math/base/assert/is_nanf.h"
     20 #include <node_api.h>
     21 #include <stdint.h>
     22 #include <assert.h>
     23 
     24 /**
     25 * Add-on namespace.
     26 */
     27 namespace stdlib_math_base_assert_is_nanf {
     28 
     29 	/**
     30 	* Tests if a single-precision floating-point numeric value is `NaN`.
     31 	*
     32 	* ## Notes
     33 	*
     34 	* -   When called from JavaScript, the function expects one argument:
     35 	*
     36 	*     -    `x`: a number
     37 	*/
     38 	napi_value node_is_nanf( napi_env env, napi_callback_info info ) {
     39 		napi_status status;
     40 
     41 		size_t argc = 1;
     42 		napi_value argv[ 1 ];
     43 		status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr );
     44 		assert( status == napi_ok );
     45 
     46 		if ( argc < 1 ) {
     47 			napi_throw_error( env, nullptr, "invalid invocation. Must provide a number." );
     48 			return nullptr;
     49 		}
     50 
     51 		napi_valuetype vtype0;
     52 		status = napi_typeof( env, argv[ 0 ], &vtype0 );
     53 		assert( status == napi_ok );
     54 
     55 		napi_value v;
     56 		double x;
     57 		if ( vtype0 == napi_number ) {
     58 			status = napi_get_value_double( env, argv[ 0 ], &x );
     59 			assert( status == napi_ok );
     60 
     61 			status = napi_create_int32( env, (int32_t)stdlib_base_is_nanf( (float)x ), &v );
     62 			assert( status == napi_ok );
     63 		} else {
     64 			status = napi_create_int32( env, 0, &v );
     65 			assert( status == napi_ok );
     66 		}
     67 
     68 		return v;
     69 	}
     70 
     71 	napi_value Init( napi_env env, napi_value exports ) {
     72 		napi_status status;
     73 		napi_value fcn;
     74 		status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_is_nanf, NULL, &fcn );
     75 		assert( status == napi_ok );
     76 		return fcn;
     77 	}
     78 
     79 	NAPI_MODULE( NODE_GYP_MODULE_NAME, Init )
     80 } // end namespace stdlib_math_base_assert_is_nanf