mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-07-04 15:27:30 +07:00
Improved Fixed utils
Made class final Changed from bit shift to lookup table
This commit is contained in:
@ -1,17 +1,24 @@
|
||||
package com.riiablo.attributes;
|
||||
|
||||
public class Fixed {
|
||||
public static int floatToIntBits(float value, int precision) {
|
||||
return (int) (value * (1 << precision));
|
||||
public final class Fixed {
|
||||
private static final int[] DIVISOR = new int[Integer.SIZE];
|
||||
static {
|
||||
for (int i = 0; i < Integer.SIZE; i++) {
|
||||
DIVISOR[i] = 1 << i;
|
||||
}
|
||||
}
|
||||
|
||||
public static float intBitsToFloat(int value, int precision) {
|
||||
final int pow2 = (1 << precision);
|
||||
public static int floatToIntBits(final float value, final int precision) {
|
||||
return (int) (value * DIVISOR[precision]);
|
||||
}
|
||||
|
||||
public static float intBitsToFloat(final int value, final int precision) {
|
||||
final int pow2 = DIVISOR[precision];
|
||||
final int mask = pow2 - 1;
|
||||
return ((value >>> precision) + ((value & mask) / (float) pow2));
|
||||
}
|
||||
|
||||
public static int intBitsToFloatFloor(int value, int precision) {
|
||||
public static int intBitsToFloatFloor(final int value, final int precision) {
|
||||
return value >>> precision;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user