Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions lib/node_modules/@stdlib/number/int64/base/to-words/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<!--

@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.

-->

# toWords

> Split a [64-bit signed integer][@stdlib/number/int64/ctor] into a higher order word and a lower order word.

<section class="usage">

## Usage

```javascript
var toWords = require( '@stdlib/number/int64/base/to-words' );
```

#### toWords( a )

Splits a [64-bit signed integer][@stdlib/number/int64/ctor] into a higher order word (32-bit unsigned integer) and a lower order word (32-bit unsigned integer).

```javascript
var Int64 = require( '@stdlib/number/int64/ctor' );

var a = new Int64( 4294967296 );
var w = toWords( a );
// returns [ 1, 0 ]

var high = w[ 0 ];
// returns 1

var low = w[ 1 ];
// returns 0
```

For negative values, the function returns the words of the two's complement representation.

```javascript
var Int64 = require( '@stdlib/number/int64/ctor' );

var a = new Int64( -1 );
var w = toWords( a );
// returns [ 4294967295, 4294967295 ]
```

#### toWords.assign( a, out, stride, offset )

Splits a [64-bit signed integer][@stdlib/number/int64/ctor] into a higher order word (32-bit unsigned integer) and a lower order word (32-bit unsigned integer) and assigns results to a provided output array.

```javascript
var Uint32Array = require( '@stdlib/array/uint32' );
var Int64 = require( '@stdlib/number/int64/ctor' );

var out = new Uint32Array( 2 );

var a = new Int64( 4294967296 );
var w = toWords.assign( a, out, 1, 0 );
// returns <Uint32Array>[ 1, 0 ]

var bool = ( w === out );
// returns true
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

```javascript
var Int64 = require( '@stdlib/number/int64/ctor' );
var toWords = require( '@stdlib/number/int64/base/to-words' );

var a = new Int64( 4294967296 );
var w = toWords( a );
// returns [ 1, 0 ]

a = new Int64( 10000000000 );
w = toWords( a );
// returns [ 2, 1410065408 ]

a = new Int64( -1 );
w = toWords( a );
// returns [ 4294967295, 4294967295 ]

a = new Int64( -1000000000000 );
w = toWords( a );
// returns [ 4294967063, 727379968 ]

a = Int64.of( 12, 34 );
w = toWords( a );
// returns [ 12, 34 ]
```

</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">

[@stdlib/number/int64/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/number/int64/ctor

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @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 Uint32Array = require( '@stdlib/array/uint32' );
var isArray = require( '@stdlib/assert/is-array' );
var MIN_SAFE_INTEGER = require( '@stdlib/constants/float64/min-safe-integer' );
var MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' );
var Int64 = require( '@stdlib/number/int64/ctor' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var toWords = require( './../lib' );


// VARIABLES //

var rand = discreteUniform( MIN_SAFE_INTEGER, MAX_SAFE_INTEGER );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var N;
var a;
var i;
var w;

N = 100;
values = [];
for ( i = 0; i < N; i++ ) {
values.push( new Int64( rand() ) );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
a = values[ i % N ];
w = toWords( a );
if ( typeof w !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( !isArray( w ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s:assign', pkg ), function benchmark( b ) {
var values;
var out;
var N;
var a;
var i;
var w;

N = 100;
values = [];
for ( i = 0; i < N; i++ ) {
values.push( new Int64( rand() ) );
}

out = new Uint32Array( 2 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
a = values[ i % N ];
w = toWords.assign( a, out, 1, 0 );
if ( typeof w !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( w !== out ) {
b.fail( 'should return the output array' );
}
b.pass( 'benchmark finished' );
b.end();
});
71 changes: 71 additions & 0 deletions lib/node_modules/@stdlib/number/int64/base/to-words/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

{{alias}}( a )
Splits a 64-bit signed integer into a higher order word (32-bit unsigned
integer) and a lower order word (32-bit unsigned integer).

The function returns an array with two elements: a higher order word and a
lower order word, respectively. The lower order word contains the less
significant bits, while the higher order word contains the more significant
bits.

For negative values, the function returns the words of the two's complement
representation.

Parameters
----------
a: Int64
64-bit signed integer.

Returns
-------
out: Array<uinteger>
Higher and lower order words.

Examples
--------
> var a = new {{alias:@stdlib/number/int64/ctor}}( 10000000000 )
<Int64>[ 10000000000n ]
> var w = {{alias}}( a )
[ 2, 1410065408 ]
> a = new {{alias:@stdlib/number/int64/ctor}}( -1 )
<Int64>[ -1n ]
> w = {{alias}}( a )
[ 4294967295, 4294967295 ]


{{alias}}.assign( a, out, stride, offset )
Splits a 64-bit signed integer into a higher order word (32-bit unsigned
integer) and a lower order word (32-bit unsigned integer) and assigns
results to a provided output array.

Parameters
----------
a: Int64
64-bit signed integer.

out: Array|TypedArray|Object
Output array.

stride: integer
Output array stride.

offset: integer
Output array index offset.

Returns
-------
out: Array|TypedArray|Object
Higher and lower order words.

Examples
--------
> var a = new {{alias:@stdlib/number/int64/ctor}}( 10000000000 )
<Int64>[ 10000000000n ]
> var out = new {{alias:@stdlib/array/uint32}}( 2 );
> var w = {{alias}}.assign( a, out, 1, 0 )
<Uint32Array>[ 2, 1410065408 ]
> var bool = ( w === out )
true

See Also
--------
Loading