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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
| | #include <uwide128.h>
#include <stdarg.h>
#if __SIZEOF_INT128__
union u { unsigned __int128 x; uwide128 s; };
__attribute__((__weak__))
uwide128 __uwide128_neg(uwide128 a) {
union u both = { .s = a, };
both.x = -both.x;
return both.s;
}
__attribute__((__weak__))
uwide128 __uwide128_add(uwide128 a, uint8_t b) {
union u both = { .s = a, };
both.x += b;
return both.s;
}
__attribute__((__weak__))
uwide128 __uwide128_sub(uwide128 a, uint8_t b) {
union u both = { .s = a, };
both.x -= b;
return both.s;
}
__attribute__((__weak__))
uwide128 __uwide128_mul(uwide128 a, uint8_t b) {
union u both = { .s = a, };
both.x *= b;
return both.s;
}
__attribute__((__weak__))
uint8_t __uwide128_div10(uwide128* a) {
union u both = { .s = *a, };
uint8_t ret = both.x % 10;
both.x /= 10;
*a = both.s;
return ret;
}
__attribute__((__weak__))
uint8_t __uwide128_div2(uwide128* a) {
union u both = { .s = *a, };
uint8_t ret = both.x % 2;
both.x /= 2;
*a = both.s;
return ret;
}
__attribute__((__weak__))
uint8_t __uwide128_div8(uwide128* a) {
union u both = { .s = *a, };
uint8_t ret = both.x % 8;
both.x /= 8;
*a = both.s;
return ret;
}
__attribute__((__weak__))
uint8_t __uwide128_div16(uwide128* a) {
union u both = { .s = *a, };
uint8_t ret = both.x % 16;
both.x /= 16;
*a = both.s;
return ret;
}
__attribute__((__weak__))
_Bool __uwide128_le(uwide128 a, uwide128 b) {
union u botha = { .s = a, };
union u bothb = { .s = b, };
return botha.x <= bothb.x;
}
__attribute__((__weak__))
_Bool __uwide128_iszero(uwide128 a) {
union u both = { .s = a, };
return !both.x;
}
uwide128 __uwide128_pop(va_list *ap)
{
return (union u){ .x = va_arg(*ap, __int128) }.s;
}
#else
__attribute__((__weak__))
_Bool __uwide128_le(uwide128 a, uwide128 b) {
return (a.v64[hi64] > b.v64[hi64])
? false
: ((a.v64[hi64] < b.v64[hi64])
? true
: (a.v64[lo64] <= b.v64[lo64]));
}
__attribute__((__weak__))
_Bool __uwide128_iszero(uwide128 a)
{
return !a.v64[0] && !a.v64[1];
}
__attribute__((__weak__))
uwide128 __uwide128_neg(uwide128 a)
{
uwide128 ret = { .v64 = { [0] = ~a.v64[0], [1] = ~a.v64[1], }, };
if (!a.v64[lo64]) ret.v64[hi64]--;
ret.v64[lo64]--;
return ret;
}
uwide128 __uwide128_add(uwide128 a, uint8_t b)
{
uwide128 ret;
uint64_t carry = a.v32[wo32_0];
carry += b;
ret.v32[wo32_0] = carry;
carry >>= 32;
carry += a.v32[wo32_1];
ret.v32[wo32_1] = carry;
carry >>= 32;
carry += a.v32[wo32_2];
ret.v32[wo32_2] = carry;
carry >>= 32;
carry += a.v32[wo32_3];
ret.v32[wo32_3] = carry;
return ret;
}
uwide128 __uwide128_sub(uwide128 a, uint8_t b)
{
uwide128 ret;
int64_t carry = a.v32[wo32_0];
carry -= b;
ret.v32[wo32_0] = carry;
carry /= UINT64_C(0x100000000);
carry += a.v32[wo32_1];
ret.v32[wo32_1] = carry;
carry /= UINT64_C(0x100000000);
carry += a.v32[wo32_2];
ret.v32[wo32_2] = carry;
carry /= UINT64_C(0x100000000);
carry += a.v32[wo32_3];
ret.v32[wo32_3] = carry;
return ret;
}
uwide128 __uwide128_mul(uwide128 a, uint8_t b)
{
uwide128 ret;
uint64_t carry;
uint64_t prod = a.v32[wo32_0];
prod *= b;
carry = prod;
ret.v32[wo32_0] = carry;
carry >>= 32;
prod = a.v32[wo32_1];
prod *= b;
carry += prod;
ret.v32[wo32_1] = carry;
carry >>= 32;
prod = a.v32[wo32_2];
prod *= b;
carry += prod;
ret.v32[wo32_2] = carry;
carry >>= 32;
prod = a.v32[wo32_3];
prod *= b;
carry += prod;
ret.v32[wo32_3] = carry;
return ret;
}
static uint8_t __uwide128_div(uwide128* a, uint8_t b)
{
uint64_t rest = a->v64[hi64] % b;
a->v64[hi64] /= b;
rest <<= 32;
rest |= a->v32[wo32_1];
a->v32[wo32_1] = rest / b;
rest %= b;
rest <<= 32;
rest |= a->v32[wo32_0];
a->v32[wo32_0] = rest / b;
rest %= b;
return rest;
}
uint8_t __uwide128_div10(uwide128* a)
{
return __uwide128_div(a, 10);
}
uint8_t __uwide128_div2(uwide128* a)
{
return __uwide128_div(a, 2);
}
uint8_t __uwide128_div8(uwide128* a)
{
return __uwide128_div(a, 8);
}
uint8_t __uwide128_div16(uwide128* a)
{
return __uwide128_div(a, 16);
}
#endif
|