| 1 |
/** |
| 2 |
* Implement $(LINK2 https://digitalmars.com/articles/b62.html, Value Range Propagation). |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 6 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/intrange.d, _intrange.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_intrange.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/intrange.d |
| 10 |
*/ |
| 11 |
|
| 12 |
module dmd.intrange; |
| 13 |
|
| 14 |
import core.stdc.stdio; |
| 15 |
|
| 16 |
import dmd.astenums : Tdchar; |
| 17 |
import dmd.mtype : Type; |
| 18 |
import dmd.globals : uinteger_t; |
| 19 |
|
| 20 |
private uinteger_t copySign(uinteger_t x, bool sign) @safe |
| 21 |
{ |
| 22 |
// return sign ? -x : x; |
| 23 |
return (x - cast(uinteger_t)sign) ^ -cast(uinteger_t)sign; |
| 24 |
} |
| 25 |
|
| 26 |
struct SignExtendedNumber |
| 27 |
{ |
| 28 |
uinteger_t value; |
| 29 |
bool negative; |
| 30 |
|
| 31 |
static SignExtendedNumber fromInteger(uinteger_t value_) @safe |
| 32 |
{ |
| 33 |
return SignExtendedNumber(value_, value_ >> 63); |
| 34 |
} |
| 35 |
|
| 36 |
static SignExtendedNumber extreme(bool minimum) @safe |
| 37 |
{ |
| 38 |
return SignExtendedNumber(minimum - 1, minimum); |
| 39 |
} |
| 40 |
|
| 41 |
static SignExtendedNumber max() @safe |
| 42 |
{ |
| 43 |
return SignExtendedNumber(ulong.max, false); |
| 44 |
} |
| 45 |
|
| 46 |
static SignExtendedNumber min() @safe |
| 47 |
{ |
| 48 |
return SignExtendedNumber(0, true); |
| 49 |
} |
| 50 |
|
| 51 |
bool isMinimum() const @safe |
| 52 |
{ |
| 53 |
return negative && value == 0; |
| 54 |
} |
| 55 |
|
| 56 |
bool opEquals(const ref SignExtendedNumber a) const @safe |
| 57 |
{ |
| 58 |
return value == a.value && negative == a.negative; |
| 59 |
} |
| 60 |
|
| 61 |
int opCmp(const ref SignExtendedNumber a) const @safe |
| 62 |
{ |
| 63 |
if (negative != a.negative) |
| 64 |
{ |
| 65 |
if (negative) |
| 66 |
return -1; |
| 67 |
else |
| 68 |
return 1; |
| 69 |
} |
| 70 |
if (value < a.value) |
| 71 |
return -1; |
| 72 |
if (value > a.value) |
| 73 |
return 1; |
| 74 |
else |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
SignExtendedNumber opUnary(string op : "++")() |
| 79 |
{ |
| 80 |
if (value != ulong.max) |
| 81 |
++value; |
| 82 |
else if (negative) |
| 83 |
{ |
| 84 |
value = 0; |
| 85 |
negative = false; |
| 86 |
} |
| 87 |
return this; |
| 88 |
} |
| 89 |
|
| 90 |
SignExtendedNumber opUnary(string op : "~")() const |
| 91 |
{ |
| 92 |
if (~value == 0) |
| 93 |
return SignExtendedNumber(~value); |
| 94 |
else |
| 95 |
return SignExtendedNumber(~value, !negative); |
| 96 |
} |
| 97 |
|
| 98 |
SignExtendedNumber opUnary(string op : "-")() const |
| 99 |
{ |
| 100 |
if (value == 0) |
| 101 |
return SignExtendedNumber(-cast(ulong)negative); |
| 102 |
else |
| 103 |
return SignExtendedNumber(-value, !negative); |
| 104 |
} |
| 105 |
|
| 106 |
SignExtendedNumber opBinary(string op : "&")(SignExtendedNumber rhs) const |
| 107 |
{ |
| 108 |
return SignExtendedNumber(value & rhs.value); |
| 109 |
} |
| 110 |
|
| 111 |
SignExtendedNumber opBinary(string op : "|")(SignExtendedNumber rhs) |
| 112 |
{ |
| 113 |
return SignExtendedNumber(value | rhs.value); |
| 114 |
} |
| 115 |
|
| 116 |
SignExtendedNumber opBinary(string op : "^")(SignExtendedNumber rhs) |
| 117 |
{ |
| 118 |
return SignExtendedNumber(value ^ rhs.value); |
| 119 |
} |
| 120 |
|
| 121 |
SignExtendedNumber opBinary(string op : "+")(SignExtendedNumber rhs) |
| 122 |
{ |
| 123 |
uinteger_t sum = value + rhs.value; |
| 124 |
const carry = sum < value && sum < rhs.value; |
| 125 |
if (negative != rhs.negative) |
| 126 |
return SignExtendedNumber(sum, !carry); |
| 127 |
if (negative) |
| 128 |
return SignExtendedNumber(carry ? sum : 0, true); |
| 129 |
else |
| 130 |
return SignExtendedNumber(carry ? ulong.max : sum, false); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
SignExtendedNumber opBinary(string op : "-")(SignExtendedNumber rhs) |
| 135 |
{ |
| 136 |
if (rhs.isMinimum()) |
| 137 |
return negative ? SignExtendedNumber(value, false) : max(); |
| 138 |
else |
| 139 |
return this + (-rhs); |
| 140 |
} |
| 141 |
|
| 142 |
SignExtendedNumber opBinary(string op : "*")(SignExtendedNumber rhs) |
| 143 |
{ |
| 144 |
// perform* saturated* multiplication, otherwise we may get bogus ranges |
| 145 |
// like 0x10 * 0x10 == 0x100 == 0. |
| 146 |
|
| 147 |
/* Special handling for zeros: |
| 148 |
INT65_MIN * 0 = 0 |
| 149 |
INT65_MIN * + = INT65_MIN |
| 150 |
INT65_MIN * - = INT65_MAX |
| 151 |
0 * anything = 0 |
| 152 |
*/ |
| 153 |
if (value == 0) |
| 154 |
{ |
| 155 |
if (!negative) |
| 156 |
return this; |
| 157 |
if (rhs.negative) |
| 158 |
return max(); |
| 159 |
else |
| 160 |
return rhs.value == 0 ? rhs : this; |
| 161 |
} |
| 162 |
else if (rhs.value == 0) |
| 163 |
return rhs * this; // don't duplicate the symmetric case. |
| 164 |
|
| 165 |
SignExtendedNumber rv; |
| 166 |
// these are != 0 now surely. |
| 167 |
uinteger_t tAbs = copySign(value, negative); |
| 168 |
uinteger_t aAbs = copySign(rhs.value, rhs.negative); |
| 169 |
rv.negative = negative != rhs.negative; |
| 170 |
if (ulong.max / tAbs < aAbs) |
| 171 |
rv.value = rv.negative - 1; |
| 172 |
else |
| 173 |
rv.value = copySign(tAbs * aAbs, rv.negative); |
| 174 |
return rv; |
| 175 |
} |
| 176 |
|
| 177 |
SignExtendedNumber opBinary(string op : "/")(SignExtendedNumber rhs) |
| 178 |
{ |
| 179 |
/* special handling for zeros: |
| 180 |
INT65_MIN / INT65_MIN = 1 |
| 181 |
anything / INT65_MIN = 0 |
| 182 |
+ / 0 = INT65_MAX (eh?) |
| 183 |
- / 0 = INT65_MIN (eh?) |
| 184 |
*/ |
| 185 |
if (rhs.value == 0) |
| 186 |
{ |
| 187 |
if (rhs.negative) |
| 188 |
return SignExtendedNumber(value == 0 && negative); |
| 189 |
else |
| 190 |
return extreme(negative); |
| 191 |
} |
| 192 |
|
| 193 |
uinteger_t aAbs = copySign(rhs.value, rhs.negative); |
| 194 |
uinteger_t rvVal; |
| 195 |
|
| 196 |
if (!isMinimum()) |
| 197 |
rvVal = copySign(value, negative) / aAbs; |
| 198 |
// Special handling for INT65_MIN |
| 199 |
// if the denominator is not a power of 2, it is same as ulong.max / x. |
| 200 |
else if (aAbs & (aAbs - 1)) |
| 201 |
rvVal = ulong.max / aAbs; |
| 202 |
// otherwise, it's the same as reversing the bits of x. |
| 203 |
else |
| 204 |
{ |
| 205 |
if (aAbs == 1) |
| 206 |
return extreme(!rhs.negative); |
| 207 |
rvVal = 1UL << 63; |
| 208 |
aAbs >>= 1; |
| 209 |
if (aAbs & 0xAAAAAAAAAAAAAAAAUL) rvVal >>= 1; |
| 210 |
if (aAbs & 0xCCCCCCCCCCCCCCCCUL) rvVal >>= 2; |
| 211 |
if (aAbs & 0xF0F0F0F0F0F0F0F0UL) rvVal >>= 4; |
| 212 |
if (aAbs & 0xFF00FF00FF00FF00UL) rvVal >>= 8; |
| 213 |
if (aAbs & 0xFFFF0000FFFF0000UL) rvVal >>= 16; |
| 214 |
if (aAbs & 0xFFFFFFFF00000000UL) rvVal >>= 32; |
| 215 |
} |
| 216 |
bool rvNeg = negative != rhs.negative; |
| 217 |
rvVal = copySign(rvVal, rvNeg); |
| 218 |
|
| 219 |
return SignExtendedNumber(rvVal, rvVal != 0 && rvNeg); |
| 220 |
} |
| 221 |
|
| 222 |
SignExtendedNumber opBinary(string op : "%")(SignExtendedNumber rhs) |
| 223 |
{ |
| 224 |
if (rhs.value == 0) |
| 225 |
return !rhs.negative ? rhs : isMinimum() ? SignExtendedNumber(0) : this; |
| 226 |
|
| 227 |
uinteger_t aAbs = copySign(rhs.value, rhs.negative); |
| 228 |
uinteger_t rvVal; |
| 229 |
|
| 230 |
// a % b == sgn(a) * abs(a) % abs(b). |
| 231 |
if (!isMinimum()) |
| 232 |
rvVal = copySign(value, negative) % aAbs; |
| 233 |
// Special handling for INT65_MIN |
| 234 |
// if the denominator is not a power of 2, it is same as ulong.max % x + 1. |
| 235 |
else if (aAbs & (aAbs - 1)) |
| 236 |
rvVal = ulong.max % aAbs + 1; |
| 237 |
// otherwise, the modulus is trivially zero. |
| 238 |
else |
| 239 |
rvVal = 0; |
| 240 |
|
| 241 |
rvVal = copySign(rvVal, negative); |
| 242 |
return SignExtendedNumber(rvVal, rvVal != 0 && negative); |
| 243 |
} |
| 244 |
|
| 245 |
SignExtendedNumber opBinary(string op : "<<")(SignExtendedNumber rhs) |
| 246 |
{ |
| 247 |
// assume left-shift the shift-amount is always unsigned. Thus negative |
| 248 |
// shifts will give huge result. |
| 249 |
if (value == 0) |
| 250 |
return this; |
| 251 |
if (rhs.negative) |
| 252 |
return extreme(negative); |
| 253 |
|
| 254 |
uinteger_t v = copySign(value, negative); |
| 255 |
|
| 256 |
// compute base-2 log of 'v' to determine the maximum allowed bits to shift. |
| 257 |
// Ref: https://graphics.stanford.edu/~seander/bithacks.html#IntegerLog |
| 258 |
|
| 259 |
// Why is this a size_t? Looks like a bug. |
| 260 |
size_t r, s; |
| 261 |
|
| 262 |
r = (v > 0xFFFFFFFFUL) << 5; v >>= r; |
| 263 |
s = (v > 0xFFFFUL ) << 4; v >>= s; r |= s; |
| 264 |
s = (v > 0xFFUL ) << 3; v >>= s; r |= s; |
| 265 |
s = (v > 0xFUL ) << 2; v >>= s; r |= s; |
| 266 |
s = (v > 0x3UL ) << 1; v >>= s; r |= s; |
| 267 |
r |= (v >> 1); |
| 268 |
|
| 269 |
uinteger_t allowableShift = 63 - r; |
| 270 |
if (rhs.value > allowableShift) |
| 271 |
return extreme(negative); |
| 272 |
else |
| 273 |
return SignExtendedNumber(value << rhs.value, negative); |
| 274 |
} |
| 275 |
|
| 276 |
SignExtendedNumber opBinary(string op : ">>")(SignExtendedNumber rhs) |
| 277 |
{ |
| 278 |
if (rhs.negative || rhs.value > 63) |
| 279 |
return negative ? SignExtendedNumber(-1, true) : SignExtendedNumber(0); |
| 280 |
if (isMinimum()) |
| 281 |
return rhs.value == 0 ? this : SignExtendedNumber(-1UL << (64 - rhs.value), true); |
| 282 |
|
| 283 |
uinteger_t x = value ^ -cast(int)negative; |
| 284 |
x >>= rhs.value; |
| 285 |
return SignExtendedNumber(x ^ -cast(int)negative, negative); |
| 286 |
} |
| 287 |
|
| 288 |
SignExtendedNumber opBinary(string op : "^^")(SignExtendedNumber rhs) |
| 289 |
{ |
| 290 |
// Not yet implemented |
| 291 |
assert(0); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
struct IntRange |
| 296 |
{ |
| 297 |
SignExtendedNumber imin, imax; |
| 298 |
|
| 299 |
this(IntRange another) @safe |
| 300 |
{ |
| 301 |
imin = another.imin; |
| 302 |
imax = another.imax; |
| 303 |
} |
| 304 |
|
| 305 |
this(SignExtendedNumber a) @safe |
| 306 |
{ |
| 307 |
imin = a; |
| 308 |
imax = a; |
| 309 |
} |
| 310 |
|
| 311 |
this(SignExtendedNumber lower, SignExtendedNumber upper) @safe |
| 312 |
{ |
| 313 |
imin = lower; |
| 314 |
imax = upper; |
| 315 |
} |
| 316 |
|
| 317 |
static IntRange fromType(Type type) |
| 318 |
{ |
| 319 |
return fromType(type, type.isUnsigned()); |
| 320 |
} |
| 321 |
|
| 322 |
static IntRange fromType(Type type, bool isUnsigned) |
| 323 |
{ |
| 324 |
if (!type.isIntegral() || type.toBasetype().isTypeVector()) |
| 325 |
return widest(); |
| 326 |
|
| 327 |
uinteger_t mask = type.sizemask(); |
| 328 |
auto lower = SignExtendedNumber(0); |
| 329 |
auto upper = SignExtendedNumber(mask); |
| 330 |
if (type.toBasetype().ty == Tdchar) |
| 331 |
upper.value = 0x10FFFFUL; |
| 332 |
else if (!isUnsigned) |
| 333 |
{ |
| 334 |
lower.value = ~(mask >> 1); |
| 335 |
lower.negative = true; |
| 336 |
upper.value = (mask >> 1); |
| 337 |
} |
| 338 |
return IntRange(lower, upper); |
| 339 |
} |
| 340 |
|
| 341 |
static IntRange fromNumbers2(SignExtendedNumber* numbers) |
| 342 |
{ |
| 343 |
if (numbers[0] < numbers[1]) |
| 344 |
return IntRange(numbers[0], numbers[1]); |
| 345 |
else |
| 346 |
return IntRange(numbers[1], numbers[0]); |
| 347 |
} |
| 348 |
|
| 349 |
static IntRange fromNumbers4(SignExtendedNumber* numbers) |
| 350 |
{ |
| 351 |
IntRange ab = fromNumbers2(numbers); |
| 352 |
IntRange cd = fromNumbers2(numbers + 2); |
| 353 |
if (cd.imin < ab.imin) |
| 354 |
ab.imin = cd.imin; |
| 355 |
if (cd.imax > ab.imax) |
| 356 |
ab.imax = cd.imax; |
| 357 |
return ab; |
| 358 |
} |
| 359 |
|
| 360 |
static IntRange widest() @safe |
| 361 |
{ |
| 362 |
return IntRange(SignExtendedNumber.min(), SignExtendedNumber.max()); |
| 363 |
} |
| 364 |
|
| 365 |
IntRange castSigned(uinteger_t mask) @safe |
| 366 |
{ |
| 367 |
// .... 0x1e7f ] [0x1e80 .. 0x1f7f] [0x1f80 .. 0x7f] [0x80 .. 0x17f] [0x180 .... |
| 368 |
// |
| 369 |
// regular signed type. We use a technique similar to the unsigned version, |
| 370 |
// but the chunk has to be offset by 1/2 of the range. |
| 371 |
uinteger_t halfChunkMask = mask >> 1; |
| 372 |
uinteger_t minHalfChunk = imin.value & ~halfChunkMask; |
| 373 |
uinteger_t maxHalfChunk = imax.value & ~halfChunkMask; |
| 374 |
int minHalfChunkNegativity = imin.negative; // 1 = neg, 0 = nonneg, -1 = chunk containing ::max |
| 375 |
int maxHalfChunkNegativity = imax.negative; |
| 376 |
if (minHalfChunk & mask) |
| 377 |
{ |
| 378 |
minHalfChunk += halfChunkMask + 1; |
| 379 |
if (minHalfChunk == 0) |
| 380 |
--minHalfChunkNegativity; |
| 381 |
} |
| 382 |
if (maxHalfChunk & mask) |
| 383 |
{ |
| 384 |
maxHalfChunk += halfChunkMask + 1; |
| 385 |
if (maxHalfChunk == 0) |
| 386 |
--maxHalfChunkNegativity; |
| 387 |
} |
| 388 |
if (minHalfChunk == maxHalfChunk && minHalfChunkNegativity == maxHalfChunkNegativity) |
| 389 |
{ |
| 390 |
imin.value &= mask; |
| 391 |
imax.value &= mask; |
| 392 |
// sign extend if necessary. |
| 393 |
imin.negative = (imin.value & ~halfChunkMask) != 0; |
| 394 |
imax.negative = (imax.value & ~halfChunkMask) != 0; |
| 395 |
halfChunkMask += 1; |
| 396 |
imin.value = (imin.value ^ halfChunkMask) - halfChunkMask; |
| 397 |
imax.value = (imax.value ^ halfChunkMask) - halfChunkMask; |
| 398 |
} |
| 399 |
else |
| 400 |
{ |
| 401 |
imin = SignExtendedNumber(~halfChunkMask, true); |
| 402 |
imax = SignExtendedNumber(halfChunkMask, false); |
| 403 |
} |
| 404 |
return this; |
| 405 |
} |
| 406 |
|
| 407 |
IntRange castUnsigned(uinteger_t mask) @safe |
| 408 |
{ |
| 409 |
// .... 0x1eff ] [0x1f00 .. 0x1fff] [0 .. 0xff] [0x100 .. 0x1ff] [0x200 .... |
| 410 |
// |
| 411 |
// regular unsigned type. We just need to see if ir steps across the |
| 412 |
// boundary of validRange. If yes, ir will represent the whole validRange, |
| 413 |
// otherwise, we just take the modulus. |
| 414 |
// e.g. [0x105, 0x107] & 0xff == [5, 7] |
| 415 |
// [0x105, 0x207] & 0xff == [0, 0xff] |
| 416 |
uinteger_t minChunk = imin.value & ~mask; |
| 417 |
uinteger_t maxChunk = imax.value & ~mask; |
| 418 |
if (minChunk == maxChunk && imin.negative == imax.negative) |
| 419 |
{ |
| 420 |
imin.value &= mask; |
| 421 |
imax.value &= mask; |
| 422 |
} |
| 423 |
else |
| 424 |
{ |
| 425 |
imin.value = 0; |
| 426 |
imax.value = mask; |
| 427 |
} |
| 428 |
imin.negative = imax.negative = false; |
| 429 |
return this; |
| 430 |
} |
| 431 |
|
| 432 |
IntRange castDchar() @safe |
| 433 |
{ |
| 434 |
// special case for dchar. Casting to dchar means "I'll ignore all |
| 435 |
// invalid characters." |
| 436 |
castUnsigned(0xFFFFFFFFUL); |
| 437 |
if (imin.value > 0x10FFFFUL) // ?? |
| 438 |
imin.value = 0x10FFFFUL; // ?? |
| 439 |
if (imax.value > 0x10FFFFUL) |
| 440 |
imax.value = 0x10FFFFUL; |
| 441 |
return this; |
| 442 |
} |
| 443 |
|
| 444 |
IntRange _cast(Type type) |
| 445 |
{ |
| 446 |
if (!type.isIntegral() || type.toBasetype().isTypeVector()) |
| 447 |
return this; |
| 448 |
if (!type.isUnsigned()) |
| 449 |
return castSigned(type.sizemask()); |
| 450 |
if (type.toBasetype().ty == Tdchar) |
| 451 |
return castDchar(); |
| 452 |
return castUnsigned(type.sizemask()); |
| 453 |
} |
| 454 |
|
| 455 |
IntRange castUnsigned(Type type) |
| 456 |
{ |
| 457 |
if (!type.isIntegral() || type.toBasetype().isTypeVector()) |
| 458 |
return castUnsigned(ulong.max); |
| 459 |
if (type.toBasetype().ty == Tdchar) |
| 460 |
return castDchar(); |
| 461 |
return castUnsigned(type.sizemask()); |
| 462 |
} |
| 463 |
|
| 464 |
bool contains(IntRange a) @safe |
| 465 |
{ |
| 466 |
return imin <= a.imin && imax >= a.imax; |
| 467 |
} |
| 468 |
|
| 469 |
bool containsZero() const @safe |
| 470 |
{ |
| 471 |
return (imin.negative && !imax.negative) |
| 472 |
|| (!imin.negative && imin.value == 0); |
| 473 |
} |
| 474 |
|
| 475 |
IntRange absNeg() const @safe |
| 476 |
{ |
| 477 |
if (imax.negative) |
| 478 |
return this; |
| 479 |
if (!imin.negative) |
| 480 |
return IntRange(-imax, -imin); |
| 481 |
SignExtendedNumber imaxAbsNeg = -imax; |
| 482 |
return IntRange(imaxAbsNeg < imin ? imaxAbsNeg : imin, |
| 483 |
SignExtendedNumber(0)); |
| 484 |
} |
| 485 |
|
| 486 |
IntRange unionWith(const ref IntRange other) const @safe |
| 487 |
{ |
| 488 |
return IntRange(imin < other.imin ? imin : other.imin, |
| 489 |
imax > other.imax ? imax : other.imax); |
| 490 |
} |
| 491 |
|
| 492 |
void unionOrAssign(IntRange other, ref bool union_) @safe |
| 493 |
{ |
| 494 |
if (!union_ || imin > other.imin) |
| 495 |
imin = other.imin; |
| 496 |
if (!union_ || imax < other.imax) |
| 497 |
imax = other.imax; |
| 498 |
union_ = true; |
| 499 |
} |
| 500 |
|
| 501 |
ref const(IntRange) dump(Exp)(const(char)* funcName, Exp e) const return |
| 502 |
{ |
| 503 |
printf("[(%c)%#018llx, (%c)%#018llx] @ %s ::: %s\n", |
| 504 |
imin.negative?'-':'+', cast(ulong)imin.value, |
| 505 |
imax.negative?'-':'+', cast(ulong)imax.value, |
| 506 |
funcName, e.toChars()); |
| 507 |
return this; |
| 508 |
} |
| 509 |
|
| 510 |
void splitBySign(ref IntRange negRange, ref bool hasNegRange, ref IntRange nonNegRange, ref bool hasNonNegRange) const @safe |
| 511 |
{ |
| 512 |
hasNegRange = imin.negative; |
| 513 |
if (hasNegRange) |
| 514 |
{ |
| 515 |
negRange.imin = imin; |
| 516 |
negRange.imax = imax.negative ? imax : SignExtendedNumber(-1, true); |
| 517 |
} |
| 518 |
hasNonNegRange = !imax.negative; |
| 519 |
if (hasNonNegRange) |
| 520 |
{ |
| 521 |
nonNegRange.imin = imin.negative ? SignExtendedNumber(0) : imin; |
| 522 |
nonNegRange.imax = imax; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
IntRange opUnary(string op:"~")() const |
| 527 |
{ |
| 528 |
return IntRange(~imax, ~imin); |
| 529 |
} |
| 530 |
|
| 531 |
IntRange opUnary(string op : "-")() |
| 532 |
{ |
| 533 |
return IntRange(-imax, -imin); |
| 534 |
} |
| 535 |
|
| 536 |
// Credits to Timon Gehr for the algorithms for &, | |
| 537 |
// https://github.com/tgehr/d-compiler/blob/master/vrange.d |
| 538 |
IntRange opBinary(string op : "&")(IntRange rhs) const |
| 539 |
{ |
| 540 |
// unsigned or identical sign bits |
| 541 |
if ((imin.negative ^ imax.negative) != 1 && (rhs.imin.negative ^ rhs.imax.negative) != 1) |
| 542 |
{ |
| 543 |
return IntRange(minAnd(this, rhs), maxAnd(this, rhs)); |
| 544 |
} |
| 545 |
|
| 546 |
IntRange l = IntRange(this); |
| 547 |
IntRange r = IntRange(rhs); |
| 548 |
|
| 549 |
// both intervals span [-1,0] |
| 550 |
if ((imin.negative ^ imax.negative) == 1 && (rhs.imin.negative ^ rhs.imax.negative) == 1) |
| 551 |
{ |
| 552 |
// cannot be larger than either l.max or r.max, set the other one to -1 |
| 553 |
SignExtendedNumber max = l.imax.value > r.imax.value ? l.imax : r.imax; |
| 554 |
|
| 555 |
// only negative numbers for minimum |
| 556 |
l.imax.value = -1; |
| 557 |
l.imax.negative = true; |
| 558 |
r.imax.value = -1; |
| 559 |
r.imax.negative = true; |
| 560 |
|
| 561 |
return IntRange(minAnd(l, r), max); |
| 562 |
} |
| 563 |
else |
| 564 |
{ |
| 565 |
// only one interval spans [-1,0] |
| 566 |
if ((l.imin.negative ^ l.imax.negative) == 1) |
| 567 |
{ |
| 568 |
swap(l, r); // r spans [-1,0] |
| 569 |
} |
| 570 |
|
| 571 |
const minAndNeg = minAnd(l, IntRange(r.imin, SignExtendedNumber(-1))); |
| 572 |
const minAndPos = minAnd(l, IntRange(SignExtendedNumber(0), r.imax)); |
| 573 |
const maxAndNeg = maxAnd(l, IntRange(r.imin, SignExtendedNumber(-1))); |
| 574 |
const maxAndPos = maxAnd(l, IntRange(SignExtendedNumber(0), r.imax)); |
| 575 |
|
| 576 |
const min = minAndNeg < minAndPos ? minAndNeg : minAndPos; |
| 577 |
const max = maxAndNeg > maxAndPos ? maxAndNeg : maxAndPos; |
| 578 |
|
| 579 |
auto range = IntRange(min, max); |
| 580 |
return range; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
// Credits to Timon Gehr for the algorithms for &, | |
| 585 |
// https://github.com/tgehr/d-compiler/blob/master/vrange.d |
| 586 |
IntRange opBinary(string op : "|")(IntRange rhs) const |
| 587 |
{ |
| 588 |
// unsigned or identical sign bits: |
| 589 |
if ((imin.negative ^ imax.negative) == 0 && (rhs.imin.negative ^ rhs.imax.negative) == 0) |
| 590 |
{ |
| 591 |
return IntRange(minOr(this, rhs), maxOr(this, rhs)); |
| 592 |
} |
| 593 |
|
| 594 |
IntRange l = IntRange(this); |
| 595 |
IntRange r = IntRange(rhs); |
| 596 |
|
| 597 |
// both intervals span [-1,0] |
| 598 |
if ((imin.negative ^ imax.negative) == 1 && (rhs.imin.negative ^ rhs.imax.negative) == 1) |
| 599 |
{ |
| 600 |
// cannot be smaller than either l.min or r.min, set the other one to 0 |
| 601 |
SignExtendedNumber min = l.imin.value < r.imin.value ? l.imin : r.imin; |
| 602 |
|
| 603 |
// only negative numbers for minimum |
| 604 |
l.imin.value = 0; |
| 605 |
l.imin.negative = false; |
| 606 |
r.imin.value = 0; |
| 607 |
r.imin.negative = false; |
| 608 |
|
| 609 |
return IntRange(min, maxOr(l, r)); |
| 610 |
} |
| 611 |
else |
| 612 |
{ |
| 613 |
// only one interval spans [-1,0] |
| 614 |
if ((imin.negative ^ imax.negative) == 1) |
| 615 |
{ |
| 616 |
swap(l, r); // r spans [-1,0] |
| 617 |
} |
| 618 |
|
| 619 |
auto minOrNeg = minOr(l, IntRange(r.imin, SignExtendedNumber(-1))); |
| 620 |
auto minOrPos = minOr(l, IntRange(SignExtendedNumber(0), r.imax)); |
| 621 |
auto maxOrNeg = maxOr(l, IntRange(r.imin, SignExtendedNumber(-1))); |
| 622 |
auto maxOrPos = maxOr(l, IntRange(SignExtendedNumber(0), r.imax)); |
| 623 |
|
| 624 |
auto min = minOrNeg < minOrPos ? minOrNeg : minOrPos; |
| 625 |
auto max = maxOrNeg > maxOrPos ? maxOrNeg : maxOrPos; |
| 626 |
|
| 627 |
auto range = IntRange(min, max); |
| 628 |
return range; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
IntRange opBinary(string op : "^")(IntRange rhs) const |
| 633 |
{ |
| 634 |
return this & ~rhs | ~this & rhs; |
| 635 |
} |
| 636 |
|
| 637 |
IntRange opBinary(string op : "+")(IntRange rhs) |
| 638 |
{ |
| 639 |
return IntRange(imin + rhs.imin, imax + rhs.imax); |
| 640 |
} |
| 641 |
|
| 642 |
IntRange opBinary(string op : "-")(IntRange rhs) |
| 643 |
{ |
| 644 |
return IntRange(imin - rhs.imax, imax - rhs.imin); |
| 645 |
} |
| 646 |
|
| 647 |
IntRange opBinary(string op : "*")(IntRange rhs) |
| 648 |
{ |
| 649 |
// [a,b] * [c,d] = [min (ac, ad, bc, bd), max (ac, ad, bc, bd)] |
| 650 |
SignExtendedNumber[4] bdy; |
| 651 |
bdy[0] = imin * rhs.imin; |
| 652 |
bdy[1] = imin * rhs.imax; |
| 653 |
bdy[2] = imax * rhs.imin; |
| 654 |
bdy[3] = imax * rhs.imax; |
| 655 |
return IntRange.fromNumbers4(bdy.ptr); |
| 656 |
} |
| 657 |
|
| 658 |
IntRange opBinary(string op : "/")(IntRange rhs) |
| 659 |
{ |
| 660 |
// Handle divide by 0 |
| 661 |
if (rhs.imax.value == 0 && rhs.imin.value == 0) |
| 662 |
return widest(); |
| 663 |
|
| 664 |
// Don't treat the whole range as divide by 0 if only one end of a range is 0. |
| 665 |
// https://issues.dlang.org/show_bug.cgi?id=15289 |
| 666 |
if (rhs.imax.value == 0) |
| 667 |
{ |
| 668 |
rhs.imax.value--; |
| 669 |
} |
| 670 |
else if(rhs.imin.value == 0) |
| 671 |
{ |
| 672 |
rhs.imin.value++; |
| 673 |
} |
| 674 |
|
| 675 |
if (!imin.negative && !imax.negative && !rhs.imin.negative && !rhs.imax.negative) |
| 676 |
{ |
| 677 |
return IntRange(imin / rhs.imax, imax / rhs.imin); |
| 678 |
} |
| 679 |
if (rhs.imin.negative && !rhs.imax.negative) // divisor spans [-1, 0, 1] |
| 680 |
{ |
| 681 |
SignExtendedNumber[4] bdy = [-imin, imin, -imax, imax]; |
| 682 |
return IntRange.fromNumbers4(bdy.ptr); |
| 683 |
} |
| 684 |
// [a,b] / [c,d] = [min (a/c, a/d, b/c, b/d), max (a/c, a/d, b/c, b/d)] |
| 685 |
SignExtendedNumber[4] bdy; |
| 686 |
bdy[0] = imin / rhs.imin; |
| 687 |
bdy[1] = imin / rhs.imax; |
| 688 |
bdy[2] = imax / rhs.imin; |
| 689 |
bdy[3] = imax / rhs.imax; |
| 690 |
|
| 691 |
return IntRange.fromNumbers4(bdy.ptr); |
| 692 |
} |
| 693 |
|
| 694 |
IntRange opBinary(string op : "%")(IntRange rhs) |
| 695 |
{ |
| 696 |
IntRange irNum = this; |
| 697 |
IntRange irDen = rhs.absNeg(); |
| 698 |
|
| 699 |
/* |
| 700 |
due to the rules of D (C)'s % operator, we need to consider the cases |
| 701 |
separately in different range of signs. |
| 702 |
|
| 703 |
case 1. [500, 1700] % [7, 23] (numerator is always positive) |
| 704 |
= [0, 22] |
| 705 |
case 2. [-500, 1700] % [7, 23] (numerator can be negative) |
| 706 |
= [-22, 22] |
| 707 |
case 3. [-1700, -500] % [7, 23] (numerator is always negative) |
| 708 |
= [-22, 0] |
| 709 |
|
| 710 |
the number 22 is the maximum absolute value in the denomator's range. We |
| 711 |
don't care about divide by zero. |
| 712 |
*/ |
| 713 |
|
| 714 |
irDen.imin = irDen.imin + SignExtendedNumber(1); |
| 715 |
irDen.imax = -irDen.imin; |
| 716 |
|
| 717 |
if (!irNum.imin.negative) |
| 718 |
{ |
| 719 |
irNum.imin.value = 0; |
| 720 |
} |
| 721 |
else if (irNum.imin < irDen.imin) |
| 722 |
{ |
| 723 |
irNum.imin = irDen.imin; |
| 724 |
} |
| 725 |
|
| 726 |
if (irNum.imax.negative) |
| 727 |
{ |
| 728 |
irNum.imax.negative = false; |
| 729 |
irNum.imax.value = 0; |
| 730 |
} |
| 731 |
else if (irNum.imax > irDen.imax) |
| 732 |
{ |
| 733 |
irNum.imax = irDen.imax; |
| 734 |
} |
| 735 |
|
| 736 |
return irNum; |
| 737 |
} |
| 738 |
|
| 739 |
IntRange opBinary(string op : "<<")(IntRange rhs) |
| 740 |
{ |
| 741 |
if (rhs.imin.negative) |
| 742 |
{ |
| 743 |
rhs = IntRange(SignExtendedNumber(0), SignExtendedNumber(64)); |
| 744 |
} |
| 745 |
|
| 746 |
SignExtendedNumber lower = imin << (imin.negative ? rhs.imax : rhs.imin); |
| 747 |
SignExtendedNumber upper = imax << (imax.negative ? rhs.imin : rhs.imax); |
| 748 |
|
| 749 |
return IntRange(lower, upper); |
| 750 |
} |
| 751 |
|
| 752 |
IntRange opBinary(string op : ">>")(IntRange rhs) |
| 753 |
{ |
| 754 |
if (rhs.imin.negative) |
| 755 |
{ |
| 756 |
rhs = IntRange(SignExtendedNumber(0), SignExtendedNumber(64)); |
| 757 |
} |
| 758 |
|
| 759 |
SignExtendedNumber lower = imin >> (imin.negative ? rhs.imin : rhs.imax); |
| 760 |
SignExtendedNumber upper = imax >> (imax.negative ? rhs.imax : rhs.imin); |
| 761 |
|
| 762 |
return IntRange(lower, upper); |
| 763 |
} |
| 764 |
|
| 765 |
IntRange opBinary(string op : ">>>")(IntRange rhs) |
| 766 |
{ |
| 767 |
if (rhs.imin.negative) |
| 768 |
{ |
| 769 |
rhs = IntRange(SignExtendedNumber(0), SignExtendedNumber(64)); |
| 770 |
} |
| 771 |
|
| 772 |
return IntRange(imin >> rhs.imax, imax >> rhs.imin); |
| 773 |
} |
| 774 |
|
| 775 |
IntRange opBinary(string op : "^^")(IntRange rhs) |
| 776 |
{ |
| 777 |
// Not yet implemented |
| 778 |
assert(0); |
| 779 |
} |
| 780 |
|
| 781 |
private: |
| 782 |
// Credits to Timon Gehr maxOr, minOr, maxAnd, minAnd |
| 783 |
// https://github.com/tgehr/d-compiler/blob/master/vrange.d |
| 784 |
static SignExtendedNumber maxOr(const IntRange lhs, const IntRange rhs) @safe |
| 785 |
{ |
| 786 |
uinteger_t x = 0; |
| 787 |
auto sign = false; |
| 788 |
auto xor = lhs.imax.value ^ rhs.imax.value; |
| 789 |
auto and = lhs.imax.value & rhs.imax.value; |
| 790 |
auto lhsc = IntRange(lhs); |
| 791 |
auto rhsc = IntRange(rhs); |
| 792 |
|
| 793 |
// Sign bit not part of the .value so we need an extra iteration |
| 794 |
if (lhsc.imax.negative ^ rhsc.imax.negative) |
| 795 |
{ |
| 796 |
sign = true; |
| 797 |
if (lhsc.imax.negative) |
| 798 |
{ |
| 799 |
if (!lhsc.imin.negative) |
| 800 |
{ |
| 801 |
lhsc.imin.value = 0; |
| 802 |
} |
| 803 |
if (!rhsc.imin.negative) |
| 804 |
{ |
| 805 |
rhsc.imin.value = 0; |
| 806 |
} |
| 807 |
} |
| 808 |
} |
| 809 |
else if (lhsc.imin.negative & rhsc.imin.negative) |
| 810 |
{ |
| 811 |
sign = true; |
| 812 |
} |
| 813 |
else if (lhsc.imax.negative & rhsc.imax.negative) |
| 814 |
{ |
| 815 |
return SignExtendedNumber(-1, false); |
| 816 |
} |
| 817 |
|
| 818 |
for (uinteger_t d = 1LU << (8 * uinteger_t.sizeof - 1); d; d >>= 1) |
| 819 |
{ |
| 820 |
if (xor & d) |
| 821 |
{ |
| 822 |
x |= d; |
| 823 |
if (lhsc.imax.value & d) |
| 824 |
{ |
| 825 |
if (~lhsc.imin.value & d) |
| 826 |
{ |
| 827 |
lhsc.imin.value = 0; |
| 828 |
} |
| 829 |
} |
| 830 |
else |
| 831 |
{ |
| 832 |
if (~rhsc.imin.value & d) |
| 833 |
{ |
| 834 |
rhsc.imin.value = 0; |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
else if (lhsc.imin.value & rhsc.imin.value & d) |
| 839 |
{ |
| 840 |
x |= d; |
| 841 |
} |
| 842 |
else if (and & d) |
| 843 |
{ |
| 844 |
x |= (d << 1) - 1; |
| 845 |
break; |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
auto range = SignExtendedNumber(x, sign); |
| 850 |
return range; |
| 851 |
} |
| 852 |
|
| 853 |
// Credits to Timon Gehr maxOr, minOr, maxAnd, minAnd |
| 854 |
// https://github.com/tgehr/d-compiler/blob/master/vrange.d |
| 855 |
static SignExtendedNumber minOr(const IntRange lhs, const IntRange rhs) @safe |
| 856 |
{ |
| 857 |
return ~maxAnd(~lhs, ~rhs); |
| 858 |
} |
| 859 |
|
| 860 |
// Credits to Timon Gehr maxOr, minOr, maxAnd, minAnd |
| 861 |
// https://github.com/tgehr/d-compiler/blob/master/vrange.d |
| 862 |
static SignExtendedNumber maxAnd(const IntRange lhs, const IntRange rhs) @safe |
| 863 |
{ |
| 864 |
uinteger_t x = 0; |
| 865 |
bool sign = false; |
| 866 |
auto lhsc = IntRange(lhs); |
| 867 |
auto rhsc = IntRange(rhs); |
| 868 |
|
| 869 |
if (lhsc.imax.negative & rhsc.imax.negative) |
| 870 |
{ |
| 871 |
sign = true; |
| 872 |
} |
| 873 |
|
| 874 |
for (uinteger_t d = 1LU << (8 * uinteger_t.sizeof - 1); d; d >>= 1) |
| 875 |
{ |
| 876 |
if (lhsc.imax.value & rhsc.imax.value & d) |
| 877 |
{ |
| 878 |
x |= d; |
| 879 |
if (~lhsc.imin.value & d) |
| 880 |
{ |
| 881 |
lhsc.imin.value = 0; |
| 882 |
} |
| 883 |
if (~rhsc.imin.value & d) |
| 884 |
{ |
| 885 |
rhsc.imin.value = 0; |
| 886 |
} |
| 887 |
} |
| 888 |
else if (~lhsc.imin.value & d && lhsc.imax.value & d) |
| 889 |
{ |
| 890 |
lhsc.imax.value |= d - 1; |
| 891 |
} |
| 892 |
else if (~rhsc.imin.value & d && rhsc.imax.value & d) |
| 893 |
{ |
| 894 |
rhsc.imax.value |= d - 1; |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
auto range = SignExtendedNumber(x, sign); |
| 899 |
return range; |
| 900 |
} |
| 901 |
|
| 902 |
// Credits to Timon Gehr maxOr, minOr, maxAnd, minAnd |
| 903 |
// https://github.com/tgehr/d-compiler/blob/master/vrange.d |
| 904 |
static SignExtendedNumber minAnd(const IntRange lhs, const IntRange rhs) @safe |
| 905 |
{ |
| 906 |
return ~maxOr(~lhs, ~rhs); |
| 907 |
} |
| 908 |
|
| 909 |
static swap(ref IntRange a, ref IntRange b) |
| 910 |
{ |
| 911 |
auto aux = a; |
| 912 |
a = b; |
| 913 |
b = aux; |
| 914 |
} |
| 915 |
} |