format.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2025 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include "format.h"
  24. #include <limits>
  25. #include <cmath>
  26. namespace dsr {
  27. uint16_t format_readU16_LE(SafePointer<const uint8_t> source) {
  28. return ((uint16_t)source[0] )
  29. | ((uint16_t)source[1] << 8);
  30. }
  31. uint32_t format_readU24_LE(SafePointer<const uint8_t> source) {
  32. return ((int32_t)source[0] )
  33. | ((int32_t)source[1] << 8 )
  34. | ((int32_t)source[2] << 16);
  35. }
  36. uint32_t format_readU32_LE(SafePointer<const uint8_t> source) {
  37. return ((uint32_t)source[0] )
  38. | ((uint32_t)source[1] << 8 )
  39. | ((uint32_t)source[2] << 16)
  40. | ((uint32_t)source[3] << 24);
  41. }
  42. uint64_t format_readU64_LE(SafePointer<const uint8_t> source) {
  43. return ((uint64_t)source[0] )
  44. | ((uint64_t)source[1] << 8 )
  45. | ((uint64_t)source[2] << 16)
  46. | ((uint64_t)source[3] << 24)
  47. | ((uint64_t)source[4] << 32)
  48. | ((uint64_t)source[5] << 40)
  49. | ((uint64_t)source[6] << 48)
  50. | ((uint64_t)source[7] << 56);
  51. }
  52. int16_t format_readI16_LE(SafePointer<const uint8_t> source) {
  53. return int16_t(format_readU16_LE(source));
  54. }
  55. int32_t format_readI24_LE(SafePointer<const uint8_t> source) {
  56. uint32_t result = format_readU24_LE(source);
  57. if (result & 0b00000000100000000000000000000000) result |= 0b11111111000000000000000000000000;
  58. return int32_t(result);
  59. }
  60. int32_t format_readI32_LE(SafePointer<const uint8_t> source) {
  61. return int32_t(format_readU32_LE(source));
  62. }
  63. int64_t format_readI64_LE(SafePointer<const uint8_t> source) {
  64. return int64_t(format_readU64_LE(source));
  65. }
  66. void format_writeU16_LE(SafePointer<uint8_t> target, uint16_t value) {
  67. target[0] = uint8_t((value & 0x00FF) );
  68. target[1] = uint8_t((value & 0xFF00) >> 8);
  69. }
  70. void format_writeU24_LE(SafePointer<uint8_t> target, uint32_t value) {
  71. target[0] = uint8_t((value & 0x0000FF) );
  72. target[1] = uint8_t((value & 0x00FF00) >> 8);
  73. target[2] = uint8_t((value & 0xFF0000) >> 16);
  74. }
  75. void format_writeU32_LE(SafePointer<uint8_t> target, uint32_t value) {
  76. target[0] = uint8_t((value & 0x000000FF) );
  77. target[1] = uint8_t((value & 0x0000FF00) >> 8);
  78. target[2] = uint8_t((value & 0x00FF0000) >> 16);
  79. target[3] = uint8_t((value & 0xFF000000) >> 24);
  80. }
  81. void format_writeU64_LE(SafePointer<uint8_t> target, uint64_t value) {
  82. target[0] = uint8_t((value & 0x00000000000000FF) );
  83. target[1] = uint8_t((value & 0x000000000000FF00) >> 8);
  84. target[2] = uint8_t((value & 0x0000000000FF0000) >> 16);
  85. target[3] = uint8_t((value & 0x00000000FF000000) >> 24);
  86. target[4] = uint8_t((value & 0x000000FF00000000) >> 32);
  87. target[5] = uint8_t((value & 0x0000FF0000000000) >> 40);
  88. target[6] = uint8_t((value & 0x00FF000000000000) >> 48);
  89. target[7] = uint8_t((value & 0xFF00000000000000) >> 56);
  90. }
  91. void format_writeI16_LE(SafePointer<uint8_t> target, int16_t value) {
  92. format_writeU16_LE(target, uint16_t(value));
  93. }
  94. void format_writeI24_LE(SafePointer<uint8_t> target, int32_t value) {
  95. format_writeU24_LE(target, uint32_t(value));
  96. }
  97. void format_writeI32_LE(SafePointer<uint8_t> target, int32_t value) {
  98. format_writeU32_LE(target, uint32_t(value));
  99. }
  100. void format_writeI64_LE(SafePointer<uint8_t> target, int64_t value) {
  101. format_writeU32_LE(target, uint64_t(value));
  102. }
  103. float format_bitsToF32_IEEE754(uint32_t bits) {
  104. bool sign = bits & 0b10000000000000000000000000000000;
  105. uint32_t exponent = (bits & 0b01111111100000000000000000000000) >> 23;
  106. uint32_t mantissa = bits & 0b00000000011111111111111111111111;
  107. if (exponent == 0b11111111) {
  108. if (mantissa == 0u) {
  109. return sign ? -std::numeric_limits<float>::infinity() : std::numeric_limits<float>::infinity();
  110. } else {
  111. return std::numeric_limits<float>::quiet_NaN();
  112. }
  113. } else if (exponent == 0 && mantissa == 0) {
  114. return 0.0f;
  115. }
  116. float value;
  117. if (exponent == 0) {
  118. value = std::ldexp(mantissa, -126 - 23);
  119. } else {
  120. value = std::ldexp((mantissa | 0b100000000000000000000000), exponent - 127 - 23);
  121. }
  122. return sign ? -value : value;
  123. }
  124. double format_bitsToF64_IEEE754(uint64_t bits) {
  125. bool sign = bits & 0b1000000000000000000000000000000000000000000000000000000000000000;
  126. uint64_t exponent = (bits & 0b0111111111110000000000000000000000000000000000000000000000000000) >> 52;
  127. uint64_t mantissa = bits & 0b0000000000001111111111111111111111111111111111111111111111111111;
  128. if (exponent == 0b11111111111) {
  129. if (mantissa == 0u) {
  130. return sign ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity();
  131. } else {
  132. return std::numeric_limits<double>::quiet_NaN();
  133. }
  134. } else if (exponent == 0 && mantissa == 0) {
  135. return 0.0;
  136. }
  137. double value;
  138. if (exponent == 0) {
  139. value = std::ldexp(mantissa, -1022 - 52);
  140. } else {
  141. value = std::ldexp((mantissa | 0b10000000000000000000000000000000000000000000000000000), exponent - 1023 - 52);
  142. }
  143. return sign ? -value : value;
  144. }
  145. }