time-to-botec

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

main.c (4995B)


      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/strided/napi/dmap.h"
     20 #include "stdlib/strided/base/dmap.h"
     21 #include <node_api.h>
     22 #include <stdint.h>
     23 #include <stdlib.h>
     24 #include <assert.h>
     25 
     26 /**
     27 * Invokes a strided array interface which applies a unary callback accepting and returning double-precision floating-point numbers to each element in a double-precision floating-point strided input array and assigns results to elements in a double-precision floating-point strided output array.
     28 *
     29 * ## Notes
     30 *
     31 * -   This function expects that the callback `info` argument provides access to the following JavaScript arguments:
     32 *
     33 *     -   `N`: number of indexed elements
     34 *     -   `X`: input array
     35 *     -   `strideX`: `X` stride length
     36 *     -   `Y`: destination array
     37 *     -   `strideY`: `Y` stride length
     38 *
     39 * @param env    environment under which the function is invoked
     40 * @param info   callback data
     41 * @param fcn    unary callback
     42 */
     43 void stdlib_strided_napi_dmap( napi_env env, napi_callback_info info, double (*fcn)( double ) ) {
     44 	napi_status status;
     45 
     46 	size_t argc = 5;
     47 	napi_value argv[ 5 ];
     48 	status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
     49 	assert( status == napi_ok );
     50 
     51 	if ( argc != 5 ) {
     52 		status = napi_throw_error( env, NULL, "invalid invocation. Must provide 5 arguments." );
     53 		assert( status == napi_ok );
     54 		return;
     55 	}
     56 
     57 	napi_valuetype vtype0;
     58 	status = napi_typeof( env, argv[ 0 ], &vtype0 );
     59 	assert( status == napi_ok );
     60 	if ( vtype0 != napi_number ) {
     61 		status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
     62 		assert( status == napi_ok );
     63 		return;
     64 	}
     65 
     66 	bool res1;
     67 	status = napi_is_typedarray( env, argv[ 1 ], &res1 );
     68 	assert( status == napi_ok );
     69 	if ( res1 == false ) {
     70 		status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a Float64Array." );
     71 		assert( status == napi_ok );
     72 		return;
     73 	}
     74 
     75 	napi_valuetype vtype2;
     76 	status = napi_typeof( env, argv[ 2 ], &vtype2 );
     77 	assert( status == napi_ok );
     78 	if ( vtype2 != napi_number ) {
     79 		status = napi_throw_type_error( env, NULL, "invalid argument. Third argument must be a number." );
     80 		assert( status == napi_ok );
     81 		return;
     82 	}
     83 
     84 	bool res3;
     85 	status = napi_is_typedarray( env, argv[ 3 ], &res3 );
     86 	assert( status == napi_ok );
     87 	if ( res3 == false ) {
     88 		status = napi_throw_type_error( env, NULL, "invalid argument. Fourth argument must be a Float64Array." );
     89 		assert( status == napi_ok );
     90 		return;
     91 	}
     92 
     93 	napi_valuetype vtype4;
     94 	status = napi_typeof( env, argv[ 4 ], &vtype4 );
     95 	assert( status == napi_ok );
     96 	if ( vtype4 != napi_number ) {
     97 		status = napi_throw_type_error( env, NULL, "invalid argument. Fifth argument must be a number." );
     98 		assert( status == napi_ok );
     99 		return;
    100 	}
    101 
    102 	int64_t N;
    103 	status = napi_get_value_int64( env, argv[ 0 ], &N );
    104 	assert( status == napi_ok );
    105 
    106 	int64_t strideX;
    107 	status = napi_get_value_int64( env, argv[ 2 ], &strideX );
    108 	assert( status == napi_ok );
    109 
    110 	int64_t strideY;
    111 	status = napi_get_value_int64( env, argv[ 4 ], &strideY );
    112 	assert( status == napi_ok );
    113 
    114 	napi_typedarray_type vtype1;
    115 	size_t xlen;
    116 	void *X;
    117 	status = napi_get_typedarray_info( env, argv[ 1 ], &vtype1, &xlen, &X, NULL, NULL );
    118 	assert( status == napi_ok );
    119 	if ( vtype1 != napi_float64_array ) {
    120 		status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a Float64Array." );
    121 		assert( status == napi_ok );
    122 		return;
    123 	}
    124 	if ( (N-1)*llabs(strideX) >= (int64_t)xlen ) {
    125 		status = napi_throw_range_error( env, NULL, "invalid argument. Second argument has insufficient elements based on the associated stride and the number of indexed elements." );
    126 		assert( status == napi_ok );
    127 		return;
    128 	}
    129 
    130 	napi_typedarray_type vtype3;
    131 	size_t ylen;
    132 	void *Y;
    133 	status = napi_get_typedarray_info( env, argv[ 3 ], &vtype3, &ylen, &Y, NULL, NULL );
    134 	assert( status == napi_ok );
    135 	if ( vtype3 != napi_float64_array ) {
    136 		status = napi_throw_type_error( env, NULL, "invalid argument. Fourth argument must be a Float64Array." );
    137 		assert( status == napi_ok );
    138 		return;
    139 	}
    140 	if ( (N-1)*llabs(strideY) >= (int64_t)ylen ) {
    141 		status = napi_throw_range_error( env, NULL, "invalid argument. Fourth argument has insufficient elements based on the associated stride and the number of indexed elements." );
    142 		assert( status == napi_ok );
    143 		return;
    144 	}
    145 
    146 	stdlib_strided_dmap( N, (double *)X, strideX, (double *)Y, strideY, fcn );
    147 
    148 	return;
    149 }