Skip to content

Commit 456903f

Browse files
committed
Add native implementation
1 parent 4c57101 commit 456903f

File tree

22 files changed

+1437
-8
lines changed

22 files changed

+1437
-8
lines changed

lib/node_modules/@stdlib/math/base/assert/is-finite/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# isfinite
2222

23-
> Test if a numeric value is finite.
23+
> Test if a double-precision floating-point numeric value is finite.
2424
2525
<section class="usage">
2626

@@ -32,7 +32,7 @@ var isfinite = require( '@stdlib/math/base/assert/is-finite' );
3232

3333
#### isfinite( x )
3434

35-
Tests if a `numeric` value is finite.
35+
Tests if a double-precision floating-point `numeric` value is finite.
3636

3737
```javascript
3838
var bool = isfinite( 3.14 );
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var isfinite = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( isfinite instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
x = ( randu()*1.0e7 ) - 5.0e6;
49+
y = isfinite( x );
50+
if ( !isBoolean( y ) ) {
51+
b.fail( 'should return a boolean' );
52+
}
53+
}
54+
b.toc();
55+
if ( !isBoolean( y ) ) {
56+
b.fail( 'should return a boolean' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
# 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+
# Define the program used for compiling C source files:
49+
ifdef C_COMPILER
50+
CC := $(C_COMPILER)
51+
else
52+
CC := gcc
53+
endif
54+
55+
# Define the command-line options when compiling C files:
56+
CFLAGS ?= \
57+
-std=c99 \
58+
-O3 \
59+
-Wall \
60+
-pedantic
61+
62+
# Determine whether to generate position independent code ([1][1], [2][2]).
63+
#
64+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
65+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
66+
ifeq ($(OS), WINNT)
67+
fPIC ?=
68+
else
69+
fPIC ?= -fPIC
70+
endif
71+
72+
# List of C targets:
73+
c_targets := benchmark.out
74+
75+
76+
# RULES #
77+
78+
#/
79+
# Compiles C source files.
80+
#
81+
# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
82+
# @param {string} [CFLAGS] - C compiler options
83+
# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code (e.g., `-fPIC`)
84+
#
85+
# @example
86+
# make
87+
#
88+
# @example
89+
# make all
90+
#/
91+
all: $(c_targets)
92+
93+
.PHONY: all
94+
95+
#/
96+
# Compiles C source files.
97+
#
98+
# @private
99+
# @param {string} CC - C compiler
100+
# @param {string} CFLAGS - C compiler flags
101+
# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code
102+
#/
103+
$(c_targets): %.out: %.c
104+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $< -lm
105+
106+
#/
107+
# Runs compiled benchmarks.
108+
#
109+
# @example
110+
# make run
111+
#/
112+
run: $(c_targets)
113+
$(QUIET) ./$<
114+
115+
.PHONY: run
116+
117+
#/
118+
# Removes generated files.
119+
#
120+
# @example
121+
# make clean
122+
#/
123+
clean:
124+
$(QUIET) -rm -f *.o *.out
125+
126+
.PHONY: clean
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
/**
20+
* Benchmark `isfinite`.
21+
*/
22+
#include <stdlib.h>
23+
#include <stdio.h>
24+
#include <math.h>
25+
#include <time.h>
26+
#include <sys/time.h>
27+
28+
#define NAME "isfinite"
29+
#define ITERATIONS 1000000
30+
#define REPEATS 3
31+
32+
/**
33+
* Prints the TAP version.
34+
*/
35+
void print_version() {
36+
printf( "TAP version 13\n" );
37+
}
38+
39+
/**
40+
* Prints the TAP summary.
41+
*
42+
* @param total total number of tests
43+
* @param passing total number of passing tests
44+
*/
45+
void print_summary( int total, int passing ) {
46+
printf( "#\n" );
47+
printf( "1..%d\n", total ); // TAP plan
48+
printf( "# total %d\n", total );
49+
printf( "# pass %d\n", passing );
50+
printf( "#\n" );
51+
printf( "# ok\n" );
52+
}
53+
54+
/**
55+
* Prints benchmarks results.
56+
*
57+
* @param elapsed elapsed time in seconds
58+
*/
59+
void print_results( double elapsed ) {
60+
double rate = (double)ITERATIONS / elapsed;
61+
printf( " ---\n" );
62+
printf( " iterations: %d\n", ITERATIONS );
63+
printf( " elapsed: %0.9f\n", elapsed );
64+
printf( " rate: %0.9f\n", rate );
65+
printf( " ...\n" );
66+
}
67+
68+
/**
69+
* Returns a clock time.
70+
*
71+
* @return clock time
72+
*/
73+
double tic() {
74+
struct timeval now;
75+
gettimeofday( &now, NULL );
76+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
77+
}
78+
79+
/**
80+
* Generates a random double on the interval [0,1].
81+
*
82+
* @return random double
83+
*/
84+
double rand_double() {
85+
int r = rand();
86+
return (double)r / ( (double)RAND_MAX + 1.0 );
87+
}
88+
89+
/**
90+
* Runs a benchmark.
91+
*
92+
* @return elapsed time in seconds
93+
*/
94+
double benchmark() {
95+
double elapsed;
96+
double x;
97+
double t;
98+
int y;
99+
int i;
100+
101+
t = tic();
102+
for ( i = 0; i < ITERATIONS; i++ ) {
103+
x = ( 1.0e7*rand_double() ) - 5.0e6;
104+
y = isfinite( x );
105+
if ( y != 0 && y != 1 ) {
106+
printf( "should return 0 or 1\n" );
107+
break;
108+
}
109+
}
110+
elapsed = tic() - t;
111+
if ( y != 0 && y != 1 ) {
112+
printf( "should return 0 or 1\n" );
113+
}
114+
return elapsed;
115+
}
116+
117+
/**
118+
* Main execution sequence.
119+
*/
120+
int main( void ) {
121+
double elapsed;
122+
int i;
123+
124+
// Use the current time to seed the random number generator:
125+
srand( time( NULL ) );
126+
127+
print_version();
128+
for ( i = 0; i < REPEATS; i++ ) {
129+
printf( "# c::%s\n", NAME );
130+
elapsed = benchmark();
131+
print_results( elapsed );
132+
printf( "ok %d benchmark finished\n", i+1 );
133+
}
134+
print_summary( REPEATS, REPEATS );
135+
}

0 commit comments

Comments
 (0)