-
Notifications
You must be signed in to change notification settings - Fork 3
/
spec.html
381 lines (368 loc) · 13.1 KB
/
spec.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<!DOCTYPE html>
<meta charset="utf-8">
<pre class="metadata">
title: Float16Array
status: proposal
stage: 3
copyright: false
contributors: Kevin Gibbons
</pre>
<div id="metadata-block">
<h1>Contributing to this Proposal</h1>
<p>You can discuss this proposal <a href="https://github.com/tc39/proposal-float16array">on GitHub</a>.</p>
</div>
<emu-clause id="sec-proposal-intro">
<h1>Introduction</h1>
<p>This proposal adds a new kind of TypedArray. TypedArrays are specified in a slightly different way from the rest of the standard library, so the specification for this proposal consists of a surprisingly small change.</p>
</emu-clause>
<emu-clause id="sec-constructor-properties-of-the-global-object">
<h1>Constructor Properties of the Global Object</h1>
<p>The following subclause is inserted.</p>
<emu-clause id="sec-float16array">
<h1><ins>Float16Array ( . . . )</ins></h1>
<p>See <emu-xref href="#sec-typedarray-constructors"></emu-xref>.</p>
</emu-clause>
</emu-clause>
<emu-clause id="sec-function-properties-of-the-math-object">
<h1>Function Properties of the Math Object</h1>
<emu-clause id="sec-math.f16round">
<h1><ins>Math.f16round ( _x_ )</ins></h1>
<p>This function performs the following steps when called:</p>
<emu-alg>
1. Let _n_ be ? ToNumber(_x_).
1. If _n_ is *NaN*, return *NaN*.
1. If _n_ is one of *+0*<sub>𝔽</sub>, *-0*<sub>𝔽</sub>, *+∞*<sub>𝔽</sub>, or *-∞*<sub>𝔽</sub>, 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_.
</emu-alg>
<emu-note>
<p>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*<sub>𝔽</sub>, for example, for which which Math.f16round(_k_) is *1.0009765625*<sub>𝔽</sub>, but Math.f16round(Math.fround(_k_)) is *1*<sub>𝔽</sub>.</p>
<p>Not all platforms provide native support for casting from binary64 to binary16. There are various libraries which can provide this, including the MIT-licensed <a href="https://half.sourceforge.net/">half</a> 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.</p>
</emu-note>
</emu-clause>
</emu-clause>
<emu-clause id="sec-typedarray-objects">
<h1>TypedArray Objects</h1>
<p>The table below is modified by the addition of a row for float16.</p>
<emu-table id="table-the-typedarray-constructors" caption="The TypedArray Constructors">
<table>
<tr>
<th>
Constructor Name and Intrinsic
</th>
<th>
Element Type
</th>
<th>
Element Size
</th>
<th>
Conversion Operation
</th>
<th>
Description
</th>
</tr>
<tr>
<td>
Int8Array<br>
<dfn>%Int8Array%</dfn>
</td>
<td>
~Int8~
</td>
<td>
1
</td>
<td>
ToInt8
</td>
<td>
8-bit two's complement signed integer
</td>
</tr>
<tr>
<td>
Uint8Array<br>
<dfn>%Uint8Array%</dfn>
</td>
<td>
~Uint8~
</td>
<td>
1
</td>
<td>
ToUint8
</td>
<td>
8-bit unsigned integer
</td>
</tr>
<tr>
<td>
Uint8ClampedArray<br>
<dfn>%Uint8ClampedArray%</dfn>
</td>
<td>
~Uint8C~
</td>
<td>
1
</td>
<td>
ToUint8Clamp
</td>
<td>
8-bit unsigned integer (clamped conversion)
</td>
</tr>
<tr>
<td>
Int16Array<br>
<dfn>%Int16Array%</dfn>
</td>
<td>
~Int16~
</td>
<td>
2
</td>
<td>
ToInt16
</td>
<td>
16-bit two's complement signed integer
</td>
</tr>
<tr>
<td>
Uint16Array<br>
<dfn>%Uint16Array%</dfn>
</td>
<td>
~Uint16~
</td>
<td>
2
</td>
<td>
ToUint16
</td>
<td>
16-bit unsigned integer
</td>
</tr>
<tr>
<td>
Int32Array<br>
<dfn>%Int32Array%</dfn>
</td>
<td>
~Int32~
</td>
<td>
4
</td>
<td>
ToInt32
</td>
<td>
32-bit two's complement signed integer
</td>
</tr>
<tr>
<td>
Uint32Array<br>
<dfn>%Uint32Array%</dfn>
</td>
<td>
~Uint32~
</td>
<td>
4
</td>
<td>
ToUint32
</td>
<td>
32-bit unsigned integer
</td>
</tr>
<tr>
<td>
BigInt64Array<br>
<dfn>%BigInt64Array%</dfn>
</td>
<td>
~BigInt64~
</td>
<td>
8
</td>
<td>
ToBigInt64
</td>
<td>
64-bit two's complement signed integer
</td>
</tr>
<tr>
<td>
BigUint64Array<br>
<dfn>%BigUint64Array%</dfn>
</td>
<td>
~BigUint64~
</td>
<td>
8
</td>
<td>
ToBigUint64
</td>
<td>
64-bit unsigned integer
</td>
</tr>
<tr>
<td>
<ins>Float16Array<br>
<dfn>%Float16Array%</dfn></ins>
</td>
<td>
<ins>~Float16~</ins>
</td>
<td>
<ins>2</ins>
</td>
<td>
</td>
<td>
<ins>16-bit IEEE floating point</ins>
</td>
</tr>
<tr>
<td>
Float32Array<br>
<dfn>%Float32Array%</dfn>
</td>
<td>
~Float32~
</td>
<td>
4
</td>
<td>
</td>
<td>
32-bit IEEE floating point
</td>
</tr>
<tr>
<td>
Float64Array<br>
<dfn>%Float64Array%</dfn>
</td>
<td>
~Float64~
</td>
<td>
8
</td>
<td>
</td>
<td>
64-bit IEEE floating point
</td>
</tr>
</table>
</emu-table>
</emu-clause>
<emu-clause id="sec-rawbytestonumeric" type="abstract operation" oldids="sec-rawbytestonumber">
<h1>
RawBytesToNumeric (
_type_: a TypedArray element type,
_rawBytes_: a List of byte values,
_isLittleEndian_: a Boolean,
): a Number or a BigInt
</h1>
<dl class="header">
</dl>
<p>The algorithm below is modified by the addition of a case for ~Float16~.</p>
<emu-alg>
1. Let _elementSize_ be the Element Size value specified in <emu-xref href="#table-the-typedarray-constructors"></emu-xref> for Element Type _type_.
1. If _isLittleEndian_ is *false*, reverse the order of the elements of _rawBytes_.
1. <ins>If _type_ is ~Float16~, then</ins>
1. <ins>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.</ins>
1. <ins>If _value_ is an IEEE 754-2019 binary16 NaN value, return the *NaN* Number value.</ins>
1. <ins>Return the Number value that corresponds to _value_.</ins>
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.
1. Return the Number value that corresponds to _value_.
1. If _type_ is ~Float64~, 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 binary64 value.
1. If _value_ is an IEEE 754-2019 binary64 NaN value, return the *NaN* Number value.
1. Return the Number value that corresponds to _value_.
1. If IsUnsignedElementType(_type_) is *true*, then
1. Let _intValue_ be the byte elements of _rawBytes_ concatenated and interpreted as a bit string encoding of an unsigned little-endian binary number.
1. Else,
1. Let _intValue_ be the byte elements of _rawBytes_ concatenated and interpreted as a bit string encoding of a binary little-endian two's complement number of bit length _elementSize_ × 8.
1. If IsBigIntElementType(_type_) is *true*, return the BigInt value that corresponds to _intValue_.
1. Otherwise, return the Number value that corresponds to _intValue_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-numerictorawbytes" type="abstract operation" oldids="sec-numbertorawbytes">
<h1>
NumericToRawBytes (
_type_: a TypedArray element type,
_value_: a Number or a BigInt,
_isLittleEndian_: a Boolean,
): a List of byte values
</h1>
<dl class="header">
</dl>
<p>The algorithm below is modified by the addition of a case for ~Float16~.</p>
<emu-alg>
1. <ins>If _type_ is ~Float16~, then</ins>
1. <ins>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.</ins>
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.
1. Else,
1. Let _n_ be the Element Size value specified in <emu-xref href="#table-the-typedarray-constructors"></emu-xref> for Element Type _type_.
1. Let _convOp_ be the abstract operation named in the Conversion Operation column in <emu-xref href="#table-the-typedarray-constructors"></emu-xref> for Element Type _type_.
1. Let _intValue_ be ℝ(_convOp_(_value_)).
1. If _intValue_ ≥ 0, then
1. Let _rawBytes_ be a List whose elements are the _n_-byte binary encoding of _intValue_. The bytes are ordered in little endian order.
1. Else,
1. Let _rawBytes_ be a List whose elements are the _n_-byte binary two's complement encoding of _intValue_. The bytes are ordered in little endian order.
1. If _isLittleEndian_ is *false*, reverse the order of the elements of _rawBytes_.
1. Return _rawBytes_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-properties-of-the-dataview-prototype-object">
<h1>Properties of the DataView Prototype Object</h1>
<p>The following two subclauses are inserted.</p>
<emu-clause id="sec-dataview.prototype.getfloat16">
<h1><ins>DataView.prototype.getFloat16 ( _byteOffset_ [ , _littleEndian_ ] )</ins></h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _v_ be the *this* value.
1. If _littleEndian_ is not present, set _littleEndian_ to *false*.
1. Return ? GetViewValue(_v_, _byteOffset_, _littleEndian_, ~Float16~).
</emu-alg>
</emu-clause>
<emu-clause id="sec-dataview.prototype.setfloat16">
<h1><ins>DataView.prototype.setFloat16 ( _byteOffset_, _value_ [ , _littleEndian_ ] )</ins></h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
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_).
</emu-alg>
</emu-clause>
</emu-clause>