Skip to content

Commit 17eec08

Browse files
Aadish JainAadish Jain
authored andcommitted
feat(special-wrapf): adding implementation
1 parent 9299501 commit 17eec08

File tree

25 files changed

+2304
-0
lines changed

25 files changed

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

0 commit comments

Comments
 (0)