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 280 281 282 283 284 285 286
|
//===- SlowDynamicAPInt.cpp - SlowDynamicAPInt Implementation -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/SlowDynamicAPInt.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace detail;
SlowDynamicAPInt::SlowDynamicAPInt(int64_t Val)
: Val(64, Val, /*isSigned=*/true) {}
SlowDynamicAPInt::SlowDynamicAPInt() : SlowDynamicAPInt(0) {}
SlowDynamicAPInt::SlowDynamicAPInt(const APInt &Val) : Val(Val) {}
SlowDynamicAPInt &SlowDynamicAPInt::operator=(int64_t Val) {
return *this = SlowDynamicAPInt(Val);
}
SlowDynamicAPInt::operator int64_t() const { return Val.getSExtValue(); }
hash_code detail::hash_value(const SlowDynamicAPInt &X) {
return hash_value(X.Val);
}
/// ---------------------------------------------------------------------------
/// Convenience operator overloads for int64_t.
/// ---------------------------------------------------------------------------
SlowDynamicAPInt &detail::operator+=(SlowDynamicAPInt &A, int64_t B) {
return A += SlowDynamicAPInt(B);
}
SlowDynamicAPInt &detail::operator-=(SlowDynamicAPInt &A, int64_t B) {
return A -= SlowDynamicAPInt(B);
}
SlowDynamicAPInt &detail::operator*=(SlowDynamicAPInt &A, int64_t B) {
return A *= SlowDynamicAPInt(B);
}
SlowDynamicAPInt &detail::operator/=(SlowDynamicAPInt &A, int64_t B) {
return A /= SlowDynamicAPInt(B);
}
SlowDynamicAPInt &detail::operator%=(SlowDynamicAPInt &A, int64_t B) {
return A %= SlowDynamicAPInt(B);
}
bool detail::operator==(const SlowDynamicAPInt &A, int64_t B) {
return A == SlowDynamicAPInt(B);
}
bool detail::operator!=(const SlowDynamicAPInt &A, int64_t B) {
return A != SlowDynamicAPInt(B);
}
bool detail::operator>(const SlowDynamicAPInt &A, int64_t B) {
return A > SlowDynamicAPInt(B);
}
bool detail::operator<(const SlowDynamicAPInt &A, int64_t B) {
return A < SlowDynamicAPInt(B);
}
bool detail::operator<=(const SlowDynamicAPInt &A, int64_t B) {
return A <= SlowDynamicAPInt(B);
}
bool detail::operator>=(const SlowDynamicAPInt &A, int64_t B) {
return A >= SlowDynamicAPInt(B);
}
SlowDynamicAPInt detail::operator+(const SlowDynamicAPInt &A, int64_t B) {
return A + SlowDynamicAPInt(B);
}
SlowDynamicAPInt detail::operator-(const SlowDynamicAPInt &A, int64_t B) {
return A - SlowDynamicAPInt(B);
}
SlowDynamicAPInt detail::operator*(const SlowDynamicAPInt &A, int64_t B) {
return A * SlowDynamicAPInt(B);
}
SlowDynamicAPInt detail::operator/(const SlowDynamicAPInt &A, int64_t B) {
return A / SlowDynamicAPInt(B);
}
SlowDynamicAPInt detail::operator%(const SlowDynamicAPInt &A, int64_t B) {
return A % SlowDynamicAPInt(B);
}
bool detail::operator==(int64_t A, const SlowDynamicAPInt &B) {
return SlowDynamicAPInt(A) == B;
}
bool detail::operator!=(int64_t A, const SlowDynamicAPInt &B) {
return SlowDynamicAPInt(A) != B;
}
bool detail::operator>(int64_t A, const SlowDynamicAPInt &B) {
return SlowDynamicAPInt(A) > B;
}
bool detail::operator<(int64_t A, const SlowDynamicAPInt &B) {
return SlowDynamicAPInt(A) < B;
}
bool detail::operator<=(int64_t A, const SlowDynamicAPInt &B) {
return SlowDynamicAPInt(A) <= B;
}
bool detail::operator>=(int64_t A, const SlowDynamicAPInt &B) {
return SlowDynamicAPInt(A) >= B;
}
SlowDynamicAPInt detail::operator+(int64_t A, const SlowDynamicAPInt &B) {
return SlowDynamicAPInt(A) + B;
}
SlowDynamicAPInt detail::operator-(int64_t A, const SlowDynamicAPInt &B) {
return SlowDynamicAPInt(A) - B;
}
SlowDynamicAPInt detail::operator*(int64_t A, const SlowDynamicAPInt &B) {
return SlowDynamicAPInt(A) * B;
}
SlowDynamicAPInt detail::operator/(int64_t A, const SlowDynamicAPInt &B) {
return SlowDynamicAPInt(A) / B;
}
SlowDynamicAPInt detail::operator%(int64_t A, const SlowDynamicAPInt &B) {
return SlowDynamicAPInt(A) % B;
}
static unsigned getMaxWidth(const APInt &A, const APInt &B) {
return std::max(A.getBitWidth(), B.getBitWidth());
}
/// ---------------------------------------------------------------------------
/// Comparison operators.
/// ---------------------------------------------------------------------------
// TODO: consider instead making APInt::compare available and using that.
bool SlowDynamicAPInt::operator==(const SlowDynamicAPInt &O) const {
unsigned Width = getMaxWidth(Val, O.Val);
return Val.sext(Width) == O.Val.sext(Width);
}
bool SlowDynamicAPInt::operator!=(const SlowDynamicAPInt &O) const {
unsigned Width = getMaxWidth(Val, O.Val);
return Val.sext(Width) != O.Val.sext(Width);
}
bool SlowDynamicAPInt::operator>(const SlowDynamicAPInt &O) const {
unsigned Width = getMaxWidth(Val, O.Val);
return Val.sext(Width).sgt(O.Val.sext(Width));
}
bool SlowDynamicAPInt::operator<(const SlowDynamicAPInt &O) const {
unsigned Width = getMaxWidth(Val, O.Val);
return Val.sext(Width).slt(O.Val.sext(Width));
}
bool SlowDynamicAPInt::operator<=(const SlowDynamicAPInt &O) const {
unsigned Width = getMaxWidth(Val, O.Val);
return Val.sext(Width).sle(O.Val.sext(Width));
}
bool SlowDynamicAPInt::operator>=(const SlowDynamicAPInt &O) const {
unsigned Width = getMaxWidth(Val, O.Val);
return Val.sext(Width).sge(O.Val.sext(Width));
}
/// ---------------------------------------------------------------------------
/// Arithmetic operators.
/// ---------------------------------------------------------------------------
/// Bring a and b to have the same width and then call op(a, b, overflow).
/// If the overflow bit becomes set, resize a and b to double the width and
/// call op(a, b, overflow), returning its result. The operation with double
/// widths should not also overflow.
APInt runOpWithExpandOnOverflow(
const APInt &A, const APInt &B,
function_ref<APInt(const APInt &, const APInt &, bool &Overflow)> Op) {
bool Overflow;
unsigned Width = getMaxWidth(A, B);
APInt Ret = Op(A.sext(Width), B.sext(Width), Overflow);
if (!Overflow)
return Ret;
Width *= 2;
Ret = Op(A.sext(Width), B.sext(Width), Overflow);
assert(!Overflow && "double width should be sufficient to avoid overflow!");
return Ret;
}
SlowDynamicAPInt SlowDynamicAPInt::operator+(const SlowDynamicAPInt &O) const {
return SlowDynamicAPInt(
runOpWithExpandOnOverflow(Val, O.Val, std::mem_fn(&APInt::sadd_ov)));
}
SlowDynamicAPInt SlowDynamicAPInt::operator-(const SlowDynamicAPInt &O) const {
return SlowDynamicAPInt(
runOpWithExpandOnOverflow(Val, O.Val, std::mem_fn(&APInt::ssub_ov)));
}
SlowDynamicAPInt SlowDynamicAPInt::operator*(const SlowDynamicAPInt &O) const {
return SlowDynamicAPInt(
runOpWithExpandOnOverflow(Val, O.Val, std::mem_fn(&APInt::smul_ov)));
}
SlowDynamicAPInt SlowDynamicAPInt::operator/(const SlowDynamicAPInt &O) const {
return SlowDynamicAPInt(
runOpWithExpandOnOverflow(Val, O.Val, std::mem_fn(&APInt::sdiv_ov)));
}
SlowDynamicAPInt detail::abs(const SlowDynamicAPInt &X) {
return X >= 0 ? X : -X;
}
SlowDynamicAPInt detail::ceilDiv(const SlowDynamicAPInt &LHS,
const SlowDynamicAPInt &RHS) {
if (RHS == -1)
return -LHS;
unsigned Width = getMaxWidth(LHS.Val, RHS.Val);
return SlowDynamicAPInt(APIntOps::RoundingSDiv(
LHS.Val.sext(Width), RHS.Val.sext(Width), APInt::Rounding::UP));
}
SlowDynamicAPInt detail::floorDiv(const SlowDynamicAPInt &LHS,
const SlowDynamicAPInt &RHS) {
if (RHS == -1)
return -LHS;
unsigned Width = getMaxWidth(LHS.Val, RHS.Val);
return SlowDynamicAPInt(APIntOps::RoundingSDiv(
LHS.Val.sext(Width), RHS.Val.sext(Width), APInt::Rounding::DOWN));
}
// The RHS is always expected to be positive, and the result
/// is always non-negative.
SlowDynamicAPInt detail::mod(const SlowDynamicAPInt &LHS,
const SlowDynamicAPInt &RHS) {
assert(RHS >= 1 && "mod is only supported for positive divisors!");
return LHS % RHS < 0 ? LHS % RHS + RHS : LHS % RHS;
}
SlowDynamicAPInt detail::gcd(const SlowDynamicAPInt &A,
const SlowDynamicAPInt &B) {
assert(A >= 0 && B >= 0 && "operands must be non-negative!");
unsigned Width = getMaxWidth(A.Val, B.Val);
return SlowDynamicAPInt(
APIntOps::GreatestCommonDivisor(A.Val.sext(Width), B.Val.sext(Width)));
}
/// Returns the least common multiple of A and B.
SlowDynamicAPInt detail::lcm(const SlowDynamicAPInt &A,
const SlowDynamicAPInt &B) {
SlowDynamicAPInt X = abs(A);
SlowDynamicAPInt Y = abs(B);
return (X * Y) / gcd(X, Y);
}
/// This operation cannot overflow.
SlowDynamicAPInt SlowDynamicAPInt::operator%(const SlowDynamicAPInt &O) const {
unsigned Width = std::max(Val.getBitWidth(), O.Val.getBitWidth());
return SlowDynamicAPInt(Val.sext(Width).srem(O.Val.sext(Width)));
}
SlowDynamicAPInt SlowDynamicAPInt::operator-() const {
if (Val.isMinSignedValue()) {
/// Overflow only occurs when the value is the minimum possible value.
APInt Ret = Val.sext(2 * Val.getBitWidth());
return SlowDynamicAPInt(-Ret);
}
return SlowDynamicAPInt(-Val);
}
/// ---------------------------------------------------------------------------
/// Assignment operators, preincrement, predecrement.
/// ---------------------------------------------------------------------------
SlowDynamicAPInt &SlowDynamicAPInt::operator+=(const SlowDynamicAPInt &O) {
*this = *this + O;
return *this;
}
SlowDynamicAPInt &SlowDynamicAPInt::operator-=(const SlowDynamicAPInt &O) {
*this = *this - O;
return *this;
}
SlowDynamicAPInt &SlowDynamicAPInt::operator*=(const SlowDynamicAPInt &O) {
*this = *this * O;
return *this;
}
SlowDynamicAPInt &SlowDynamicAPInt::operator/=(const SlowDynamicAPInt &O) {
*this = *this / O;
return *this;
}
SlowDynamicAPInt &SlowDynamicAPInt::operator%=(const SlowDynamicAPInt &O) {
*this = *this % O;
return *this;
}
SlowDynamicAPInt &SlowDynamicAPInt::operator++() {
*this += 1;
return *this;
}
SlowDynamicAPInt &SlowDynamicAPInt::operator--() {
*this -= 1;
return *this;
}
/// ---------------------------------------------------------------------------
/// Printing.
/// ---------------------------------------------------------------------------
void SlowDynamicAPInt::print(raw_ostream &OS) const { OS << Val; }
void SlowDynamicAPInt::dump() const { print(dbgs()); }
|