Skip to content

Commit 4cd557a

Browse files
committed
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into develop
2 parents 484ecaa + 62b461e commit 4cd557a

64 files changed

Lines changed: 3623 additions & 104 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/node_modules/@stdlib/math/base/assert/is-nan/include/isnan.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
2-
* Header file containing function declarations for computing the hypotenuse while avoiding overflow and underflow.
2+
* Header file containing function declarations for testing if a numeric value is `NaN`.
33
*/
4+
#include <stdbool.h>
5+
46
#ifndef STDLIB_ISNAN_H
57
#define STDLIB_ISNAN_H
68

lib/node_modules/@stdlib/math/base/assert/is-nan/src/Makefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ else
123123
LDFLAGS ?=
124124
endif
125125

126+
# List of external includes:
127+
INCLUDE ?=
128+
129+
# List of external source files:
130+
SOURCE_FILES ?=
131+
132+
# List of external object files:
133+
OBJECT_FILES ?=
134+
126135
# List of C targets:
127136
c_objects := isnan.o
128137

@@ -152,7 +161,7 @@ all: $(c_objects) $(addon_objects) addon.node wasm
152161
# This target compiles C source files.
153162

154163
$(c_objects): %.o: %.c
155-
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -I ../include -c -o $@ $<
164+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -I ../include -c -o $@ $<
156165

157166

158167
# Compile add-ons.
@@ -168,6 +177,7 @@ $(addon_objects): %.o: %.cpp
168177
-I "$(NODE_DIR)/deps/uv/include" \
169178
-I "$(NODE_DIR)/deps/v8/include" \
170179
-I "$(INCLUDE_NAN)" \
180+
$(INCLUDE) \
171181
-I ../include \
172182
-c \
173183
-o $@ \
@@ -183,8 +193,10 @@ $(wasm_objects): %.wasm: %.c
183193
$(EMCCFLAGS) \
184194
$(EMCC_WASM_FLAGS) \
185195
$(fPIC) \
196+
$(INCLUDE) \
186197
-I ../include \
187198
-o $*.js \
199+
$(SOURCE_FILES) \
188200
$<
189201
$(QUIET) rm -f $*.js
190202

@@ -204,7 +216,7 @@ $(wat_objects): %.wat: %.wasm
204216
# This target generates a Node.js add-on by creating a shared object which can be linked to by other libraries and executables.
205217

206218
addon.node: $(addon_objects)
207-
$(QUIET) $(LD) -shared $(LDFLAGS) $(fPIC) -o $@ $(c_objects) $<
219+
$(QUIET) $(LD) -shared $(LDFLAGS) $(fPIC) -o $@ $(OBJECT_FILES) $(c_objects) $<
208220

209221

210222
# Generate WebAssembly.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var resolve = require( 'path' ).resolve;
6+
var bench = require( '@stdlib/bench' );
7+
var randu = require( '@stdlib/random/base/randu' );
8+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
9+
var tryRequire = require( '@stdlib/utils/try-require' );
10+
var pkg = require( './../package.json' ).name;
11+
12+
13+
// VARIABLES //
14+
15+
var isNegativeZero = tryRequire( resolve( __dirname, './../lib/native.js' ) );
16+
var opts = {
17+
'skip': ( isNegativeZero instanceof Error )
18+
};
19+
20+
21+
// MAIN //
22+
23+
bench( pkg+'::native', opts, function benchmark( b ) {
24+
var x;
25+
var y;
26+
var i;
27+
28+
b.tic();
29+
for ( i = 0; i < b.iterations; i++ ) {
30+
x = ( randu()*1.0e7 ) - 5.0e6;
31+
y = isNegativeZero( x );
32+
if ( !isBoolean( y ) ) {
33+
b.fail( 'should return a boolean' );
34+
}
35+
}
36+
b.toc();
37+
if ( !isBoolean( y ) ) {
38+
b.fail( 'should return a boolean' );
39+
}
40+
b.pass( 'benchmark finished' );
41+
b.end();
42+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var hasWebAssemblySupport = require( '@stdlib/utils/detect-wasm-support' );
7+
var randu = require( '@stdlib/random/base/randu' );
8+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
9+
var pkg = require( './../package.json' ).name;
10+
var isNegativeZero = require( './../lib/wasm.js' );
11+
12+
13+
// VARIABLES //
14+
15+
var opts = {
16+
'skip': !hasWebAssemblySupport()
17+
};
18+
19+
20+
// MAIN //
21+
22+
bench( pkg+'::wasm', opts, function benchmark( b ) {
23+
var x;
24+
var y;
25+
var i;
26+
27+
b.tic();
28+
for ( i = 0; i < b.iterations; i++ ) {
29+
x = ( randu()*1.0e7 ) - 5.0e6;
30+
y = isNegativeZero( x );
31+
if ( !isBoolean( y ) ) {
32+
b.fail( 'should return a boolean' );
33+
}
34+
}
35+
b.toc();
36+
if ( !isBoolean( y ) ) {
37+
b.fail( 'should return a boolean' );
38+
}
39+
b.pass( 'benchmark finished' );
40+
b.end();
41+
});
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
# VARIABLES #
3+
4+
ifndef VERBOSE
5+
QUIET := @
6+
endif
7+
8+
# Determine the OS:
9+
#
10+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
11+
# [2]: http://stackoverflow.com/a/27776822/2225624
12+
OS ?= $(shell uname)
13+
ifneq (, $(findstring MINGW,$(OS)))
14+
OS := WINNT
15+
else
16+
ifneq (, $(findstring MSYS,$(OS)))
17+
OS := WINNT
18+
else
19+
ifneq (, $(findstring CYGWIN,$(OS)))
20+
OS := WINNT
21+
endif
22+
endif
23+
endif
24+
25+
# Define the program used for compiling C source files:
26+
ifdef C_COMPILER
27+
CC := $(C_COMPILER)
28+
else
29+
CC := gcc
30+
endif
31+
32+
# Define the command-line options when compiling C files:
33+
CFLAGS ?= \
34+
-std=c99 \
35+
-O3 \
36+
-Wall \
37+
-pedantic
38+
39+
# Determine whether to generate [position independent code][1]:
40+
#
41+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
42+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
43+
ifeq ($(OS), WINNT)
44+
fPIC ?=
45+
else
46+
fPIC ?= -fPIC
47+
endif
48+
49+
# List of C targets:
50+
c_targets := benchmark.out
51+
52+
53+
# TARGETS #
54+
55+
# Default target.
56+
#
57+
# This target is the default target.
58+
59+
all: $(c_targets)
60+
61+
.PHONY: all
62+
63+
64+
# Compile C source.
65+
#
66+
# This target compiles C source files.
67+
68+
$(c_targets): %.out: %.c
69+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $< -lm
70+
71+
72+
# Run a benchmark.
73+
#
74+
# This target runs a benchmark.
75+
76+
run: $(c_targets)
77+
$(QUIET) ./$<
78+
79+
.PHONY: run
80+
81+
82+
# Perform clean-up.
83+
#
84+
# This target removes generated files.
85+
86+
clean:
87+
$(QUIET) -rm -f *.o *.out
88+
89+
.PHONY: clean
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* Benchmark `is-negative-zero`.
3+
*/
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
#include <stdbool.h>
7+
#include <math.h>
8+
#include <sys/time.h>
9+
10+
#define NAME "is-negative-zero"
11+
#define ITERATIONS 1000000
12+
#define REPEATS 3
13+
14+
/**
15+
* Prints the TAP version.
16+
*/
17+
void print_version() {
18+
printf( "TAP version 13\n" );
19+
}
20+
21+
/**
22+
* Prints the TAP summary.
23+
*
24+
* @param total total number of tests
25+
* @param passing total number of passing tests
26+
*/
27+
void print_summary( int total, int passing ) {
28+
printf( "#\n" );
29+
printf( "1..%d\n", total ); // TAP plan
30+
printf( "# total %d\n", total );
31+
printf( "# pass %d\n", passing );
32+
printf( "#\n" );
33+
printf( "# ok\n" );
34+
}
35+
36+
/**
37+
* Prints benchmarks results.
38+
*
39+
* @param elapsed elapsed time in seconds
40+
*/
41+
void print_results( double elapsed ) {
42+
double rate = (double)ITERATIONS / elapsed;
43+
printf( " ---\n" );
44+
printf( " iterations: %d\n", ITERATIONS );
45+
printf( " elapsed: %0.9f\n", elapsed );
46+
printf( " rate: %0.9f\n", rate );
47+
printf( " ...\n" );
48+
}
49+
50+
/**
51+
* Returns a clock time.
52+
*
53+
* @return clock time
54+
*/
55+
double tic() {
56+
struct timeval now;
57+
gettimeofday( &now, NULL );
58+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
59+
}
60+
61+
/**
62+
* Generates a random double on the interval [0,1].
63+
*
64+
* @return random double
65+
*/
66+
double rand_double() {
67+
int r = rand();
68+
return (double)r / ( (double)RAND_MAX + 1.0 );
69+
}
70+
71+
/**
72+
* Tests if a numeric value is negative zero.
73+
*
74+
* @param x value to test
75+
* @returns boolean indicating if a numeric value is negative zero
76+
*/
77+
bool is_negative_zero( double x ) {
78+
return ( 1.0/x == -INFINITY );
79+
}
80+
81+
/**
82+
* Runs a benchmark.
83+
*
84+
* @return elapsed time in seconds
85+
*/
86+
double benchmark() {
87+
double elapsed;
88+
double x;
89+
double t;
90+
bool y;
91+
int i;
92+
93+
t = tic();
94+
for ( i = 0; i < ITERATIONS; i++ ) {
95+
x = ( 1.0e7*rand_double() ) - 5.0e6;
96+
y = is_negative_zero( x );
97+
if ( y != true && y != false ) {
98+
printf( "should return true or false\n" );
99+
break;
100+
}
101+
}
102+
elapsed = tic() - t;
103+
if ( y != true && y != false ) {
104+
printf( "should return true or false\n" );
105+
}
106+
return elapsed;
107+
}
108+
109+
/**
110+
* Main execution sequence.
111+
*/
112+
int main( void ) {
113+
double elapsed;
114+
int i;
115+
116+
// Use the current time to seed the random number generator:
117+
srand( time( NULL ) );
118+
119+
print_version();
120+
for ( i = 0; i < REPEATS; i++ ) {
121+
printf( "# c::%s\n", NAME );
122+
elapsed = benchmark();
123+
print_results( elapsed );
124+
printf( "ok %d benchmark finished\n", i+1 );
125+
}
126+
print_summary( REPEATS, REPEATS );
127+
}

0 commit comments

Comments
 (0)