Add BitShiftUtil square() and pow()

This commit is contained in:
James Seibel
2022-11-06 21:40:21 -06:00
parent 8099925dc2
commit 2429cbbb52
4 changed files with 23 additions and 3 deletions
@@ -32,4 +32,24 @@ public class BitShiftUtil
*/
public static int half(int value) { return value >> 1; }
/**
* Equivalent to: <br>
* value << 1, <br>
* value^2, <br>
* Math.pow(value, 2) <br><br>
*
* Note: Math.pow() isn't identical for large values where bits would be lost in the shift, however for medium to small values they function the same.
*/
public static int square(int value) { return value << 1; }
/**
* Equivalent to: <br>
* value << power, <br>
* value^power, <br>
* Math.pow(value, power) <br><br>
*
* Note: Math.pow() isn't identical for large values where bits would be lost in the shift, however for medium to small values they function the same.
*/
public static int pow(int value, int power) { return value << power; }
}
@@ -86,7 +86,7 @@ public class DhLodPos implements Comparable<DhLodPos>
this.z * 2 + BitShiftUtil.half(child0to3 & 2));
}
/** Returns this position's child index in its parent */
public int getChildIndexOfParent() { return (this.x & 1) + ((this.z & 1) << 1); }
public int getChildIndexOfParent() { return (this.x & 1) + BitShiftUtil.square(this.z & 1); }
public boolean overlaps(DhLodPos other)
{
@@ -25,7 +25,7 @@ public class DhLodUnit
/** @return the size of this LOD unit in Minecraft blocks */
public int toBlockWidth() { return this.numberOfLodSectionsWide << this.detailLevel; }
public int toBlockWidth() { return BitShiftUtil.pow(this.numberOfLodSectionsWide, this.detailLevel); }
/** @return the LOD Unit relative to the given block width and detail level */
public static DhLodUnit fromBlockWidth(int blockWidth, byte targetDetailLevel) { return new DhLodUnit(targetDetailLevel, Math.floorDiv(blockWidth, BitShiftUtil.powerOfTwo(targetDetailLevel))); }
@@ -92,7 +92,7 @@ public class DhSectionPos
this.sectionZ * 2 + BitShiftUtil.half(child0to3 & 2));
}
/** Returns this position's child index in its parent */
public int getChildIndexOfParent() { return (this.sectionX & 1) + ((this.sectionZ & 1) << 1); }
public int getChildIndexOfParent() { return (this.sectionX & 1) + BitShiftUtil.square(this.sectionZ & 1); }
/** Applies the given consumer to all 4 of this position's children. */
public void forEachChild(Consumer<DhSectionPos> callback)