Skip to content
Open
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
120 changes: 120 additions & 0 deletions lib/node_modules/@stdlib/number/uint64/base/to-words/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<!--

@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 unsigned integer][uint64] into a higher order word and a lower order word.

<section class="usage">

## Usage

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

#### toWords( a )

Splits a [64-bit unsigned integer][uint64] into a higher order word (32-bit unsigned integer) and a lower order word (32-bit unsigned integer).

```javascript
var Uint64 = require( '@stdlib/number/uint64/ctor' );

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

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

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

By default, the function returns an `array` containing two elements: a higher order word and a lower order word. The lower order word contains the less significant bits, while the higher order word contains the more significant bits.

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

Splits a [64-bit unsigned integer][uint64] 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 Uint64 = require( '@stdlib/number/uint64/ctor' );

var out = new Uint32Array( 2 );

var a = new Uint64( 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 toWords = require( '@stdlib/number/uint64/base/to-words' );
var Uint64 = require( '@stdlib/number/uint64/ctor' );

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

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

a = new Uint64.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">

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

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

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

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @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 UINT32_MAX = require( '@stdlib/constants/uint32/max' );
var Uint64 = require( '@stdlib/number/uint64/ctor' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var toWords = require( './../lib' );


// MAIN //

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

values = discreteUniform( 100, 0, UINT32_MAX, {
'dtype': 'uint32'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
a = Uint64.of( values[ i % values.length ], values[ (i+1) % values.length ] ); // eslint-disable-line max-len
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 a;
var w;
var i;

values = discreteUniform( 100, 0, UINT32_MAX, {
'dtype': 'uint32'
});

out = new Uint32Array( 2 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
a = Uint64.of( values[ i % values.length ], values[ (i+1) % values.length ] ); // eslint-disable-line max-len
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();
});
64 changes: 64 additions & 0 deletions lib/node_modules/@stdlib/number/uint64/base/to-words/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

{{alias}}( a )
Splits a 64-bit unsigned 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.

Parameters
----------
a: Uint64
64-bit unsigned integer.

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

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


{{alias}}.assign( a, out, stride, offset )
Splits a 64-bit unsigned 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: Uint64
64-bit unsigned 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/uint64/ctor}}( 10000000000 )
<Uint64>[ 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
--------
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.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Collection, NumericArray } from '@stdlib/types/array';
import { Uint64 } from '@stdlib/types/number';

/**
* Interface describing `toWords`.
*/
interface ToWords {
/**
* Splits a 64-bit unsigned integer into a higher order word (unsigned 32-bit integer) and a lower order word (unsigned 32-bit integer).
*
* @param a - input value
* @returns output array
*
* @example
* var Uint64 = require( '@stdlib/number/uint64/ctor' );
*
* var a = new Uint64( 4294967296 );
* var w = toWords( a );
* // returns [ 1, 0 ]
*/
( a: Uint64 ): Array<number>;

/**
* Splits a 64-bit unsigned integer into a higher order word (unsigned 32-bit integer) and a lower order word (unsigned 32-bit integer) and assigns results to a provided output array.
*
* @param a - input value
* @param out - output array
* @param stride - stride length
* @param offset - starting index
* @returns output array
*
* @example
* var Uint32Array = require( '@stdlib/array/uint32' );
* var Uint64 = require( '@stdlib/number/uint64/ctor' );
*
* var out = new Uint32Array( 2 );
*
* var a = new Uint64( 4294967296 );
* var w = toWords.assign( a, out, 1, 0 );
* // returns <Uint32Array>[ 1, 0 ]
*
* var bool = ( w === out );
* // returns true
*/
assign<T extends NumericArray | Collection<number>>( a: Uint64, out: T, stride: number, offset: number ): T;
}

/**
* Splits a 64-bit unsigned integer into a higher order word (unsigned 32-bit integer) and a lower order word (unsigned 32-bit integer).
*
* @param a - input value
* @returns output array
*
* @example
* var Uint64 = require( '@stdlib/number/uint64/ctor' );
*
* var a = new Uint64( 4294967296 );
* var w = toWords( a );
* // returns [ 1, 0 ]
*
* @example
* var Uint32Array = require( '@stdlib/array/uint32' );
* var Uint64 = require( '@stdlib/number/uint64/ctor' );
*
* var out = new Uint32Array( 2 );
*
* var a = new Uint64( 4294967296 );
* var w = toWords.assign( a, out, 1, 0 );
* // returns <Uint32Array>[ 1, 0 ]
*
* var bool = ( w === out );
* // returns true
*/
declare var toWords: ToWords;


// EXPORTS //

export = toWords;
Loading
Loading