pack_num.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #pragma once
  2. #include <util/system/compiler.h>
  3. #include <util/system/defaults.h>
  4. #include <util/system/unaligned_mem.h>
  5. namespace NKikimr {
  6. constexpr size_t MAX_PACKED32_SIZE = 5;
  7. constexpr size_t MAX_PACKED64_SIZE = 9;
  8. Y_FORCE_INLINE size_t GetPack32Length(ui32 value) {
  9. if (value < 0x80) {
  10. return 1;
  11. }
  12. else if (value < 0x4000) {
  13. return 2;
  14. }
  15. else if (value < 0x200000) {
  16. return 3;
  17. }
  18. else {
  19. return 5;
  20. }
  21. }
  22. [[nodiscard]] Y_FORCE_INLINE size_t Pack32(ui32 value, char buffer[MAX_PACKED32_SIZE]) {
  23. if (value < 0x80) {
  24. buffer[0] = value << 0x1;
  25. return 1;
  26. }
  27. else if (value < 0x4000) {
  28. WriteUnaligned<ui16>(((ui16*)(buffer)), ui16((value << 2) | 0x1));
  29. return 2;
  30. }
  31. else if (value < 0x200000) {
  32. WriteUnaligned<ui32>(((ui32*)(buffer)), ui32((value << 3) | 0x3));
  33. return 3;
  34. }
  35. else {
  36. buffer[0] = 0x7;
  37. WriteUnaligned<ui32>((ui32*)(buffer + 1), ui32(value));
  38. return 5;
  39. }
  40. }
  41. [[nodiscard]] Y_FORCE_INLINE size_t Unpack32(const char* buffer, size_t length, ui32& value) {
  42. if (Y_UNLIKELY(!length)) {
  43. return 0;
  44. }
  45. size_t count;
  46. if (0 == (buffer[0] & 0x1)) {
  47. count = 1;
  48. value = ((const ui8*)buffer)[0] >> 1;
  49. } else if (0 == (buffer[0] & 0x2)) {
  50. count = 2;
  51. if (Y_UNLIKELY(length < count)) {
  52. return 0;
  53. }
  54. value = ReadUnaligned<ui16>((const ui16*)buffer) >> 2;
  55. } else if (0 == (buffer[0] & 0x4)) {
  56. count = 3;
  57. if (Y_UNLIKELY(length < count)) {
  58. return 0;
  59. }
  60. value = (ReadUnaligned<ui16>((const ui16*)buffer) >> 3) | (((const ui8*)buffer)[2] << 13);
  61. } else {
  62. count = 5;
  63. if (Y_UNLIKELY(length < count || buffer[0] != 0x7)) {
  64. return 0;
  65. }
  66. value = ReadUnaligned<ui32>((const ui32*)(buffer + 1));
  67. }
  68. return count;
  69. }
  70. Y_FORCE_INLINE size_t GetPack64Length(ui64 value) {
  71. if (value < 0x80) {
  72. return 1;
  73. }
  74. else if (value < 0x4000) {
  75. return 2;
  76. }
  77. else if (value < 0x200000) {
  78. return 3;
  79. }
  80. else if (value < 0x10000000) {
  81. return 4;
  82. }
  83. else if (value < 0x800000000) {
  84. return 5;
  85. }
  86. else if (value < 0x40000000000) {
  87. return 6;
  88. }
  89. else if (value < 0x2000000000000) {
  90. return 7;
  91. }
  92. else {
  93. return 9;
  94. }
  95. }
  96. [[nodiscard]] Y_FORCE_INLINE size_t Pack64(ui64 value, char buffer[MAX_PACKED64_SIZE]) {
  97. if (value < 0x80) {
  98. buffer[0] = value << 1;
  99. return 1;
  100. }
  101. else if (value < 0x4000) {
  102. WriteUnaligned<ui16>((ui16*)(buffer), ui16((value << 2) | 0x1));
  103. return 2;
  104. }
  105. else if (value < 0x200000) {
  106. WriteUnaligned<ui32>((ui32*)(buffer), ui32((value << 3) | 0x3));
  107. return 3;
  108. }
  109. else if (value < 0x10000000) {
  110. WriteUnaligned<ui32>((ui32*)(buffer), ui32((value << 4) | 0x7));
  111. return 4;
  112. }
  113. else if (value < 0x800000000) {
  114. WriteUnaligned<ui64>((ui64*)(buffer), ui64((value << 5) | 0xF));
  115. return 5;
  116. }
  117. else if (value < 0x40000000000) {
  118. WriteUnaligned<ui64>((ui64*)(buffer), ui64((value << 6) | 0x1F));
  119. return 6;
  120. }
  121. else if (value < 0x2000000000000) {
  122. WriteUnaligned<ui64>((ui64*)(buffer), ui64((value << 7) | 0x3F));
  123. return 7;
  124. }
  125. else {
  126. buffer[0] = 0x7F;
  127. WriteUnaligned<ui64>((ui64*)(buffer + 1), value);
  128. return 9;
  129. }
  130. }
  131. [[nodiscard]] Y_FORCE_INLINE size_t Unpack64(const char* buffer, size_t length, ui64& value) {
  132. if (Y_UNLIKELY(!length)) {
  133. return 0;
  134. }
  135. size_t count;
  136. if (0 == (buffer[0] & 0x1)) {
  137. count = 1;
  138. value = ((const ui8*)buffer)[0] >> 1;
  139. } else if (0 == (buffer[0] & 0x2)) {
  140. count = 2;
  141. if (Y_UNLIKELY(length < count)) {
  142. return 0;
  143. }
  144. value = ReadUnaligned<ui16>((const ui16*)buffer) >> 2;
  145. } else if (0 == (buffer[0] & 0x4)) {
  146. count = 3;
  147. if (Y_UNLIKELY(length < count)) {
  148. return 0;
  149. }
  150. value = (ui64(ReadUnaligned<ui16>((const ui16*)buffer)) >> 3) | (ui64(((const ui8*)buffer)[2]) << 13);
  151. } else if (0 == (buffer[0] & 0x8)) {
  152. count = 4;
  153. if (Y_UNLIKELY(length < count)) {
  154. return 0;
  155. }
  156. value = ReadUnaligned<ui32>((const ui32*)buffer) >> 4;
  157. } else if (0 == (buffer[0] & 0x10)) {
  158. count = 5;
  159. if (Y_UNLIKELY(length < count)) {
  160. return 0;
  161. }
  162. value = (ui64(ReadUnaligned<ui32>((const ui32*)buffer)) >> 5) | (ui64(((const ui8*)buffer)[4]) << 27);
  163. } else if (0 == (buffer[0] & 0x20)) {
  164. count = 6;
  165. if (Y_UNLIKELY(length < count)) {
  166. return 0;
  167. }
  168. value = (ui64(ReadUnaligned<ui32>((const ui32*)buffer)) >> 6) | (ui64(ReadUnaligned<ui16>(((const ui16*)buffer) + 2)) << 26);
  169. } else if (0 == (buffer[0] & 0x40)) {
  170. count = 7;
  171. if (Y_UNLIKELY(length < count)) {
  172. return 0;
  173. }
  174. value = (ui64(ReadUnaligned<ui32>((const ui32*)buffer)) >> 7) | (ui64(ReadUnaligned<ui16>(((const ui16*)buffer) + 2)) << 25) | (ui64(((const ui8*)buffer)[6]) << 41);
  175. } else {
  176. count = 9;
  177. if (Y_UNLIKELY(length < count || buffer[0] != 0x7F)) {
  178. return 0;
  179. }
  180. value = ReadUnaligned<ui64>((const ui64*)(buffer + 1));
  181. }
  182. return count;
  183. }
  184. } // NKikimr