coding_lean.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. // Encoding independent of machine byte order:
  6. // * Fixed-length numbers are encoded with least-significant byte first
  7. // (little endian, native order on Intel and others)
  8. //
  9. // More functions in coding.h
  10. #pragma once
  11. #include <cstdint>
  12. #include <cstring>
  13. #include "port/port.h" // for port::kLittleEndian
  14. namespace ROCKSDB_NAMESPACE {
  15. // Lower-level versions of Put... that write directly into a character buffer
  16. // REQUIRES: dst has enough space for the value being written
  17. // -- Implementation of the functions declared above
  18. inline void EncodeFixed16(char* buf, uint16_t value) {
  19. if (port::kLittleEndian) {
  20. memcpy(buf, &value, sizeof(value));
  21. } else {
  22. buf[0] = value & 0xff;
  23. buf[1] = (value >> 8) & 0xff;
  24. }
  25. }
  26. inline void EncodeFixed32(char* buf, uint32_t value) {
  27. if (port::kLittleEndian) {
  28. memcpy(buf, &value, sizeof(value));
  29. } else {
  30. buf[0] = value & 0xff;
  31. buf[1] = (value >> 8) & 0xff;
  32. buf[2] = (value >> 16) & 0xff;
  33. buf[3] = (value >> 24) & 0xff;
  34. }
  35. }
  36. inline void EncodeFixed64(char* buf, uint64_t value) {
  37. if (port::kLittleEndian) {
  38. memcpy(buf, &value, sizeof(value));
  39. } else {
  40. buf[0] = value & 0xff;
  41. buf[1] = (value >> 8) & 0xff;
  42. buf[2] = (value >> 16) & 0xff;
  43. buf[3] = (value >> 24) & 0xff;
  44. buf[4] = (value >> 32) & 0xff;
  45. buf[5] = (value >> 40) & 0xff;
  46. buf[6] = (value >> 48) & 0xff;
  47. buf[7] = (value >> 56) & 0xff;
  48. }
  49. }
  50. // Lower-level versions of Get... that read directly from a character buffer
  51. // without any bounds checking.
  52. inline uint16_t DecodeFixed16(const char* ptr) {
  53. if (port::kLittleEndian) {
  54. // Load the raw bytes
  55. uint16_t result;
  56. memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
  57. return result;
  58. } else {
  59. return ((static_cast<uint16_t>(static_cast<unsigned char>(ptr[0]))) |
  60. (static_cast<uint16_t>(static_cast<unsigned char>(ptr[1])) << 8));
  61. }
  62. }
  63. inline uint32_t DecodeFixed32(const char* ptr) {
  64. if (port::kLittleEndian) {
  65. // Load the raw bytes
  66. uint32_t result;
  67. memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
  68. return result;
  69. } else {
  70. return ((static_cast<uint32_t>(static_cast<unsigned char>(ptr[0]))) |
  71. (static_cast<uint32_t>(static_cast<unsigned char>(ptr[1])) << 8) |
  72. (static_cast<uint32_t>(static_cast<unsigned char>(ptr[2])) << 16) |
  73. (static_cast<uint32_t>(static_cast<unsigned char>(ptr[3])) << 24));
  74. }
  75. }
  76. inline uint64_t DecodeFixed64(const char* ptr) {
  77. if (port::kLittleEndian) {
  78. // Load the raw bytes
  79. uint64_t result;
  80. memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
  81. return result;
  82. } else {
  83. uint64_t lo = DecodeFixed32(ptr);
  84. uint64_t hi = DecodeFixed32(ptr + 4);
  85. return (hi << 32) | lo;
  86. }
  87. }
  88. } // namespace ROCKSDB_NAMESPACE