PostgreSQL Source Code: src/include/common/int128.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#ifndef INT128_H
18#define INT128_H
19
20
21
22
23
24#ifndef USE_NATIVE_INT128
25#ifdef HAVE_INT128
26#define USE_NATIVE_INT128 1
27#else
28#define USE_NATIVE_INT128 0
29#endif
30#endif
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45#if USE_NATIVE_INT128
46
48
49#define PG_INT128_HI_INT64(i128) ((int64) ((i128) >> 64))
50#define PG_INT128_LO_UINT64(i128) ((uint64) (i128))
51
52#else
53
64
65#define PG_INT128_HI_INT64(i128) ((i128).hi)
66#define PG_INT128_LO_UINT64(i128) ((i128).lo)
67
68#endif
69
70
71
72
73
76{
77#if USE_NATIVE_INT128
78 return (((int128) hi) << 64) + lo;
79#else
81
83 val.lo = lo;
84 return val;
85#endif
86}
87
88
89
90
91static inline void
93{
94#if USE_NATIVE_INT128
96#else
97
98
99
100
101
102
103
104
105
107
108 i128->lo += v;
110#endif
111}
112
113
114
115
116static inline void
118{
119#if USE_NATIVE_INT128
121#else
122
123
124
125
126
127
128
129
130
131
132
134
135 i128->lo += v;
137#endif
138}
139
140
141
142
143static inline void
145{
146#if USE_NATIVE_INT128
148#else
151#endif
152}
153
154
155
156
157static inline void
159{
160#if USE_NATIVE_INT128
162#else
163
164
165
166
167
169
170 i128->lo -= v;
172#endif
173}
174
175
176
177
178static inline void
180{
181#if USE_NATIVE_INT128
183#else
184
186
187 i128->lo -= v;
189#endif
190}
191
192
193
194
195
196#define INT64_HI_INT32(i64) ((int32) ((i64) >> 32))
197#define INT64_LO_UINT32(i64) ((uint32) (i64))
198
199
200
201
202static inline void
204{
205#if USE_NATIVE_INT128
206
207
208
209
211#else
212
214 "arithmetic right shift is needed");
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
236 {
242
243
245
246
250
251
255
256
258 }
259#endif
260}
261
262
263
264
265static inline void
267{
268#if USE_NATIVE_INT128
270#else
271
273 {
279
280
282
283
287
288
292
293
295 }
296#endif
297}
298
299
300
301
302
303
304
305
306
307static inline void
309{
310#if USE_NATIVE_INT128
312
315#else
316
317
318
319
320
321
322
323
324
325
326
327
334
335
336 if (i128->hi < 0)
337 {
340 if (n_lo != 0)
342 }
343 else
344 {
347 }
348
349
350 d = abs(v);
351
352
353 q = n_hi / d;
354 r = n_hi % d;
356
357
358 tmp = (r << 32) + (n_lo >> 32);
359 q = tmp / d;
360 r = tmp % d;
361
362
364 n_lo = q << 32;
365 q = tmp / d;
366 r = tmp % d;
368
369
371
372
373 if ((i128->hi < 0) != (v < 0))
374 {
377 if (n_lo != 0)
379 }
382#endif
383}
384
385
386
387
388static inline bool
390{
391#if USE_NATIVE_INT128
392 return x == 0;
393#else
394 return x.hi == 0 && x.lo == 0;
395#endif
396}
397
398
399
400
401static inline int
403{
404#if USE_NATIVE_INT128
405 if (x < 0)
406 return -1;
407 if (x > 0)
408 return 1;
409 return 0;
410#else
411 if (x.hi < 0)
412 return -1;
413 if (x.hi > 0)
414 return 1;
415 if (x.lo > 0)
416 return 1;
417 return 0;
418#endif
419}
420
421
422
423
424static inline int
426{
427#if USE_NATIVE_INT128
429 return -1;
431 return 1;
432 return 0;
433#else
435 return -1;
437 return 1;
439 return -1;
441 return 1;
442 return 0;
443#endif
444}
445
446
447
448
451{
452#if USE_NATIVE_INT128
454#else
456
459 return val;
460#endif
461}
462
463
464
465
466
467static inline int64
469{
470#if USE_NATIVE_INT128
472#else
474#endif
475}
476
477#endif
#define StaticAssertDecl(condition, errmessage)
static void int128_sub_uint64(INT128 *i128, uint64 v)
static void int128_add_uint64(INT128 *i128, uint64 v)
static INT128 make_int128(int64 hi, uint64 lo)
static bool int128_is_zero(INT128 x)
static void int128_sub_int64_mul_int64(INT128 *i128, int64 x, int64 y)
#define INT64_HI_INT32(i64)
static int int128_sign(INT128 x)
static void int128_add_int128(INT128 *i128, INT128 v)
static void int128_sub_int64(INT128 *i128, int64 v)
static int int128_compare(INT128 x, INT128 y)
static INT128 int64_to_int128(int64 v)
static void int128_add_int64(INT128 *i128, int64 v)
#define INT64_LO_UINT32(i64)
static int64 int128_to_int64(INT128 val)
static void int128_div_mod_int32(INT128 *i128, int32 v, int32 *remainder)
static void int128_add_int64_mul_int64(INT128 *i128, int64 x, int64 y)