From d430aceebed01d216340e9aa0d5572ddc9945422 Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Wed, 26 Feb 2025 22:42:52 -0800 Subject: [PATCH] Normative: add Float16Array and Math.f16round (#3532) --- spec.html | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/spec.html b/spec.html index 3e3632e311..3e04727057 100644 --- a/spec.html +++ b/spec.html @@ -3539,6 +3539,17 @@

Well-Known Intrinsic Objects

The FinalizationRegistry constructor () + + + %Float16Array% + + + `Float16Array` + + + The Float16Array constructor () + + %Float32Array% @@ -30109,6 +30120,11 @@

FinalizationRegistry ( . . . )

See .

+ +

Float16Array ( . . . )

+

See .

+
+

Float32Array ( . . . )

See .

@@ -32752,6 +32768,23 @@

Math.fround ( _x_ )

+ +

Math.f16round ( _x_ )

+

This function performs the following steps when called:

+ + 1. Let _n_ be ? ToNumber(_x_). + 1. If _n_ is *NaN*, return *NaN*. + 1. If _n_ is one of *+0*𝔽, *-0*𝔽, *+∞*𝔽, or *-∞*𝔽, return _n_. + 1. Let _n16_ be the result of converting _n_ to IEEE 754-2019 binary16 format using roundTiesToEven mode. + 1. Let _n64_ be the result of converting _n16_ to IEEE 754-2019 binary64 format. + 1. Return the ECMAScript Number value corresponding to _n64_. + + +

This operation is not the same as casting to binary32 and then to binary16 because of the possibility of double-rounding: consider the number _k_ = *1.00048828125000022204*𝔽, for example, for which Math.f16round(_k_) is *1.0009765625*𝔽, but Math.f16round(Math.fround(_k_)) is *1*𝔽.

+

Not all platforms provide native support for casting from binary64 to binary16. There are various libraries which can provide this, including the MIT-licensed half library. Alternatively, it is possible to first cast from binary64 to binary32 under roundTiesToEven and then check whether the result could lead to incorrect double-rounding. The cases which could can be handled explicitly by adjusting the mantissa of the binary32 value so that it is the value which would be produced by performing the initial cast under roundTiesToOdd. Casting the adjusted value to binary16 under roundTiesToEven then produces the correct value.

+
+
+

Math.hypot ( ..._args_ )

Given zero or more arguments, this function returns the square root of the sum of squares of its arguments.

@@ -41123,6 +41156,23 @@

TypedArray Objects

64-bit unsigned integer + + + Float16Array
+ %Float16Array% + + + ~float16~ + + + 2 + + + + + 16-bit IEEE floating point + + Float32Array
@@ -44159,6 +44209,10 @@

1. Let _elementSize_ be the Element Size value specified in for Element Type _type_. 1. If _isLittleEndian_ is *false*, reverse the order of the elements of _rawBytes_. + 1. If _type_ is ~float16~, then + 1. Let _value_ be the byte elements of _rawBytes_ concatenated and interpreted as a little-endian bit string encoding of an IEEE 754-2019 binary16 value. + 1. If _value_ is an IEEE 754-2019 binary16 NaN value, return the *NaN* Number value. + 1. Return the Number value that corresponds to _value_. 1. If _type_ is ~float32~, then 1. Let _value_ be the byte elements of _rawBytes_ concatenated and interpreted as a little-endian bit string encoding of an IEEE 754-2019 binary32 value. 1. If _value_ is an IEEE 754-2019 binary32 NaN value, return the *NaN* Number value. @@ -44242,7 +44296,9 @@

- 1. If _type_ is ~float32~, then + 1. If _type_ is ~float16~, then + 1. Let _rawBytes_ be a List whose elements are the 2 bytes that are the result of converting _value_ to IEEE 754-2019 binary16 format using roundTiesToEven mode. The bytes are arranged in little endian order. If _value_ is *NaN*, _rawBytes_ may be set to any implementation chosen IEEE 754-2019 binary16 format Not-a-Number encoding. An implementation must always choose the same encoding for each implementation distinguishable *NaN* value. + 1. Else if _type_ is ~float32~, then 1. Let _rawBytes_ be a List whose elements are the 4 bytes that are the result of converting _value_ to IEEE 754-2019 binary32 format using roundTiesToEven mode. The bytes are arranged in little endian order. If _value_ is *NaN*, _rawBytes_ may be set to any implementation chosen IEEE 754-2019 binary32 format Not-a-Number encoding. An implementation must always choose the same encoding for each implementation distinguishable *NaN* value. 1. Else if _type_ is ~float64~, then 1. Let _rawBytes_ be a List whose elements are the 8 bytes that are the IEEE 754-2019 binary64 format encoding of _value_. The bytes are arranged in little endian order. If _value_ is *NaN*, _rawBytes_ may be set to any implementation chosen IEEE 754-2019 binary64 format Not-a-Number encoding. An implementation must always choose the same encoding for each implementation distinguishable *NaN* value. @@ -45175,6 +45231,16 @@

DataView.prototype.getBigUint64 ( _byteOffset_ [ , _littleEndian_ ] )

+ +

DataView.prototype.getFloat16 ( _byteOffset_ [ , _littleEndian_ ] )

+

This method performs the following steps when called:

+ + 1. Let _v_ be the *this* value. + 1. If _littleEndian_ is not present, set _littleEndian_ to *false*. + 1. Return ? GetViewValue(_v_, _byteOffset_, _littleEndian_, ~float16~). + +
+

DataView.prototype.getFloat32 ( _byteOffset_ [ , _littleEndian_ ] )

This method performs the following steps when called:

@@ -45271,6 +45337,16 @@

DataView.prototype.setBigUint64 ( _byteOffset_, _value_ [ , _littleEndian_ ] + +

DataView.prototype.setFloat16 ( _byteOffset_, _value_ [ , _littleEndian_ ] )

+

This method performs the following steps when called:

+ + 1. Let _v_ be the *this* value. + 1. If _littleEndian_ is not present, set _littleEndian_ to *false*. + 1. Return ? SetViewValue(_v_, _byteOffset_, _littleEndian_, ~float16~, _value_). + +
+

DataView.prototype.setFloat32 ( _byteOffset_, _value_ [ , _littleEndian_ ] )

This method performs the following steps when called: