Repository navigation
feat: add number/int64/base/add
#16571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
impawstarlight
wants to merge
4
commits into
stdlib-js:develop
Choose a base branch
from
impawstarlight:feature/int64-base-add
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,590
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
188 changes: 188 additions & 0 deletions
188
lib/node_modules/@stdlib/number/int64/base/add/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # add | ||
|
|
||
| > Add two 64-bit signed integers. | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var add = require( '@stdlib/number/int64/base/add' ); | ||
| ``` | ||
|
|
||
| #### add( a, b ) | ||
|
|
||
| Adds two 64-bit signed integers. | ||
|
|
||
| ```javascript | ||
| var Int64 = require( '@stdlib/number/int64/ctor' ); | ||
|
|
||
| var a = new Int64( 5 ); | ||
| var b = new Int64( 10 ); | ||
|
|
||
| var v = add( a, b ); | ||
| // returns <Int64>[ 15n ] | ||
| ``` | ||
|
|
||
| The function supports the following parameters: | ||
|
|
||
| - **a**: first 64-bit signed integer. | ||
| - **b**: second 64-bit signed integer. | ||
|
|
||
| #### add.assign( ah, al, bh, bl, out, stride, offset ) | ||
|
|
||
| Adds two 64-bit signed integers and assigns results to a provided output array. | ||
|
|
||
| ```javascript | ||
| var Uint32Array = require( '@stdlib/array/uint32' ); | ||
|
|
||
| var out = new Uint32Array( 2 ); | ||
| var v = add.assign( 1, 2, 3, 4, out, 1, 0 ); | ||
| // returns <Uint32Array>[ 4, 6 ] | ||
|
|
||
| var bool = ( out === v ); | ||
| // returns true | ||
| ``` | ||
|
|
||
| The function supports the following parameters: | ||
|
|
||
| - **ah**: high 32-bit word of the first 64-bit signed integer. | ||
| - **al**: low 32-bit word of the first 64-bit signed integer. | ||
| - **bh**: high 32-bit word of the second 64-bit signed integer. | ||
| - **bl**: low 32-bit word of the second 64-bit signed integer. | ||
| - **out**: output array. | ||
| - **stride**: stride length for `out`. | ||
| - **offset**: starting index for `out`. | ||
|
|
||
| #### add.strided( a, sa, oa, b, sb, ob, out, so, oo ) | ||
|
|
||
| Adds two 64-bit signed integers stored in integer-valued strided array views and assigns results to a provided strided output array. | ||
|
|
||
| ```javascript | ||
| var Uint32Array = require( '@stdlib/array/uint32' ); | ||
|
|
||
| var a = new Uint32Array( [ 1, 2 ] ); | ||
| var b = new Uint32Array( [ 3, 4 ] ); | ||
| var out = new Uint32Array( 2 ); | ||
|
|
||
| var v = add.strided( a, 1, 0, b, 1, 0, out, 1, 0 ); | ||
| // returns <Uint32Array>[ 4, 6 ] | ||
|
|
||
| var bool = ( out === v ); | ||
| // returns true | ||
| ``` | ||
|
|
||
| The function supports the following parameters: | ||
|
|
||
| - **a**: first 64-bit signed integer strided array view containing interleaved 32-bit unsigned integer high and low order words. | ||
| - **sa**: stride length for `a`. | ||
| - **oa**: starting index for `a`. | ||
| - **b**: second 64-bit signed integer strided array view containing interleaved 32-bit unsigned integer high and low order words. | ||
| - **sb**: stride length for `b`. | ||
| - **ob**: starting index for `b`. | ||
| - **out**: output array. | ||
| - **so**: stride length for `out`. | ||
| - **oo**: starting index for `out`. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - Unlike signed integer overflow in C, overflow behavior is well-defined and follows two's complement wraparound semantics. | ||
|
impawstarlight marked this conversation as resolved.
Resolved
|
||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var Int64 = require( '@stdlib/number/int64/ctor' ); | ||
| var add = require( '@stdlib/number/int64/base/add' ); | ||
|
|
||
| // Addition beyond JavaScript's safe integer limit (10^18 > 2^53 - 1): | ||
| var a = new Int64( 1e18 ); | ||
| var b = new Int64( 1 ); | ||
| var v = add( a, b ); | ||
| console.log( '%s + %s = %s', a, b, v ); | ||
| // => '1000000000000000000 + 1 = 1000000000000000001' | ||
|
|
||
| a = new Int64( -1e18 ); | ||
| b = new Int64( -1 ); | ||
| v = add( a, b ); | ||
| console.log( '%s + %s = %s', a, b, v ); | ||
| // => '-1000000000000000000 + -1 = -1000000000000000001' | ||
|
|
||
| a = new Int64( 1e18 ); | ||
| b = new Int64( -1 ); | ||
| v = add( a, b ); | ||
| console.log( '%s + %s = %s', a, b, v ); | ||
| // => '1000000000000000000 + -1 = 999999999999999999' | ||
|
|
||
| // Signed two's complement overflow wrap-around (INT64_MAX + 1 = INT64_MIN): | ||
| a = Int64.of( 0x7fffffff, 0xffffffff ); | ||
| b = new Int64( 1 ); | ||
| v = add( a, b ); | ||
| console.log( '%s + %s = %s', a, b, v ); | ||
| // => '9223372036854775807 + 1 = -9223372036854775808' | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| <!-- <related-links> --> | ||
|
|
||
| <!-- </related-links> --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
130 changes: 130 additions & 0 deletions
130
lib/node_modules/@stdlib/number/int64/base/add/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var Uint32Array = require( '@stdlib/array/uint32' ); | ||
| var bench = require( '@stdlib/bench' ); | ||
| var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); | ||
| var Int64 = require( '@stdlib/number/int64/ctor' ); | ||
| var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var add = require( './../lib' ); | ||
|
|
||
|
|
||
| // VARIABLES // | ||
|
|
||
| var options = { | ||
| 'dtype': 'uint32' | ||
| }; | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var values; | ||
| var out; | ||
| var N; | ||
| var x; | ||
| var y; | ||
| var z; | ||
| var i; | ||
|
|
||
| N = 100; | ||
| x = discreteUniform( N, 0, UINT32_MAX, options ); | ||
| values = []; | ||
| for ( i = 0; i < N; i++ ) { | ||
| values.push( Int64.of( x[ i ], x[ (i+1)%N ] ) ); | ||
| } | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| y = values[ i%N ]; | ||
| z = values[ (i+1)%N ]; | ||
| out = add( y, z ); | ||
| if ( typeof out !== 'object' ) { | ||
| b.fail( 'should return an object' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !( out instanceof Int64 ) ) { | ||
| b.fail( 'should return an Int64 instance' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
|
|
||
| bench( format( '%s:assign', pkg ), function benchmark( b ) { | ||
| var out; | ||
| var N; | ||
| var x; | ||
| var i; | ||
|
|
||
| N = 100; | ||
| x = discreteUniform( N, 0, UINT32_MAX, options ); | ||
|
|
||
| out = new Uint32Array( 2 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| // eslint-disable-next-line max-len | ||
| out = add.assign( x[ i%N ], x[ (i+1)%N ], x[ (i+2)%N ], x[ (i+3)%N ], out, 1, 0 ); | ||
| if ( typeof out !== 'object' ) { | ||
| b.fail( 'should return an object' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !( out instanceof Uint32Array ) ) { | ||
| b.fail( 'should return a Uint32Array' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
|
|
||
| bench( format( '%s:strided', pkg ), function benchmark( b ) { | ||
| var out; | ||
| var N; | ||
| var x; | ||
| var y; | ||
| var i; | ||
| var j; | ||
|
|
||
| N = 50; | ||
| x = discreteUniform( N*2, 0, UINT32_MAX, options ); | ||
| y = discreteUniform( N*2, 0, UINT32_MAX, options ); | ||
|
|
||
| out = new Uint32Array( 2 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| j = ( i % N ) * 2; | ||
| out = add.strided( x, 1, j, y, 1, j, out, 1, 0 ); | ||
| if ( typeof out !== 'object' ) { | ||
| b.fail( 'should return an object' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !( out instanceof Uint32Array ) ) { | ||
| b.fail( 'should return a Uint32Array' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are going to document parameters below, you should document parameters here.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Artifact from Complex128 reference. This has been going unnoticed for quite a while. Need to update all the previously merged Uint64 base packages as well as the Complex number packages I guess?