PBUtility.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2018 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #ifndef MMKV_PBUTILITY_H
  21. #define MMKV_PBUTILITY_H
  22. #ifdef __cplusplus
  23. #include "MMKVPredef.h"
  24. #include <cstdint>
  25. #ifndef MMKV_WIN32
  26. # ifndef likely
  27. # define unlikely(x) (__builtin_expect(bool(x), 0))
  28. # define likely(x) (__builtin_expect(bool(x), 1))
  29. # endif
  30. #else
  31. # ifndef likely
  32. # define unlikely(x) (x)
  33. # define likely(x) (x)
  34. # endif
  35. #endif
  36. namespace mmkv {
  37. template <typename T, typename P>
  38. union Converter {
  39. static_assert(sizeof(T) == sizeof(P), "size not match");
  40. T first;
  41. P second;
  42. };
  43. static inline int64_t Float64ToInt64(double v) {
  44. Converter<double, int64_t> converter;
  45. converter.first = v;
  46. return converter.second;
  47. }
  48. static inline int32_t Float32ToInt32(float v) {
  49. Converter<float, int32_t> converter;
  50. converter.first = v;
  51. return converter.second;
  52. }
  53. static inline double Int64ToFloat64(int64_t v) {
  54. Converter<double, int64_t> converter;
  55. converter.second = v;
  56. return converter.first;
  57. }
  58. static inline float Int32ToFloat32(int32_t v) {
  59. Converter<float, int32_t> converter;
  60. converter.second = v;
  61. return converter.first;
  62. }
  63. static inline uint64_t Int64ToUInt64(int64_t v) {
  64. Converter<int64_t, uint64_t> converter;
  65. converter.first = v;
  66. return converter.second;
  67. }
  68. static inline int64_t UInt64ToInt64(uint64_t v) {
  69. Converter<int64_t, uint64_t> converter;
  70. converter.second = v;
  71. return converter.first;
  72. }
  73. static inline uint32_t Int32ToUInt32(int32_t v) {
  74. Converter<int32_t, uint32_t> converter;
  75. converter.first = v;
  76. return converter.second;
  77. }
  78. static inline int32_t UInt32ToInt32(uint32_t v) {
  79. Converter<int32_t, uint32_t> converter;
  80. converter.second = v;
  81. return converter.first;
  82. }
  83. static inline int32_t logicalRightShift32(int32_t value, uint32_t spaces) {
  84. return UInt32ToInt32((Int32ToUInt32(value) >> spaces));
  85. }
  86. static inline int64_t logicalRightShift64(int64_t value, uint32_t spaces) {
  87. return UInt64ToInt64((Int64ToUInt64(value) >> spaces));
  88. }
  89. constexpr uint32_t LittleEdian32Size = 4;
  90. constexpr uint32_t pbFloatSize() {
  91. return LittleEdian32Size;
  92. }
  93. constexpr uint32_t pbFixed32Size() {
  94. return LittleEdian32Size;
  95. }
  96. constexpr uint32_t LittleEdian64Size = 8;
  97. constexpr uint32_t pbDoubleSize() {
  98. return LittleEdian64Size;
  99. }
  100. constexpr uint32_t pbBoolSize() {
  101. return 1;
  102. }
  103. extern uint32_t pbRawVarint32Size(uint32_t value);
  104. static inline uint32_t pbRawVarint32Size(int32_t value) {
  105. return pbRawVarint32Size(Int32ToUInt32(value));
  106. }
  107. extern uint32_t pbUInt64Size(uint64_t value);
  108. static inline uint32_t pbInt64Size(int64_t value) {
  109. return pbUInt64Size(Int64ToUInt64(value));
  110. }
  111. static inline uint32_t pbInt32Size(int32_t value) {
  112. if (value >= 0) {
  113. return pbRawVarint32Size(value);
  114. } else {
  115. return 10;
  116. }
  117. }
  118. static inline uint32_t pbUInt32Size(uint32_t value) {
  119. return pbRawVarint32Size(value);
  120. }
  121. static inline uint32_t pbMMBufferSize(const MMBuffer &data) {
  122. auto valueLength = static_cast<uint32_t>(data.length());
  123. return valueLength + pbUInt32Size(valueLength);
  124. }
  125. constexpr uint32_t Fixed32Size = pbFixed32Size();
  126. } // namespace mmkv
  127. #endif
  128. #endif //MMKV_PBUTILITY_H