Skip to content

Commit 6ff148b

Browse files
committed
Update example
1 parent 29edb39 commit 6ff148b

File tree

2 files changed

+291
-16
lines changed

2 files changed

+291
-16
lines changed
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2018 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+
# VARIABLES #
20+
21+
ifndef VERBOSE
22+
QUIET := @
23+
else
24+
QUIET :=
25+
endif
26+
27+
# Determine the OS ([1][1], [2][2]).
28+
#
29+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
30+
# [2]: http://stackoverflow.com/a/27776822/2225624
31+
OS ?= $(shell uname)
32+
ifneq (, $(findstring MINGW,$(OS)))
33+
OS := WINNT
34+
else
35+
ifneq (, $(findstring MSYS,$(OS)))
36+
OS := WINNT
37+
else
38+
ifneq (, $(findstring CYGWIN,$(OS)))
39+
OS := WINNT
40+
else
41+
ifneq (, $(findstring Windows_NT,$(OS)))
42+
OS := WINNT
43+
endif
44+
endif
45+
endif
46+
endif
47+
48+
# Determine the absolute path of the Makefile (see http://blog.jgc.org/2007/01/what-makefile-am-i-in.html):
49+
this_dir := $(dir $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))
50+
51+
# Remove the trailing slash:
52+
this_dir := $(patsubst %/,%,$(this_dir))
53+
54+
# Define the command for Node.js:
55+
NODE ?= node
56+
57+
# Define the Node.js directory path.
58+
#
59+
# ## Notes
60+
#
61+
# 1. The directory is expected to contain include header files.
62+
# 2. This only works on Mac and Linux.
63+
NODE_DIR ?= $(shell $(NODE) -e 'console.log( path.resolve( process.execPath, "..", ".." ) );')
64+
65+
# Determine path to NAN header files:
66+
INCLUDE_NAN ?= $(shell node -e 'require( "nan" );')
67+
68+
# Define the program used for compiling C source files:
69+
ifdef C_COMPILER
70+
CC := $(C_COMPILER)
71+
else
72+
CC := gcc
73+
endif
74+
75+
# Define the command-line options when compiling C files:
76+
CFLAGS ?= \
77+
-std=c99 \
78+
-O3 \
79+
-Wall \
80+
-pedantic
81+
82+
# Define the program used for compiling C++ source files:
83+
ifdef CXX_COMPILER
84+
CXX := $(CXX_COMPILER)
85+
else
86+
CXX := g++
87+
endif
88+
89+
# Define the command-line options when compiling C++ files:
90+
CXXFLAGS ?= \
91+
-std=c++11 \
92+
-O3 \
93+
-Wall \
94+
-pedantic
95+
96+
# Determine whether to generate position independent code ([1][1], [2][2]).
97+
#
98+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
99+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
100+
ifeq ($(OS), WINNT)
101+
fPIC ?=
102+
else
103+
fPIC ?= -fPIC
104+
endif
105+
106+
# Define the program for linking compiled files:
107+
ifdef LINKER
108+
LD := $(LINKER)
109+
else
110+
LD := g++
111+
endif
112+
113+
# Define the command-line options when linking compiled files:
114+
ifeq ($(OS), Darwin)
115+
LDFLAGS ?= \
116+
-undefined dynamic_lookup \
117+
-Wl,-no_pie \
118+
-Wl,-search_paths_first
119+
else
120+
LDFLAGS ?=
121+
endif
122+
123+
# List of external includes:
124+
INCLUDE ?=
125+
126+
# List of external source files:
127+
SOURCE_FILES ?=
128+
129+
# List of external object files:
130+
OBJECT_FILES ?=
131+
132+
# List of libraries (e.g., `-lopenblas -lpthread`):
133+
LIBRARIES ?=
134+
135+
# List of library paths (e.g., `-L /foo/bar -L /beep/boop`):
136+
LIBPATH ?=
137+
138+
# List of C targets:
139+
c_objects := $(this_dir)/../../src/binary.o
140+
141+
# List of C++ add-on targets:
142+
addon_objects := addon.o
143+
144+
145+
# RULES #
146+
147+
#/
148+
# Compiles source files.
149+
#
150+
# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
151+
# @param {string} [CXX_COMPILER] - C++ compiler (e.g., `gcc`)
152+
# @param {string} [LINKER] - program for linking compiled files (e.g., `g++`)
153+
# @param {string} [CFLAGS] - C compiler options
154+
# @param {string} [CXXFLAGS] - C++ compiler options
155+
# @param {string} [LDFLAGS] - linker options
156+
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
157+
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
158+
# @param {string} [SOURCE_FILES] - list of external source files
159+
# @param {string} [OBJECT_FILES] - list of external object files
160+
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
161+
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`)
162+
#
163+
# @example
164+
# make
165+
#
166+
# @example
167+
# make all
168+
#/
169+
all: $(c_objects) $(addon_objects) addon.node
170+
171+
.PHONY: all
172+
173+
#/
174+
# Compiles C source files.
175+
#
176+
# @private
177+
# @param {string} CC - C compiler (e.g., `gcc`)
178+
# @param {string} CFLAGS - C compiler options
179+
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
180+
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
181+
#/
182+
$(c_objects): %.o: %.c
183+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -I ../../include -c -o $@ $<
184+
185+
#/
186+
# Compiles Node.js native add-ons.
187+
#
188+
# @private
189+
# @param {string} CXX - C++ compiler (e.g., `g++`)
190+
# @param {string} CXXFLAGS - C++ compiler options
191+
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
192+
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop/`)
193+
# @param {string} INCLUDE_NAN - directory containing NAN header files
194+
# @param {string} NODE_DIR - Node.js directory path
195+
#/
196+
$(addon_objects): %.o: %.cpp
197+
$(QUIET) $(CXX) \
198+
$(CXXFLAGS) \
199+
$(fPIC) \
200+
-I "$(NODE_DIR)/include/node" \
201+
-I "$(NODE_DIR)/src" \
202+
-I "$(NODE_DIR)/deps/uv/include" \
203+
-I "$(NODE_DIR)/deps/v8/include" \
204+
-I "$(INCLUDE_NAN)" \
205+
$(INCLUDE) \
206+
-I ../../include \
207+
-c \
208+
-o $@ \
209+
$<
210+
211+
#/
212+
# Creates a Node.js add-on shared object which can be linked to by other libraries and executables.
213+
#
214+
# @param {string} [LINKER] - program for linking compiled files (e.g., `g++`)
215+
# @param {string} [LDFLAGS] - linker flags
216+
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
217+
# @param {string} [OBJECT_FILES] - list of external object files to link
218+
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar`)
219+
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas`)
220+
#
221+
# @example
222+
# make addon.node
223+
#/
224+
addon.node: $(addon_objects)
225+
$(QUIET) $(LD) -shared $(LDFLAGS) $(fPIC) -o $@ $(OBJECT_FILES) $(c_objects) $< $(LIBPATH) $(LIBRARIES)
226+
227+
#/
228+
# Runs compiled examples.
229+
#
230+
# @example
231+
# make run
232+
#/
233+
run: addon.node
234+
$(QUIET) $(NODE) -e "var add = require( '$(this_dir)/addon.node' ).add; var x = new Float64Array( [0, 1, 2, 3, 4] ); var y = new Float64Array( [5, 6, 7, 8] ); var z = new Float64Array( x.length ); add( 5, x, 1, y, 1, z, 1 ); console.log( z );"
235+
236+
.PHONY: run
237+
238+
#/
239+
# Removes generated Node.js add-on files.
240+
#
241+
# @example
242+
# make clean-addon
243+
#/
244+
clean-addon:
245+
$(QUIET) -rm -f *.o *.node
246+
247+
.PHONY: clean-addon
248+
249+
#/
250+
# Removes generated files.
251+
#
252+
# @example
253+
# make clean
254+
#/
255+
clean: clean-addon
256+
257+
.PHONY: clean

lib/node_modules/@stdlib/strided/common/examples/addon/addon.cpp

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
#include <stdint.h>
2323
#include <nan.h>
24-
#include "stdlib_strided.h"
24+
#include "stdlib/strided_binary.h"
2525

2626
/**
2727
* Add-on namespace.
@@ -32,9 +32,20 @@ namespace addon_strided_add {
3232
using Nan::TypedArrayContents;
3333
using Nan::ThrowTypeError;
3434
using Nan::ThrowError;
35-
using v8::Number;
36-
using v8::Local;
3735
using v8::Value;
36+
using v8::Float64Array;
37+
38+
/**
39+
* Adds two doubles.
40+
*
41+
* @private
42+
* @param x first double
43+
* @param y second double
44+
* @return sum
45+
*/
46+
double add( double x, double y ) {
47+
return x + y;
48+
}
3849

3950
/**
4051
* Adds each element in `X` to a corresponding element in `Y` and assigns the result to an element in `Z`.
@@ -53,8 +64,8 @@ namespace addon_strided_add {
5364
*/
5465
void node_add( const FunctionCallbackInfo<Value>& info ) {
5566
uint8_t bytesPerElement;
56-
uint64_t byteLength;
57-
uint64_t len;
67+
size_t byteLength;
68+
size_t len;
5869

5970
int64_t strides[ 3 ];
6071
int64_t dims[ 1 ];
@@ -95,31 +106,38 @@ namespace addon_strided_add {
95106
TypedArrayContents<uint8_t> Z( info[ 5 ] );
96107

97108
// Convert the strides from units of elements to units of bytes...
98-
byteLength = (uint64_t)( X->ByteLength() ); // total bytes in view
99-
len = (uint64_t)( X->Length() ); // number of view elements
109+
if ( info[ 1 ]->IsFloat64Array() ) {
110+
byteLength = (uint64_t)( info[ 1 ].As<Float64Array>()->ByteLength() ); // total bytes in view
111+
}
112+
len = (uint64_t)( X.length() ); // number of view elements
100113
bytesPerElement = (uint8_t)( byteLength / len ); // should evenly divide
101114
strides[ 0 ] *= bytesPerElement; // stride in units of bytes
102115

103-
byteLength = (uint64_t)( Y->ByteLength() );
104-
len = (uint64_t)( Y->Length() );
116+
if ( info[ 3 ]->IsFloat64Array() ) {
117+
byteLength = (uint64_t)( info[ 3 ].As<Float64Array>()->ByteLength() );
118+
}
119+
len = (uint64_t)( Y.length() );
105120
bytesPerElement = (uint8_t)( byteLength / len );
106121
strides[ 1 ] *= bytesPerElement;
107122

108-
byteLength = (uint64_t)( Z->ByteLength() );
109-
len = (uint64_t)( Z->Length() );
123+
if ( info[ 5 ]->IsFloat64Array() ) {
124+
byteLength = (uint64_t)( info[ 5 ].As<Float64Array>()->ByteLength() );
125+
}
126+
len = (uint64_t)( Z.length() );
110127
bytesPerElement = (uint8_t)( byteLength / len );
111128
strides[ 2 ] *= bytesPerElement;
112129

113130
// Add the arrays to our array of array pointers:
114-
arrays[ 0 ] = X;
115-
arrays[ 1 ] = Y;
116-
arrays[ 2 ] = Z;
131+
arrays[ 0 ] = *X;
132+
arrays[ 1 ] = *Y;
133+
arrays[ 2 ] = *Z;
117134

118-
//
135+
// Perform addition:
136+
stdlib_strided_dd_d( arrays, dims, strides, add );
119137
}
120138

121139
NAN_MODULE_INIT( Init ) {
122-
Nan::Export( target, "addd", node_add );
140+
Nan::Export( target, "add", node_add );
123141
}
124142

125143
NODE_MODULE( addon, Init )

0 commit comments

Comments
 (0)