forked from catid/libcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigMath128.inh
150 lines (120 loc) · 2.19 KB
/
BigMath128.inh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
CAT_INLINE void u128_set(u128 &r, const u64 x)
{
r = x;
}
CAT_INLINE void u128_set(u128 &r, const u64 low, const u64 high)
{
r = ((u128)high << 64) | low;
}
CAT_INLINE u128 u128_sum(const u64 x, const u64 y)
{
return (u128)x + y;
}
CAT_INLINE u128 u128_sum(const u128 x, const u64 y)
{
return x + y;
}
CAT_INLINE u128 u128_sum(const u128 x, const u128 y)
{
return x + y;
}
CAT_INLINE u128 u128_diff(const u64 x, const u64 y)
{
return (s128)x - y;
}
CAT_INLINE u128 u128_diff(const u128 x, const u64 y)
{
return x - y;
}
CAT_INLINE u128 u128_diff(const u128 x, const u128 y)
{
return x - y;
}
CAT_INLINE void u128_add(u128 &r, const u128 x)
{
r += x;
}
CAT_INLINE void u128_add(u128 &r, const u64 x)
{
r += x;
}
CAT_INLINE u128 u128_not(const u128 x)
{
return ~x;
}
CAT_INLINE void u128_or(u128 &r, const u64 x)
{
r |= x;
}
CAT_INLINE u128 u128_and(const u128 x, const u128 y)
{
return x & y;
}
CAT_INLINE void u128_set_bit(u128 &r, int x)
{
r |= (u128)1 << x;
}
CAT_INLINE u32 u128_get_bits(const u128 x, int offset)
{
return (u32)(x >> offset);
}
CAT_INLINE void u128_carry_add(u128 &r, const u64 x)
{
r = (u128)x + (u64)(r >> 64);
}
CAT_INLINE void u128_borrow_add(u128 &r, const u64 x)
{
r = ((s128)r >> 64) + x;
}
CAT_INLINE u128 u128_neg(const u128 x)
{
return -(s128)x;
}
CAT_INLINE void u128_sub(u128 &r, const u128 x)
{
r = (s128)r - x;
}
CAT_INLINE void u128_sub(u128 &r, const u64 x)
{
r = (s128)r - x;
}
CAT_INLINE void u128_rshift(u128 &r, int shift)
{
r >>= shift;
}
CAT_INLINE void u128_lshift(u128 &r, int shift)
{
r <<= shift;
}
CAT_INLINE u128 u128_prod(const u64 x, const u64 y)
{
return (u128)x * y;
}
CAT_INLINE u128 u128_prod_sum(const u64 x, const u64 y, const u64 z)
{
return (u128)x * y + z;
}
CAT_INLINE u128 u128_prod_63_sum(const u64 x, const u64 y, const u64 z)
{
return (u128)x * y + z;
}
CAT_INLINE u128 u128_lshift_sum(const u64 x, int shift, const u64 z)
{
return ((u128)x << shift) + z;
}
CAT_INLINE u64 u128_high(const u128 x)
{
return (u64)(x >> 64);
}
CAT_INLINE u64 u128_low(const u128 x)
{
return (u64)x;
}
CAT_INLINE bool u128_is_neg(const u128 x)
{
return (s128)x < 0;
}
CAT_INLINE void u128_clear_msb(u128 &r)
{
r &= 0x7fffffffffffffffULL;
}