index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.encodeF32 = encodeF32;
  6. exports.encodeF64 = encodeF64;
  7. exports.decodeF32 = decodeF32;
  8. exports.decodeF64 = decodeF64;
  9. exports.DOUBLE_PRECISION_MANTISSA = exports.SINGLE_PRECISION_MANTISSA = exports.NUMBER_OF_BYTE_F64 = exports.NUMBER_OF_BYTE_F32 = void 0;
  10. var _ieee = require("@xtuc/ieee754");
  11. /**
  12. * According to https://webassembly.github.io/spec/binary/values.html#binary-float
  13. * n = 32/8
  14. */
  15. var NUMBER_OF_BYTE_F32 = 4;
  16. /**
  17. * According to https://webassembly.github.io/spec/binary/values.html#binary-float
  18. * n = 64/8
  19. */
  20. exports.NUMBER_OF_BYTE_F32 = NUMBER_OF_BYTE_F32;
  21. var NUMBER_OF_BYTE_F64 = 8;
  22. exports.NUMBER_OF_BYTE_F64 = NUMBER_OF_BYTE_F64;
  23. var SINGLE_PRECISION_MANTISSA = 23;
  24. exports.SINGLE_PRECISION_MANTISSA = SINGLE_PRECISION_MANTISSA;
  25. var DOUBLE_PRECISION_MANTISSA = 52;
  26. exports.DOUBLE_PRECISION_MANTISSA = DOUBLE_PRECISION_MANTISSA;
  27. function encodeF32(v) {
  28. var buffer = [];
  29. (0, _ieee.write)(buffer, v, 0, true, SINGLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F32);
  30. return buffer;
  31. }
  32. function encodeF64(v) {
  33. var buffer = [];
  34. (0, _ieee.write)(buffer, v, 0, true, DOUBLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F64);
  35. return buffer;
  36. }
  37. function decodeF32(bytes) {
  38. var buffer = Buffer.from(bytes);
  39. return (0, _ieee.read)(buffer, 0, true, SINGLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F32);
  40. }
  41. function decodeF64(bytes) {
  42. var buffer = Buffer.from(bytes);
  43. return (0, _ieee.read)(buffer, 0, true, DOUBLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F64);
  44. }