/
int128.d
279 lines (242 loc) · 7.2 KB
/
int128.d
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Written in the D programming language
/**
* Implements a signed 128 bit integer type.
*
Author: Walter Bright
Copyright: Copyright (c) 2022, D Language Foundation
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0)
Source: $(PHOBOSSRC std/int128.d)
*/
module std.int128;
private import core.int128;
/***********************************
* 128 bit signed integer type.
*/
public struct Int128
{
@safe pure nothrow @nogc:
Cent data;
/****************
* Construct an `Int128` from a `long` value.
* The upper 64 bits are formed by sign extension.
* Params:
* lo = signed lower 64 bits
*/
this(long lo)
{
data.lo = lo;
data.hi = lo < 0 ? ~0L : 0;
}
/****************
* Construct an `Int128` from a `long` value.
* Params:
* hi = upper 64 bits
* lo = lower 64 bits
*/
this(long hi, long lo)
{
data.hi = hi;
data.lo = lo;
}
this(Cent data)
{
this.data = data;
}
bool opEquals(long lo) const
{
return data.hi == 0 && data.lo == lo;
}
bool opEquals(Int128 op2) const
{
return data.hi == op2.data.hi && data.lo == op2.data.lo;
}
// +Int128
Int128 opUnary(string op)() const
if (op == "+")
{
return this;
}
// -Int128
Int128 opUnary(string op)() const
if (op == "-")
{
return Int128(neg(this.data));
}
// ~Int128
Int128 opUnary(string op)() const
if (op == "~")
{
return Int128(com(this.data));
}
// cast(T)Int128
bool opCast(T : bool)() const
{
return tst(this.data);
}
// Int128 + Int128
Int128 opBinary(string op)(Int128 op2) const
if (op == "+" || op == "-" ||
op == "*" || op == "/" || op == "%" ||
op == "&" || op == "|" || op == "^")
{
if (op == "+")
return Int128(add(this.data, op2.data));
else if (op == "-")
return Int128(sub(this.data, op2.data));
else if (op == "*")
return Int128(mul(this.data, op2.data));
else if (op == "/")
return Int128(div(this.data, op2.data));
else if (op == "%")
{
Cent modulus;
divmod(this.data, op2.data, modulus);
return Int128(modulus);
}
else if (op == "&")
return Int128(and(this.data, op2.data));
else if (op == "|")
return Int128(or(this.data, op2.data));
else if (op == "^")
return Int128(xor(this.data, op2.data));
else
assert(0);
}
// Int128 + long
Int128 opBinary(string op)(long lo) const
if (op == "+" || op == "-" ||
op == "*" || op == "/" || op == "%" ||
op == "&" || op == "|" || op == "^")
{
return mixin("this " ~ op ~ " Int128(0, lo)");
}
// long + Int128
Int128 opBinaryRight(string op)(long lo) const
if (op == "+" || op == "-" ||
op == "*" || op == "/" || op == "%" ||
op == "&" || op == "|" || op == "^")
{
mixin("return Int128(0, lo) " ~ op ~ " this;");
}
// Int128 << uint
Int128 opBinary(string op)(long n) const
if (op == "<<")
{
return Int128(shl(this.data, cast(uint)n));
}
// Int128 >> uint
Int128 opBinary(string op)(long n) const
if (op == ">>")
{
return Int128(sar(this.data, cast(uint)n));
}
// Int128 >>> uint
Int128 opBinary(string op)(long n) const
if (op == ">>>")
{
return Int128(shr(this.data, cast(uint)n));
}
ref Int128 opOpAssign(string op)(Int128 op2)
if (op == "+" || op == "-" ||
op == "*" || op == "/" || op == "%" ||
op == "&" || op == "|" || op == "^" ||
op == "<<" || op == ">>" || op == ">>>")
{
mixin("this = this " ~ op ~ " op2;");
return this;
}
ref Int128 opOpAssign(string op)(long op2)
if (op == "+" || op == "-" ||
op == "*" || op == "/" || op == "%" ||
op == "&" || op == "|" || op == "^" ||
op == "<<" || op == ">>" || op == ">>>")
{
mixin("this = this " ~ op ~ " op2;");
return this;
}
int opCmp(Int128 op2)
{
return this == op2 ? 0 : gt(this.data, op2.data) * 2 - 1;
}
int opCmp(long op2)
{
return opCmp(Int128(0, op2));
}
enum min = Int128(long.min, 0); /// minimum value
enum max = Int128(long.max, ulong.max); /// maximum value
}
/********************************************* Tests ************************************/
version (unittest)
{
import core.stdc.stdio;
void print(Int128 c)
{
printf("%lld, %lld\n", c.data.hi, c.data.lo);
}
void printx(Int128 c)
{
printf("%llx, %llx\n", c.data.hi, c.data.lo);
}
}
unittest
{
Int128 c = Int128(5, 6);
assert(c == c);
assert(c == +c);
assert(c == - -c);
assert(~c == Int128(~5, ~6));
assert(!!c);
assert(!Int128());
assert(c + Int128(10, 20) == Int128(15, 26));
assert(c - Int128(1, 2) == Int128(4, 4));
assert(c * Int128(100, 2) == Int128(610, 12));
assert(c / Int128(3, 2) == Int128(0, 1));
assert(c % Int128(3, 2) == Int128(2, 4));
assert((c & Int128(3, 2)) == Int128(1, 2));
assert((c | Int128(3, 2)) == Int128(7, 6));
assert((c ^ Int128(3, 2)) == Int128(6, 4));
assert(c + 15 == Int128(5, 21));
assert(c - 15 == Int128(4, -9));
assert(c * 15 == Int128(75, 90));
assert(c / 15 == Int128(0, 6148914691236517205));
assert(c % 15 == Int128(0, 11));
assert((c & 15) == Int128(0, 6));
assert((c | 15) == Int128(5, 15));
assert((c ^ 15) == Int128(5, 9));
assert(15 + c == Int128(5, 21));
assert(15 - c == Int128(-5, 9));
assert(15 * c == Int128(75, 90));
assert(15 / c == Int128(0, 0));
assert(15 % c == Int128(0, 15));
assert((15 & c) == Int128(0, 6));
assert((15 | c) == Int128(5, 15));
assert((15 ^ c) == Int128(5, 9));
assert(c << 1 == Int128(10, 12));
assert(-c >> 1 == Int128(-3, 9223372036854775805));
assert(-c >>> 1 == Int128(9223372036854775805, 9223372036854775805));
assert((c += 1) == Int128(5, 7));
assert((c -= 1) == Int128(5, 6));
assert((c += Int128(0, 1)) == Int128(5, 7));
assert((c -= Int128(0, 1)) == Int128(5, 6));
assert((c *= 2) == Int128(10, 12));
assert((c /= 2) == Int128(5, 6));
assert((c %= 2) == Int128());
c += Int128(5, 6);
assert((c *= Int128(10, 20)) == Int128(160, 120));
assert((c /= Int128(10, 20)) == Int128(0, 15));
c += Int128(72, 0);
assert((c %= Int128(10, 20)) == Int128(1, -125));
assert((c &= Int128(3, 20)) == Int128(1, 0));
assert((c |= Int128(8, 2)) == Int128(9, 2));
assert((c ^= Int128(8, 2)) == Int128(1, 0));
c |= Int128(10, 5);
assert((c <<= 1) == Int128(11 * 2, 5 * 2));
assert((c >>>= 1) == Int128(11, 5));
c = Int128(long.min, long.min);
assert((c >>= 1) == Int128(long.min >> 1, cast(ulong)long.min >> 1));
assert(-Int128.min == Int128.min);
assert(Int128.max + 1 == Int128.min);
c = Int128(5, 6);
assert(c < Int128(6, 5));
assert(c > 10);
}