Skip to content

Commit 9373eae

Browse files
committed
feat!: add implementation of stdlib/math/base/special/minmaxabsf
This commit adds the implementation of stdlib/math/base/special/minmaxabsf function. It includes a README.md file, benchmarks, tests, examples. The minmaxabsf function returns the minimum and maximum single-precision floating-point values for any two numbers. Closes: #6163
1 parent 8dc9043 commit 9373eae

File tree

26 files changed

+2052
-303
lines changed

26 files changed

+2052
-303
lines changed

etc/npm/deps.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
acorn@"^8.1.0" acorn-loose@"^8.0.2" acorn-walk@"^8.0.2" d3-format@"^1.0.0" d3-scale@"^1.0.0" d3-shape@"^1.0.0" d3-time-format@"^2.0.0" debug@"^2.6.9" glob@"^7.0.5" minimist@"^1.2.0" readable-stream@"^2.1.4" resolve@"^1.1.7" vdom-to-html@"^2.3.0" virtual-dom@"^2.1.1" 0x@"^4.10.2" @cspell/eslint-plugin@"^8.8.0" @commitlint/cli@"^17.4.4" @commitlint/cz-commitlint@"^17.4.4" @conventional-commits/parser@"^0.4.1" @kaciras/deasync@"^1.0.1" @types/node@"^13.9.0" @typescript-eslint/parser@"^6.9.1" @typescript-eslint/eslint-plugin@"^6.9.1" ajv@"^5.2.2" browser-pack-flat@"^3.0.0" browserify@"^17.0.0" bundle-collapser@"^1.3.0" c8@"^7.12.0" chai@"^3.5.0" cheerio@"^1.0.0-rc.12" commitizen@"^4.3.0" common-shakeify@"^0.6.0" conventional-changelog-conventionalcommits@"^5.0.0" doctrine@"^3.0.0" editorconfig-checker@"^6.0.0" envify@"^4.0.0" eslint@"^8.57.0" eslint-plugin-node@"^11.1.0" eslint-plugin-expect-type@"^0.2.3" eslint-plugin-import@"^2.29.0" eslint-plugin-jsdoc@"^46.8.2" exorcist@"^2.0.0" factor-bundle@"^2.5.0" gh-pages@"git+https://github.com/Planeshifter/gh-pages.git#main" inquirer@"^8.0.0" jscodeshift@"^0.15.0" jsdoc@"^3.4.0" lunr@"^2.3.9" mathjax-node@"^2.0.1" mathjax-node-sre@"^3.0.0" mkdirp@"^0.5.1" mustache@"^4.0.0" parse-link-header@"^1.0.1" plato@"^1.5.0" process@"^0.11.10" proxyquire@"^2.0.0" proxyquire-universal@"^2.0.0" proxyquireify@"^3.1.1" read-installed@"^4.0.3" rehype@"^9.0.0" rehype-highlight@"^3.0.0" remark@"^11.0.1" remark-cli@"^7.0.0" remark-frontmatter@"^1.2.0" remark-html@"^10.0.0" remark-lint@"^6.0.0" remark-lint-blockquote-indentation@"^1.0.0" remark-lint-checkbox-character-style@"^1.0.0" remark-lint-checkbox-content-indent@"^1.0.0" remark-lint-code-block-style@"^1.0.0" remark-lint-definition-case@"^1.0.0" remark-lint-definition-spacing@"^1.0.0" remark-lint-emphasis-marker@"^1.0.0" remark-lint-fenced-code-flag@"^1.0.0" remark-lint-fenced-code-marker@"^1.0.0" remark-lint-file-extension@"^1.0.0" remark-lint-final-definition@"^1.0.0" remark-lint-final-newline@"^1.0.0" remark-lint-first-heading-level@"^1.1.0" remark-lint-hard-break-spaces@"^1.0.1" remark-lint-heading-increment@"^1.0.0" remark-lint-heading-style@"^1.0.0" remark-lint-linebreak-style@"^1.0.0" remark-lint-link-title-style@"^1.0.0" remark-lint-list-item-bullet-indent@"^1.0.0" remark-lint-list-item-content-indent@"^1.0.0" remark-lint-list-item-indent@"^1.0.0" remark-lint-list-item-spacing@"^1.1.0" remark-lint-maximum-heading-length@"^1.0.0" remark-lint-maximum-line-length@"^1.0.0" remark-lint-no-auto-link-without-protocol@"^1.0.0" remark-lint-no-blockquote-without-marker@"^2.0.0" remark-lint-no-consecutive-blank-lines@"^1.0.0" remark-lint-no-duplicate-definitions@"^1.0.0" remark-lint-no-duplicate-headings@"^1.0.0" remark-lint-no-duplicate-headings-in-section@"^1.0.0" remark-lint-no-emphasis-as-heading@"^1.0.0" remark-lint-no-empty-url@"^1.0.1" remark-lint-no-file-name-articles@"^1.0.0" remark-lint-no-file-name-consecutive-dashes@"^1.0.0" remark-lint-no-file-name-irregular-characters@"^1.0.0" remark-lint-no-file-name-mixed-case@"^1.0.0" remark-lint-no-file-name-outer-dashes@"^1.0.1" remark-lint-no-heading-content-indent@"^1.0.0" remark-lint-no-heading-indent@"^1.0.0" remark-lint-no-heading-like-paragraph@"^1.0.0" remark-lint-no-heading-punctuation@"^1.0.0" remark-lint-no-html@"^1.0.0" remark-lint-no-inline-padding@"^1.0.0" remark-lint-no-literal-urls@"^1.0.0" remark-lint-no-missing-blank-lines@"^1.0.0" remark-lint-no-multiple-toplevel-headings@"^1.0.0" remark-lint-no-paragraph-content-indent@"^1.0.1" remark-lint-no-reference-like-url@"^1.0.0" remark-lint-no-shell-dollars@"^1.0.0" remark-lint-no-shortcut-reference-image@"^1.0.0" remark-lint-no-shortcut-reference-link@"^1.0.1" remark-lint-no-table-indentation@"^1.0.0" remark-lint-no-tabs@"^1.0.0" remark-lint-no-trailing-spaces@"^3.0.2" remark-lint-no-undefined-references@"^1.0.0" remark-lint-no-unused-definitions@"^1.0.0" remark-lint-ordered-list-marker-style@"^1.0.0" remark-lint-ordered-list-marker-value@"^1.0.0" remark-lint-rule-style@"^1.0.0" remark-lint-strong-marker@"^1.0.0" remark-lint-table-cell-padding@"^1.0.0" remark-lint-table-pipe-alignment@"^1.0.0" remark-lint-table-pipes@"^1.0.0" remark-lint-unordered-list-marker-style@"^1.0.0" remark-slug@"^5.0.0" remark-unlink@"^2.0.0" remark-validate-links@"^9.0.1" remark-vdom@"^8.0.0" semver@"^6.0.0" source-map-explorer@"^2.5.3" spdx-license-ids@"^3.0.0" tap-min@"git+https://github.com/Planeshifter/tap-min.git" tap-spec@"5.x.x" tap-summary@"^4.0.0" tap-xunit@"^2.2.0" tape@"git+https://github.com/kgryte/tape.git#fix/globby" to-vfile@"^6.0.0" typedoc@"git+https://github.com/kgryte/typedoc.git#0.16.11-patch" typescript@"4.3.5" uglify-js@"^3.17.4" uglifyify@"^5.0.0" unified-lint-rule@"^1.0.1" unist-util-visit@"^2.0.0" unist-util-visit-parents@"^3.1.1" yaml@"^1.0.0" node-gyp@"^9.3.1"
1+

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ Returns the minimum and maximum absolute single-precision floating-point values
152152
```c
153153
float min;
154154
float max;
155-
float out = stdlib_base_minmaxabsf( -4.2f, 3.14f, &min, &max );
156-
// returns [ 3.14, 4.2 ]
155+
stdlib_base_minmaxabsf( -4.2f, 3.14f, &min, &max );
156+
// [ 3.14, 4.2 ]
157157

158-
out = stdlib_base_minmaxabsf( 0.0f, -0.0f, &min, &max );
159-
// returns [ 0.0, 0.0 ]
158+
stdlib_base_minmaxabsf( 0.0f, -0.0f, &min, &max );
159+
// [ 0.0, 0.0 ]
160160
```
161161
162162
The function accepts the following arguments:
@@ -211,7 +211,7 @@ int main(void) {
211211

212212
// Compute min and max values
213213
for (i = 0; i < 10; i++) {
214-
stdlib_base_minmaxf(x1[i], x2[i], &min, &max);
214+
stdlib_base_minmaxabsf(x1[i], x2[i], &min, &max);
215215
printf("x1[%d]: %f, x2[%d]: %f, minmaxf(x1[%d], x2[%d]): (%f, %f)\n",
216216
i, x1[i], i, x2[i], i, i, min, max);
217217
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randn' );
25+
var isFloat32Array = require( '@stdlib/assert/is-float32array' );
26+
var isArray = require( '@stdlib/assert/is-array' );
27+
var minf = require( '@stdlib/math/base/special/minf' );
28+
var maxf = require( '@stdlib/math/base/special/maxf' );
29+
var float32Array = require( '@stdlib/array/float32' );
30+
var pkg = require( './../package.json' ).name;
31+
var minmaxabsf = require( './../lib' );
32+
33+
34+
// MAIN //
35+
36+
bench( pkg, function benchmark( b ) {
37+
var x;
38+
var y;
39+
var z;
40+
var i;
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
x = ( randu()*1000.0 ) - 500.0;
45+
y = ( randu()*1000.0 ) - 500.0;
46+
z = minmaxabsf( x, y );
47+
if ( z.length !== 2 ) {
48+
b.fail( 'should have expected length' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isFloat32Array( z ) ) {
53+
b.fail( 'should return an array' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
58+
59+
bench( pkg+':assign', function benchmark( b ) {
60+
var x;
61+
var y;
62+
var z;
63+
var i;
64+
65+
z = new float32Array( 2 );
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
x = ( randu()*1000.0 ) - 500.0;
69+
y = ( randu()*1000.0 ) - 500.0;
70+
z = minmaxabsf.assign( x, y, z, 1, 0 );
71+
if ( z.length !== 2 ) {
72+
b.fail( 'should have expected length' );
73+
}
74+
}
75+
b.toc();
76+
if ( !isFloat32Array( z ) ) {
77+
b.fail( 'should return an array' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
# 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 includes (e.g., `-I /foo/bar -I /beep/boop/include`):
73+
INCLUDE ?=
74+
75+
# List of source files:
76+
SOURCE_FILES ?=
77+
78+
# List of libraries (e.g., `-lopenblas -lpthread`):
79+
LIBRARIES ?=
80+
81+
# List of library paths (e.g., `-L /foo/bar -L /beep/boop`):
82+
LIBPATH ?=
83+
84+
# List of C targets:
85+
c_targets := benchmark.out
86+
87+
88+
# RULES #
89+
90+
#/
91+
# Compiles source files.
92+
#
93+
# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
94+
# @param {string} [CFLAGS] - C compiler options
95+
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
96+
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
97+
# @param {string} [SOURCE_FILES] - list of source files
98+
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
99+
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`)
100+
#
101+
# @example
102+
# make
103+
#
104+
# @example
105+
# make all
106+
#/
107+
all: $(c_targets)
108+
109+
.PHONY: all
110+
111+
#/
112+
# Compiles C source files.
113+
#
114+
# @private
115+
# @param {string} CC - C compiler (e.g., `gcc`)
116+
# @param {string} CFLAGS - C compiler options
117+
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
118+
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`)
119+
# @param {string} SOURCE_FILES - list of source files
120+
# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`)
121+
# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`)
122+
#/
123+
$(c_targets): %.out: %.c
124+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES)
125+
126+
#/
127+
# Runs compiled benchmarks.
128+
#
129+
# @example
130+
# make run
131+
#/
132+
run: $(c_targets)
133+
$(QUIET) ./$<
134+
135+
.PHONY: run
136+
137+
#/
138+
# Removes generated files.
139+
#
140+
# @example
141+
# make clean
142+
#/
143+
clean:
144+
$(QUIET) -rm -f *.o *.out
145+
146+
.PHONY: clean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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/minmaxabsf.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 "minmaxabsf"
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 t;
95+
float x[ 100 ];
96+
float y[ 100 ];
97+
float min;
98+
float max;
99+
int i;
100+
101+
for ( i = 0; i < 100; i++ ) {
102+
x[ i ] = ( 1000.0f * rand_double() ) - 500.0f;
103+
y[ i ] = ( 1000.0f * rand_double() ) - 500.0f;
104+
}
105+
106+
t = tic();
107+
for ( i = 0; i < ITERATIONS; i++ ) {
108+
stdlib_base_minmaxabsf( x[ i % 100 ], y[ i % 100 ], &min, &max );
109+
if ( min != min || max != max ) {
110+
printf( "should not return NaN\n" );
111+
break;
112+
}
113+
}
114+
elapsed = tic() - t;
115+
if ( min != min || max != max ) {
116+
printf( "should not return NaN\n" );
117+
}
118+
return elapsed;
119+
}
120+
121+
/**
122+
* Main execution sequence.
123+
*/
124+
int main( void ) {
125+
double elapsed;
126+
int i;
127+
128+
// Use the current time to seed the random number generator:
129+
srand( time( NULL ) );
130+
131+
print_version();
132+
for ( i = 0; i < REPEATS; i++ ) {
133+
printf( "# c::native::%s\n", NAME );
134+
elapsed = benchmark();
135+
print_results( elapsed );
136+
printf( "ok %d benchmark finished\n", i+1 );
137+
}
138+
print_summary( REPEATS, REPEATS );
139+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
julia 1.5
2+
BenchmarkTools 0.5.0

0 commit comments

Comments
 (0)