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
157 changes: 157 additions & 0 deletions lib/node_modules/@stdlib/strided/base/reinterpret-float16/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<!--

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

-->

# reinterpret

> Reinterpret a [`Float16Array`][@stdlib/array/float16] as a [`Uint16Array`][@stdlib/array/uint16].

<!-- 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 reinterpret = require( '@stdlib/strided/base/reinterpret-float16' );
```

#### reinterpret( x, offset )

Returns a [`Uint16Array`][@stdlib/array/uint16] view of a [`Float16Array`][@stdlib/array/float16].

```javascript
var Float16Array = require( '@stdlib/array/float16' );

var x = new Float16Array( 10 );

var view = reinterpret( x, 0 );
// returns <Uint16Array>

var bool = ( view.buffer === x.buffer );
// returns true

var len = view.length;
// returns 10
```

The `offset` argument specifies the starting index of the returned [`Uint16Array`][@stdlib/array/uint16] view relative to the [`Float16Array`][@stdlib/array/float16].

```javascript
var Float16Array = require( '@stdlib/array/float16' );

var x = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );

var view = reinterpret( x, 2 );
// returns <Uint16Array>

var len = view.length;
// returns 6

var v = view[ 0 ];
// returns 16896

v = view[ 1 ];
// returns 17408
```

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

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var Float16Array = require( '@stdlib/array/float16' );
var reinterpret = require( '@stdlib/strided/base/reinterpret-float16' );

// Define a half-precision floating-point number array:
var x = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
// returns <Float16Array>

// Reinterpret as a `uint16` array:
var view = reinterpret( x, 0 );
// returns <Uint16Array>

// Set view elements:
view[ 0 ] = 16384; // 2.0
view[ 1 ] = 15360; // 1.0

// Get the first element of the half-precision floating-point number array:
var v = x[ 0 ];
// returns 2.0

// Get the second element of the half-precision floating-point number array:
v = x[ 1 ];
// returns 1.0
```

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

[@stdlib/array/float16]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float16

[@stdlib/array/uint16]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint16

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @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 Float16Array = require( '@stdlib/array/float16' );
var isUint16Array = require( '@stdlib/assert/is-Uint16Array' );
var pkg = require( './../package.json' ).name;
var reinterpret = require( './../lib' );


// MAIN //

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

values = [
new Float16Array( 10 ),
new Float16Array( 5 ),
new Float16Array( 20 )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = reinterpret( values[ i%values.length ], 1 );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isUint16Array( out ) ) {
b.fail( 'should return a Uint16Array' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

{{alias}}( x, offset )
Returns a Uint16Array view of a Float16Array.

Parameters
----------
x: Float16Array
Input array.

offset: integer
Starting index of the view relative to the Float16Array.

Returns
-------
out: Uint16Array
Uint16Array view.

Examples
--------
> var x = new {{alias:@stdlib/array/float16}}( 10 );
> var out = {{alias}}( x, 0 )
<Uint16Array>
> var bool = ( out.buffer === x.buffer )
true

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* @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 { Float16Array } from '@stdlib/types/array';

/**
* Reinterprets a `Float16Array` as a `Uint16Array`.
*
* @param x - input array
* @param offset - starting index
* @returns `Uint16Array` view
*
* @example
* var Float16Array = require( '@stdlib/array/float16' );
*
* var x = new Float16Array( 10 );
*
* var out = reinterpret( x, 0 );
* // returns <Uint16Array>
*
* var bool = ( out.buffer === x.buffer );
* // returns true
*/
declare function reinterpret( x: Float16Array, offset: number ): Uint16Array;


// EXPORTS //

export = reinterpret;
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* @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.
*/

import Float16Array = require( '@stdlib/array/float16' );
import reinterpret = require( './index' );


// TESTS //

// The function returns a Uint16Array...
{
reinterpret( new Float16Array( 10 ), 0 ); // $ExpectType Uint16Array
}

// The compiler throws an error if not provided a first argument which is a Float16Array...
{
reinterpret( '10', 0 ); // $ExpectError
reinterpret( 10, 0 ); // $ExpectError
reinterpret( true, 0 ); // $ExpectError
reinterpret( false, 0 ); // $ExpectError
reinterpret( null, 0 ); // $ExpectError
reinterpret( undefined, 0 ); // $ExpectError
reinterpret( [], 0 ); // $ExpectError

Check failure on line 38 in lib/node_modules/@stdlib/strided/base/reinterpret-float16/docs/types/test.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Expected an error on this line, but found none
reinterpret( {}, 0 ); // $ExpectError
reinterpret( ( x: number ): number => x, 0 ); // $ExpectError
}

// The compiler throws an error if not provided a second argument which is a number...
{
const x = new Float16Array( 10 );

reinterpret( x, '10' ); // $ExpectError
reinterpret( x, true ); // $ExpectError
reinterpret( x, false ); // $ExpectError
reinterpret( x, null ); // $ExpectError
reinterpret( x, undefined ); // $ExpectError
reinterpret( x, [] ); // $ExpectError
reinterpret( x, {} ); // $ExpectError
reinterpret( x, ( x: number ): number => x ); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @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';

var Float16Array = require( '@stdlib/array/float16' );
var reinterpret = require( './../lib' );

// Define a half-precision floating-point number array:
var x = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
// returns <Float16Array>

// Reinterpret as a `uint16` array:
var view = reinterpret( x, 0 );
// returns <Uint16Array>

// Set view elements:
view[ 0 ] = 16384; // 2.0
view[ 1 ] = 15360; // 1.0

// Get the first element of the half-precision floating-point number array:
var v = x[ 0 ];
// returns 2.0

// Get the second element of the half-precision floating-point number array:
v = x[ 1 ];
// returns 1.0

console.log( v );
Loading
Loading