Coverage Report

Created: 2024-07-27 06:53

/src/rocksdb/util/coding_lean.h
Line
Count
Source (jump to first uncovered line)
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
6
// Encoding independent of machine byte order:
7
// * Fixed-length numbers are encoded with least-significant byte first
8
//   (little endian, native order on Intel and others)
9
//
10
// More functions in coding.h
11
12
#pragma once
13
14
#include <cstdint>
15
#include <cstring>
16
17
#include "port/port.h"  // for port::kLittleEndian
18
19
namespace ROCKSDB_NAMESPACE {
20
21
// Lower-level versions of Put... that write directly into a character buffer
22
// REQUIRES: dst has enough space for the value being written
23
// -- Implementation of the functions declared above
24
0
inline void EncodeFixed16(char* buf, uint16_t value) {
25
0
  if (port::kLittleEndian) {
26
0
    memcpy(buf, &value, sizeof(value));
27
0
  } else {
28
0
    buf[0] = value & 0xff;
29
0
    buf[1] = (value >> 8) & 0xff;
30
0
  }
31
0
}
32
33
2.83M
inline void EncodeFixed32(char* buf, uint32_t value) {
34
2.83M
  if (port::kLittleEndian) {
35
2.83M
    memcpy(buf, &value, sizeof(value));
36
2.83M
  } else {
37
0
    buf[0] = value & 0xff;
38
0
    buf[1] = (value >> 8) & 0xff;
39
0
    buf[2] = (value >> 16) & 0xff;
40
0
    buf[3] = (value >> 24) & 0xff;
41
0
  }
42
2.83M
}
43
44
5.99M
inline void EncodeFixed64(char* buf, uint64_t value) {
45
5.99M
  if (port::kLittleEndian) {
46
5.99M
    memcpy(buf, &value, sizeof(value));
47
5.99M
  } else {
48
0
    buf[0] = value & 0xff;
49
0
    buf[1] = (value >> 8) & 0xff;
50
0
    buf[2] = (value >> 16) & 0xff;
51
0
    buf[3] = (value >> 24) & 0xff;
52
0
    buf[4] = (value >> 32) & 0xff;
53
0
    buf[5] = (value >> 40) & 0xff;
54
0
    buf[6] = (value >> 48) & 0xff;
55
0
    buf[7] = (value >> 56) & 0xff;
56
0
  }
57
5.99M
}
58
59
// Lower-level versions of Get... that read directly from a character buffer
60
// without any bounds checking.
61
62
0
inline uint16_t DecodeFixed16(const char* ptr) {
63
0
  if (port::kLittleEndian) {
64
    // Load the raw bytes
65
0
    uint16_t result;
66
0
    memcpy(&result, ptr, sizeof(result));  // gcc optimizes this to a plain load
67
0
    return result;
68
0
  } else {
69
0
    return ((static_cast<uint16_t>(static_cast<unsigned char>(ptr[0]))) |
70
0
            (static_cast<uint16_t>(static_cast<unsigned char>(ptr[1])) << 8));
71
0
  }
72
0
}
73
74
11.9M
inline uint32_t DecodeFixed32(const char* ptr) {
75
11.9M
  if (port::kLittleEndian) {
76
    // Load the raw bytes
77
11.9M
    uint32_t result;
78
11.9M
    memcpy(&result, ptr, sizeof(result));  // gcc optimizes this to a plain load
79
11.9M
    return result;
80
11.9M
  } else {
81
0
    return ((static_cast<uint32_t>(static_cast<unsigned char>(ptr[0]))) |
82
0
            (static_cast<uint32_t>(static_cast<unsigned char>(ptr[1])) << 8) |
83
0
            (static_cast<uint32_t>(static_cast<unsigned char>(ptr[2])) << 16) |
84
0
            (static_cast<uint32_t>(static_cast<unsigned char>(ptr[3])) << 24));
85
0
  }
86
11.9M
}
87
88
139M
inline uint64_t DecodeFixed64(const char* ptr) {
89
139M
  if (port::kLittleEndian) {
90
    // Load the raw bytes
91
139M
    uint64_t result;
92
139M
    memcpy(&result, ptr, sizeof(result));  // gcc optimizes this to a plain load
93
139M
    return result;
94
139M
  } else {
95
0
    uint64_t lo = DecodeFixed32(ptr);
96
0
    uint64_t hi = DecodeFixed32(ptr + 4);
97
0
    return (hi << 32) | lo;
98
0
  }
99
139M
}
100
101
}  // namespace ROCKSDB_NAMESPACE