From b769fb82a13c73a72eab899e1547e5a95dbebc14 Mon Sep 17 00:00:00 2001 From: gururaj1512 Date: Fri, 30 Jan 2026 02:13:05 +0530 Subject: [PATCH] feat: add `number/float16/base/normalize` --- .../number/float16/base/normalize/README.md | 271 ++++++++++++++++++ .../base/normalize/benchmark/benchmark.js | 79 +++++ .../normalize/benchmark/benchmark.native.js | 64 +++++ .../base/normalize/benchmark/c/Makefile | 146 ++++++++++ .../base/normalize/benchmark/c/benchmark.c | 139 +++++++++ .../number/float16/base/normalize/binding.gyp | 170 +++++++++++ .../float16/base/normalize/docs/repl.txt | 80 ++++++ .../base/normalize/docs/types/index.d.ts | 124 ++++++++ .../float16/base/normalize/docs/types/test.ts | 109 +++++++ .../base/normalize/examples/c/Makefile | 146 ++++++++++ .../base/normalize/examples/c/example.c | 39 +++ .../float16/base/normalize/examples/index.js | 41 +++ .../float16/base/normalize/include.gypi | 53 ++++ .../stdlib/number/float16/base/normalize.h | 41 +++ .../float16/base/normalize/lib/assign.js | 119 ++++++++ .../float16/base/normalize/lib/index.js | 68 +++++ .../number/float16/base/normalize/lib/main.js | 67 +++++ .../float16/base/normalize/lib/native.js | 75 +++++ .../float16/base/normalize/manifest.json | 94 ++++++ .../float16/base/normalize/package.json | 75 +++++ .../float16/base/normalize/src/Makefile | 70 +++++ .../number/float16/base/normalize/src/addon.c | 118 ++++++++ .../number/float16/base/normalize/src/main.c | 73 +++++ .../base/normalize/test/test.assign.js | 184 ++++++++++++ .../float16/base/normalize/test/test.js | 40 +++ .../float16/base/normalize/test/test.main.js | 97 +++++++ .../base/normalize/test/test.native.js | 107 +++++++ 27 files changed, 2689 insertions(+) create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/README.md create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/c/benchmark.c create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/binding.gyp create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/examples/index.js create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/include.gypi create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/include/stdlib/number/float16/base/normalize.h create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/lib/assign.js create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/lib/index.js create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/lib/main.js create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/lib/native.js create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/manifest.json create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/package.json create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/src/Makefile create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/src/addon.c create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/src/main.c create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/test/test.assign.js create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/test/test.js create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/test/test.main.js create mode 100644 lib/node_modules/@stdlib/number/float16/base/normalize/test/test.native.js diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/README.md b/lib/node_modules/@stdlib/number/float16/base/normalize/README.md new file mode 100644 index 000000000000..cd01e72225a9 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/README.md @@ -0,0 +1,271 @@ + + +# normalize + +> Return a normal number `y` and exponent `exp` satisfying `x = y * 2^exp`. + +
+ +## Usage + +```javascript +var normalize = require( '@stdlib/number/float16/base/normalize' ); +``` + +#### normalize( x ) + +Returns a normal number `y` and exponent `exp` satisfying `x = y * 2^exp`. + +```javascript +var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); + +var out = normalize( toFloat16( 1.401e-6 ) ); +// returns [ 0.00146484375, -10 ] +``` + +By default, the function returns `y` and `exp` as a two-element `array`. + +```javascript +var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var pow = require( '@stdlib/math/base/special/pow' ); + +var out = normalize( toFloat16( 1.401e-6 ) ); +// returns [ 0.00146484375, -10 ] + +var y = out[ 0 ]; +var exp = out[ 1 ]; + +var bool = ( y*pow(2, exp) === toFloat16(1.401e-6) ); +// returns true +``` + +The function expects a finite, non-zero [half-precision floating-point number][ieee754] `x`. If `x == 0`, + +```javascript +var out = normalize( 0.0 ); +// returns [ 0.0, 0 ]; +``` + +If `x` is either positive or negative `infinity` or `NaN`, + +```javascript +var PINF = require( '@stdlib/constants/float16/pinf' ); +var NINF = require( '@stdlib/constants/float16/ninf' ); + +var out = normalize( PINF ); +// returns [ Infinity, 0 ] + +out = normalize( NINF ); +// returns [ -Infinity, 0 ] + +out = normalize( NaN ); +// returns [ NaN, 0 ] +``` + +#### normalize( x, out, stride, offset ) + +Returns a normal number `y` and exponent `exp` satisfying `x = y * 2^exp` and assigns results to a provided output array. + +```javascript +var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( '@stdlib/array/float16' ); + +var out = new Float16Array( 2 ); + +var v = normalize.assign( toFloat16( 1.401e-6 ), out, 1, 0 ); +// returns [ 0.00146484375, -10 ] + +var bool = ( v === out ); +// returns true +``` + +
+ + + +
+ +## Notes + +- While the function accepts higher precision [floating-point numbers][ieee754], beware that providing such numbers can be a source of subtle bugs as the relation `x = y * 2^exp` may **not** hold. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var normalize = require( '@stdlib/number/float16/base/normalize' ); + +var frac; +var exp; +var x; +var v; +var i; + +// Generate denormalized half-precision floating-point numbers and then normalize them... +for ( i = 0; i < 100; i++ ) { + frac = 0.26 + (randu()*10.0); + exp = 5 + round( randu()*4.0 ); + x = frac * pow( 10.0, -exp ); + x = toFloat16( x ); + v = normalize( x ); + console.log( '%d = %d * 2^%d = %d', x, v[0], v[1], v[0]*pow(2.0, v[1]) ); +} +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/number/float16/base/normalize.h" +``` + +#### stdlib_base_float16_normalize( x, \*y, \*exp ) + +Returns a normal number `y` and exponent `exp` satisfying `x = y * 2^exp`. + +```c +#include "stdlib/number/float16/ctor.h" +#include + +stdlib_float16_t x; +stdlib_float16_t y; +int16_t exp; + +x = stdlib_float16_from_bits( 51648 ); // => -11.5 + +stdlib_base_float16_normalize( x, &y, &exp ); +``` + +The function accepts the following arguments: + +- **x**: `[in] stdlib_float16_t` input value. +- **y**: `[out] stdlib_float16_t*` destination for normal number. +- **exp**: `[out] int16_t*` destination for exponent. + +```c +void stdlib_base_float16_normalize( const stdlib_float16_t x, stdlib_float16_t *y, int16_t *exp ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/number/float16/ctor.h" +#include "stdlib/number/float16/base/normalize.h" +#include "stdlib/number/float32/base/to_float16.h" +#include "stdlib/number/float16/base/to_float32.h" +#include +#include +#include + +int main( void ) { + float x[] = { 4.0f, 0.0f, -0.0f, 1.0f, -1.0f, 3.14f, -3.14f, 1.0e-6f, -1.0e-6f, 1.0f/0.0f, -1.0f/0.0f, 0.0f/0.0f }; + + stdlib_float16_t v; + stdlib_float16_t y; + int16_t exp; + int i; + for ( i = 0; i < 12; i++ ) { + v = stdlib_base_float32_to_float16( x[ i ] ); + stdlib_base_float16_normalize( v, &y, &exp ); + printf( "%f => y: %f, exp: %" PRId16 "\n", v, stdlib_base_float16_to_float32( y ), exp ); + } +} +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/benchmark.js b/lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/benchmark.js new file mode 100644 index 000000000000..a14511113904 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/benchmark.js @@ -0,0 +1,79 @@ +/** +* @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 uniform = require( '@stdlib/random/array/uniform' ); +var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var map = require( '@stdlib/array/base/map' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var isArray = require( '@stdlib/assert/is-array' ); +var pkg = require( './../package.json' ).name; +var normalize = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var x; + var y; + var i; + + x = map( uniform( 100, -5.0e4, 5.0e4 ), naryFunction( toFloat16, 1 ) ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = normalize( x[ i%x.length ] ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( y ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':assign', function benchmark( b ) { + var out; + var x; + var y; + var i; + + out = [ 0.0, 0.0 ]; + x = map( uniform( 100, -5.0e4, 5.0e4 ), naryFunction( toFloat16, 1 ) ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = normalize.assign( x[ i%x.length ], out, 1, 0 ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( y ) || y !== out ) { + b.fail( 'should return the output array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/benchmark.native.js new file mode 100644 index 000000000000..128f5d4e2978 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/benchmark.native.js @@ -0,0 +1,64 @@ +/** +* @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 resolve = require( 'path' ).resolve; +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var map = require( '@stdlib/array/base/map' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var normalize = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( normalize instanceof Error ) +}; + + +// MAIN // + +bench( pkg+'::native', opts, function benchmark( b ) { + var x; + var y; + var i; + + x = map( uniform( 100, -5.0e4, 5.0e4 ), naryFunction( toFloat16, 1 ) ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = normalize( x[ i%x.length ] ); + if ( typeof y !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isFloat16Array( y ) ) { + b.fail( 'should return a Float16Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/c/Makefile b/lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/c/Makefile new file mode 100644 index 000000000000..979768abbcec --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := benchmark.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/c/benchmark.c new file mode 100644 index 000000000000..ebef1be61cfb --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/benchmark/c/benchmark.c @@ -0,0 +1,139 @@ +/** +* @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. +*/ + +#include "stdlib/number/float16/base/normalize.h" +#include "stdlib/number/float32/base/to_float16.h" +#include "stdlib/number/float16/ctor.h" +#include +#include +#include +#include +#include +#include + +#define NAME "float16_normalize" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param elapsed elapsed time in seconds +*/ +static void print_results( double elapsed ) { + double rate = (double)ITERATIONS / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [0,1). +* +* @return random number +*/ +static float rand_float( void ) { + int r = rand(); + return (float)r / ( (float)RAND_MAX + 1.0f ); +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark( void ) { + stdlib_float16_t x[ 100 ]; + stdlib_float16_t y; + double elapsed; + int16_t exp; + int i; + + srand( time( NULL ) ); + + for ( i = 0; i < 100; i++ ) { + x[ i ] = stdlib_base_float32_to_float16( ( 200.0f * rand_float() ) - 100.0f ); + } + + elapsed = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + stdlib_base_float16_normalize( x[ i%100 ], &y, &exp ); + if ( y != y || exp < 0 ) { + printf( "unexpected results\n" ); + } + } + elapsed = tic() - elapsed; + if ( y != y || exp < 0 ) { + printf( "unexpected results\n" ); + } + return elapsed; +} +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int i; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + for ( i = 0; i < REPEATS; i++ ) { + printf( "# c::native::%s\n", NAME ); + elapsed = benchmark(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", i+1 ); + } + print_summary( REPEATS, REPEATS ); +} diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/binding.gyp b/lib/node_modules/@stdlib/number/float16/base/normalize/binding.gyp new file mode 100644 index 000000000000..0d6508a12e99 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/binding.gyp @@ -0,0 +1,170 @@ +# @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. + +# A `.gyp` file for building a Node.js native add-on. +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # List of files to include in this file: + 'includes': [ + './include.gypi', + ], + + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Target name should match the add-on export name: + 'addon_target_name%': 'addon', + + # Set variables based on the host OS: + 'conditions': [ + [ + 'OS=="win"', + { + # Define the object file suffix: + 'obj': 'obj', + }, + { + # Define the object file suffix: + 'obj': 'o', + } + ], # end condition (OS=="win") + ], # end conditions + }, # end variables + + # Define compile targets: + 'targets': [ + + # Target to generate an add-on: + { + # The target name should match the add-on export name: + 'target_name': '<(addon_target_name)', + + # Define dependencies: + 'dependencies': [], + + # Define directories which contain relevant include headers: + 'include_dirs': [ + # Local include directory: + '<@(include_dirs)', + ], + + # List of source files: + 'sources': [ + '<@(src_files)', + ], + + # Settings which should be applied when a target's object files are used as linker input: + 'link_settings': { + # Define libraries: + 'libraries': [ + '<@(libraries)', + ], + + # Define library directories: + 'library_dirs': [ + '<@(library_dirs)', + ], + }, + + # C/C++ compiler flags: + 'cflags': [ + # Enable commonly used warning options: + '-Wall', + + # Aggressive optimization: + '-O3', + ], + + # C specific compiler flags: + 'cflags_c': [ + # Specify the C standard to which a program is expected to conform: + '-std=c99', + ], + + # C++ specific compiler flags: + 'cflags_cpp': [ + # Specify the C++ standard to which a program is expected to conform: + '-std=c++11', + ], + + # Linker flags: + 'ldflags': [], + + # Apply conditions based on the host OS: + 'conditions': [ + [ + 'OS=="mac"', + { + # Linker flags: + 'ldflags': [ + '-undefined dynamic_lookup', + '-Wl,-no-pie', + '-Wl,-search_paths_first', + ], + }, + ], # end condition (OS=="mac") + [ + 'OS!="win"', + { + # C/C++ flags: + 'cflags': [ + # Generate platform-independent code: + '-fPIC', + ], + }, + ], # end condition (OS!="win") + ], # end conditions + }, # end target <(addon_target_name) + + # Target to copy a generated add-on to a standard location: + { + 'target_name': 'copy_addon', + + # Declare that the output of this target is not linked: + 'type': 'none', + + # Define dependencies: + 'dependencies': [ + # Require that the add-on be generated before building this target: + '<(addon_target_name)', + ], + + # Define a list of actions: + 'actions': [ + { + 'action_name': 'copy_addon', + 'message': 'Copying addon...', + + # Explicitly list the inputs in the command-line invocation below: + 'inputs': [], + + # Declare the expected outputs: + 'outputs': [ + '<(addon_output_dir)/<(addon_target_name).node', + ], + + # Define the command-line invocation: + 'action': [ + 'cp', + '<(PRODUCT_DIR)/<(addon_target_name).node', + '<(addon_output_dir)/<(addon_target_name).node', + ], + }, + ], # end actions + }, # end target copy_addon + ], # end targets +} diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/docs/repl.txt b/lib/node_modules/@stdlib/number/float16/base/normalize/docs/repl.txt new file mode 100644 index 000000000000..f9138d01e8b2 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/docs/repl.txt @@ -0,0 +1,80 @@ + +{{alias}}( x ) + Returns a normal number `y` and exponent `exp` satisfying `x = y * 2^exp` as + an array. + + The first element of the returned array corresponds to `y` and the second to + `exp`. + + While the function accepts higher precision floating-point numbers, beware + that providing such numbers can be a source of subtle bugs as the relation + `x = y * 2^exp` may not hold. + + Parameters + ---------- + x: number + Half-precision floating-point number. + + Returns + ------- + out: Array + An array containing `y` and `exp`. + + Examples + -------- + > var out = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 1.401e-6 ) ) + [ 0.00146484375, -10 ] + > var y = out[ 0 ]; + > var exp = out[ 1 ]; + > var bool = ( y*{{alias:@stdlib/math/base/special/pow}}(2,exp) === {{alias:@stdlib/number/float64/base/to-float16}}(1.401e-6) ) + true + + // Special cases: + > out = {{alias}}( {{alias:@stdlib/constants/float16/pinf}} ) + [ Infinity, 0 ] + > out = {{alias}}( {{alias:@stdlib/constants/float16/ninf}} ) + [ -Infinity, 0 ] + > out = {{alias}}( NaN ) + [ NaN, 0 ] + + +{{alias}}.assign( x, out, stride, offset ) + Returns a normal number `y` and exponent `exp` satisfying `x = y * 2^exp` and + assigns results to a provided output array. + + The first element of the returned array corresponds to `y` and the second to + `exp`. + + While the function accepts higher precision floating-point numbers, beware + that providing such numbers can be a source of subtle bugs as the relation + `x = y * 2^exp` may not hold. + + Parameters + ---------- + x: number + Half-precision floating-point number. + + out: Array|TypedArray|Object + Output array. + + stride: integer + Output array stride. + + offset: integer + Output array index offset. + + Returns + ------- + out: Array|TypedArray|Object + An array containing `y` and `exp`. + + Examples + -------- + > out = new {{alias:@stdlib/array/float16}}( 2 ); + > var v = {{alias}}.assign( {{alias:@stdlib/number/float64/base/to-float16}}( 1.401e-6 ), out, 1, 0 ) + [ 0.00146484375, -10.0 ] + > bool = ( v === out ) + true + + See Also + -------- diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/docs/types/index.d.ts b/lib/node_modules/@stdlib/number/float16/base/normalize/docs/types/index.d.ts new file mode 100644 index 000000000000..d15b5259beb0 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/docs/types/index.d.ts @@ -0,0 +1,124 @@ +/* +* @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 { Collection } from '@stdlib/types/array'; + +/** +* Interface describing `normalize`. +*/ +interface Normalize { + /** + * Returns a normal number `y` and exponent `exp` satisfying \\(x = y \cdot 2^\mathrm{exp}\\). + * + * @param x - half-precision floating-point number + * @returns output array + * + * @example + * var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); + * + * var v = normalize( toFloat16( 1.401e-6 ) ); + * // returns [ 0.00146484375, -10 ] + * + * @example + * var v = normalize( 0.0 ); + * // returns [ 0.0, 0 ] + * + * @example + * var PINF = require( '@stdlib/constants/float16/pinf' ); + * + * var v = normalize( PINF ); + * // returns [ +Infinity, 0 ] + * + * @example + * var NINF = require( '@stdlib/constants/float16/ninf' ); + * + * var v = normalize( NINF ); + * // returns [ -Infinity, 0 ] + * + * @example + * var v = normalize( NaN ); + * // returns [ NaN, 0 ] + */ + ( x: number ): Array; + + /** + * Returns a normal number `y` and exponent `exp` satisfying \\(x = y \cdot 2^\mathrm{exp}\\) and assigns results to a provided output array. + * + * @param x - half-precision floating-point number + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @returns output array + * + * @example + * var Float16Array = require( '@stdlib/array/float16' ); + * var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); + * + * var out = new Float16Array( 2 ); + * + * var v = normalize.assign( toFloat16( 1.401e-6 ), out, 1, 0 ); + * // returns [ 0.00146484375, -10 ] + * + * var bool = ( v === out ); + * // returns true + */ + assign( x: number, out: Collection, stride: number, offset: number ): Collection; +} + +/** +* Returns a normal number `y` and exponent `exp` satisfying \\(x = y \cdot 2^\mathrm{exp}\\). +* +* @param x - half-precision floating-point number +* @returns output array +* +* @example +* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +* +* var v = normalize( toFloat16( 1.401e-6 ) ); +* // returns [ 0.00146484375, -10 ] +* +* @example +* var v = normalize( 0.0 ); +* // returns [ 0.0, 0 ] +* +* @example +* var PINF = require( '@stdlib/constants/float16/pinf' ); +* +* var v = normalize( PINF ); +* // returns [ +Infinity, 0 ] +* +* @example +* var NINF = require( '@stdlib/constants/float16/ninf' ); +* +* var v = normalize( NINF ); +* // returns [ -Infinity, 0 ] +* +* @example +* var v = normalize( NaN ); +* // returns [ NaN, 0 ] +*/ +declare var normalize: Normalize; + + +// EXPORTS // + +export = normalize; diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/docs/types/test.ts b/lib/node_modules/@stdlib/number/float16/base/normalize/docs/types/test.ts new file mode 100644 index 000000000000..15b69d58c6d6 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/docs/types/test.ts @@ -0,0 +1,109 @@ +/* +* @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 normalize = require( './index' ); +import toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); + + +// TESTS // + +// The function returns an array... +{ + normalize( toFloat16( 1.401e-6 ) ); // $ExpectType number[] +} + +// The compiler throws an error if the function is provided a last argument that is not a number... +{ + normalize( '5' ); // $ExpectError + normalize( true ); // $ExpectError + normalize( false ); // $ExpectError + normalize( null ); // $ExpectError + normalize( {} ); // $ExpectError + normalize( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + normalize(); // $ExpectError +} + +// Attached to the main export is an `assign` method which returns an array-like object containing numbers... +{ + const out = [ 0.0, 0.0 ]; + + normalize.assign( toFloat16( 1.401e-6 ), out, 1, 0 ); // $ExpectType Collection +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not a number... +{ + const out = [ 0.0, 0.0 ]; + + normalize.assign( true, out, 1, 0 ); // $ExpectError + normalize.assign( false, out, 1, 0 ); // $ExpectError + normalize.assign( '5', out, 1, 0 ); // $ExpectError + normalize.assign( null, out, 1, 0 ); // $ExpectError + normalize.assign( [], out, 1, 0 ); // $ExpectError + normalize.assign( {}, out, 1, 0 ); // $ExpectError + normalize.assign( ( x: number ): number => x, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an array-like object... +{ + normalize.assign( 1.0, 1, 1, 0 ); // $ExpectError + normalize.assign( 1.0, true, 1, 0 ); // $ExpectError + normalize.assign( 1.0, false, 1, 0 ); // $ExpectError + normalize.assign( 1.0, null, 1, 0 ); // $ExpectError + normalize.assign( 1.0, {}, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not a number... +{ + const out = [ 0.0, 0.0 ]; + + normalize.assign( 1.0, out, '5', 0 ); // $ExpectError + normalize.assign( 1.0, out, true, 0 ); // $ExpectError + normalize.assign( 1.0, out, false, 0 ); // $ExpectError + normalize.assign( 1.0, out, null, 0 ); // $ExpectError + normalize.assign( 1.0, out, [], 0 ); // $ExpectError + normalize.assign( 1.0, out, {}, 0 ); // $ExpectError + normalize.assign( 1.0, out, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fourth argument which is not a number... +{ + const out = [ 0.0, 0.0 ]; + + normalize.assign( 1.0, out, 1, '5' ); // $ExpectError + normalize.assign( 1.0, out, 1, true ); // $ExpectError + normalize.assign( 1.0, out, 1, false ); // $ExpectError + normalize.assign( 1.0, out, 1, null ); // $ExpectError + normalize.assign( 1.0, out, 1, [] ); // $ExpectError + normalize.assign( 1.0, out, 1, {} ); // $ExpectError + normalize.assign( 1.0, out, 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const out = [ 0.0, 0.0 ]; + + normalize.assign(); // $ExpectError + normalize.assign( 1.0 ); // $ExpectError + normalize.assign( 1.0, out ); // $ExpectError + normalize.assign( 1.0, out, 1 ); // $ExpectError + normalize.assign( 1.0, out, 1, 0, 1 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/examples/c/Makefile b/lib/node_modules/@stdlib/number/float16/base/normalize/examples/c/Makefile new file mode 100644 index 000000000000..c8f8e9a1517b --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/examples/c/example.c b/lib/node_modules/@stdlib/number/float16/base/normalize/examples/c/example.c new file mode 100644 index 000000000000..ea99e962d7aa --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/examples/c/example.c @@ -0,0 +1,39 @@ +/** +* @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. +*/ + +#include "stdlib/number/float16/ctor.h" +#include "stdlib/number/float16/base/normalize.h" +#include "stdlib/number/float32/base/to_float16.h" +#include "stdlib/number/float16/base/to_float32.h" +#include +#include +#include + +int main( void ) { + float x[] = { 4.0f, 0.0f, -0.0f, 1.0f, -1.0f, 3.14f, -3.14f, 1.0e-6f, -1.0e-6f, 1.0f/0.0f, -1.0f/0.0f, 0.0f/0.0f }; + + stdlib_float16_t v; + stdlib_float16_t y; + int16_t exp; + int i; + for ( i = 0; i < 12; i++ ) { + v = stdlib_base_float32_to_float16( x[ i ] ); + stdlib_base_float16_normalize( v, &y, &exp ); + printf( "%f => y: %f, exp: %" PRId16 "\n", x[ i ], stdlib_base_float16_to_float32( y ), exp ); + } +} diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/examples/index.js b/lib/node_modules/@stdlib/number/float16/base/normalize/examples/index.js new file mode 100644 index 000000000000..4d1862f70512 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/examples/index.js @@ -0,0 +1,41 @@ +/** +* @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 randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var normalize = require( './../lib' ); + +var frac; +var exp; +var x; +var v; +var i; + +// Generate denormalized half-precision floating-point numbers and then normalize them... +for ( i = 0; i < 100; i++ ) { + frac = 0.26 + (randu()*10.0); + exp = 5 + round( randu()*4.0 ); + x = frac * pow( 10.0, -exp ); + x = toFloat16( x ); + v = normalize( x ); + console.log( '%d = %d * 2^%d = %d', x, v[0], v[1], v[0]*pow(2.0, v[1]) ); +} diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/include.gypi b/lib/node_modules/@stdlib/number/float16/base/normalize/include.gypi new file mode 100644 index 000000000000..bee8d41a2caf --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/include.gypi @@ -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. + +# A GYP include file for building a Node.js native add-on. +# +# Main documentation: +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Source directory: + 'src_dir': './src', + + # Include directories: + 'include_dirs': [ + ' + +/* +* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. +*/ +#ifdef __cplusplus +extern "C" { +#endif + +/** +* Returns a normal number `y` and exponent `exp` satisfying `x = y * 2^exp`. +*/ +void stdlib_base_float16_normalize( const stdlib_float16_t x, stdlib_float16_t *y, int16_t *exp ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_NUMBER_FLOAT16_BASE_NORMALIZE_H diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/lib/assign.js b/lib/node_modules/@stdlib/number/float16/base/normalize/lib/assign.js new file mode 100644 index 000000000000..e3adf29c4f69 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/lib/assign.js @@ -0,0 +1,119 @@ +/** +* @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 FLOAT16_SMALLEST_NORMAL = require( '@stdlib/constants/float16/smallest-normal' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var PINF = require( '@stdlib/constants/float16/pinf' ); +var NINF = require( '@stdlib/constants/float16/ninf' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var isFloat16Array = require( '@stdlib/assert/is-float16array' ); +var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); + + +// VARIABLES // + +var SCALAR = 1024; // (1<<10) + + +// FUNCTIONS // + +/** +* Assigns values to output array, handling Float16Array and regular arrays. +* +* @private +* @param {Collection} out - output array +* @param {NonNegativeInteger} offset - output array index offset +* @param {integer} stride - output array stride +* @param {number} val1 - first value to assign +* @param {number} val2 - second value to assign +*/ +function assignValues( out, offset, stride, val1, val2 ) { + if ( isFloat16Array( out ) ) { + out.set( val1, offset ); + out.set( val2, offset + stride ); + } else { + out[ offset ] = val1; + out[ offset + stride ] = val2; + } +} + + +// MAIN // + +/** +* Returns a normal number `y` and exponent `exp` satisfying \\(x = y \cdot 2^\mathrm{exp}\\) and assigns results to a provided output array. +* +* @private +* @param {number} x - half-precision floating-point number +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array index offset +* @returns {Collection} output array +* +* @example +* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +* +* var v = normalize( toFloat16( 1.401e-6 ), [ 0.0, 0.0 ], 1, 0 ); +* // returns [ 0.00146484375, -10 ] +* +* @example +* var v = normalize( 0.0, [ 0.0, 0.0 ], 1, 0 ); +* // returns [ 0.0, 0 ]; +* +* @example +* var PINF = require( '@stdlib/constants/float16/pinf' ); +* +* var v = normalize( PINF, [ 0.0, 0.0 ], 1, 0 ); +* // returns [ +Infinity, 0 ] +* +* @example +* var NINF = require( '@stdlib/constants/float16/ninf' ); +* +* var v = normalize( NINF, [ 0.0, 0.0 ], 1, 0 ); +* // returns [ -Infinity, 0 ] +* +* @example +* var v = normalize( NaN, [ 0.0, 0.0 ], 1, 0 ); +* // returns [ NaN, 0 ] +*/ +function normalize( x, out, stride, offset ) { + if ( + isnan( x ) || + x === PINF || + x === NINF + ) { + assignValues( out, offset, stride, x, 0 ); + return out; + } + if ( x !== 0.0 && abs( x ) < FLOAT16_SMALLEST_NORMAL ) { + x = toFloat16( x*SCALAR ); + assignValues( out, offset, stride, x, -10 ); + return out; + } + assignValues( out, offset, stride, x, 0 ); + return out; +} + + +// EXPORTS // + +module.exports = normalize; diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/lib/index.js b/lib/node_modules/@stdlib/number/float16/base/normalize/lib/index.js new file mode 100644 index 000000000000..a02354fdd920 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/lib/index.js @@ -0,0 +1,68 @@ +/** +* @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'; + +/** +* Return a normal number `y` and exponent `exp` satisfying \\(x = y \cdot 2^\mathrm{exp}\\). +* +* @module @stdlib/number/float16/base/normalize +* +* @example +* var pow = require( '@stdlib/math/base/special/pow' ); +* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +* var normalize = require( '@stdlib/number/float16/base/normalize' ); +* +* var out = normalize( toFloat16( 1.401e-6 ) ); +* // returns [ 0.00146484375, -10 ] +* +* var y = out[ 0 ]; +* var exp = out[ 1 ]; +* +* var bool = ( y*pow(2,exp) === toFloat16(1.401e-6) ); +* // returns true +* +* @example +* var Float16Array = require( '@stdlib/array/float16' ); +* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +* var normalize = require( '@stdlib/number/float16/base/normalize' ); +* +* var out = new Float16Array( 2 ); +* +* var v = normalize.assign( toFloat16( 1.401e-6 ), out, 1, 0 ); +* // returns [ 0.00146484375, -10.0 ] +* +* var bool = ( v === out ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/lib/main.js b/lib/node_modules/@stdlib/number/float16/base/normalize/lib/main.js new file mode 100644 index 000000000000..72951a153708 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/lib/main.js @@ -0,0 +1,67 @@ +/** +* @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 assign = require( './assign.js' ); + + +// MAIN // + +/** +* Returns a normal number `y` and exponent `exp` satisfying \\(x = y \cdot 2^\mathrm{exp}\\). +* +* @param {number} x - half-precision floating-point number +* @returns {Array} output array +* +* @example +* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +* +* var v = normalize( toFloat16( 1.401e-6 ) ); +* // returns [ 0.00146484375, -10 ] +* +* @example +* var v = normalize( 0.0 ); +* // returns [ 0.0, 0 ] +* +* @example +* var PINF = require( '@stdlib/constants/float16/pinf' ); +* +* var v = normalize( PINF ); +* // returns [ +Infinity, 0 ] +* +* @example +* var NINF = require( '@stdlib/constants/float16/ninf' ); +* +* var v = normalize( NINF ); +* // returns [ -Infinity, 0 ] +* +* @example +* var v = normalize( NaN ); +* // returns [ NaN, 0 ] +*/ +function normalize( x ) { + return assign( x, [ 0.0, 0.0 ], 1, 0 ); +} + + +// EXPORTS // + +module.exports = normalize; diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/lib/native.js b/lib/node_modules/@stdlib/number/float16/base/normalize/lib/native.js new file mode 100644 index 000000000000..860bc4b72b56 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/lib/native.js @@ -0,0 +1,75 @@ +/** +* @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 Float16Array = require( '@stdlib/array/float16' ); +var Float32Array = require( '@stdlib/array/float32' ); +var addon = require( './../src/addon.node' ); + + +// MAIN // + +/** +* Returns a normal number `y` and exponent `exp` satisfying \\(x = y \cdot 2^\mathrm{exp}\\). +* +* @private +* @param {number} x - input value +* @returns {Float16Array} output array +* +* @example +* +* @example +* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +* +* var v = normalize( toFloat16( 1.401e-6 ) ); +* // returns [ 0.00146484375, -10 ] +* +* @example +* var v = normalize( 0.0 ); +* // returns [ 0, 0 ] +* +* @example +* var PINF = require( '@stdlib/constants/float16/pinf' ); +* +* var v = normalize( PINF ); +* // returns [ Infinity, 0 ] +* +* @example +* var NINF = require( '@stdlib/constants/float16/ninf' ); +* +* var v = normalize( NINF ); +* // returns [ -Infinity, 0 ] +* +* @example +* var v = normalize( NaN ); +* // returns [ NaN, 0 ] +*/ +function normalize( x ) { + var out = new Float32Array( 2 ); + addon( out, x ); + out = new Float16Array( out ); + return out; +} + + +// EXPORTS // + +module.exports = normalize; diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/manifest.json b/lib/node_modules/@stdlib/number/float16/base/normalize/manifest.json new file mode 100644 index 000000000000..0bbaf0bf0fac --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/manifest.json @@ -0,0 +1,94 @@ +{ + "options": { + "task": "build", + "wasm": false + }, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "task": "build", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/number/float64/base/to-float16", + "@stdlib/number/float16/base/mul", + "@stdlib/number/float32/base/to-float16", + "@stdlib/number/float16/base/to-float32", + "@stdlib/number/float16/ctor", + "@stdlib/math/base/assert/is-infinitef", + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/special/absf" + ] + }, + { + "task": "benchmark", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/number/float16/base/mul", + "@stdlib/number/float32/base/to-float16", + "@stdlib/number/float16/base/to-float32", + "@stdlib/number/float16/ctor", + "@stdlib/math/base/assert/is-infinitef", + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/special/absf" + ] + }, + { + "task": "examples", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/number/float16/base/mul", + "@stdlib/number/float32/base/to-float16", + "@stdlib/number/float16/base/to-float32", + "@stdlib/number/float16/ctor", + "@stdlib/math/base/assert/is-infinitef", + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/special/absf" + ] + } + ] +} diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/package.json b/lib/node_modules/@stdlib/number/float16/base/normalize/package.json new file mode 100644 index 000000000000..3298ef7b1944 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/number/float16/base/normalize", + "version": "0.0.0", + "description": "Return a normal number `y` and exponent `exp` satisfying `x = y * 2^exp`.", + "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", + "gypfile": true, + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "include": "./include", + "lib": "./lib", + "src": "./src", + "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", + "stdtypes", + "base", + "utilities", + "utility", + "utils", + "util", + "types", + "type", + "float16", + "half", + "floating-point", + "ieee754", + "denormalized", + "normalize", + "subnormal", + "number", + "normal", + "float" + ] +} diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/src/Makefile b/lib/node_modules/@stdlib/number/float16/base/normalize/src/Makefile new file mode 100644 index 000000000000..2caf905cedbe --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + + +# RULES # + +#/ +# Removes generated files for building an add-on. +# +# @example +# make clean-addon +#/ +clean-addon: + $(QUIET) -rm -f *.o *.node + +.PHONY: clean-addon + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: clean-addon + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/src/addon.c b/lib/node_modules/@stdlib/number/float16/base/normalize/src/addon.c new file mode 100644 index 000000000000..4a314b2d6e3a --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/src/addon.c @@ -0,0 +1,118 @@ +/** +* @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. +*/ + +#include "stdlib/number/float16/base/normalize.h" +#include "stdlib/number/float64/base/to_float16.h" +#include "stdlib/number/float16/ctor.h" +#include +#include +#include +#include + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon( napi_env env, napi_callback_info info ) { + napi_status status; + + // Get callback arguments: + size_t argc = 2; + napi_value argv[ 2 ]; + status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); + assert( status == napi_ok ); + + // Check whether we were provided the correct number of arguments: + if ( argc < 2 ) { + status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." ); + assert( status == napi_ok ); + return NULL; + } + if ( argc > 2 ) { + status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." ); + assert( status == napi_ok ); + return NULL; + } + + bool res; + status = napi_is_typedarray( env, argv[ 0 ], &res ); + assert( status == napi_ok ); + if ( res == false ) { + status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a Float64Array." ); + assert( status == napi_ok ); + return NULL; + } + + napi_valuetype vtype1; + status = napi_typeof( env, argv[ 1 ], &vtype1 ); + assert( status == napi_ok ); + if ( vtype1 != napi_number ) { + status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." ); + assert( status == napi_ok ); + return NULL; + } + + napi_typedarray_type vtype0; + size_t len; + void *Out; + status = napi_get_typedarray_info( env, argv[ 0 ], &vtype0, &len, &Out, NULL, NULL ); + assert( status == napi_ok ); + if ( vtype0 != napi_float32_array ) { + status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a Float32Array." ); + assert( status == napi_ok ); + return NULL; + } + if ( len != 2 ) { + status = napi_throw_range_error( env, NULL, "invalid argument. First argument must have 2 elements." ); + assert( status == napi_ok ); + return NULL; + } + + double value; + status = napi_get_value_double( env, argv[ 1 ], &value ); + assert( status == napi_ok ); + + stdlib_float16_t y; + int16_t exp; + stdlib_base_float16_normalize( stdlib_base_float64_to_float16( value ), &y, &exp ); + + float *op = (float *)Out; + op[ 0 ] = y; + op[ 1 ] = (float)exp; + + return NULL; +} + +/** +* Initializes a Node-API module. +* +* @param env environment under which the function is invoked +* @param exports exports object +* @return main export +*/ +static napi_value init( napi_env env, napi_value exports ) { + napi_value fcn; + napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn ); + assert( status == napi_ok ); + return fcn; +} + +NAPI_MODULE( NODE_GYP_MODULE_NAME, init ) diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/src/main.c b/lib/node_modules/@stdlib/number/float16/base/normalize/src/main.c new file mode 100644 index 000000000000..61d619899e7d --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/src/main.c @@ -0,0 +1,73 @@ +/** +* @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. +*/ + +#include "stdlib/number/float16/base/normalize.h" +#include "stdlib/number/float16/base/mul.h" +#include "stdlib/number/float32/base/to_float16.h" +#include "stdlib/number/float16/base/to_float32.h" +#include "stdlib/number/float16/ctor.h" +#include "stdlib/math/base/assert/is_infinitef.h" +#include "stdlib/math/base/assert/is_nanf.h" +#include "stdlib/math/base/special/absf.h" +#include + +// VARIABLES // + +static const uint16_t UINT16_POSITIVE_ZERO = 0x0000; // 0x0000 = 0 => 0 00000 0000000000 +static const uint16_t UINT16_NEGATIVE_ZERO = 0x8000; // 0x8000 = 32768 => 1 00000 0000000000 + +/** +* Extracts a normal number `y` and exponent `exp` satisfying `x = y * 2^exp`. +* +* @param x input value +* @param y destination for normal number +* @param exp destination for exponent +* +* @example +* #include "stdlib/number/float16/ctor.h" +* #include +* +* stdlib_float16_t x; +* stdlib_float16_t y; +* int16_t exp; +* +* x = stdlib_float16_from_bits( 51648 ); // => -11.5 +* +* stdlib_base_float16_normalize( x, &y, &exp ); +*/ +void stdlib_base_float16_normalize( const stdlib_float16_t x, stdlib_float16_t *y, int16_t *exp ) { + stdlib_float16_t SCALAR; + uint16_t z; + + SCALAR = stdlib_base_float32_to_float16( 1024.0f ); // (1<<10) + if ( stdlib_base_is_nanf( stdlib_base_float16_to_float32( x ) ) || stdlib_base_is_infinitef( stdlib_base_float16_to_float32( x ) ) ) { + *y = x; + *exp = 0; + return; + } + + z = stdlib_float16_to_bits( x ); + if ( ( z != UINT16_POSITIVE_ZERO || z != UINT16_NEGATIVE_ZERO ) && stdlib_base_absf( stdlib_base_float16_to_float32( x ) ) < 6.103515625e-5f ) { + *y = stdlib_base_float16_mul( x, SCALAR ); + *exp = -10; + return; + } + *y = x; + *exp = 0; + return; +} diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/test/test.assign.js b/lib/node_modules/@stdlib/number/float16/base/normalize/test/test.assign.js new file mode 100644 index 000000000000..aadb6cc87418 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/test/test.assign.js @@ -0,0 +1,184 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var PINF = require( '@stdlib/constants/float16/pinf' ); +var NINF = require( '@stdlib/constants/float16/ninf' ); +var Float16Array = require( '@stdlib/array/float16' ); +var FLOAT16_SMALLEST_NORMAL = require( '@stdlib/constants/float16/smallest-normal' ); +var FLOAT16_SMALLEST_SUBNORMAL = require( '@stdlib/constants/float16/smallest-subnormal' ); // eslint-disable-line id-length +var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var normalize = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof normalize, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function normalizes a denormalized number, returning a normal number and an exponent', function test( t ) { + var frac; + var exp; + var out; + var x1; + var x; + var v; + var i; + + // Smallest denormalized number: + out = [ 0.0, 0.0 ]; + v = normalize( FLOAT16_SMALLEST_SUBNORMAL, out, 1, 0 ); + t.strictEqual( v, out, 'returns output array' ); + t.ok( v[ 0 ] >= FLOAT16_SMALLEST_NORMAL, 'returns a normal number' ); + t.strictEqual( v[ 0 ]*pow( 2.0, v[ 1 ] ), FLOAT16_SMALLEST_SUBNORMAL, 'x = y * 2^exp' ); + + // Other subnormals... + for ( i = 0; i < 1000; i++ ) { + frac = 0.26 + (randu()*10.0); // 0.26 prevents underflow + exp = -5 - round( randu()*1.0 ); + x = frac * pow( 10.0, exp ); + x = toFloat16( x ); + + out = [ 0.0, 0.0 ]; + v = normalize( x, out, 1, 0 ); + t.strictEqual( v, out, 'returns output array' ); + t.ok( v[ 0 ] >= FLOAT16_SMALLEST_NORMAL, 'returns a normal number ' + v[0] ); + + x1 = v[ 0 ] * pow( 2.0, v[ 1 ] ); + x1 = toFloat16( x1 ); + t.strictEqual( x1, x, 'y*2^exp=x. y='+v[0]+', exp='+v[1]+', x='+x ); + } + t.end(); +}); + +tape( 'the function returns `[0,0]` if provided a `0`', function test( t ) { + var out; + var val; + + out = [ 0, 0 ]; + val = normalize( 0.0, out, 1, 0 ); + t.strictEqual( val, out, 'returns output array' ); + t.deepEqual( val, [0.0, 0], 'returns [0,0]' ); + t.end(); +}); + +tape( 'the function returns `[+inf,0]` if provided a `+infinity`', function test( t ) { + var out; + var val; + + out = [ 0.0, 0.0 ]; + val = normalize( PINF, out, 1, 0 ); + t.strictEqual( val, out, 'returns output array' ); + t.deepEqual( val, [PINF, 0], 'returns [+inf,0]' ); + t.end(); +}); + +tape( 'the function returns `[-inf,0]` if provided a `-infinity`', function test( t ) { + var out; + var val; + + out = [ 0.0, 0.0 ]; + val = normalize( NINF, out, 1, 0 ); + t.strictEqual( val, out, 'returns output array' ); + t.deepEqual( val, [NINF, 0], 'returns [-inf,0]' ); + t.end(); +}); + +tape( 'the function returns `[NaN,0]` if provided a `NaN`', function test( t ) { + var out; + var val; + + out = [ 0.0, 0.0 ]; + val = normalize( NaN, out, 1, 0 ); + t.strictEqual( val, out, 'returns output array' ); + t.strictEqual( isnan( val[0] ), true, 'first element is NaN' ); + t.strictEqual( val[1], 0, 'second element is 0' ); + t.end(); +}); + +tape( 'the function supports providing an output array (array)', function test( t ) { + var out; + var val; + + out = [ 3.14, 3.14 ]; + val = normalize( 0.0, out, 1, 0 ); + + t.strictEqual( val, out, 'returns output array' ); + t.strictEqual( val[ 0 ], 0.0, 'first element is 0' ); + t.strictEqual( val[ 1 ], 0, 'second element is 0' ); + + t.end(); +}); + +tape( 'the function supports providing an output array (typed array)', function test( t ) { + var out; + var val; + + out = new Float16Array([ 3.14, 3.14 ]); + + val = normalize( 0.0, out, 1, 0 ); + + t.strictEqual( val, out, 'returns output array' ); + t.strictEqual( val.get( 0 ), 0.0, 'first element is 0' ); + t.strictEqual( val.get( 1 ), 0, 'second element is 0' ); + + t.end(); +}); + +tape( 'the function supports specifying a stride', function test( t ) { + var out; + var val; + + out = new Float16Array( 4 ); + val = normalize( toFloat16( 1.401e-6 ), out, 2, 0 ); + + t.strictEqual( val, out, 'returns output array' ); + t.strictEqual( val.get( 0 ), 0.00146484375, 'returns expected value' ); + t.strictEqual( val.get( 1 ), 0, 'returns expected value' ); + t.strictEqual( val.get( 2 ), -10, 'returns expected value' ); + t.strictEqual( val.get( 3 ), 0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying an offset', function test( t ) { + var out; + var val; + + out = new Float16Array( 4 ); + val = normalize( toFloat16( 1.401e-6 ), out, 2, 1 ); + + t.strictEqual( val, out, 'returns output array' ); + t.strictEqual( val.get( 0 ), 0, 'returns expected value' ); + t.strictEqual( val.get( 1 ), 0.00146484375, 'returns expected value' ); + t.strictEqual( val.get( 2 ), 0, 'returns expected value' ); + t.strictEqual( val.get( 3 ), -10, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/test/test.js b/lib/node_modules/@stdlib/number/float16/base/normalize/test/test.js new file mode 100644 index 000000000000..69534edf38a0 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/test/test.js @@ -0,0 +1,40 @@ +/** +* @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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var normalize = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof normalize, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( hasOwnProp( normalize, 'assign' ), true, 'has property' ); + t.strictEqual( typeof normalize.assign, 'function', 'has method' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/test/test.main.js b/lib/node_modules/@stdlib/number/float16/base/normalize/test/test.main.js new file mode 100644 index 000000000000..d627dbb140ab --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/test/test.main.js @@ -0,0 +1,97 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var PINF = require( '@stdlib/constants/float16/pinf' ); +var NINF = require( '@stdlib/constants/float16/ninf' ); +var FLOAT16_SMALLEST_NORMAL = require( '@stdlib/constants/float16/smallest-normal' ); +var FLOAT16_SMALLEST_SUBNORMAL = require( '@stdlib/constants/float16/smallest-subnormal' ); // eslint-disable-line id-length +var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var normalize = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof normalize, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function normalizes a denormalized number, returning a normal number and an exponent', function test( t ) { + var frac; + var exp; + var x1; + var x; + var v; + var i; + + // Smallest denormalized number: + v = normalize( FLOAT16_SMALLEST_SUBNORMAL ); + t.ok( v[ 0 ] >= FLOAT16_SMALLEST_NORMAL, 'returns a normal number' ); + t.strictEqual( v[ 0 ]*pow( 2.0, v[ 1 ] ), FLOAT16_SMALLEST_SUBNORMAL, 'x = y * 2^exp' ); + + // Other subnormals... + for ( i = 0; i < 1000; i++ ) { + frac = 0.26 + (randu()*10.0); // 0.26 prevents underflow + exp = -5 - round( randu()*1.0 ); + x = frac * pow( 10.0, exp ); + x = toFloat16( x ); + + v = normalize( x ); + t.ok( v[ 0 ] >= FLOAT16_SMALLEST_NORMAL, 'returns a normal number ' + v[0] ); + + x1 = v[ 0 ] * pow( 2.0, v[ 1 ] ); + x1 = toFloat16( x1 ); + t.strictEqual( x1, x, 'y*2^exp=x. y='+v[0]+', exp='+v[1]+', x='+x ); + } + t.end(); +}); + +tape( 'the function returns `[0,0]` if provided a `0`', function test( t ) { + var val = normalize( 0.0 ); + t.deepEqual( val, [0.0, 0], 'returns [0,0]' ); + t.end(); +}); + +tape( 'the function returns `[+inf,0]` if provided a `+infinity`', function test( t ) { + var val = normalize( PINF ); + t.deepEqual( val, [PINF, 0], 'returns [+inf,0]' ); + t.end(); +}); + +tape( 'the function returns `[-inf,0]` if provided a `-infinity`', function test( t ) { + var val = normalize( NINF ); + t.deepEqual( val, [NINF, 0], 'returns [-inf,0]' ); + t.end(); +}); + +tape( 'the function returns `[NaN,0]` if provided a `NaN`', function test( t ) { + var val = normalize( NaN ); + t.strictEqual( isnan( val[0] ), true, 'first element is NaN' ); + t.strictEqual( val[1], 0, 'second element is 0' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/number/float16/base/normalize/test/test.native.js b/lib/node_modules/@stdlib/number/float16/base/normalize/test/test.native.js new file mode 100644 index 000000000000..db11a5894ad7 --- /dev/null +++ b/lib/node_modules/@stdlib/number/float16/base/normalize/test/test.native.js @@ -0,0 +1,107 @@ +/** +* @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 resolve = require( 'path' ).resolve; +var tape = require( 'tape' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var PINF = require( '@stdlib/constants/float16/pinf' ); +var NINF = require( '@stdlib/constants/float16/ninf' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var FLOAT16_SMALLEST_NORMAL = require( '@stdlib/constants/float16/smallest-normal' ); +var FLOAT16_SMALLEST_SUBNORMAL = require( '@stdlib/constants/float16/smallest-subnormal' ); // eslint-disable-line id-length +var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var Float16Array = require( '@stdlib/array/float16' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var normalize = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( normalize instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof normalize, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function normalizes a denormalized number, returning a normal number and an exponent', opts, function test( t ) { + var frac; + var exp; + var x1; + var x; + var v; + var i; + + // Smallest denormalized number: + v = normalize( FLOAT16_SMALLEST_SUBNORMAL ); + t.ok( v.get( 0 ) >= FLOAT16_SMALLEST_NORMAL, 'returns a normal number' ); + t.strictEqual( v.get( 0 )*pow( 2.0, v.get( 1 ) ), FLOAT16_SMALLEST_SUBNORMAL, 'x = y * 2^exp' ); + + // Other subnormals... + for ( i = 0; i < 1000; i++ ) { + frac = 0.26 + (randu()*10.0); // 0.26 prevents underflow + exp = -5 - round( randu()*1.0 ); + x = frac * pow( 10.0, exp ); + x = toFloat16( x ); + + v = normalize( x ); + t.ok( v.get( 0 ) >= FLOAT16_SMALLEST_NORMAL, 'returns a normal number ' + v.get( 0 ) ); + + x1 = v.get( 0 ) * pow( 2.0, v.get( 1 ) ); + x1 = toFloat16( x1 ); + t.strictEqual( x1, x, 'y*2^exp=x. y='+v.get( 0 )+', exp='+v.get( 1 )+', x='+x ); + } + t.end(); +}); + +tape( 'the function returns `[0,0]` if provided a `0`', opts, function test( t ) { + var val = normalize( 0.0 ); + t.deepEqual( val, new Float16Array([ 0.0, 0 ]), 'returns [0,0]' ); + t.end(); +}); + +tape( 'the function returns `[+inf,0]` if provided a `+infinity`', opts, function test( t ) { + var val = normalize( PINF ); + t.deepEqual( val, new Float16Array([ PINF, 0 ]), 'returns [+inf,0]' ); + t.end(); +}); + +tape( 'the function returns `[-inf,0]` if provided a `-infinity`', opts, function test( t ) { + var val = normalize( NINF ); + t.deepEqual( val, new Float16Array([ NINF, 0 ]), 'returns [-inf,0]' ); + t.end(); +}); + +tape( 'the function returns `[NaN,0]` if provided a `NaN`', opts, function test( t ) { + var val = normalize( NaN ); + t.strictEqual( isnan( val.get( 0 ) ), true, 'first element is NaN' ); + t.strictEqual( val.get( 1 ), 0, 'second element is 0' ); + t.end(); +});