From f45168a133cb37d02707ca3da8f295ba03de6f94 Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Tue, 8 Sep 2020 13:01:51 -0700 Subject: [PATCH] Improved Fixed utils Made class final Changed from bit shift to lookup table --- core/src/com/riiablo/attributes/Fixed.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/core/src/com/riiablo/attributes/Fixed.java b/core/src/com/riiablo/attributes/Fixed.java index 0e933865..4004fb1d 100644 --- a/core/src/com/riiablo/attributes/Fixed.java +++ b/core/src/com/riiablo/attributes/Fixed.java @@ -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; }