File: addon.cpp

package info (click to toggle)
node-stdlib 0.0.96%2Bds1%2B~cs0.0.429-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 421,476 kB
  • sloc: javascript: 1,562,831; ansic: 109,702; lisp: 49,823; cpp: 27,224; python: 7,871; sh: 6,807; makefile: 6,089; fortran: 3,102; awk: 387
file content (218 lines) | stat: -rw-r--r-- 6,781 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "stdlib/strided/common/binary.h"
#include <node_api.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

/**
* Adds two doubles.
*
* @private
* @param x  first double
* @param y  second double
* @return   sum
*/
static inline double add( double x, double y ) {
	return x + y;
}

/**
* Add-on namespace.
*/
namespace addon_strided_add {

	/**
	* Adds each element in `X` to a corresponding element in `Y` and assigns the result to an element in `Z`.
	*
	* ## Notes
	*
	* -   When called from JavaScript, the function expects the following arguments:
	*
	*     -   `N`: number of indexed elements
	*     -   `X`: input array
	*     -   `strideX`: `X` stride length
	*     -   `Y`: input array
	*     -   `strideY`: `Y` stride length
	*     -   `Z`: destination array
	*     -   `strideZ`: `Z` stride length
	*/
	 napi_value node_add( napi_env env, napi_callback_info info ) {
		napi_status status;
		uint8_t *arrays[ 3 ];
		int64_t strides[ 3 ];
		int64_t shape[ 1 ];

		size_t argc = 7;
		napi_value argv[ 7 ];
		status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr );
		assert( status == napi_ok );

		if ( argc < 7 ) {
			napi_throw_error( env, nullptr, "invalid invocation. Must provide 7 arguments." );
			return nullptr;
		}

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

		bool res1;
		status = napi_is_typedarray( env, argv[ 1 ], &res1 );
		assert( status == napi_ok );
		if ( res1 == false ) {
			napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float64Array." );
			return nullptr;
		}

		napi_valuetype vtype2;
		status = napi_typeof( env, argv[ 2 ], &vtype2 );
		assert( status == napi_ok );
		if ( vtype2 != napi_number ) {
			napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a number." );
			return nullptr;
		}

		bool res3;
		status = napi_is_typedarray( env, argv[ 3 ], &res3 );
		assert( status == napi_ok );
		if ( res3 == false ) {
			napi_throw_type_error( env, nullptr, "invalid argument. Fourth argument must be a Float64Array." );
			return nullptr;
		}

		napi_valuetype vtype4;
		status = napi_typeof( env, argv[ 4 ], &vtype4 );
		assert( status == napi_ok );
		if ( vtype4 != napi_number ) {
			napi_throw_type_error( env, nullptr, "invalid argument. Fifth argument must be a number." );
			return nullptr;
		}

		bool res5;
		status = napi_is_typedarray( env, argv[ 5 ], &res5 );
		assert( status == napi_ok );
		if ( res5 == false ) {
			napi_throw_type_error( env, nullptr, "invalid argument. Sixth argument must be a Float64Array." );
			return nullptr;
		}

		napi_valuetype vtype6;
		status = napi_typeof( env, argv[ 6 ], &vtype6 );
		assert( status == napi_ok );
		if ( vtype6 != napi_number ) {
			napi_throw_type_error( env, nullptr, "invalid argument. Seventh argument must be a number." );
			return nullptr;
		}

		// Retrieve the number of elements:
		int64_t N;
		status = napi_get_value_int64( env, argv[ 0 ], &N );
		assert( status == napi_ok );

		shape[ 0 ] = N;

		// Retrieve the strides:
		int64_t strideX;
		status = napi_get_value_int64( env, argv[ 2 ], &strideX );
		assert( status == napi_ok );

		int64_t strideY;
		status = napi_get_value_int64( env, argv[ 4 ], &strideY );
		assert( status == napi_ok );

		int64_t strideZ;
		status = napi_get_value_int64( env, argv[ 6 ], &strideZ );
		assert( status == napi_ok );

		// Convert the strides from units of elements to units of bytes:
		strides[ 0 ] = strideX * 8; // 8 bytes per double
		strides[ 1 ] = strideY * 8;
		strides[ 2 ] = strideZ * 8;

		// Get the underlying byte arrays:
		napi_typedarray_type vtype1;
		size_t xlen;
		void *X;
		status = napi_get_typedarray_info( env, argv[ 1 ], &vtype1, &xlen, &X, nullptr, nullptr );
		assert( status == napi_ok );
		if ( vtype1 != napi_float64_array ) {
			napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float64Array." );
			return nullptr;
		}
		if ( (N-1)*llabs(strideX) >= (int64_t)xlen ) {
			napi_throw_range_error( env, nullptr, "invalid argument. Second argument has insufficient elements based on the associated stride and the number of indexed elements." );
			return nullptr;
		}

		napi_typedarray_type vtype3;
		size_t ylen;
		void *Y;
		status = napi_get_typedarray_info( env, argv[ 3 ], &vtype3, &ylen, &Y, nullptr, nullptr );
		assert( status == napi_ok );
		if ( vtype3 != napi_float64_array ) {
			napi_throw_type_error( env, nullptr, "invalid argument. Fourth argument must be a Float64Array." );
			return nullptr;
		}
		if ( (N-1)*llabs(strideY) >= (int64_t)ylen ) {
			napi_throw_range_error( env, nullptr, "invalid argument. Fourth argument has insufficient elements based on the associated stride and the number of indexed elements." );
			return nullptr;
		}

		napi_typedarray_type vtype5;
		size_t zlen;
		void *Z;
		status = napi_get_typedarray_info( env, argv[ 5 ], &vtype5, &zlen, &Z, nullptr, nullptr );
		assert( status == napi_ok );
		if ( vtype5 != napi_float64_array ) {
			napi_throw_type_error( env, nullptr, "invalid argument. Sixth argument must be a Float64Array." );
			return nullptr;
		}
		if ( (N-1)*llabs(strideZ) >= (int64_t)zlen ) {
			napi_throw_range_error( env, nullptr, "invalid argument. Sixth argument has insufficient elements based on the associated stride and the number of indexed elements." );
			return nullptr;
		}

		// Add the arrays to our array of array pointers:
		arrays[ 0 ] = (uint8_t *)X;
		arrays[ 1 ] = (uint8_t *)Y;
		arrays[ 2 ] = (uint8_t *)Z;

		// Perform addition:
		stdlib_strided_dd_d( arrays, shape, strides, (void *)add );

		return nullptr;
	}

	napi_value Init( napi_env env, napi_value exports ) {
		napi_status status;
		napi_value fcn;
		status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_add, NULL, &fcn );
		assert( status == napi_ok );
		return fcn;
	}

	NAPI_MODULE( NODE_GYP_MODULE_NAME, Init )
} // end namespace addon_strided_add