Skip to content

Commit 169c8cb

Browse files
committed
feat: add number/uint8/base/mul
--- 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: passed - 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 590e647 commit 169c8cb

File tree

26 files changed

+2016
-0
lines changed

26 files changed

+2016
-0
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# mul
22+
23+
> Multiply two unsigned 8-bit integers.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var mul = require( '@stdlib/number/uint8/base/mul' );
41+
```
42+
43+
#### mul( x, y )
44+
45+
Multiplies two unsigned 8-bit integers.
46+
47+
```javascript
48+
var v = mul( 5, 1 );
49+
// returns 5
50+
51+
v = mul( 5, 2 );
52+
// returns 10
53+
54+
v = mul( 5, 0 );
55+
// returns 0
56+
```
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
63+
64+
<section class="notes">
65+
66+
</section>
67+
68+
<!-- /.notes -->
69+
70+
<!-- Package usage examples. -->
71+
72+
<section class="examples">
73+
74+
## Examples
75+
76+
<!-- eslint no-undef: "error" -->
77+
78+
```javascript
79+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
80+
var logEachMap = require( '@stdlib/console/log-each-map' );
81+
var mul = require( '@stdlib/number/uint8/base/mul' );
82+
83+
var opts = {
84+
'dtype': 'uint8'
85+
};
86+
87+
// Create arrays of random values:
88+
var x = discreteUniform( 100, 0, 50, opts );
89+
var y = discreteUniform( 100, 0, 50, opts );
90+
91+
// Perform element-wise multiplication:
92+
logEachMap( '%d * %d = %d', x, y, mul );
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- C interface documentation. -->
100+
101+
* * *
102+
103+
<section class="c">
104+
105+
## C APIs
106+
107+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
108+
109+
<section class="intro">
110+
111+
</section>
112+
113+
<!-- /.intro -->
114+
115+
<!-- C usage documentation. -->
116+
117+
<section class="usage">
118+
119+
### Usage
120+
121+
```c
122+
#include "stdlib/number/uint8/base/mul.h"
123+
```
124+
125+
#### stdlib_base_uint8_mul( x, y )
126+
127+
Multiplies two unsigned 8-bit integers.
128+
129+
```c
130+
#include <stdint.h>
131+
132+
uint8_t v = stdlib_base_uint8_mul( 5, 2 );
133+
// returns 10
134+
```
135+
136+
The function accepts the following arguments:
137+
138+
- **x**: `[in] uint8_t` first input value.
139+
- **y**: `[in] uint8_t` second input value.
140+
141+
```c
142+
uint8_t stdlib_base_uint8_mul( const uint8_t x, const uint8_t y );
143+
```
144+
145+
</section>
146+
147+
<!-- /.usage -->
148+
149+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="notes">
152+
153+
</section>
154+
155+
<!-- /.notes -->
156+
157+
<!-- C API usage examples. -->
158+
159+
<section class="examples">
160+
161+
### Examples
162+
163+
```c
164+
#include "stdlib/number/uint8/base/mul.h"
165+
#include <stdint.h>
166+
#include <stdio.h>
167+
168+
int main( void ) {
169+
const uint8_t x[] = { 3, 5, 10, 12 };
170+
const uint8_t y[] = { 6, 2, 11, 24 };
171+
172+
uint8_t z;
173+
int i;
174+
for ( i = 0; i < 4; i++ ) {
175+
z = stdlib_base_uint8_mul( x[ i ], y[ i ] );
176+
printf( "%d * %d = %d\n", x[ i ], y[ i ], z );
177+
}
178+
}
179+
```
180+
181+
</section>
182+
183+
<!-- /.examples -->
184+
185+
</section>
186+
187+
<!-- /.c -->
188+
189+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
190+
191+
<section class="related">
192+
193+
</section>
194+
195+
<!-- /.related -->
196+
197+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
198+
199+
<section class="links">
200+
201+
</section>
202+
203+
<!-- /.links -->
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 bench = require( '@stdlib/bench' );
24+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
25+
var pkg = require( './../package.json' ).name;
26+
var mul = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var x;
33+
var y;
34+
var i;
35+
36+
x = discreteUniform( 100, 10, 12, {
37+
'dtype': 'uint8'
38+
});
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
y = mul( x[ i%x.length ], 5 );
43+
if ( y > 100 ) {
44+
b.fail( 'unexpected result' );
45+
}
46+
}
47+
b.toc();
48+
if ( y > 100 ) {
49+
b.fail( 'unexpected result' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( pkg+'::inline', function benchmark( b ) {
56+
var x;
57+
var y;
58+
var i;
59+
60+
x = discreteUniform( 100, 10, 12, {
61+
'dtype': 'uint8'
62+
});
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
y = x[ i%x.length ] * 5;
67+
if ( y > 100 ) {
68+
b.fail( 'unexpected result' );
69+
}
70+
}
71+
b.toc();
72+
if ( y > 100 ) {
73+
b.fail( 'unexpected result' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
});
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, 10, 12, {
46+
'dtype': 'uint8'
47+
});
48+
49+
b.tic();
50+
for ( i = 0; i < b.iterations; i++ ) {
51+
y = mul( x[ i%x.length ], 5 );
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+
});

0 commit comments

Comments
 (0)