Skip to content
Permalink
Browse files

Make long packed y signed

  • Loading branch information...
kenzierocks committed Jul 19, 2019
1 parent 47559aa commit 12abdecbf5279a334651e54a5004570fc123846f
@@ -21,36 +21,29 @@

public class BitMath {

public static final long BITS_26 = 0x03_FF_FF_FF;
public static final long BITS_24 = 0x00_FF_FF_FF;
public static final long BITS_20 = 0x00_0F_FF_FF;
public static final int BITS_14 = 0x3F_FF;
public static final int BITS_12 = 0x0F_FF;
public static final int BITS_8 = 0xFF;
public static final int BITS_6 = 0x3F;
public static final int BITS_4 = 0x0F;

private static final int FIX_SIGN_SHIFT = 32 - 26;
public static int mask(int bits) {
return ~(~0 << bits);
}

public static int unpackX(long packed) {
return fixSign26((int) (packed & BITS_26));
return extractSigned(packed, 0, 26);
}

public static int unpackZ(long packed) {
return fixSign26((int) ((packed >> 26) & BITS_26));
return extractSigned(packed, 26, 26);
}

public static int unpackY(long packed) {
return (int) ((packed >> (26 + 26)) & BITS_12);
return extractSigned(packed, 26 + 26, 12);
}

public static int extractSigned(long i, int shift, int bits) {
return fixSign((int) i >> shift & mask(bits), bits);
}

/**
* Fix horizontal sign -- we have a 26-bit two's-complement int,
* we need it to be a 32-bit two's-complement int.
*/
public static int fixSign26(int h) {
public static int fixSign(int i, int bits) {
// Using https://stackoverflow.com/a/29266331/436524
return (h << FIX_SIGN_SHIFT) >> FIX_SIGN_SHIFT;
return i << (32 - bits) >> (32 - bits);
}

private BitMath() {
@@ -24,8 +24,7 @@
import java.util.Comparator;

import static com.google.common.base.Preconditions.checkArgument;
import static com.sk89q.worldedit.math.BitMath.BITS_12;
import static com.sk89q.worldedit.math.BitMath.BITS_26;
import static com.sk89q.worldedit.math.BitMath.mask;
import static com.sk89q.worldedit.math.BitMath.unpackX;
import static com.sk89q.worldedit.math.BitMath.unpackY;
import static com.sk89q.worldedit.math.BitMath.unpackZ;
@@ -84,6 +83,8 @@ public static void checkLongPackable(BlockVector3 location) {
"Location exceeds long packing limits: %s", location);
}

private static final long BITS_26 = mask(26);
private static final long BITS_12 = mask(12);

public static BlockVector3 fromLongPackedForm(long packed) {
return at(unpackX(packed), unpackY(packed), unpackZ(packed));
@@ -41,11 +41,8 @@
import java.util.function.BiFunction;
import java.util.function.Function;

import static com.sk89q.worldedit.math.BitMath.BITS_20;
import static com.sk89q.worldedit.math.BitMath.BITS_24;
import static com.sk89q.worldedit.math.BitMath.BITS_6;
import static com.sk89q.worldedit.math.BitMath.BITS_8;
import static com.sk89q.worldedit.math.BitMath.fixSign26;
import static com.sk89q.worldedit.math.BitMath.fixSign;
import static com.sk89q.worldedit.math.BitMath.mask;

/**
* A space-efficient map implementation for block locations.
@@ -77,6 +74,10 @@ public static BlockMap copyOf(Map<? extends BlockVector3, ? extends BaseBlock> s
* Order (lowest to highest) is x-z-y.
*/

private static final long BITS_24 = mask(24);
private static final long BITS_20 = mask(20);
private static final int BITS_8 = mask(8);
private static final int BITS_6 = mask(6);

private static long toGroupKey(BlockVector3 location) {
return ((location.getX() >>> 6) & BITS_20)
@@ -99,9 +100,9 @@ private static int toInnerKey(BlockVector3 location) {

private static BlockVector3 reconstructLocation(long group, int inner) {
int groupX = (int) ((group & GROUP_X) << 6);
int x = fixSign26(groupX | (inner & INNER_X));
int x = fixSign(groupX | (inner & INNER_X), 26);
int groupZ = (int) ((group & GROUP_Z) >>> (20 - 6));
int z = fixSign26(groupZ | ((inner & INNER_Z) >>> 6));
int z = fixSign(groupZ | ((inner & INNER_Z) >>> 6), 26);
int groupY = (int) ((group & GROUP_Y) >>> (20 + 20 - 8));
int y = groupY | ((inner & INNER_Y) >>> (6 + 6));
return BlockVector3.at(x, y, z);

0 comments on commit 12abdec

Please sign in to comment.
You can’t perform that action at this time.