time-to-botec

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

addon.cpp (4832B)


      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/blas/base/sdot.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_blas_base_sdot {
     30 
     31 	/**
     32 	* Computes the dot product of two single-precision floating-point vectors.
     33 	*
     34 	* ## Notes
     35 	*
     36 	* -   When called from JavaScript, the function expects five arguments:
     37 	*
     38 	*     -   `N`: number of indexed elements
     39 	*     -   `X`: input array
     40 	*     -   `strideX`: `X` stride length
     41 	*     -   `Y`: destination array
     42 	*     -   `strideY`: `Y` stride length
     43 	*/
     44 	napi_value node_sdot( napi_env env, napi_callback_info info ) {
     45 		napi_status status;
     46 
     47 		size_t argc = 5;
     48 		napi_value argv[ 5 ];
     49 		status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr );
     50 		assert( status == napi_ok );
     51 
     52 		if ( argc < 5 ) {
     53 			napi_throw_error( env, nullptr, "invalid invocation. Must provide 5 arguments." );
     54 			return nullptr;
     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 			napi_throw_type_error( env, nullptr, "invalid argument. First argument must be a number." );
     62 			return nullptr;
     63 		}
     64 
     65 		bool res1;
     66 		status = napi_is_typedarray( env, argv[ 1 ], &res1 );
     67 		assert( status == napi_ok );
     68 		if ( res1 == false ) {
     69 			napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float32Array." );
     70 			return nullptr;
     71 		}
     72 
     73 		napi_valuetype vtype2;
     74 		status = napi_typeof( env, argv[ 2 ], &vtype2 );
     75 		assert( status == napi_ok );
     76 		if ( vtype2 != napi_number ) {
     77 			napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a number." );
     78 			return nullptr;
     79 		}
     80 
     81 		bool res3;
     82 		status = napi_is_typedarray( env, argv[ 3 ], &res3 );
     83 		assert( status == napi_ok );
     84 		if ( res3 == false ) {
     85 			napi_throw_type_error( env, nullptr, "invalid argument. Fourth argument must be a Float32Array." );
     86 			return nullptr;
     87 		}
     88 
     89 		napi_valuetype vtype4;
     90 		status = napi_typeof( env, argv[ 4 ], &vtype4 );
     91 		assert( status == napi_ok );
     92 		if ( vtype4 != napi_number ) {
     93 			napi_throw_type_error( env, nullptr, "invalid argument. Fifth argument must be a number." );
     94 			return nullptr;
     95 		}
     96 
     97 		int64_t N;
     98 		status = napi_get_value_int64( env, argv[ 0 ], &N );
     99 		assert( status == napi_ok );
    100 
    101 		int64_t strideX;
    102 		status = napi_get_value_int64( env, argv[ 2 ], &strideX );
    103 		assert( status == napi_ok );
    104 
    105 		int64_t strideY;
    106 		status = napi_get_value_int64( env, argv[ 4 ], &strideY );
    107 		assert( status == napi_ok );
    108 
    109 		napi_typedarray_type vtype1;
    110 		size_t xlen;
    111 		void *X;
    112 		status = napi_get_typedarray_info( env, argv[ 1 ], &vtype1, &xlen, &X, nullptr, nullptr );
    113 		assert( status == napi_ok );
    114 		if ( vtype1 != napi_float32_array ) {
    115 			napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float32Array." );
    116 			return nullptr;
    117 		}
    118 		if ( (N-1)*llabs(strideX) >= (int64_t)xlen ) {
    119 			napi_throw_range_error( env, nullptr, "invalid argument. Second argument has insufficient elements based on the associated stride and the number of indexed elements." );
    120 			return nullptr;
    121 		}
    122 
    123 		napi_typedarray_type vtype3;
    124 		size_t ylen;
    125 		void *Y;
    126 		status = napi_get_typedarray_info( env, argv[ 3 ], &vtype3, &ylen, &Y, nullptr, nullptr );
    127 		assert( status == napi_ok );
    128 		if ( vtype3 != napi_float32_array ) {
    129 			napi_throw_type_error( env, nullptr, "invalid argument. Fourth argument must be a Float32Array." );
    130 			return nullptr;
    131 		}
    132 		if ( (N-1)*llabs(strideY) >= (int64_t)ylen ) {
    133 			napi_throw_range_error( env, nullptr, "invalid argument. Fourth argument has insufficient elements based on the associated stride and the number of indexed elements." );
    134 			return nullptr;
    135 		}
    136 
    137 		napi_value v;
    138 		status = napi_create_double( env, (double)c_sdot( N, (float *)X, strideX, (float *)Y, strideY ), &v );
    139 		assert( status == napi_ok );
    140 
    141 		return v;
    142 	}
    143 
    144 	napi_value Init( napi_env env, napi_value exports ) {
    145 		napi_status status;
    146 		napi_value fcn;
    147 		status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_sdot, NULL, &fcn );
    148 		assert( status == napi_ok );
    149 		return fcn;
    150 	}
    151 
    152 	NAPI_MODULE( NODE_GYP_MODULE_NAME, Init )
    153 } // end namespace stdlib_blas_base_sdot