add bitfield methods to embed/extract smaller values from a larger value
This commit is contained in:
parent
823d940f55
commit
14e58aad5e
|
@ -1,8 +1,13 @@
|
|||
package com.blarg.gdx;
|
||||
|
||||
import com.blarg.gdx.math.MathHelpers;
|
||||
|
||||
// this class exists because i am stupid and can never remember the operators used for setting/clearing/toggling bits
|
||||
|
||||
public final class Bitfield {
|
||||
|
||||
// checking for individual bits
|
||||
|
||||
public static boolean isSet(long bit, long bitfield) {
|
||||
return (bitfield & bit) != 0;
|
||||
}
|
||||
|
@ -19,6 +24,8 @@ public final class Bitfield {
|
|||
return (bitfield & bit) != 0;
|
||||
}
|
||||
|
||||
// setting individual bits
|
||||
|
||||
public static long set(long bit, long bitfield) {
|
||||
bitfield |= bit;
|
||||
return (bitfield | bit);
|
||||
|
@ -39,6 +46,8 @@ public final class Bitfield {
|
|||
return bitfield;
|
||||
}
|
||||
|
||||
// clearing individual bits
|
||||
|
||||
public static long clear(long bit, long bitfield) {
|
||||
bitfield &= ~bit;
|
||||
return bitfield;
|
||||
|
@ -59,6 +68,8 @@ public final class Bitfield {
|
|||
return bitfield;
|
||||
}
|
||||
|
||||
// toggling individual bits on/off
|
||||
|
||||
public static long toggle(long bit, long bitfield) {
|
||||
bitfield ^= bit;
|
||||
return bitfield;
|
||||
|
@ -78,4 +89,54 @@ public final class Bitfield {
|
|||
bitfield ^= bit;
|
||||
return bitfield;
|
||||
}
|
||||
|
||||
// extracting smaller int-type values from a subset of bits out of a larger value (bitfield)
|
||||
|
||||
public static long extract(long mask, int shift, long bitfield) {
|
||||
return (bitfield & mask) >> shift;
|
||||
}
|
||||
|
||||
public static int extract(int mask, int shift, int bitfield) {
|
||||
return (bitfield & mask) >> shift;
|
||||
}
|
||||
|
||||
public static short extract(short mask, int shift, short bitfield) {
|
||||
return (short)((bitfield & mask) >> shift);
|
||||
}
|
||||
|
||||
public static byte extract(byte mask, int shift, byte bitfield) {
|
||||
return (byte)((bitfield & mask) >> shift);
|
||||
}
|
||||
|
||||
// setting smaller int-type values in a subset of bits in a larger value (bitfield)
|
||||
|
||||
public static long embed(long value, long bitmask, int shift, int valueMaxBitLength, long bitfield) {
|
||||
long maxValue = MathHelpers.pow(2, valueMaxBitLength) - 1;
|
||||
long actualValue = (value > maxValue ? maxValue : value) << shift;
|
||||
return (clear(bitmask, bitfield) | actualValue);
|
||||
}
|
||||
|
||||
public static int embed(int value, int bitmask, int shift, int valueMaxBitLength, int bitfield) {
|
||||
int maxValue = MathHelpers.pow(2, valueMaxBitLength) - 1;
|
||||
int actualValue = (value > maxValue ? maxValue : value) << shift;
|
||||
return (clear(bitmask, bitfield) | actualValue);
|
||||
}
|
||||
|
||||
public static short embed(short value, short bitmask, int shift, int valueMaxBitLength, short bitfield) {
|
||||
short maxValue = (short)(MathHelpers.pow(2, valueMaxBitLength) - 1);
|
||||
short actualValue = (short)((value > maxValue ? maxValue : value) << shift);
|
||||
return (short)(clear(bitmask, bitfield) | actualValue);
|
||||
}
|
||||
|
||||
public static byte embed(byte value, byte bitmask, int shift, int valueMaxBitLength, byte bitfield) {
|
||||
byte maxValue = (byte)(MathHelpers.pow(2, valueMaxBitLength) - 1);
|
||||
byte actualValue = (byte)((value > maxValue ? maxValue : value) << shift);
|
||||
return (byte)(clear(bitmask, bitfield) | actualValue);
|
||||
}
|
||||
|
||||
|
||||
public static int getMaskFor(int startBit, int numBits) {
|
||||
int temp = MathHelpers.pow(2, startBit);
|
||||
return (temp * (MathHelpers.pow(2, numBits))) - temp;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -90,6 +90,38 @@ public final class MathHelpers {
|
|||
return (diff <= largest * epsilon);
|
||||
}
|
||||
|
||||
public static float fastInverseSqrt(float x) {
|
||||
float xhalf = 0.5f * x;
|
||||
int i = Float.floatToIntBits(x);
|
||||
i = 0x5f3759df - (i >> 1);
|
||||
x = Float.intBitsToFloat(i);
|
||||
x = x * (1.5f - xhalf * x * x);
|
||||
return x;
|
||||
}
|
||||
|
||||
public static double fastInverseSqrt(double x) {
|
||||
double xhalf = 0.5d * x;
|
||||
long i = Double.doubleToLongBits(x);
|
||||
i = 0x5fe6ec85e7de30daL - (i >> 1);
|
||||
x = Double.longBitsToDouble(i);
|
||||
x = x * (1.5d - xhalf * x * x);
|
||||
return x;
|
||||
}
|
||||
|
||||
public static int pow(int number, int power) {
|
||||
int result = 1;
|
||||
for (int i = 0; i < power; ++i)
|
||||
result *= number;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static long pow(long number, int power) {
|
||||
long result = 1;
|
||||
for (int i = 0; i < power; ++i)
|
||||
result *= number;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int sign(int value) {
|
||||
if (value < 0)
|
||||
return -1;
|
||||
|
|
Loading…
Reference in a new issue