================
@@ -183,6 +184,40 @@ static llvm::Error
TypeCheck(llvm::ArrayRef<DataStackElement> data,
return TypeCheck(data.drop_back(1), type2, type1);
}
+/// Three-way compare two 64-bit integers as mathematical integers, i.e.
without
+/// reinterpreting one's bit pattern as the other's type. A negative Int always
+/// compares less than any UInt, and a UInt with the high bit set (>= 2^63)
+/// always compares greater than any Int: neither is truncated or
reinterpreted.
+static std::optional<int> Compare(const DataStackElement &x,
+ const DataStackElement &y) {
+ if (auto *ux = std::get_if<uint64_t>(&x)) {
+ if (auto *uy = std::get_if<uint64_t>(&y))
+ return Compare(*ux, *uy);
+ if (auto *sy = std::get_if<int64_t>(&y))
+ return Compare(*ux, *sy);
+ } else if (auto *sx = std::get_if<int64_t>(&x)) {
+ if (auto *sy = std::get_if<int64_t>(&y))
+ return Compare(*sx, *sy);
+ if (auto *uy = std::get_if<uint64_t>(&y))
+ return Compare(*sx, *uy);
+ }
+ return std::nullopt;
+}
+
+static int Compare(uint64_t x, uint64_t y) {
+
+ return x < y ? -1 : (x > y ? 1 : 0);
+}
+static int Compare(int64_t x, int64_t y) {
+ return x < y ? -1 : (x > y ? 1 : 0);
+}
+static int Compare(int64_t x, uint64_t y) {
+ if (x < 0)
+ return -1;
+ return Compare((uint64_t)x, y);
+}
+static int Compare(uint64_t x, int64_t y) { return -Compare(y, x); }
+
----------------
Michael137 wrote:
Basically a reimplementation of the C++20 `cmp_less`/etc.? Shame we can't use
those here. But maybe a FIXME to replace this once it's available? Didn't find
anything in the LLVM support library. Closest was the comparison on
`ScaledNumber`
https://github.com/llvm/llvm-project/pull/210171
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits