Line data Source code
1 : // Copyright 2016 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_WASM_LEB_HELPER_H_
6 : #define V8_WASM_LEB_HELPER_H_
7 :
8 : #include <cstddef>
9 : #include <cstdint>
10 :
11 : namespace v8 {
12 : namespace internal {
13 : namespace wasm {
14 :
15 : constexpr size_t kPaddedVarInt32Size = 5;
16 : constexpr size_t kMaxVarInt32Size = 5;
17 : constexpr size_t kMaxVarInt64Size = 10;
18 :
19 : class LEBHelper {
20 : public:
21 : // Write a 32-bit unsigned LEB to {dest}, updating {dest} to point after
22 : // the last uint8_t written. No safety checks.
23 : static void write_u32v(uint8_t** dest, uint32_t val) {
24 4262445 : while (val >= 0x80) {
25 1058969 : *((*dest)++) = static_cast<uint8_t>(0x80 | (val & 0x7F));
26 1058969 : val >>= 7;
27 : }
28 3203476 : *((*dest)++) = static_cast<uint8_t>(val & 0x7F);
29 : }
30 :
31 : // Write a 32-bit signed LEB to {dest}, updating {dest} to point after
32 : // the last uint8_t written. No safety checks.
33 1897826 : static void write_i32v(uint8_t** dest, int32_t val) {
34 1897826 : if (val >= 0) {
35 1895805 : while (val >= 0x40) { // prevent sign extension.
36 199648 : *((*dest)++) = static_cast<uint8_t>(0x80 | (val & 0x7F));
37 199648 : val >>= 7;
38 : }
39 1696157 : *((*dest)++) = static_cast<uint8_t>(val & 0xFF);
40 : } else {
41 248466 : while ((val >> 6) != -1) {
42 46797 : *((*dest)++) = static_cast<uint8_t>(0x80 | (val & 0x7F));
43 46797 : val >>= 7;
44 : }
45 201669 : *((*dest)++) = static_cast<uint8_t>(val & 0x7F);
46 : }
47 1897826 : }
48 :
49 : // Write a 64-bit unsigned LEB to {dest}, updating {dest} to point after
50 : // the last uint8_t written. No safety checks.
51 : static void write_u64v(uint8_t** dest, uint64_t val) {
52 372 : while (val >= 0x80) {
53 302 : *((*dest)++) = static_cast<uint8_t>(0x80 | (val & 0x7F));
54 302 : val >>= 7;
55 : }
56 70 : *((*dest)++) = static_cast<uint8_t>(val & 0x7F);
57 : }
58 :
59 : // Write a 64-bit signed LEB to {dest}, updating {dest} to point after
60 : // the last uint8_t written. No safety checks.
61 140 : static void write_i64v(uint8_t** dest, int64_t val) {
62 140 : if (val >= 0) {
63 372 : while (val >= 0x40) { // prevent sign extension.
64 303 : *((*dest)++) = static_cast<uint8_t>(0x80 | (val & 0x7F));
65 303 : val >>= 7;
66 : }
67 69 : *((*dest)++) = static_cast<uint8_t>(val & 0xFF);
68 : } else {
69 413 : while ((val >> 6) != -1) {
70 342 : *((*dest)++) = static_cast<uint8_t>(0x80 | (val & 0x7F));
71 342 : val >>= 7;
72 : }
73 71 : *((*dest)++) = static_cast<uint8_t>(val & 0x7F);
74 : }
75 140 : }
76 :
77 : // TODO(titzer): move core logic for decoding LEBs from decoder.h to here.
78 :
79 : // Compute the size of {val} if emitted as an LEB32.
80 : static inline size_t sizeof_u32v(size_t val) {
81 : size_t size = 0;
82 1574778 : do {
83 1574778 : size++;
84 1574778 : val = val >> 7;
85 : } while (val > 0);
86 : return size;
87 : }
88 :
89 : // Compute the size of {val} if emitted as an LEB32.
90 : static inline size_t sizeof_i32v(int32_t val) {
91 : size_t size = 1;
92 90177 : if (val >= 0) {
93 4090 : while (val >= 0x40) { // prevent sign extension.
94 2741 : size++;
95 2741 : val >>= 7;
96 : }
97 : } else {
98 268004 : while ((val >> 6) != -1) {
99 179176 : size++;
100 179176 : val >>= 7;
101 : }
102 : }
103 : return size;
104 : }
105 :
106 : // Compute the size of {val} if emitted as an unsigned LEB64.
107 : static inline size_t sizeof_u64v(uint64_t val) {
108 : size_t size = 0;
109 744 : do {
110 744 : size++;
111 744 : val = val >> 7;
112 : } while (val > 0);
113 : return size;
114 : }
115 :
116 : // Compute the size of {val} if emitted as a signed LEB64.
117 : static inline size_t sizeof_i64v(int64_t val) {
118 : size_t size = 1;
119 280 : if (val >= 0) {
120 744 : while (val >= 0x40) { // prevent sign extension.
121 606 : size++;
122 606 : val >>= 7;
123 : }
124 : } else {
125 826 : while ((val >> 6) != -1) {
126 684 : size++;
127 684 : val >>= 7;
128 : }
129 : }
130 : return size;
131 : }
132 : };
133 :
134 : } // namespace wasm
135 : } // namespace internal
136 : } // namespace v8
137 :
138 : #endif // V8_WASM_LEB_HELPER_H_
|