Skip to content

Commit 9cc8e4f

Browse files
feat: add uint64/base/add
PR-URL: #12518 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 118a1ea commit 9cc8e4f

15 files changed

Lines changed: 1549 additions & 0 deletions

File tree

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# add
22+
23+
> Add two 64-bit unsigned integers.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var add = require( '@stdlib/number/uint64/base/add' );
37+
```
38+
39+
#### add( a, b )
40+
41+
Adds two 64-bit unsigned integers.
42+
43+
```javascript
44+
var Uint64 = require( '@stdlib/number/uint64/ctor' );
45+
46+
var a = new Uint64( 5 );
47+
var b = new Uint64( 10 );
48+
49+
var v = add( a, b );
50+
// returns <Uint64>[ 15n ]
51+
```
52+
53+
#### add.assign( ah, al, bh, bl, out, so, oo )
54+
55+
Adds two 64-bit unsigned integers and assigns results to a provided output array.
56+
57+
```javascript
58+
var Uint32Array = require( '@stdlib/array/uint32' );
59+
60+
var out = new Uint32Array( 2 );
61+
var v = add.assign( 0, 5, 0, 10, out, 1, 0 );
62+
// returns <Uint32Array>[ 0, 15 ]
63+
64+
var bool = ( out === v );
65+
// returns true
66+
```
67+
68+
The function supports the following parameters:
69+
70+
- **ah**: high 32-bit word of the first 64-bit unsigned integer.
71+
- **al**: low 32-bit word of the first 64-bit unsigned integer.
72+
- **bh**: high 32-bit word of the second 64-bit unsigned integer.
73+
- **bl**: low 32-bit word of the second 64-bit unsigned integer.
74+
- **out**: output array.
75+
- **so**: stride length for `out`.
76+
- **oo**: starting index for `out`.
77+
78+
#### add.strided( a, sa, oa, b, sb, ob, out, so, oo )
79+
80+
Adds two 64-bit unsigned integers stored in integer-valued strided array views and assigns results to a provided strided output array.
81+
82+
```javascript
83+
var Uint32Array = require( '@stdlib/array/uint32' );
84+
85+
var a = new Uint32Array( [ 0, 5 ] );
86+
var b = new Uint32Array( [ 0, 10 ] );
87+
var out = new Uint32Array( 2 );
88+
89+
var v = add.strided( a, 1, 0, b, 1, 0, out, 1, 0 );
90+
// returns <Uint32Array>[ 0, 15 ]
91+
92+
var bool = ( out === v );
93+
// returns true
94+
```
95+
96+
The function supports the following parameters:
97+
98+
- **a**: first 64-bit unsigned integer strided array view containing interleaved 32-bit unsigned integer high and low order words.
99+
- **sa**: stride length for `a`.
100+
- **oa**: starting index for `a`.
101+
- **b**: second 64-bit unsigned integer strided array view containing interleaved 32-bit unsigned integer high and low order words.
102+
- **sb**: stride length for `b`.
103+
- **ob**: starting index for `b`.
104+
- **out**: output array.
105+
- **so**: stride length for `out`.
106+
- **oo**: starting index for `out`.
107+
108+
</section>
109+
110+
<!-- /.usage -->
111+
112+
<section class="examples">
113+
114+
## Examples
115+
116+
<!-- eslint no-undef: "error" -->
117+
118+
```javascript
119+
var Uint64 = require( '@stdlib/number/uint64/ctor' );
120+
var add = require( '@stdlib/number/uint64/base/add' );
121+
122+
var a = new Uint64( 5 );
123+
var b = new Uint64( 10 );
124+
var v = add( a, b );
125+
// returns <Uint64>[ 15n ]
126+
127+
a = new Uint64( 1234567890 );
128+
b = new Uint64( 8765432109 );
129+
v = add( a, b );
130+
// returns <Uint64>[ 9999999999n ]
131+
```
132+
133+
</section>
134+
135+
<!-- /.examples -->
136+
137+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
138+
139+
<section class="related">
140+
141+
</section>
142+
143+
<!-- /.related -->
144+
145+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
146+
147+
<section class="links">
148+
149+
<!-- <related-links> -->
150+
151+
<!-- </related-links> -->
152+
153+
</section>
154+
155+
<!-- /.links -->
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 Uint32Array = require( '@stdlib/array/uint32' );
24+
var bench = require( '@stdlib/bench' );
25+
var UINT32_MAX = require( '@stdlib/constants/uint32/max' );
26+
var Uint64 = require( '@stdlib/number/uint64/ctor' );
27+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var add = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'uint32'
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg, function benchmark( b ) {
43+
var values;
44+
var out;
45+
var N;
46+
var x;
47+
var y;
48+
var z;
49+
var i;
50+
51+
N = 100;
52+
x = discreteUniform( N, 0, UINT32_MAX, options );
53+
values = [];
54+
for ( i = 0; i < N; i++ ) {
55+
values.push( Uint64.of( x[ i ], x[ (i+1)%N ] ) );
56+
}
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
y = values[ i%N ];
61+
z = values[ (i+1)%N ];
62+
out = add( y, z );
63+
if ( typeof out !== 'object' ) {
64+
b.fail( 'should return an object' );
65+
}
66+
}
67+
b.toc();
68+
if ( !( out instanceof Uint64 ) ) {
69+
b.fail( 'should return a Uint64 instance' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});
74+
75+
bench( format( '%s:assign', pkg ), function benchmark( b ) {
76+
var out;
77+
var N;
78+
var x;
79+
var i;
80+
81+
N = 100;
82+
x = discreteUniform( N, 0, UINT32_MAX, options );
83+
84+
out = new Uint32Array( 2 );
85+
86+
b.tic();
87+
for ( i = 0; i < b.iterations; i++ ) {
88+
// eslint-disable-next-line max-len
89+
out = add.assign( x[ i%N ], x[ (i+1)%N ], x[ (i+2)%N ], x[ (i+3)%N ], out, 1, 0 );
90+
if ( typeof out !== 'object' ) {
91+
b.fail( 'should return an object' );
92+
}
93+
}
94+
b.toc();
95+
if ( !( out instanceof Uint32Array ) ) {
96+
b.fail( 'should return a Uint32Array' );
97+
}
98+
b.pass( 'benchmark finished' );
99+
b.end();
100+
});
101+
102+
bench( format( '%s:strided', pkg ), function benchmark( b ) {
103+
var out;
104+
var N;
105+
var x;
106+
var y;
107+
var i;
108+
var j;
109+
110+
N = 50;
111+
x = discreteUniform( N*2, 0, UINT32_MAX, options );
112+
y = discreteUniform( N*2, 0, UINT32_MAX, options );
113+
114+
out = new Uint32Array( 2 );
115+
116+
b.tic();
117+
for ( i = 0; i < b.iterations; i++ ) {
118+
j = ( i % N ) * 2;
119+
out = add.strided( x, 1, j, y, 1, j, out, 1, 0 );
120+
if ( typeof out !== 'object' ) {
121+
b.fail( 'should return an object' );
122+
}
123+
}
124+
b.toc();
125+
if ( !( out instanceof Uint32Array ) ) {
126+
b.fail( 'should return a Uint32Array' );
127+
}
128+
b.pass( 'benchmark finished' );
129+
b.end();
130+
});

0 commit comments

Comments
 (0)