Skip to content

Commit 4b55b3a

Browse files
committed
feat: add C API and clean-up
--- 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: passed - 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 b299663 commit 4b55b3a

File tree

20 files changed

+1499
-42
lines changed

20 files changed

+1499
-42
lines changed

lib/node_modules/@stdlib/number/uint32/base/mul/README.md

Lines changed: 99 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,116 @@ v = mul( 2147483648>>>0, 5>>>0 ); // 2^31 * 5 = 10737418240 => 32-bit integer ov
7171
<!-- eslint no-undef: "error" -->
7272

7373
```javascript
74-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
75-
var UINT32_MAX = require( '@stdlib/constants/uint32/max' );
74+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
75+
var logEachMap = require( '@stdlib/console/log-each-map' );
7676
var mul = require( '@stdlib/number/uint32/base/mul' );
7777

78-
var randi;
79-
var a;
80-
var b;
81-
var y;
82-
var i;
78+
var opts = {
79+
'dtype': 'uint32'
80+
};
8381

84-
randi = discreteUniform( 0, UINT32_MAX );
82+
// Create arrays of random values:
83+
var x = discreteUniform( 100, 0, 50, opts );
84+
var y = discreteUniform( 100, 0, 50, opts );
8585

86-
for ( i = 0; i < 100; i++ ) {
87-
a = randi()>>>0;
88-
b = randi()>>>0;
89-
y = mul( a, b );
90-
console.log( '%d x %d = %d', a, b, y );
86+
// Perform element-wise multiplication:
87+
logEachMap( '%d * %d = %d', x, y, mul );
88+
```
89+
90+
</section>
91+
92+
<!-- /.examples -->
93+
94+
<!-- C interface documentation. -->
95+
96+
* * *
97+
98+
<section class="c">
99+
100+
## C APIs
101+
102+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
103+
104+
<section class="intro">
105+
106+
</section>
107+
108+
<!-- /.intro -->
109+
110+
<!-- C usage documentation. -->
111+
112+
<section class="usage">
113+
114+
### Usage
115+
116+
```c
117+
#include "stdlib/number/uint32/base/mul.h"
118+
```
119+
120+
#### stdlib_base_uint32_mul( x, y )
121+
122+
Multiplies two unsigned 32-bit integers.
123+
124+
```c
125+
#include <stdint.h>
126+
127+
uint32_t v = stdlib_base_uint32_mul( 5, 2 );
128+
// returns 10
129+
```
130+
131+
The function accepts the following arguments:
132+
133+
- **x**: `[in] uint32_t` first input value.
134+
- **y**: `[in] uint32_t` second input value.
135+
136+
```c
137+
uint32_t stdlib_base_uint32_mul( const uint32_t x, const uint32_t y );
138+
```
139+
140+
</section>
141+
142+
<!-- /.usage -->
143+
144+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
145+
146+
<section class="notes">
147+
148+
</section>
149+
150+
<!-- /.notes -->
151+
152+
<!-- C API usage examples. -->
153+
154+
<section class="examples">
155+
156+
### Examples
157+
158+
```c
159+
#include "stdlib/number/uint32/base/mul.h"
160+
#include <stdint.h>
161+
#include <stdio.h>
162+
163+
int main( void ) {
164+
const uint32_t x[] = { 3, 5, 10, 12 };
165+
const uint32_t y[] = { 6, 2, 11, 24 };
166+
167+
uint32_t z;
168+
int i;
169+
for ( i = 0; i < 4; i++ ) {
170+
z = stdlib_base_uint32_mul( x[ i ], y[ i ] );
171+
printf( "%u * %u = %u\n", x[ i ], y[ i ], z );
172+
}
91173
}
92174
```
93175

94176
</section>
95177

96178
<!-- /.examples -->
97179

180+
</section>
181+
182+
<!-- /.c -->
183+
98184
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
99185

100186
<section class="related">

lib/node_modules/@stdlib/number/uint32/base/mul/benchmark/benchmark.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var minstd = require( '@stdlib/random/base/minstd' );
25-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
24+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2625
var pkg = require( './../package.json' ).name;
2726
var mul = require( './../lib' );
2827

@@ -34,38 +33,44 @@ bench( pkg, function benchmark( b ) {
3433
var y;
3534
var i;
3635

36+
x = discreteUniform( 100, 0, 10, {
37+
'dtype': 'uint32'
38+
});
39+
3740
b.tic();
3841
for ( i = 0; i < b.iterations; i++ ) {
39-
x = minstd();
40-
y = mul( x>>>0, x>>>0 );
41-
if ( isnan( y ) ) {
42-
b.fail( 'should not return NaN' );
42+
y = mul( x[ i%x.length ], 10 );
43+
if ( y > 100 ) {
44+
b.fail( 'unexpected result' );
4345
}
4446
}
4547
b.toc();
46-
if ( isnan( y ) ) {
47-
b.fail( 'should not return NaN' );
48+
if ( y > 100 ) {
49+
b.fail( 'unexpected result' );
4850
}
4951
b.pass( 'benchmark finished' );
5052
b.end();
5153
});
5254

53-
bench( pkg+'::naive_multiplication', function benchmark( b ) {
55+
bench( pkg+'::inline', function benchmark( b ) {
5456
var x;
5557
var y;
5658
var i;
5759

60+
x = discreteUniform( 100, 0, 10, {
61+
'dtype': 'uint32'
62+
});
63+
5864
b.tic();
5965
for ( i = 0; i < b.iterations; i++ ) {
60-
x = minstd();
61-
y = ( x>>>0 ) * ( x>>>0 );
62-
if ( isnan( y ) ) {
63-
b.fail( 'should not return NaN' );
66+
y = ( x[ i%x.length ]>>>0 ) * 5;
67+
if ( y > 100 ) {
68+
b.fail( 'unexpected result' );
6469
}
6570
}
6671
b.toc();
67-
if ( isnan( y ) ) {
68-
b.fail( 'should not return NaN' );
72+
if ( y > 100 ) {
73+
b.fail( 'unexpected result' );
6974
}
7075
b.pass( 'benchmark finished' );
7176
b.end();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var tryRequire = require( '@stdlib/utils/try-require' );
27+
var pkg = require( './../package.json' ).name;
28+
29+
30+
// VARIABLES //
31+
32+
var mul = tryRequire( resolve( __dirname, './../lib/native.js' ) );
33+
var opts = {
34+
'skip': ( mul instanceof Error )
35+
};
36+
37+
38+
// MAIN //
39+
40+
bench( pkg+'::native', opts, function benchmark( b ) {
41+
var x;
42+
var y;
43+
var i;
44+
45+
x = discreteUniform( 100, 0, 10, {
46+
'dtype': 'uint32'
47+
});
48+
49+
b.tic();
50+
for ( i = 0; i < b.iterations; i++ ) {
51+
y = mul( x[ i%x.length ], 10 );
52+
if ( y > 100 ) {
53+
b.fail( 'unexpected result' );
54+
}
55+
}
56+
b.toc();
57+
if ( y > 100 ) {
58+
b.fail( 'unexpected result' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
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) 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+
# 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

0 commit comments

Comments
 (0)