DataView.mjs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { safeIfNeeded } from "./_util/arrayIterator.mjs";
  2. import { convertToNumber, roundToFloat16Bits } from "./_util/converter.mjs";
  3. import {
  4. DataViewPrototypeGetUint16,
  5. DataViewPrototypeSetUint16,
  6. } from "./_util/primordials.mjs";
  7. /**
  8. * returns an unsigned 16-bit float at the specified byte offset from the start of the DataView
  9. *
  10. * @param {DataView} dataView
  11. * @param {number} byteOffset
  12. * @param {[boolean]} opts
  13. * @returns {number}
  14. */
  15. export function getFloat16(dataView, byteOffset, ...opts) {
  16. return convertToNumber(
  17. DataViewPrototypeGetUint16(dataView, byteOffset, ...safeIfNeeded(opts))
  18. );
  19. }
  20. /**
  21. * stores an unsigned 16-bit float value at the specified byte offset from the start of the DataView
  22. *
  23. * @param {DataView} dataView
  24. * @param {number} byteOffset
  25. * @param {number} value
  26. * @param {[boolean]} opts
  27. */
  28. export function setFloat16(dataView, byteOffset, value, ...opts) {
  29. return DataViewPrototypeSetUint16(
  30. dataView,
  31. byteOffset,
  32. roundToFloat16Bits(value),
  33. ...safeIfNeeded(opts)
  34. );
  35. }