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