Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 48x 48x 48x 48x 1x 1x 47x 48x 2x 2x 2x 2x 48x 35x 35x 45x 10x 10x 35x 35x 48x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 24x 24x 8x 8x 24x 3x 3x 13x 24x 1x 1x 1x 1x 12x 12x 12x 12x 12x 24x 24x 12x 24x 10x 10x 10x 10x 10x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 22x 22x 10x 10x 22x 10x 10x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 539x 539x 2x 539x 525x 537x 12x 12x 527x 527x 527x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 531x 531x 531x 4x 4x 4x 4x 4x 4x | /**
* @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.
*/
/* eslint-disable max-len, no-restricted-syntax, no-invalid-this */
'use strict';
// MODULES //
var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' );
var isUint32Array = require( '@stdlib/assert/is-uint32array' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var isBigInt = require( '@stdlib/assert/is-bigint' ).isPrimitive;
var isCollection = require( '@stdlib/assert/is-collection' );
var isBetween = require( '@stdlib/assert/is-between' );
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
var BigInt = require( '@stdlib/bigint/ctor' );
var Uint32Array = require( '@stdlib/array/uint32' );
var join = require( '@stdlib/array/base/join' );
var UINT32_MAX = require( '@stdlib/constants/uint32/max' );
var MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' );
var format = require( '@stdlib/string/format' );
var indices = require( './indices.js' );
var int2str = require( './to_string.js' );
// VARIABLES //
var HAS_BIGINT = hasBigIntSupport();
var TWO_32 = 0x100000000; // 2^32
// MAIN //
/**
* Unsigned 64-bit integer constructor.
*
* @constructor
* @param {(NonNegativeInteger|bigint)} value - integer value
* @throws {TypeError} must invoke using the `new` keyword
* @throws {TypeError} must provide a number on the interval [0, 2^64]
* @returns {Uint64} unsigned 64-bit integer
*
* @example
* var v = new Uint64( 5 );
* // returns <Uint64>
*/
function Uint64( value ) {
var buffer;
var v;
if ( !( this instanceof Uint64 ) ) {
throw new TypeError( 'invalid invocation. Constructor must be called with the `new` keyword.' );
}
buffer = new Uint32Array( 2 );
if ( isBigInt( value ) ) {
v = BigInt.asUintN( 64, value );
if ( v !== value ) {
throw new TypeError( format( 'invalid argument. Must provide an integer on the interval [0, 2^64-1]. Value: `%s`.', value ) );
}
buffer[ indices.HIGH ] = v >> BigInt( 32 );
buffer[ indices.LOW ] = v & BigInt( UINT32_MAX );
} else if ( isInteger( value ) && isBetween( value, 0, MAX_SAFE_INTEGER ) ) {
buffer[ indices.HIGH ] = ( value / TWO_32 ) >>> 0;
buffer[ indices.LOW ] = value >>> 0;
} else {
throw new TypeError( format( 'invalid argument. Must provide an integer on the interval [0, 2^53-1]. Value: `%s`.', value ) );
}
setReadOnly( this, '_buffer', buffer );
return this;
}
/**
* Constructor name.
*
* @name name
* @memberof Uint64
* @readonly
* @type {string}
* @default 'Uint64'
*
* @example
* var name = Uint64.name;
* // returns 'Uint64'
*/
setReadOnly( Uint64, 'name', 'Uint64' );
/**
* Creates a new unsigned 64-bit integer from an array-like object containing a high and low word.
*
* @name from
* @memberof Uint64
* @type {Function}
* @param {Collection} words - high and low words
* @throws {TypeError} must provide an array-like object containing two elements
* @returns {Uint64} unsigned 64-bit integer
*
* @example
* var x = Uint64.from( [ 1234, 5678 ] );
* // returns <Uint64>
*/
setReadOnly( Uint64, 'from', function fromWords( words ) {
var v;
if ( !isCollection( words ) ) {
throw new TypeError( format( 'invalid argument. Must provide an array. Value: `%s`.', words ) );
}
if ( words.length !== 2 ) {
throw new TypeError( format( 'invalid argument. Must provide an array of length 2. Value: `[%s]`.', join( words, ',' ) ) );
}
v = new Uint64( 0 );
if ( isUint32Array( words ) ) {
v._buffer[ indices.HIGH ] = words[ 0 ]; // eslint-disable-line no-underscore-dangle
v._buffer[ indices.LOW ] = words[ 1 ]; // eslint-disable-line no-underscore-dangle
return v;
}
// TODO: consider supporting any iterable object
if (
isInteger( words[ 0 ] ) &&
isInteger( words[ 1 ] ) &&
isBetween( words[ 0 ], 0, UINT32_MAX ) &&
isBetween( words[ 1 ], 0, UINT32_MAX )
) {
// TODO: consider whether we should support accessor arrays here
v._buffer[ indices.HIGH ] = words[ 0 ]; // eslint-disable-line no-underscore-dangle
v._buffer[ indices.LOW ] = words[ 1 ]; // eslint-disable-line no-underscore-dangle
return v;
}
throw new TypeError( format( 'invalid argument. Must provide an array of integer values on the interval [%u, %u]. Value: `[%s]`.', 0, UINT32_MAX, join( words, ', ' ) ) );
});
/**
* Creates a new unsigned 64-bit integer from a high and low word.
*
* @name of
* @memberof Uint64
* @type {Function}
* @param {NonNegativeInteger} high - high word
* @param {NonNegativeInteger} low - low word
* @throws {TypeError} first argument must be a nonnegative integer
* @throws {TypeError} second argument must be a nonnegative integer
* @returns {Uint64} unsigned 64-bit integer
*
* @example
* var x = Uint64.of( 1234, 5678 );
* // returns <Uint64>
*/
setReadOnly( Uint64, 'of', function of( high, low ) {
var v;
if ( !isInteger( high ) || !isBetween( high, 0, UINT32_MAX ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a nonnegative integer on the interval [%u, %u]. Value: `%s`.', 0, UINT32_MAX, high ) );
}
if ( !isInteger( low ) || !isBetween( low, 0, UINT32_MAX ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer on the interval [%u, %u]. Value: `%s`.', 0, UINT32_MAX, low ) );
}
v = new Uint64( 0 );
v._buffer[ indices.HIGH ] = high; // eslint-disable-line no-underscore-dangle
v._buffer[ indices.LOW ] = low; // eslint-disable-line no-underscore-dangle
return v;
});
/**
* Size (in bytes) of the underlying value.
*
* @name BYTES_PER_ELEMENT
* @memberof Uint64
* @type {integer}
* @returns {integer} size in bytes
*
* @example
* var nbytes = Uint64.BYTES_PER_ELEMENT;
* // returns 8
*/
setReadOnly( Uint64, 'BYTES_PER_ELEMENT', 8 );
/**
* Size (in bytes) of the underlying value.
*
* @name BYTES_PER_ELEMENT
* @memberof Uint64.prototype
* @type {integer}
* @returns {integer} size in bytes
*
* @example
* var x = new Uint64( 5 );
*
* var nbytes = x.BYTES_PER_ELEMENT;
* // returns 8
*/
setReadOnly( Uint64.prototype, 'BYTES_PER_ELEMENT', 8 );
/**
* Size (in bytes) of the underlying value.
*
* @name byteLength
* @memberof Uint64.prototype
* @type {integer}
* @returns {integer} size in bytes
*
* @example
* var x = new Uint64( 5 );
*
* var nbytes = x.byteLength;
* // returns 8
*/
setReadOnly( Uint64.prototype, 'byteLength', 8 );
/**
* Returns the high 32-bit word of an unsigned 64-bit integer.
*
* @name hi
* @memberof Uint64.prototype
* @type {uinteger}
* @returns {uinteger} 32-bit unsigned integer
*
* @example
* var x = Uint64.from( [ 1234, 5678 ] );
*
* var w = x.hi;
* // returns 1234
*/
setReadOnlyAccessor( Uint64.prototype, 'hi', function getHighWord() {
return this._buffer[ indices.HIGH ];
});
/**
* Returns the low 32-bit word of an unsigned 64-bit integer.
*
* @name lo
* @memberof Uint64.prototype
* @type {uinteger}
* @returns {uinteger} 32-bit unsigned integer
*
* @example
* var x = Uint64.from( [ 1234, 5678 ] );
*
* var w = x.lo;
* // returns 5678
*/
setReadOnlyAccessor( Uint64.prototype, 'lo', function getLowWord() {
return this._buffer[ indices.LOW ];
});
/**
* Serializes an unsigned 64-bit integer as a string.
*
* @name toString
* @memberof Uint64.prototype
* @type {Function}
* @param {PositiveInteger} [radix=10] - radix (base) to use for string conversion (2-36)
* @throws {TypeError} must provide an integer on the interval [2,36]
* @returns {string} serialized unsigned 64-bit integer
*
* @example
* var str = new Uint64( 5 ).toString();
* // returns '5'
*
* str = Uint64.from( [ 1, 0 ] ).toString();
* // returns '4294967296'
*
* str = new Uint64( 100000000001 ).toString();
* // returns '100000000001'
*
*/
setReadOnly( Uint64.prototype, 'toString', function toString( radix ) {
var rad;
if ( arguments.length < 1 ) {
rad = 10;
} else if ( isInteger( radix ) && isBetween( radix, 2, 36 ) ) {
rad = radix;
} else {
throw new TypeError( format( 'invalid argument. Must provide an integer on the interval [%d, %d]. Value: `%s`.', 2, 36, radix ) );
}
if ( HAS_BIGINT ) {
return BigInt( this.valueOf() ).toString( rad );
}
return int2str( [ this.hi, this.lo ], rad );
});
/**
* Serializes an unsigned 64-bit integer as a JSON object.
*
* ## Notes
*
* - `JSON.stringify()` implicitly calls this method when stringifying a `Uint64` instance.
*
* @name toJSON
* @memberof Uint64.prototype
* @type {Function}
* @returns {Object} serialized unsigned 64-bit integer
*
* @example
* var x = new Uint64( 5 );
*
* var y = x.toJSON();
* // returns { 'type': 'Uint64', 'words': [ 0, 5 ] }
*/
setReadOnly( Uint64.prototype, 'toJSON', function toJSON() {
return {
'type': 'Uint64',
'words': [ this.hi, this.lo ]
};
});
/**
* Converts an unsigned 64-bit integer to a primitive value.
*
* @name valueOf
* @memberof Uint64.prototype
* @type {Function}
* @returns {(number|bigint)} primitive value
*
* @example
* var x = new Uint64( 5 );
*
* var v = x.valueOf();
* // e.g., returns <bigint>
*/
setReadOnly( Uint64.prototype, 'valueOf', function valueOf() {
if ( HAS_BIGINT ) {
return ( BigInt( this._buffer[ indices.HIGH ] ) << BigInt( 32 ) ) | BigInt( this._buffer[ indices.LOW ] );
}
return ( this._buffer[ indices.HIGH ] * TWO_32 ) + this._buffer[ indices.LOW ];
});
// EXPORTS //
module.exports = Uint64;
|