From 6be553837a65a57939949249f0e0eb2bbf4ac8a6 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Mon, 22 Jun 2026 16:20:13 +0530 Subject: [PATCH] feat: add float16 dtype support to array/zero-to-like --- .../@stdlib/array/zero-to-like/README.md | 1 + .../array/zero-to-like/benchmark/benchmark.js | 22 +++++ .../benchmark/benchmark.length.float16.js | 96 +++++++++++++++++++ .../@stdlib/array/zero-to-like/docs/repl.txt | 1 + .../array/zero-to-like/docs/types/index.d.ts | 36 +++++++ .../array/zero-to-like/docs/types/test.ts | 2 + .../@stdlib/array/zero-to-like/package.json | 3 + .../@stdlib/array/zero-to-like/test/test.js | 33 +++++++ 8 files changed, 194 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/zero-to-like/benchmark/benchmark.length.float16.js diff --git a/lib/node_modules/@stdlib/array/zero-to-like/README.md b/lib/node_modules/@stdlib/array/zero-to-like/README.md index 2a34888075c5..f902fd989f7a 100644 --- a/lib/node_modules/@stdlib/array/zero-to-like/README.md +++ b/lib/node_modules/@stdlib/array/zero-to-like/README.md @@ -53,6 +53,7 @@ The function recognizes the following data types: - `float64`: double-precision floating-point numbers (IEEE 754) - `float32`: single-precision floating-point numbers (IEEE 754) +- `float16`: half-precision floating-point numbers (IEEE 754) - `complex128`: double-precision complex floating-point numbers - `complex64`: single-precision complex floating-point numbers - `int32`: 32-bit two's complement signed integers diff --git a/lib/node_modules/@stdlib/array/zero-to-like/benchmark/benchmark.js b/lib/node_modules/@stdlib/array/zero-to-like/benchmark/benchmark.js index fcdeb20f683d..d5992a57b7b0 100644 --- a/lib/node_modules/@stdlib/array/zero-to-like/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/array/zero-to-like/benchmark/benchmark.js @@ -97,6 +97,28 @@ bench( format( '%s:dtype=float32', pkg ), function benchmark( b ) { b.end(); }); +bench( format( '%s:dtype=float16', pkg ), function benchmark( b ) { + var arr; + var x; + var i; + + x = empty( 0, 'float16' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = zeroToLike( x ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isTypedArrayLike( arr ) ) { + b.fail( 'should return a typed array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + bench( format( '%s:dtype=complex128', pkg ), function benchmark( b ) { var arr; var x; diff --git a/lib/node_modules/@stdlib/array/zero-to-like/benchmark/benchmark.length.float16.js b/lib/node_modules/@stdlib/array/zero-to-like/benchmark/benchmark.length.float16.js new file mode 100644 index 000000000000..33ae2ef71c3a --- /dev/null +++ b/lib/node_modules/@stdlib/array/zero-to-like/benchmark/benchmark.length.float16.js @@ -0,0 +1,96 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 pow = require( '@stdlib/math/base/special/pow' ); +var isTypedArray = require( '@stdlib/assert/is-typed-array' ); +var empty = require( '@stdlib/array/empty' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var zeroToLike = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = empty( len, 'float16' ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = zeroToLike( x ); + if ( arr.length !== len ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isTypedArray( arr ) ) { + b.fail( 'should return a typed array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:dtype=float16,len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/zero-to-like/docs/repl.txt b/lib/node_modules/@stdlib/array/zero-to-like/docs/repl.txt index 6c86b632d725..50d5710857b0 100644 --- a/lib/node_modules/@stdlib/array/zero-to-like/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/zero-to-like/docs/repl.txt @@ -8,6 +8,7 @@ - float64: double-precision floating-point numbers (IEEE 754). - float32: single-precision floating-point numbers (IEEE 754). + - float16: half-precision floating-point numbers (IEEE 754). - complex128: double-precision complex floating-point numbers. - complex64: single-precision complex floating-point numbers. - int32: 32-bit two's complement signed integers. diff --git a/lib/node_modules/@stdlib/array/zero-to-like/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/zero-to-like/docs/types/index.d.ts index 88f15b241ca5..c8804d5f343d 100644 --- a/lib/node_modules/@stdlib/array/zero-to-like/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/zero-to-like/docs/types/index.d.ts @@ -58,6 +58,24 @@ declare function zeroToLike( x: AnyArray, dtype: 'float64' ): Float64Array; */ declare function zeroToLike( x: AnyArray, dtype: 'float32' ): Float32Array; +/** +* Generates a linearly spaced numeric array whose elements increment by 1 starting from zero and having the same length as a provided input array. +* +* @param x - input array from which to derive the output array length +* @param dtype - data type +* @returns linearly spaced numeric array +* +* @example +* var zeros = require( '@stdlib/array/zeros' ); +* +* var x = zeros( 2, 'float64' ); +* // returns [ 0.0, 0.0 ] +* +* var y = zeroToLike( x, 'float16' ); +* // returns [ 0.0, 1.0 ] +*/ +declare function zeroToLike( x: AnyArray, dtype: 'float16' ): Float16Array; + /** * Generates a linearly spaced numeric array whose elements increment by 1 starting from zero and having the same length as a provided input array. * @@ -282,6 +300,24 @@ declare function zeroToLike( x: Float64Array, dtype?: NumericDataType ): Float64 */ declare function zeroToLike( x: Float32Array, dtype?: NumericDataType ): Float32Array; +/** +* Generates a linearly spaced numeric array whose elements increment by 1 starting from zero and having the same length and data type as a provided input array. +* +* @param x - input array from which to derive the output array length +* @param dtype - data type +* @returns linearly spaced numeric array +* +* @example +* var zeros = require( '@stdlib/array/zeros' ); +* +* var x = zeros( 2, 'float16' ); +* // returns [ 0.0, 0.0 ] +* +* var y = zeroToLike( x ); +* // returns [ 0.0, 1.0 ] +*/ +declare function zeroToLike( x: Float16Array, dtype?: NumericDataType ): Float16Array; + /** * Generates a linearly spaced numeric array whose elements increment by 1 starting from zero and having the same length and data type as a provided input array. * diff --git a/lib/node_modules/@stdlib/array/zero-to-like/docs/types/test.ts b/lib/node_modules/@stdlib/array/zero-to-like/docs/types/test.ts index f79d23af271f..47943d17a2ad 100644 --- a/lib/node_modules/@stdlib/array/zero-to-like/docs/types/test.ts +++ b/lib/node_modules/@stdlib/array/zero-to-like/docs/types/test.ts @@ -28,6 +28,7 @@ import zeroToLike = require( './index' ); zeroToLike( [ 0, 0 ] ); // $ExpectType number[] zeroToLike( new Float64Array( [ 0, 0 ] ) ); // $ExpectType Float64Array zeroToLike( new Float32Array( [ 0, 0 ] ) ); // $ExpectType Float32Array + zeroToLike( new Float16Array( [ 0, 0 ] ) ); // $ExpectType Float16Array zeroToLike( new Complex128Array( [ 0, 0 ] ) ); // $ExpectType Complex128Array zeroToLike( new Complex64Array( [ 0, 0 ] ) ); // $ExpectType Complex64Array zeroToLike( new Int32Array( [ 0, 0 ] ) ); // $ExpectType Int32Array @@ -40,6 +41,7 @@ import zeroToLike = require( './index' ); zeroToLike( [ 0, 0 ], 'float64' ); // $ExpectType Float64Array zeroToLike( [ 0, 0 ], 'float32' ); // $ExpectType Float32Array + zeroToLike( [ 0, 0 ], 'float16' ); // $ExpectType Float16Array zeroToLike( [ 0, 0 ], 'complex128' ); // $ExpectType Complex128Array zeroToLike( [ 0, 0 ], 'complex64' ); // $ExpectType Complex64Array zeroToLike( [ 0, 0 ], 'int32' ); // $ExpectType Int32Array diff --git a/lib/node_modules/@stdlib/array/zero-to-like/package.json b/lib/node_modules/@stdlib/array/zero-to-like/package.json index ab0d2129a5a2..c9fd7695e680 100644 --- a/lib/node_modules/@stdlib/array/zero-to-like/package.json +++ b/lib/node_modules/@stdlib/array/zero-to-like/package.json @@ -63,6 +63,7 @@ "matrix", "float64array", "float32array", + "float16array", "int32array", "uint32array", "int16array", @@ -83,6 +84,8 @@ "single", "float", "single-precision", + "half", + "half-precision", "float32", "ieee754", "integer", diff --git a/lib/node_modules/@stdlib/array/zero-to-like/test/test.js b/lib/node_modules/@stdlib/array/zero-to-like/test/test.js index e755a8611999..9b89c0c5fc8a 100644 --- a/lib/node_modules/@stdlib/array/zero-to-like/test/test.js +++ b/lib/node_modules/@stdlib/array/zero-to-like/test/test.js @@ -23,6 +23,7 @@ var tape = require( 'tape' ); var Float64Array = require( '@stdlib/array/float64' ); var Float32Array = require( '@stdlib/array/float32' ); +var Float16Array = require( '@stdlib/array/float16' ); var Int32Array = require( '@stdlib/array/int32' ); var Uint32Array = require( '@stdlib/array/uint32' ); var Int16Array = require( '@stdlib/array/int16' ); @@ -203,6 +204,38 @@ tape( 'the function returns a filled array (dtype=float32)', function test( t ) t.end(); }); +tape( 'the function returns a filled array (float16)', function test( t ) { + var expected; + var arr; + var x; + + x = new Float16Array( 5 ); + expected = new Float16Array( [ 0.0, 1.0, 2.0, 3.0, 4.0 ] ); + + arr = zeroToLike( x ); + t.strictEqual( instanceOf( arr, Float16Array ), true, 'returns expected value' ); + t.strictEqual( arr.length, expected.length, 'returns expected value' ); + t.deepEqual( arr, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a filled array (dtype=float16)', function test( t ) { + var expected; + var arr; + var x; + + x = new Float64Array( 5 ); + expected = new Float16Array( [ 0.0, 1.0, 2.0, 3.0, 4.0 ] ); + + arr = zeroToLike( x, 'float16' ); + t.strictEqual( instanceOf( arr, Float16Array ), true, 'returns expected value' ); + t.strictEqual( arr.length, expected.length, 'returns expected value' ); + t.deepEqual( arr, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns a filled array (complex128)', function test( t ) { var expected; var arr;