@@ -244,142 +244,139 @@ function yyBuffer( _size, _type, _alignment, _srcbytebuff ) {
244
244
}
245
245
}
246
246
247
- if ( ! DataView . prototype . getFloat16 ) {
248
- // #################################################################################################
249
- /// Function: getFloat16
250
- /// Description:
251
- /// Retrieve and decode a 16-bit floating-point number from a DataView.
252
- ///
253
- /// Parameters:
254
- /// byteOffset - The offset where the 16-bit float is stored.
255
- /// littleEndian - Specifies the endianness of the data (true for little-endian, false for big-endian).
256
- ///
257
- /// Returns:
258
- /// The decoded 16-bit floating-point number.
259
- /// If the input data is invalid or out of range, NaN is returned.
260
- // #################################################################################################
261
- DataView . prototype . getFloat16 = function ( byteOffset , littleEndian ) {
262
- // Define a custom getFloat16 method if it doesn't exist
263
- // byteOffset: The offset where the 16-bit float is stored
264
- // littleEndian: Specifies the endianness of the data
265
-
266
- // Used references: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
267
-
268
- // Read the 16-bit float as an unsigned integer
269
- const uint16 = this . getUint16 ( byteOffset , littleEndian ) ;
270
-
271
- // Extract the sign bit (bit 15)
272
- const sign = ( uint16 & 0x8000 ) ? - 1 : 1 ;
273
-
274
- // Extract the exponent bits (bits 10-14)
275
- const exponent = ( uint16 >> 10 ) & 0x1F ;
276
-
277
- // Extract the mantissa bits (bits 0-9)
278
- const mantissa = uint16 & 0x3FF ;
279
-
280
- if ( exponent === 0 ) {
281
- // If exponent is 0, it's a denormalized number or zero
282
- if ( mantissa === 0 ) {
283
- // Zero
284
- return sign * 0.0 ;
285
- } else {
286
- // Denormalized number
287
- // Calculate the value using the formula for denormalized numbers
288
- return sign * Math . pow ( 2 , - 14 ) * ( mantissa / 1024 ) ;
289
- }
290
- } else if ( exponent === 31 ) {
291
- // If exponent is 31, it's infinity or NaN
292
- if ( mantissa === 0 ) {
293
- // Positive or negative infinity
294
- return ( sign === 1 ) ? Infinity : - Infinity ;
295
- } else {
296
- // NaN
297
- return NaN ;
298
- }
247
+
248
+ // #################################################################################################
249
+ /// Function: yygetFloat16
250
+ /// Description:
251
+ /// Retrieve and decode a 16-bit floating-point number from a DataView.
252
+ ///
253
+ /// Parameters:
254
+ /// byteOffset - The offset where the 16-bit float is stored.
255
+ /// littleEndian - Specifies the endianness of the data (true for little-endian, false for big-endian).
256
+ ///
257
+ /// Returns:
258
+ /// The decoded 16-bit floating-point number.
259
+ /// If the input data is invalid or out of range, NaN is returned.
260
+ // #################################################################################################
261
+ DataView . prototype . yygetFloat16 = function ( byteOffset , littleEndian ) {
262
+ // byteOffset: The offset where the 16-bit float is stored
263
+ // littleEndian: Specifies the endianness of the data
264
+
265
+ // Used references: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
266
+
267
+ // Read the 16-bit float as an unsigned integer
268
+ const uint16 = this . getUint16 ( byteOffset , littleEndian ) ;
269
+
270
+ // Extract the sign bit (bit 15)
271
+ const sign = ( uint16 & 0x8000 ) ? - 1 : 1 ;
272
+
273
+ // Extract the exponent bits (bits 10-14)
274
+ const exponent = ( uint16 >> 10 ) & 0x1F ;
275
+
276
+ // Extract the mantissa bits (bits 0-9)
277
+ const mantissa = uint16 & 0x3FF ;
278
+
279
+ if ( exponent === 0 ) {
280
+ // If exponent is 0, it's a denormalized number or zero
281
+ if ( mantissa === 0 ) {
282
+ // Zero
283
+ return sign * 0.0 ;
299
284
} else {
300
- // Normalized number
301
- // Calculate the value using the formula for normalized numbers
302
- return sign * Math . pow ( 2 , exponent - 15 ) * ( 1 + mantissa / 1024 ) ;
285
+ // Denormalized number
286
+ // Calculate the value using the formula for denormalized numbers
287
+ return sign * Math . pow ( 2 , - 14 ) * ( mantissa / 1024 ) ;
303
288
}
304
- } ;
305
- }
306
-
307
- if ( ! DataView . prototype . setFloat16 ) {
308
- // #################################################################################################
309
- /// Function: setFloat16
310
- /// Description:
311
- /// Encode and write a 16-bit floating-point number (float16) to a DataView.
312
- ///
313
- /// Parameters:
314
- /// offset - The offset where the float16 will be written in the DataView.
315
- /// value - The float16 value to encode and write.
316
- /// littleEndian - Optional. Specifies the endianness of the data (true for little-endian, false for big-endian).
317
- ///
318
- /// Returns:
319
- /// None.
320
- ///
321
- /// Details:
322
- /// The function encodes the provided float16 value as per IEEE 754-2008 standard for half-precision
323
- /// floating-point numbers and writes it to the specified offset in the DataView. If the value is
324
- /// outside the representable range for float16, a RangeError is thrown.
325
- ///
326
- /// Special cases:
327
- /// - If the value is 0, it is written as a positive zero.
328
- /// - If the value is NaN, it is written as the NaN representation.
329
- ///
330
- /// Example Usage:
331
- /// const dataView = new DataView(new ArrayBuffer(2));
332
- /// dataView.setFloat16(0, 1.0, true); // Write float16 value 1.0 in little-endian
333
- // #################################################################################################
334
- DataView . prototype . setFloat16 = function ( offset , value , littleEndian = true ) {
335
- let sign = 0 ;
336
- let exponent = 0 ;
337
- let mantissa = 0 ;
338
-
339
- // Used references: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
340
- if ( isNaN ( value ) ) {
341
- // If the value is NaN, use the standard NaN representation for float16.
342
- mantissa = 0x200 ;
343
- exponent = 0x1F ;
344
- } else if ( value === Infinity || value === - Infinity ) {
345
- // Handling Infinity.
346
- exponent = 0x1F ;
347
- } else if ( value === 0 ) {
348
- // Handling zero (both positive and negative).
349
- sign = ( 1 / value === - Infinity ) ? 0x8000 : 0 ;
289
+ } else if ( exponent === 31 ) {
290
+ // If exponent is 31, it's infinity or NaN
291
+ if ( mantissa === 0 ) {
292
+ // Positive or negative infinity
293
+ return ( sign === 1 ) ? Infinity : - Infinity ;
350
294
} else {
351
- sign = value < 0 ? 0x8000 : 0 ;
352
- value = Math . abs ( value ) ;
353
-
354
- if ( value >= Math . pow ( 2 , - 14 ) ) {
355
- // Handle normal numbers.
356
- let exponentAndMantissa = Math . floor ( Math . log2 ( value ) + 15 ) ;
295
+ // NaN
296
+ return NaN ;
297
+ }
298
+ } else {
299
+ // Normalized number
300
+ // Calculate the value using the formula for normalized numbers
301
+ return sign * Math . pow ( 2 , exponent - 15 ) * ( 1 + mantissa / 1024 ) ;
302
+ }
303
+ } ;
304
+
305
+ // #################################################################################################
306
+ /// Function: yysetFloat16
307
+ /// Description:
308
+ /// Encode and write a 16-bit floating-point number (float16) to a DataView.
309
+ ///
310
+ /// Parameters:
311
+ /// offset - The offset where the float16 will be written in the DataView.
312
+ /// value - The float16 value to encode and write.
313
+ /// littleEndian - Optional. Specifies the endianness of the data (true for little-endian, false for big-endian).
314
+ ///
315
+ /// Returns:
316
+ /// None.
317
+ ///
318
+ /// Details:
319
+ /// The function encodes the provided float16 value as per IEEE 754-2008 standard for half-precision
320
+ /// floating-point numbers and writes it to the specified offset in the DataView. If the value is
321
+ /// outside the representable range for float16, a RangeError is thrown.
322
+ ///
323
+ /// Special cases:
324
+ /// - If the value is 0, it is written as a positive zero.
325
+ /// - If the value is NaN, it is written as the NaN representation.
326
+ ///
327
+ /// Example Usage:
328
+ /// const dataView = new DataView(new ArrayBuffer(2));
329
+ /// dataView.yysetFloat16(0, 1.0, true); // Write float16 value 1.0 in little-endian
330
+ // #################################################################################################
331
+ DataView . prototype . yysetFloat16 = function ( offset , value , littleEndian = true ) {
332
+ let sign = 0 ;
333
+ let exponent = 0 ;
334
+ let mantissa = 0 ;
335
+
336
+ // Used references: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
337
+ if ( isNaN ( value ) ) {
338
+ // If the value is NaN, use the standard NaN representation for float16.
339
+ mantissa = 0x200 ;
340
+ exponent = 0x1F ;
341
+ } else if ( value === Infinity || value === - Infinity ) {
342
+ // Handling Infinity.
343
+ exponent = 0x1F ;
344
+ } else if ( value === 0 ) {
345
+ // Handling zero (both positive and negative).
346
+ sign = ( 1 / value === - Infinity ) ? 0x8000 : 0 ;
347
+ } else {
348
+ sign = value < 0 ? 0x8000 : 0 ;
349
+ value = Math . abs ( value ) ;
350
+
351
+ if ( value >= Math . pow ( 2 , - 14 ) ) {
352
+ // Handle normal numbers.
353
+ let exponentAndMantissa = Math . floor ( Math . log2 ( value ) + 15 ) ;
354
+ exponent = exponentAndMantissa ;
355
+ mantissa = Math . floor ( ( value / Math . pow ( 2 , exponent - 15 ) - 1 ) * 1024 ) ;
356
+
357
+ if ( mantissa === 1024 ) {
358
+ // We might end in an exponent overflow state.
359
+ exponentAndMantissa += 1 ;
357
360
exponent = exponentAndMantissa ;
358
- mantissa = Math . floor ( ( value / Math . pow ( 2 , exponent - 15 ) - 1 ) * 1024 ) ;
359
-
360
- if ( mantissa === 1024 ) {
361
- // This needs to be done otherwise this will cause exponent overflow.
362
- exponentAndMantissa += 1 ;
363
- exponent = exponentAndMantissa ;
364
- mantissa = 0 ;
365
- }
366
-
367
- // Check if we overflow into Infinity.
368
- if ( exponentAndMantissa > 30 ) {
369
- // Handle overflow to Infinity.
370
- exponent = 0x1F ;
371
- mantissa = 0 ;
372
- }
373
- } else {
374
- // Handle subnormal numbers.
375
- mantissa = Math . floor ( value / Math . pow ( 2 , - 24 ) ) ;
361
+ mantissa = 0 ;
362
+ }
363
+
364
+ // Check if we overflow into Infinity.
365
+ if ( exponentAndMantissa > 30 ) {
366
+ // Handle overflow to Infinity.
367
+ exponent = 0x1F ;
368
+ mantissa = 0 ;
376
369
}
370
+ } else {
371
+ // Handle subnormal numbers.
372
+ mantissa = Math . floor ( value / Math . pow ( 2 , - 24 ) ) ;
377
373
}
378
-
379
- const float16 = sign | ( exponent << 10 ) | mantissa ;
380
- this . setUint16 ( offset , float16 , littleEndian ) ;
381
- } ;
382
- }
374
+ }
375
+
376
+ const float16 = sign | ( exponent << 10 ) | mantissa ;
377
+ this . setUint16 ( offset , float16 , littleEndian ) ;
378
+ } ;
379
+
383
380
384
381
385
382
// #############################################################################################
@@ -610,7 +607,7 @@ yyBuffer.prototype.yyb_read = function(_type) {
610
607
break ;
611
608
612
609
case eBuffer_F16 :
613
- res = this . m_DataView . getFloat16 ( this . m_BufferIndex , true ) ;
610
+ res = this . m_DataView . yygetFloat16 ( this . m_BufferIndex , true ) ;
614
611
this . m_BufferIndex += 2 ;
615
612
break ;
616
613
case eBuffer_F32 :
@@ -1406,7 +1403,7 @@ yyBuffer.prototype.yyb_write = function(_type, _value) {
1406
1403
break ;
1407
1404
1408
1405
case eBuffer_F16 :
1409
- this . m_DataView . setFloat16 ( this . m_BufferIndex , _value , true ) ;
1406
+ this . m_DataView . yysetFloat16 ( this . m_BufferIndex , _value , true ) ;
1410
1407
this . m_BufferIndex += 2 ;
1411
1408
break ;
1412
1409
case eBuffer_F32 :
@@ -1488,7 +1485,7 @@ yyBuffer.prototype.yyb_peek = function(_type, _offset) {
1488
1485
break ;
1489
1486
1490
1487
case eBuffer_F16 :
1491
- res = this . m_DataView . getFloat16 ( _offset , true ) ;
1488
+ res = this . m_DataView . yygetFloat16 ( _offset , true ) ;
1492
1489
break ;
1493
1490
case eBuffer_F32 :
1494
1491
res = this . m_DataView . getFloat32 ( _offset , true ) ;
0 commit comments