MAINTAINERS: Update email address for Stefan Hajnoczi
[qemu/agraf.git] / int128.h
blobb3864b6cd460d1379b437e3d062450e52a733794
1 #ifndef INT128_H
2 #define INT128_H
4 typedef struct Int128 Int128;
6 struct Int128 {
7 uint64_t lo;
8 int64_t hi;
9 };
11 static inline Int128 int128_make64(uint64_t a)
13 return (Int128) { a, 0 };
16 static inline uint64_t int128_get64(Int128 a)
18 assert(!a.hi);
19 return a.lo;
22 static inline Int128 int128_zero(void)
24 return int128_make64(0);
27 static inline Int128 int128_one(void)
29 return int128_make64(1);
32 static inline Int128 int128_2_64(void)
34 return (Int128) { 0, 1 };
37 static inline Int128 int128_add(Int128 a, Int128 b)
39 Int128 r = { a.lo + b.lo, a.hi + b.hi };
40 r.hi += (r.lo < a.lo) || (r.lo < b.lo);
41 return r;
44 static inline Int128 int128_neg(Int128 a)
46 a.lo = ~a.lo;
47 a.hi = ~a.hi;
48 return int128_add(a, int128_one());
51 static inline Int128 int128_sub(Int128 a, Int128 b)
53 return int128_add(a, int128_neg(b));
56 static inline bool int128_nonneg(Int128 a)
58 return a.hi >= 0;
61 static inline bool int128_eq(Int128 a, Int128 b)
63 return a.lo == b.lo && a.hi == b.hi;
66 static inline bool int128_ne(Int128 a, Int128 b)
68 return !int128_eq(a, b);
71 static inline bool int128_ge(Int128 a, Int128 b)
73 return int128_nonneg(int128_sub(a, b));
76 static inline bool int128_lt(Int128 a, Int128 b)
78 return !int128_ge(a, b);
81 static inline bool int128_le(Int128 a, Int128 b)
83 return int128_ge(b, a);
86 static inline bool int128_gt(Int128 a, Int128 b)
88 return !int128_le(a, b);
91 static inline bool int128_nz(Int128 a)
93 return a.lo || a.hi;
96 static inline Int128 int128_min(Int128 a, Int128 b)
98 return int128_le(a, b) ? a : b;
101 static inline Int128 int128_max(Int128 a, Int128 b)
103 return int128_ge(a, b) ? a : b;
106 static inline void int128_addto(Int128 *a, Int128 b)
108 *a = int128_add(*a, b);
111 static inline void int128_subfrom(Int128 *a, Int128 b)
113 *a = int128_sub(*a, b);
116 #endif