Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var float64ToInt32 = require( '@stdlib/number/float64/base/to-float32' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var isEven = require( './../lib' );
Expand All @@ -31,14 +30,21 @@ var isEven = require( './../lib' );
// MAIN //

bench( pkg, function benchmark( b ) {
var opts;
var len;
var x;
var y;
var i;

opts = {
'dtype': 'int32'
};
len = 100;
x = discreteUniform( len, -5.0e6, 5.0e6, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1.0e7 ) - 5.0e6;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the number that's get generated here is not getting round , is it a mistake or intentional ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely a mistake. Feel free to fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh sorry i realised that i have been using a extra package float64toInt32 rather than using opts in discreteuniform

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have corrected it

y = isEven( float64ToInt32( x ) );
y = isEven( x[ i % len ] );
if ( typeof y !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var float64ToInt32 = require( '@stdlib/number/float64/base/to-float32' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;
Expand All @@ -40,14 +39,21 @@ var opts = {
// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var opts;
var len;
var x;
var y;
var i;

opts = {
'dtype': 'int32'
};
len = 100;
x = discreteUniform( len, -5.0e6, 5.0e6, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1.0e7 ) - 5.0e6;
y = iseven( float64ToInt32( x ) );
y = iseven( x[ i % len ] );
if ( typeof y !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,18 @@ static double rand_double( void ) {
*/
static double benchmark( void ) {
double elapsed;
double x;
double x[ 100 ];
double t;
bool b;
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = round( (rand_double()*200.0) - 100.0 );
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = ( rand_double() * 200.0 ) - 100.0;
b = stdlib_base_int32_is_even( (int32_t)x );
b = stdlib_base_int32_is_even( (int32_t)x[ i%100 ] );
if ( b != true && b != false ) {
printf( "should return either true or false\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@
"include": [
"./include"
],
"libraries": [],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": []
"dependencies": [
"@stdlib/napi/export",
"@stdlib/napi/argv",
"@stdlib/napi/argv-int32",
"@stdlib/napi/create-int32"
]
},
{
"task": "benchmark",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
*/

#include "stdlib/math/base/assert/int32_is_even.h"
#include "stdlib/napi/argv.h"
#include "stdlib/napi/argv_int32.h"
#include "stdlib/napi/create_int32.h"
#include "stdlib/napi/export.h"
#include <node_api.h>
#include <stdint.h>
#include <assert.h>

/**
* Receives JavaScript callback invocation data.
Expand All @@ -29,60 +32,10 @@
* @return Node-API value
*/
static napi_value addon( napi_env env, napi_callback_info info ) {
napi_status status;

// Get callback arguments:
size_t argc = 1;
napi_value argv[ 1 ];
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
assert( status == napi_ok );

// Check whether we were provided the correct number of arguments:
if ( argc < 1 ) {
status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
assert( status == napi_ok );
return NULL;
}
if ( argc > 1 ) {
status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
assert( status == napi_ok );
return NULL;
}

napi_valuetype vtype0;
status = napi_typeof( env, argv[ 0 ], &vtype0 );
assert( status == napi_ok );
if ( vtype0 != napi_number ) {
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
assert( status == napi_ok );
return NULL;
}

int32_t x;
status = napi_get_value_int32( env, argv[ 0 ], &x );
assert( status == napi_ok );

bool result = stdlib_base_int32_is_even( x );

napi_value v;
status = napi_create_int32( env, (int32_t)result, &v );
assert( status == napi_ok );

return v;
}

/**
* Initializes a Node-API module.
*
* @param env environment under which the function is invoked
* @param exports exports object
* @return main export
*/
static napi_value init( napi_env env, napi_value exports ) {
napi_value fcn;
napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
assert( status == napi_ok );
return fcn;
STDLIB_NAPI_ARGV( env, info, argv, argc, 1 );
STDLIB_NAPI_ARGV_INT32( env, x, argv, 0 );
STDLIB_NAPI_CREATE_INT32( env, (int32_t)stdlib_base_int32_is_even( x ), out );
return out;
}

NAPI_MODULE( NODE_GYP_MODULE_NAME, init )
STDLIB_NAPI_MODULE_EXPORT_FCN( addon )
Loading