Debian Codegraph @

src/ulid_transform/ulid_uint128.hh

raw symbols SCIP
1 #ifndef ULID_UINT128_HH
2 #define ULID_UINT128_HH
3
4 #include <array>
5 #include <chrono>
6 #include <cstdlib>
7 #include <ctime>
8 #include <functional>
9 #include <random>
10 #include <vector>
11
12 #include "splitmix64.hh"
13 #include "ulid_base32.hh"
14
15 namespace ulid {
16
17 /**
18 * ULID is a 16 byte Universally Unique Lexicographically Sortable Identifier
19 * */
20 typedef __uint128_t ULID;
21
22 /**
23 * EncodeTimestamp will encode the int64_t timestamp to the passed ulid
24 * */
25 inline void EncodeTimestamp(int64_t timestamp, ULID& ulid)
26 {
27 ULID t = static_cast<uint8_t>(timestamp >> 40);
28
29 t <<= 8;
30 t |= static_cast<uint8_t>(timestamp >> 32);
31
32 t <<= 8;
33 t |= static_cast<uint8_t>(timestamp >> 24);
34
35 t <<= 8;
36 t |= static_cast<uint8_t>(timestamp >> 16);
37
38 t <<= 8;
39 t |= static_cast<uint8_t>(timestamp >> 8);
40
41 t <<= 8;
42 t |= static_cast<uint8_t>(timestamp);
43
44 t <<= 80;
45
46 ULID mask = 1;
47 mask <<= 80;
48 mask--;
49
50 ulid = t | (ulid & mask);
51 }
52
53 /**
54 * EncodeTime will encode the time point to the passed ulid
55 * */
56 inline void EncodeTime(std::chrono::time_point<std::chrono::system_clock> time_point, ULID& ulid)
57 {
58 auto time_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(time_point);
59 int64_t timestamp = time_ms.time_since_epoch().count();
60 EncodeTimestamp(timestamp, ulid);
61 }
62
63 /**
64 * EncodeTimeSystemClockNow will encode a ULID using the time obtained using
65 * std::chrono::system_clock::now() by taking the timestamp in milliseconds.
66 * */
67 inline void EncodeTimeSystemClockNow(ULID& ulid)
68 {
69 EncodeTime(std::chrono::system_clock::now(), ulid);
70 }
71
72 /**
73 * EncodeEntropyFast will encode using SplitMix64
74 * with only 2 generated values (providing 128 bits, of which 80 are used).
75 * */
76 inline void EncodeEntropyFast(ULID& ulid)
77 {
78 static thread_local SplitMix64 gen([]() {
79 // Use multiple entropy sources for seeding
80 uint64_t seed = static_cast<uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
81 seed ^= static_cast<uint64_t>(std::random_device { }()) << 32;
82 seed ^= static_cast<uint64_t>(std::random_device { }());
83 return seed;
84 }());
85 constexpr ULID lower80 = (static_cast<ULID>(1) << 80) - 1;
86 ulid = (ulid >> 80) << 80; // Clear lower 80 bits
87 uint64_t first_draw = gen();
88 uint64_t second_draw = gen();
89 ulid |= ((static_cast<ULID>(first_draw) << 16) | (second_draw & 0xFFFF)) & lower80;
90 }
91
92 /**
93 * MarshalTo will marshal a ULID to the passed character array.
94 *
95 * Implementation taken directly from oklog/ulid
96 * (https://sourcegraph.com/github.com/oklog/ulid@0774f81f6e44af5ce5e91c8d7d76cf710e889ebb/-/blob/ulid.go#L162-190)
97 *
98 * timestamp:
99 * dst[0]: first 3 bits of data[0]
100 * dst[1]: last 5 bits of data[0]
101 * dst[2]: first 5 bits of data[1]
102 * dst[3]: last 3 bits of data[1] + first 2 bits of data[2]
103 * dst[4]: bits 3-7 of data[2]
104 * dst[5]: last bit of data[2] + first 4 bits of data[3]
105 * dst[6]: last 4 bits of data[3] + first bit of data[4]
106 * dst[7]: bits 2-6 of data[4]
107 * dst[8]: last 2 bits of data[4] + first 3 bits of data[5]
108 * dst[9]: last 5 bits of data[5]
109 *
110 * entropy:
111 * follows similarly, except now all components are set to 5 bits.
112 * */
113 inline void MarshalTo(const ULID& ulid, char dst[26])
114 {
115 // Decompose into two 64-bit halves kept in registers to avoid
116 // repeated memory loads the compiler generates with __uint128_t.
117 const uint64_t hi = static_cast<uint64_t>(ulid >> 64);
118 const uint64_t lo = static_cast<uint64_t>(ulid);
119
120 // 10 char timestamp (3 + 9*5 = 48 bits)
121 dst[0] = Encoding[(hi >> 61) & 0x07];
122 dst[1] = Encoding[(hi >> 56) & 0x1F];
123 dst[2] = Encoding[(hi >> 51) & 0x1F];
124 dst[3] = Encoding[(hi >> 46) & 0x1F];
125 dst[4] = Encoding[(hi >> 41) & 0x1F];
126 dst[5] = Encoding[(hi >> 36) & 0x1F];
127 dst[6] = Encoding[(hi >> 31) & 0x1F];
128 dst[7] = Encoding[(hi >> 26) & 0x1F];
129 dst[8] = Encoding[(hi >> 21) & 0x1F];
130 dst[9] = Encoding[(hi >> 16) & 0x1F];
131
132 // 16 char entropy (80 bits)
133 dst[10] = Encoding[(hi >> 11) & 0x1F];
134 dst[11] = Encoding[(hi >> 6) & 0x1F];
135 dst[12] = Encoding[(hi >> 1) & 0x1F];
136 dst[13] = Encoding[((hi & 1) << 4) | ((lo >> 60) & 0x0F)];
137 dst[14] = Encoding[(lo >> 55) & 0x1F];
138 dst[15] = Encoding[(lo >> 50) & 0x1F];
139 dst[16] = Encoding[(lo >> 45) & 0x1F];
140 dst[17] = Encoding[(lo >> 40) & 0x1F];
141 dst[18] = Encoding[(lo >> 35) & 0x1F];
142 dst[19] = Encoding[(lo >> 30) & 0x1F];
143 dst[20] = Encoding[(lo >> 25) & 0x1F];
144 dst[21] = Encoding[(lo >> 20) & 0x1F];
145 dst[22] = Encoding[(lo >> 15) & 0x1F];
146 dst[23] = Encoding[(lo >> 10) & 0x1F];
147 dst[24] = Encoding[(lo >> 5) & 0x1F];
148 dst[25] = Encoding[lo & 0x1F];
149 }
150
151 /**
152 * MarshalBinaryTo will Marshal a ULID to the passed byte array
153 * */
154 inline void MarshalBinaryTo(const ULID& ulid, uint8_t dst[16])
155 {
156 // Use bswap to convert each 64-bit half from host order to big-endian,
157 // instead of 16 individual byte shifts.
158 uint64_t high = __builtin_bswap64(static_cast<uint64_t>(ulid >> 64));
159 uint64_t low = __builtin_bswap64(static_cast<uint64_t>(ulid));
160 __builtin_memcpy(dst, &high, 8);
161 __builtin_memcpy(dst + 8, &low, 8);
162 }
163
164 /**
165 * UnmarshalFrom will unmarshal a ULID from the passed character array.
166 * */
167 inline void UnmarshalFrom(const char str[26], ULID& ulid)
168 {
169 // timestamp
170 ulid = (dec[int(str[0])] << 5) | dec[int(str[1])];
171
172 ulid <<= 8;
173 ulid |= (dec[int(str[2])] << 3) | (dec[int(str[3])] >> 2);
174
175 ulid <<= 8;
176 ulid |= (dec[int(str[3])] << 6) | (dec[int(str[4])] << 1) | (dec[int(str[5])] >> 4);
177
178 ulid <<= 8;
179 ulid |= (dec[int(str[5])] << 4) | (dec[int(str[6])] >> 1);
180
181 ulid <<= 8;
182 ulid |= (dec[int(str[6])] << 7) | (dec[int(str[7])] << 2) | (dec[int(str[8])] >> 3);
183
184 ulid <<= 8;
185 ulid |= (dec[int(str[8])] << 5) | dec[int(str[9])];
186
187 // entropy
188 ulid <<= 8;
189 ulid |= (dec[int(str[10])] << 3) | (dec[int(str[11])] >> 2);
190
191 ulid <<= 8;
192 ulid |= (dec[int(str[11])] << 6) | (dec[int(str[12])] << 1) | (dec[int(str[13])] >> 4);
193
194 ulid <<= 8;
195 ulid |= (dec[int(str[13])] << 4) | (dec[int(str[14])] >> 1);
196
197 ulid <<= 8;
198 ulid |= (dec[int(str[14])] << 7) | (dec[int(str[15])] << 2) | (dec[int(str[16])] >> 3);
199
200 ulid <<= 8;
201 ulid |= (dec[int(str[16])] << 5) | dec[int(str[17])];
202
203 ulid <<= 8;
204 ulid |= (dec[int(str[18])] << 3) | (dec[int(str[19])] >> 2);
205
206 ulid <<= 8;
207 ulid |= (dec[int(str[19])] << 6) | (dec[int(str[20])] << 1) | (dec[int(str[21])] >> 4);
208
209 ulid <<= 8;
210 ulid |= (dec[int(str[21])] << 4) | (dec[int(str[22])] >> 1);
211
212 ulid <<= 8;
213 ulid |= (dec[int(str[22])] << 7) | (dec[int(str[23])] << 2) | (dec[int(str[24])] >> 3);
214
215 ulid <<= 8;
216 ulid |= (dec[int(str[24])] << 5) | dec[int(str[25])];
217 }
218
219 /**
220 * UnmarshalBinaryFrom will unmarshal a ULID from the passed byte array.
221 * */
222 inline void UnmarshalBinaryFrom(const uint8_t b[16], ULID& ulid)
223 {
224 // timestamp
225 ulid = b[0];
226
227 ulid <<= 8;
228 ulid |= b[1];
229
230 ulid <<= 8;
231 ulid |= b[2];
232
233 ulid <<= 8;
234 ulid |= b[3];
235
236 ulid <<= 8;
237 ulid |= b[4];
238
239 ulid <<= 8;
240 ulid |= b[5];
241
242 // entropy
243 ulid <<= 8;
244 ulid |= b[6];
245
246 ulid <<= 8;
247 ulid |= b[7];
248
249 ulid <<= 8;
250 ulid |= b[8];
251
252 ulid <<= 8;
253 ulid |= b[9];
254
255 ulid <<= 8;
256 ulid |= b[10];
257
258 ulid <<= 8;
259 ulid |= b[11];
260
261 ulid <<= 8;
262 ulid |= b[12];
263
264 ulid <<= 8;
265 ulid |= b[13];
266
267 ulid <<= 8;
268 ulid |= b[14];
269
270 ulid <<= 8;
271 ulid |= b[15];
272 }
273
274 }; // namespace ulid
275
276 #endif // ULID_UINT128_HH