time-to-botec

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

main.c (3540B)


      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/mskunary.h"
     20 #include "stdlib/strided/base/function_object.h"
     21 #include "stdlib/strided/napi/addon_arguments.h"
     22 #include <node_api.h>
     23 #include <stdint.h>
     24 #include <assert.h>
     25 
     26 /**
     27 * Invokes a strided array interface which applies a unary callback to an input strided array according to a mask strided array and based on provided JavaScript arguments.
     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 *     -   `Mask`: mask array
     37 *     -   `strideMask`: `Mask` stride length
     38 *     -   `Y`: destination array
     39 *     -   `strideY`: `Y` stride length
     40 *
     41 * @param env    environment under which the function is invoked
     42 * @param info   callback data
     43 * @param obj    strided array function object
     44 */
     45 void stdlib_strided_napi_mskunary( napi_env env, napi_callback_info info, const struct StridedFunctionObject *obj ) {
     46 	napi_status status;
     47 
     48 	// Total number of input arguments:
     49 	int64_t nargs = 7;
     50 
     51 	// Number of input strided array arguments (including the mask strided array):
     52 	int64_t nin = 2;
     53 
     54 	// Get callback arguments:
     55 	size_t argc = 7;
     56 	napi_value argv[ 7 ];
     57 	status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
     58 	assert( status == napi_ok );
     59 
     60 	// Check whether we were provided the correct number of arguments:
     61 	int64_t argc64 = (int64_t)argc;
     62 	if ( argc64 < nargs ) {
     63 		status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
     64 		assert( status == napi_ok );
     65 		return;
     66 	}
     67 	if ( argc64 > nargs ) {
     68 		status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
     69 		assert( status == napi_ok );
     70 		return;
     71 	}
     72 	// Process the provided arguments:
     73 	uint8_t *arrays[ 3 ];
     74 	int64_t strides[ 3 ];
     75 	int64_t shape[ 1 ];
     76 	int32_t types[ 3 ];
     77 
     78 	napi_value err;
     79 	status = stdlib_strided_napi_addon_arguments( env, argv, nargs, nin, arrays, shape, strides, types, &err );
     80 	assert( status == napi_ok );
     81 
     82 	// Check whether processing was successful:
     83 	if ( err != NULL ) {
     84 		status = napi_throw( env, err );
     85 		assert( status == napi_ok );
     86 		return;
     87 	}
     88 	// Resolve the strided array function satisfying the input array types:
     89 	int64_t idx = stdlib_strided_function_dispatch_index_of( obj, types );
     90 
     91 	// Check whether we were able to successfully resolve a strided array function:
     92 	if ( idx < 0 ) {
     93 		status = napi_throw_type_error( env, NULL, "invalid arguments. Unable to resolve a strided array function supporting the provided array argument data types." );
     94 		assert( status == napi_ok );
     95 		return;
     96 	}
     97 	// Retrieve the strided array function:
     98 	StridedArrayFcn fcn = obj->functions[ idx ];
     99 
    100 	// Retrieve the associated function data:
    101 	void *clbk = obj->data[ idx ];
    102 
    103 	// Evaluate the strided array function:
    104 	fcn( arrays, shape, strides, clbk );
    105 
    106 	return;
    107 }