Skip to content

Instantly share code, notes, and snippets.

@regehr
Created January 12, 2020 21:39
Show Gist options
  • Save regehr/cd7a4ab7b45092c873ae50085f4ed7f6 to your computer and use it in GitHub Desktop.
Save regehr/cd7a4ab7b45092c873ae50085f4ed7f6 to your computer and use it in GitHub Desktop.
static APInt XXXones(const KnownBits &x, const KnownBits &y) {
const int WIDTH = x.One.getBitWidth();
const APInt t0 = operator+(y.Zero, APInt(WIDTH, 1));
const APInt t1 = operator+(t0, x.One);
const APInt t2 = (APInt::getSignedMinValue(WIDTH).isOneValue()) ? (x.Zero) : (x.Zero);
const APInt t3 = operator+(x.One, t2);
const APInt t4 = ~x.One;
const APInt t5 = (t4.isNegative()) ? (x.Zero) : (x.Zero);
const APInt t6 = operator-(y.Zero, t5);
APInt t7 = APInt::getAllOnesValue(WIDTH);
t7.setLowBits((y.Zero.logBase2() >= WIDTH) ? 0 : (y.Zero.logBase2()));
const APInt t8 = operator&(t7, t6);
const APInt t9 = operator&(t8, t3);
const APInt t10 = operator&(t9, t1);
return t10 & ~APInt::getMinValue(WIDTH);
}
// in: 0d5c2d2d2d0d2c0d9c1b3f0d1c1b3f2d2d4d0d0c0d0c0b0g0d7c5i0d1c0b3f2d2d4d0d0c0d00c1b0g1d0d1c0e0i0d2c0b4f3d0d4c0d2c1b8j2k0b0f0b0f0b0f
// out: 0d5c2d2d2d0d2c0d9c1b3f0d1c1b3f2d2d4d0d0c0d0c0b0g0d7c5i0d1c0b3f2d2d4d0d0c0d00c1b0g1d0d1c0e0i0d2c0b4f3d0d4c0d2c1b8j2k0b0f0b0f0b0f
static APInt XXXzeroes(const KnownBits &x, const KnownBits &y) {
const int WIDTH = x.One.getBitWidth();
const APInt t0 = -y.One;
const APInt t1 = operator+(t0, x.One);
const APInt t2 = ~t1;
const APInt t3 = operator-(y.Zero, x.Zero);
const APInt t4 = ~t3;
const APInt t5 = operator&(t2, t4);
const APInt t6 = operator+(x.Zero, x.One);
const APInt t7 = operator&(t6, t5);
return t7;
}
// in: 2d2d1d2d0d1c1d0d3c1e0b3f0e1d2d0d0c0d2c0b4f0e1b0f2d0d1c0d0c0b3f0b0f0d3c
// out: 2d2d1d2d0d1c1d0d3c1e0b3f0e1d2d0d0c0d2c0b4f0e1b0f2d0d1c0d0c0b3f0b0f0d3c
KnownBits f(const KnownBits &x, const KnownBits &y) {
KnownBits r;
r.Zero = XXXzeroes(x, y);
r.One = XXXones(x, y);
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment