-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(number): add number/uint64/ctor
#10908
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
33
commits into
stdlib-js:develop
Choose a base branch
from
impawstarlight:feature/uint64
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.
Open
Changes from 31 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
26f9a3f
chore: copy main.js from `number/float16/ctor`
impawstarlight c9b0b08
chore: rename float16 stuff to uint64
impawstarlight dd5a4d6
feat: add asBigUint64 function to truncate BigInt to unsigned 64-bit …
impawstarlight 03e0b6a
feat: add toString, toJSON, and valueOf
impawstarlight b27d0e1
feat: implement Uint64 constructor with support for BigInt, Uint32Arr…
impawstarlight 7c40edd
feat: add README and package.json
impawstarlight 8ecf428
feat: add index.js
impawstarlight 64e4a30
docs: remove `value` property from README since it should be private …
impawstarlight b438d53
test: implement initial tests
impawstarlight 77609f7
docs: update todo comment
impawstarlight e367b17
bench: add initial benchmarks
impawstarlight aa937a4
docs: add types
impawstarlight de8ca59
docs: add javascript example
impawstarlight 3420ebf
fix: resovle lint errors
impawstarlight fc00588
fix: resolve lint errors
impawstarlight afbeaa2
feat: implement toString with high low words
impawstarlight 9d932db
feat: update Uint64 constructor to support setting preferred represen…
impawstarlight 3926d9a
chore: remove unused paths from package.json
impawstarlight f1ab12d
bench: fix benchmark error
impawstarlight b2fd1f6
chore: update copyright years
impawstarlight b6e19aa
test: add more tests
impawstarlight 8097552
fix: fix unnecessary leading zeros in toString()
impawstarlight cef9e28
feat: implement string parsing in Uint64 constructor
impawstarlight 85a9378
test: add tests for parsing prefixed and whitespace-padded strings in…
impawstarlight 2945c32
fix: update TODO comments for clarity and valueOf implementation
impawstarlight aae8b27
refactor: refactor parsing logic out of Uint64 constructor and more
impawstarlight 45ecf21
test: update and add tests
impawstarlight 071df31
docs: update type declaration
impawstarlight 0ee526d
docs: fix type declaration
impawstarlight 0a9a5ea
Merge branch 'develop' into feature/uint64
impawstarlight 7cd12ee
refactor: update implementation and split out logic for materializing…
kgryte f220a9c
docs: update typescript declaration with `from` and `of` static methods
impawstarlight 35b1369
docs: fix type annotations for Uint64 constructor, valueOf method, an…
impawstarlight 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
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,268 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # Uint64 | ||
|
|
||
| > Unsigned 64-bit integer. | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- Package usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var Uint64 = require( '@stdlib/number/uint64/ctor' ); | ||
| ``` | ||
|
|
||
| #### Uint64( value ) | ||
|
|
||
| Unsigned 64-bit integer constructor. | ||
|
|
||
| ```javascript | ||
| var x = new Uint64( 5 ); | ||
| // returns <Uint64> | ||
| ``` | ||
|
|
||
| * * * | ||
|
|
||
| ## Properties | ||
|
|
||
| #### Uint64.name | ||
|
|
||
| Static property returning the constructor name. | ||
|
|
||
| ```javascript | ||
| var str = Uint64.name; | ||
| // returns 'Uint64' | ||
| ``` | ||
|
|
||
| #### Uint64.BYTES_PER_ELEMENT | ||
|
|
||
| Size (in bytes) of the underlying value. | ||
|
|
||
| ```javascript | ||
| var nbytes = Uint64.BYTES_PER_ELEMENT; | ||
| // returns 8 | ||
| ``` | ||
|
|
||
| #### Uint64.prototype.BYTES_PER_ELEMENT | ||
|
|
||
| Size (in bytes) of the underlying value. | ||
|
|
||
| ```javascript | ||
| var x = new Uint64( 5 ); | ||
|
|
||
| var nbytes = x.BYTES_PER_ELEMENT; | ||
| // returns 8 | ||
| ``` | ||
|
|
||
| #### Uint64.prototype.byteLength | ||
|
|
||
| Size (in bytes) of the underlying value. | ||
|
|
||
| ```javascript | ||
| var x = new Uint64( 5 ); | ||
|
|
||
| var nbytes = x.byteLength; | ||
| // returns 8 | ||
| ``` | ||
|
|
||
| #### Uint64.prototype.hi | ||
|
|
||
| High 32-bit word of an unsigned 64-bit integer. | ||
|
|
||
| ```javascript | ||
| var x = Uint64.from( [ 1234, 5678 ] ); | ||
|
|
||
| var w = x.hi; | ||
| // returns 1234 | ||
| ``` | ||
|
|
||
| #### Uint64.prototype.lo | ||
|
|
||
| Low 32-bit word of an unsigned 64-bit integer. | ||
|
|
||
| ```javascript | ||
| var x = Uint64.from( [ 1234, 5678 ] ); | ||
|
|
||
| var w = x.lo; | ||
| // returns 5678 | ||
| ``` | ||
|
|
||
| * * * | ||
|
|
||
| ## Methods | ||
|
|
||
| ### Static Methods | ||
|
|
||
| #### Uint64.from( array ) | ||
|
|
||
| Creates a new unsigned 64-bit integer from an array-like object containing a high and low word. | ||
|
|
||
| ```javascript | ||
| var x = Uint64.from( [ 1234, 5678 ] ); | ||
| // returns <Uint64> | ||
| ``` | ||
|
|
||
| #### Uint64.of( high, low ) | ||
|
|
||
| Creates a new unsigned 64-bit integer from a high and low word. | ||
|
|
||
| ```javascript | ||
| var x = Uint64.of( 1234, 5678 ); | ||
| // returns <Uint64> | ||
| ``` | ||
|
|
||
| ### Accessor Methods | ||
|
|
||
| These methods do **not** mutate a `Uint64` instance and, instead, return an unsigned 64-bit integer representation. | ||
|
|
||
| #### Uint64.prototype.toString() | ||
|
|
||
| Returns a string representation of a `Uint64` instance. | ||
|
|
||
| ```javascript | ||
| var x = new Uint64( 5 ); | ||
| var str = x.toString(); | ||
| // returns '5' | ||
| ``` | ||
|
|
||
| #### Uint64.prototype.toJSON() | ||
|
|
||
| Returns a [JSON][json] representation of a `Uint64` instance. [`JSON.stringify()`][mdn-json-stringify] implicitly calls this method when stringifying a `Uint64` instance. | ||
|
|
||
| ```javascript | ||
| var x = new Uint64( 5 ); | ||
|
|
||
| var o = x.toJSON(); | ||
| /* | ||
| { | ||
| "type": "Uint64", | ||
| "high": 0, | ||
| "low": 5 | ||
| } | ||
| */ | ||
| ``` | ||
|
|
||
| To [revive][mdn-json-parse] a `Uint64` instance from a [JSON][json] string, see [@stdlib/number/uint64/reviver][@stdlib/number/uint64/reviver]. | ||
|
|
||
| #### Uint64.prototype.valueOf() | ||
|
|
||
| Converts a `Uint64` instance to a primitive value. | ||
|
|
||
| ```javascript | ||
| var x = new Uint64( 5 ); | ||
|
|
||
| var v = x.valueOf(); | ||
| // e.g., returns <bigint> | ||
| ``` | ||
|
|
||
| </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 | ||
|
|
||
| - An unsigned 64-bit integer has a range of \[`0`, `2^64-1`\]. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| * * * | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var Uint64 = require( '@stdlib/number/uint64/ctor' ); | ||
|
|
||
| var x = new Uint64( 1234 ); | ||
|
|
||
| console.log( 'type: %s', typeof x ); | ||
| // => 'type: object' | ||
|
|
||
| console.log( 'str: %s', x ); | ||
| // => 'str: 1234' | ||
|
|
||
| console.log( 'JSON: %s', JSON.stringify( x ) ); | ||
| // => 'JSON: {"type":"Uint64","words":[0,1234]}' | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
|
|
||
| <!-- 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. --> | ||
|
|
||
| <section class="references"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- 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"> | ||
|
|
||
| [json]: http://www.json.org/ | ||
|
|
||
| [mdn-json-stringify]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify | ||
|
|
||
| [mdn-json-parse]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse | ||
|
|
||
| [@stdlib/number/uint64/reviver]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/number/uint64/reviver | ||
|
|
||
| <!-- <related-links> --> | ||
|
|
||
| <!-- </related-links> --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> |
95 changes: 95 additions & 0 deletions
95
lib/node_modules/@stdlib/number/uint64/ctor/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,95 @@ | ||
| /** | ||
| * @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 bench = require( '@stdlib/bench' ); | ||
| var randi = require( '@stdlib/random/base/randi' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var Uint64 = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( format( '%s::constructor', pkg ), function benchmark( b ) { | ||
| var z; | ||
| var i; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| z = new Uint64( i ); | ||
| if ( typeof z !== 'object' ) { | ||
| b.fail( 'should return a Uint64 instance' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !( z instanceof Uint64 ) ) { | ||
| b.fail( 'should return a Uint64 instance' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
|
|
||
| // FIXME: add additional benchmarks | ||
|
|
||
| bench( format( '%s:toString', pkg ), function benchmark( b ) { | ||
| var o; | ||
| var z; | ||
| var i; | ||
|
|
||
| z = new Uint64( randi() ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| o = z.toString(); | ||
| if ( typeof o !== 'string' ) { | ||
| b.fail( 'should return a string' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( typeof o !== 'string' ) { | ||
| b.fail( 'should return a string' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
|
|
||
| bench( format( '%s:toJSON', pkg ), function benchmark( b ) { | ||
| var o; | ||
| var z; | ||
| var i; | ||
|
|
||
| z = new Uint64( randi() ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| o = z.toJSON(); | ||
| if ( typeof o !== 'object' ) { | ||
| b.fail( 'should return an object' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( typeof o !== 'object' ) { | ||
| b.fail( 'should return an object' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.