Skip to content

Commit 0735981

Browse files
committed
add c implementation for minmax
1 parent c30cdec commit 0735981

File tree

25 files changed

+1163
-12
lines changed

25 files changed

+1163
-12
lines changed

lib/node_modules/@stdlib/math/base/special/minmax/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2018 The Stdlib Authors.
5+
Copyright (c) 2024 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.

lib/node_modules/@stdlib/math/base/special/minmax/benchmark/benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
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) 2024 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-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var minmax = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( minmax 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 = minmax( x );
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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2024 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+
endif
25+
26+
# Specify the path to Cephes:
27+
CEPHES ?=
28+
29+
# Specify a list of Cephes source files:
30+
CEPHES_SRC ?=
31+
32+
# Determine the OS:
33+
#
34+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
35+
# [2]: http://stackoverflow.com/a/27776822/2225624
36+
OS ?= $(shell uname)
37+
ifneq (, $(findstring MINGW,$(OS)))
38+
OS := WINNT
39+
else
40+
ifneq (, $(findstring MSYS,$(OS)))
41+
OS := WINNT
42+
else
43+
ifneq (, $(findstring CYGWIN,$(OS)))
44+
OS := WINNT
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]:
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+
# TARGETS #
78+
79+
# Default target.
80+
#
81+
# This target is the default target.
82+
83+
all: $(c_targets)
84+
85+
.PHONY: all
86+
87+
88+
# Compile C source.
89+
#
90+
# This target compiles C source files.
91+
92+
$(c_targets): %.out: %.c
93+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $(CEPHES_SRC) $< -lm
94+
95+
96+
# Run a benchmark.
97+
#
98+
# This target runs a benchmark.
99+
100+
run: $(c_targets)
101+
$(QUIET) ./$<
102+
103+
.PHONY: run
104+
105+
106+
# Perform clean-up.
107+
#
108+
# This target removes generated files.
109+
110+
clean:
111+
$(QUIET) -rm -f *.o *.out
112+
113+
.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) 2024 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/math/base/special/minmax.h"
20+
#include <stdlib.h>
21+
#include <stdio.h>
22+
#include <math.h>
23+
#include <time.h>
24+
#include <sys/time.h>
25+
26+
#define NAME "minmax"
27+
#define ITERATIONS 1000000
28+
#define REPEATS 3
29+
30+
/**
31+
* Prints the TAP version.
32+
*/
33+
static void print_version( void ) {
34+
printf( "TAP version 13\n" );
35+
}
36+
37+
/**
38+
* Prints the TAP summary.
39+
*
40+
* @param total total number of tests
41+
* @param passing total number of passing tests
42+
*/
43+
static void print_summary( int total, int passing ) {
44+
printf( "#\n" );
45+
printf( "1..%d\n", total ); // TAP plan
46+
printf( "# total %d\n", total );
47+
printf( "# pass %d\n", passing );
48+
printf( "#\n" );
49+
printf( "# ok\n" );
50+
}
51+
52+
/**
53+
* Prints benchmarks results.
54+
*
55+
* @param elapsed elapsed time in seconds
56+
*/
57+
static void print_results( double elapsed ) {
58+
double rate = (double)ITERATIONS / elapsed;
59+
printf( " ---\n" );
60+
printf( " iterations: %d\n", ITERATIONS );
61+
printf( " elapsed: %0.9f\n", elapsed );
62+
printf( " rate: %0.9f\n", rate );
63+
printf( " ...\n" );
64+
}
65+
66+
/**
67+
* Returns a clock time.
68+
*
69+
* @return clock time
70+
*/
71+
static double tic( void ) {
72+
struct timeval now;
73+
gettimeofday( &now, NULL );
74+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
75+
}
76+
77+
/**
78+
* Generates a random number on the interval [0,1).
79+
*
80+
* @return random number
81+
*/
82+
static double rand_double( void ) {
83+
int r = rand();
84+
return (double)r / ( (double)RAND_MAX + 1.0 );
85+
}
86+
87+
/**
88+
* Runs a benchmark.
89+
*
90+
* @return elapsed time in seconds
91+
*/
92+
static double benchmark( void ) {
93+
double elapsed;
94+
double x1;
95+
double x2;
96+
minmax_result y;
97+
double t;
98+
int i;
99+
100+
t = tic();
101+
for ( i = 0; i < ITERATIONS; i++ ) {
102+
x1 = ( 20.0*rand_double() ) - 10.0;
103+
x2 = ( 20.0*rand_double() ) - 10.0;
104+
y = stdlib_base_minmax( x1, x2 );
105+
if ( y.max != y.max && y.min != y.min ) {
106+
printf( "should not return NaN\n" );
107+
break;
108+
}
109+
}
110+
elapsed = tic() - t;
111+
if ( y.max != y.max && y.min != y.min ) {
112+
printf( "should not return NaN\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)