From 8bd75a9b7e792e6a01b00534270bf58ec542f577 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Tue, 5 May 2026 01:05:31 +0530 Subject: [PATCH] feat: add strided/base/reinterpret-float16 --- .../base/reinterpret-float16/README.md | 157 ++++++++++++++++++ .../benchmark/benchmark.js | 56 +++++++ .../base/reinterpret-float16/docs/repl.txt | 28 ++++ .../reinterpret-float16/docs/types/index.d.ts | 48 ++++++ .../reinterpret-float16/docs/types/test.ts | 55 ++++++ .../reinterpret-float16/examples/index.js | 44 +++++ .../base/reinterpret-float16/lib/index.js | 46 +++++ .../base/reinterpret-float16/lib/main.js | 53 ++++++ .../base/reinterpret-float16/package.json | 63 +++++++ .../base/reinterpret-float16/test/test.js | 101 +++++++++++ 10 files changed, 651 insertions(+) create mode 100644 lib/node_modules/@stdlib/strided/base/reinterpret-float16/README.md create mode 100644 lib/node_modules/@stdlib/strided/base/reinterpret-float16/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/strided/base/reinterpret-float16/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/strided/base/reinterpret-float16/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/strided/base/reinterpret-float16/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/strided/base/reinterpret-float16/examples/index.js create mode 100644 lib/node_modules/@stdlib/strided/base/reinterpret-float16/lib/index.js create mode 100644 lib/node_modules/@stdlib/strided/base/reinterpret-float16/lib/main.js create mode 100644 lib/node_modules/@stdlib/strided/base/reinterpret-float16/package.json create mode 100644 lib/node_modules/@stdlib/strided/base/reinterpret-float16/test/test.js diff --git a/lib/node_modules/@stdlib/strided/base/reinterpret-float16/README.md b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/README.md new file mode 100644 index 000000000000..70e5e4a38200 --- /dev/null +++ b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/README.md @@ -0,0 +1,157 @@ + + +# reinterpret + +> Reinterpret a [`Float16Array`][@stdlib/array/float16] as a [`Uint16Array`][@stdlib/array/uint16]. + + + +
+ +
+ + + + + +
+ +## 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 + +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 + +var len = view.length; +// returns 6 + +var v = view[ 0 ]; +// returns 16896 + +v = view[ 1 ]; +// returns 17408 +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```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 + +// Reinterpret as a `uint16` array: +var view = reinterpret( x, 0 ); +// returns + +// 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 +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/strided/base/reinterpret-float16/benchmark/benchmark.js b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/benchmark/benchmark.js new file mode 100644 index 000000000000..6b1997ea2b7d --- /dev/null +++ b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/benchmark/benchmark.js @@ -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(); +}); diff --git a/lib/node_modules/@stdlib/strided/base/reinterpret-float16/docs/repl.txt b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/docs/repl.txt new file mode 100644 index 000000000000..f29ce362c3c0 --- /dev/null +++ b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/docs/repl.txt @@ -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 ) + + > var bool = ( out.buffer === x.buffer ) + true + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/strided/base/reinterpret-float16/docs/types/index.d.ts b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/docs/types/index.d.ts new file mode 100644 index 000000000000..0b3c0b78fcf7 --- /dev/null +++ b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/docs/types/index.d.ts @@ -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 + +/// + +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 +* +* var bool = ( out.buffer === x.buffer ); +* // returns true +*/ +declare function reinterpret( x: Float16Array, offset: number ): Uint16Array; + + +// EXPORTS // + +export = reinterpret; diff --git a/lib/node_modules/@stdlib/strided/base/reinterpret-float16/docs/types/test.ts b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/docs/types/test.ts new file mode 100644 index 000000000000..eb632a523a1a --- /dev/null +++ b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/docs/types/test.ts @@ -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 + 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 +} diff --git a/lib/node_modules/@stdlib/strided/base/reinterpret-float16/examples/index.js b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/examples/index.js new file mode 100644 index 000000000000..641d77f77f71 --- /dev/null +++ b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/examples/index.js @@ -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 + +// Reinterpret as a `uint16` array: +var view = reinterpret( x, 0 ); +// returns + +// 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 ); diff --git a/lib/node_modules/@stdlib/strided/base/reinterpret-float16/lib/index.js b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/lib/index.js new file mode 100644 index 000000000000..a998b08fb37a --- /dev/null +++ b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/lib/index.js @@ -0,0 +1,46 @@ +/** +* @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'; + +/** +* Reinterpret a `Float16Array` as a `Uint16Array`. +* +* @module @stdlib/strided/base/reinterpret-float16 +* +* @example +* var Float16Array = require( '@stdlib/array/float16' ); +* var reinterpret = require( '@stdlib/strided/base/reinterpret-float16' ); +* +* var x = new Float16Array( 10 ); +* +* var out = reinterpret( x, 0 ); +* // returns +* +* var bool = ( out.buffer === x.buffer ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/strided/base/reinterpret-float16/lib/main.js b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/lib/main.js new file mode 100644 index 000000000000..709e8b6a5037 --- /dev/null +++ b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/lib/main.js @@ -0,0 +1,53 @@ +/** +* @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 Uint16Array = require( '@stdlib/array/uint16' ); + + +// MAIN // + +/** +* Reinterprets a `Float16Array` as a `Uint16Array`. +* +* @param {Float16Array} x - input array +* @param {NonNegativeInteger} offset - starting index +* @returns {Uint16Array} `Uint16Array` view +* +* @example +* var Float16Array = require( '@stdlib/array/float16' ); +* +* var x = new Float16Array( 10 ); +* +* var out = reinterpret( x, 0 ); +* // returns +* +* var bool = ( out.buffer === x.buffer ); +* // returns true +*/ +function reinterpret( x, offset ) { + return new Uint16Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offset), x.length-offset ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = reinterpret; diff --git a/lib/node_modules/@stdlib/strided/base/reinterpret-float16/package.json b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/package.json new file mode 100644 index 000000000000..b5637ad31ae6 --- /dev/null +++ b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/package.json @@ -0,0 +1,63 @@ +{ + "name": "@stdlib/strided/base/reinterpret-float16", + "version": "0.0.0", + "description": "Reinterpret a Float16Array as a Uint16Array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "base", + "strided", + "array", + "float16", + "uint16", + "reinterpret", + "cast", + "view" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/strided/base/reinterpret-float16/test/test.js b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/test/test.js new file mode 100644 index 000000000000..a44fede0e39d --- /dev/null +++ b/lib/node_modules/@stdlib/strided/base/reinterpret-float16/test/test.js @@ -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 tape = require( 'tape' ); +var Float16Array = require( '@stdlib/array/float16' ); +var isUint16Array = require( '@stdlib/assert/is-Uint16Array' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var reinterpret = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof reinterpret, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function reinterprets a half-precision floating-point number array', function test( t ) { + var values; + var v; + var i; + + values = [ + new Float16Array( 10 ), + new Float16Array( 5 ), + new Float16Array( 20 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + v = reinterpret( values[ i ], 0 ); + t.strictEqual( isUint16Array( v ), true, 'returns a Uint16Array' ); + t.strictEqual( v.buffer, values[ i ].buffer, 'returns a view' ); + t.strictEqual( v.length, values[ i ].length, 'has expected length' ); + t.strictEqual( v.byteOffset, 0, 'has expected byte offset' ); + } + t.end(); +}); + +tape( 'the function reinterprets a half-precision floating-point number array (byte offset)', function test( t ) { + var values; + var buf; + var v; + var i; + + buf = new ArrayBuffer( 1000 ); + values = [ + new Float16Array( buf, 160, 10 ), + new Float16Array( buf, 16, 8 ), + new Float16Array( buf, 56, 20 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + v = reinterpret( values[ i ], 0 ); + t.strictEqual( isUint16Array( v ), true, 'returns a Uint16Array' ); + t.strictEqual( v.buffer, buf, 'returns a view' ); + t.strictEqual( v.length, values[ i ].length, 'has expected length' ); + t.strictEqual( v.byteOffset, values[ i ].byteOffset, 'has expected byte offset' ); + } + t.end(); +}); + +tape( 'the function reinterprets a half-precision floating-point number array (index offset)', function test( t ) { + var values; + var v; + var i; + + values = [ + new Float16Array( 10 ), + new Float16Array( 5 ), + new Float16Array( 20 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + v = reinterpret( values[ i ], 2 ); + t.strictEqual( isUint16Array( v ), true, 'returns a Uint16Array' ); + t.strictEqual( v.buffer, values[ i ].buffer, 'returns a view' ); + t.strictEqual( v.length, (values[ i ].length-2), 'has expected length' ); + t.strictEqual( v.byteOffset, 4, 'has expected byte offset' ); + } + t.end(); +});