4 typedef struct Int128 Int128
;
11 static inline Int128
int128_make64(uint64_t a
)
13 return (Int128
) { a
, 0 };
16 static inline uint64_t int128_get64(Int128 a
)
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
);
44 static inline Int128
int128_neg(Int128 a
)
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
)
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
)
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
);