Skip to content

Commit 456053e

Browse files
committed
feat: add C implementation of stdlib/math/base/special/minn
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent ac07e90 commit 456053e

File tree

15 files changed

+1097
-3
lines changed

15 files changed

+1097
-3
lines changed

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,102 @@ for ( i = 0; i < 100; i++ ) {
117117

118118
<!-- /.examples -->
119119

120+
<!-- C interface documentation. -->
121+
122+
* * *
123+
124+
<section class="c">
125+
126+
## C APIs
127+
128+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
129+
130+
<section class="intro">
131+
132+
</section>
133+
134+
<!-- /.intro -->
135+
136+
<!-- C usage documentation. -->
137+
138+
<section class="usage">
139+
140+
### Usage
141+
142+
```c
143+
#include "stdlib/math/base/special/minn.h"
144+
```
145+
146+
#### stdlib_base_minn( count, values )
147+
148+
Returns the minimum value.
149+
150+
```c
151+
double vals1[] = {4.2};
152+
double v = stdlib_base_minn( 1, vals1 );
153+
// returns 4.2
154+
155+
double vals2[] = {3.14, 4.2};
156+
v = stdlib_base_minn( 2, vals2 );
157+
// returns 3.14
158+
```
159+
160+
The function accepts the following arguments:
161+
162+
- **count**: `[in] int` input value.
163+
- **values**: `[in] doubles*` input value.
164+
165+
```c
166+
float stdlib_base_minn( int count, const double* values );
167+
```
168+
169+
</section>
170+
171+
<!-- /.usage -->
172+
173+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
174+
175+
<section class="notes">
176+
177+
</section>
178+
179+
<!-- /.notes -->
180+
181+
<!-- C API usage examples. -->
182+
183+
<section class="examples">
184+
185+
### Examples
186+
187+
```c
188+
#include "stdlib/math/base/special/minn.h"
189+
#include <stdlib.h>
190+
#include <stdio.h>
191+
192+
int main( void ) {
193+
double values[ 2 ];
194+
double v;
195+
int i;
196+
197+
for ( i = 0; i < 100; i++ ) {
198+
values[0] = ( ( (double)rand() / (double)RAND_MAX ) * 200.0 ) - 100.0;
199+
values[1] = ( ( (double)rand() / (double)RAND_MAX ) * 200.0 ) - 100.0;
200+
v = stdlib_base_minn( 2, values );
201+
printf( "x: %f, y: %f, minn(x, y): %f\n", values[0], values[1], v );
202+
}
203+
204+
return 0;
205+
}
206+
```
207+
208+
</section>
209+
210+
<!-- /.examples -->
211+
212+
</section>
213+
214+
<!-- /.c -->
215+
120216
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
121217
122218
<section class="references">
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 uniform = require( '@stdlib/random/array/uniform' );
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 minn = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( minn instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var z;
45+
var i;
46+
47+
x = uniform( 100, -500.0, 500.0 );
48+
y = uniform( 100, -500.0, 500.0 );
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
z = minn( x[ i%x.length ], y[ i%y.length ] );
53+
if ( isnan( z ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnan( z ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});

lib/node_modules/@stdlib/math/base/special/minn/benchmark/c/benchmark.c

Lines changed: 7 additions & 3 deletions
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) 2025 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.
@@ -16,13 +16,14 @@
1616
* limitations under the License.
1717
*/
1818

19+
#include "stdlib/math/base/special/minn.h"
1920
#include <stdlib.h>
2021
#include <stdio.h>
2122
#include <math.h>
2223
#include <time.h>
2324
#include <sys/time.h>
2425

25-
#define NAME "min"
26+
#define NAME "minn"
2627
#define ITERATIONS 1000000
2728
#define REPEATS 3
2829

@@ -92,6 +93,7 @@ static double benchmark( void ) {
9293
double elapsed;
9394
double x[ 100 ];
9495
double y[ 100 ];
96+
double v[ 2 ];
9597
double z;
9698
double t;
9799
int i;
@@ -103,7 +105,9 @@ static double benchmark( void ) {
103105

104106
t = tic();
105107
for ( i = 0; i < ITERATIONS; i++ ) {
106-
z = fmin( x[ i%100 ], y[ i%100 ] );
108+
v[ 0 ] = x[ i%100 ];
109+
v[ 1 ] = y[ i%100 ];
110+
z = stdlib_base_minn( 2, v );
107111
if ( z != z ) {
108112
printf( "should not return NaN\n" );
109113
break;
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# @license Apache-2.0
2+
#
3+
# Copyright (c) 2025 The Stdlib Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# A `.gyp` file for building a Node.js native add-on.
18+
#
19+
# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md
20+
# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md
21+
{
22+
# List of files to include in this file:
23+
'includes': [
24+
'./include.gypi',
25+
],
26+
27+
# Define variables to be used throughout the configuration for all targets:
28+
'variables': {
29+
# Target name should match the add-on export name:
30+
'addon_target_name%': 'addon',
31+
32+
# Set variables based on the host OS:
33+
'conditions': [
34+
[
35+
'OS=="win"',
36+
{
37+
# Define the object file suffix:
38+
'obj': 'obj',
39+
},
40+
{
41+
# Define the object file suffix:
42+
'obj': 'o',
43+
}
44+
], # end condition (OS=="win")
45+
], # end conditions
46+
}, # end variables
47+
48+
# Define compile targets:
49+
'targets': [
50+
51+
# Target to generate an add-on:
52+
{
53+
# The target name should match the add-on export name:
54+
'target_name': '<(addon_target_name)',
55+
56+
# Define dependencies:
57+
'dependencies': [],
58+
59+
# Define directories which contain relevant include headers:
60+
'include_dirs': [
61+
# Local include directory:
62+
'<@(include_dirs)',
63+
],
64+
65+
# List of source files:
66+
'sources': [
67+
'<@(src_files)',
68+
],
69+
70+
# Settings which should be applied when a target's object files are used as linker input:
71+
'link_settings': {
72+
# Define libraries:
73+
'libraries': [
74+
'<@(libraries)',
75+
],
76+
77+
# Define library directories:
78+
'library_dirs': [
79+
'<@(library_dirs)',
80+
],
81+
},
82+
83+
# C/C++ compiler flags:
84+
'cflags': [
85+
# Enable commonly used warning options:
86+
'-Wall',
87+
88+
# Aggressive optimization:
89+
'-O3',
90+
],
91+
92+
# C specific compiler flags:
93+
'cflags_c': [
94+
# Specify the C standard to which a program is expected to conform:
95+
'-std=c99',
96+
],
97+
98+
# C++ specific compiler flags:
99+
'cflags_cpp': [
100+
# Specify the C++ standard to which a program is expected to conform:
101+
'-std=c++11',
102+
],
103+
104+
# Linker flags:
105+
'ldflags': [],
106+
107+
# Apply conditions based on the host OS:
108+
'conditions': [
109+
[
110+
'OS=="mac"',
111+
{
112+
# Linker flags:
113+
'ldflags': [
114+
'-undefined dynamic_lookup',
115+
'-Wl,-no-pie',
116+
'-Wl,-search_paths_first',
117+
],
118+
},
119+
], # end condition (OS=="mac")
120+
[
121+
'OS!="win"',
122+
{
123+
# C/C++ flags:
124+
'cflags': [
125+
# Generate platform-independent code:
126+
'-fPIC',
127+
],
128+
},
129+
], # end condition (OS!="win")
130+
], # end conditions
131+
}, # end target <(addon_target_name)
132+
133+
# Target to copy a generated add-on to a standard location:
134+
{
135+
'target_name': 'copy_addon',
136+
137+
# Declare that the output of this target is not linked:
138+
'type': 'none',
139+
140+
# Define dependencies:
141+
'dependencies': [
142+
# Require that the add-on be generated before building this target:
143+
'<(addon_target_name)',
144+
],
145+
146+
# Define a list of actions:
147+
'actions': [
148+
{
149+
'action_name': 'copy_addon',
150+
'message': 'Copying addon...',
151+
152+
# Explicitly list the inputs in the command-line invocation below:
153+
'inputs': [],
154+
155+
# Declare the expected outputs:
156+
'outputs': [
157+
'<(addon_output_dir)/<(addon_target_name).node',
158+
],
159+
160+
# Define the command-line invocation:
161+
'action': [
162+
'cp',
163+
'<(PRODUCT_DIR)/<(addon_target_name).node',
164+
'<(addon_output_dir)/<(addon_target_name).node',
165+
],
166+
},
167+
], # end actions
168+
}, # end target copy_addon
169+
], # end targets
170+
}

0 commit comments

Comments
 (0)