Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Fast Math: fast floor / round / ceil  (Read 6856 times) 0 Members and 1 Guest are viewing this topic.
Riven

« JGO Overlord »

Medals: 1336
Projects: 4
Exp: 16 years

 « Posted 2011-05-09 20:51:53 »

Calling Math.floor() simply takes too long. The problem with optimizing is that you can't simply cast the floatingpoint number to an integer, because that will result in invalid results for negative numbers. If we know our input values are in a specific range, we can safely add a certain (big) constant to the input, cast the guaranteed positive value to an integer and subtract the constant again.

The code is ~9x faster than Math.floor(). Replacing the doubles with floats makes it faster, but the results are rather... random, so don't.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `public class FastMath{   private static final int    BIG_ENOUGH_INT   = 16 * 1024;   private static final double BIG_ENOUGH_FLOOR = BIG_ENOUGH_INT;   private static final double BIG_ENOUGH_ROUND = BIG_ENOUGH_INT + 0.5;   public static int fastFloor(float x) {      return (int) (x + BIG_ENOUGH_FLOOR) - BIG_ENOUGH_INT;   }   public static int fastRound(float x) {      return (int) (x + BIG_ENOUGH_ROUND) - BIG_ENOUGH_INT;   }   public static int fastCeil(float x) {       return BIG_ENOUGH_INT - (int)(BIG_ENOUGH_FLOOR-x); // credit: roquen   }}`

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Nate

« JGO Bitwise Duke »

Medals: 167
Projects: 4
Exp: 14 years

Esoteric Software

 « Reply #1 - Posted 2011-05-10 04:23:07 »

Thanks Riven, I used this in libgdx. You might want to use my javadocs there, which give the valid input ranges:

Roquen

JGO Kernel

Medals: 517

 « Reply #2 - Posted 2011-05-10 16:08:29 »

floor has an additional limitation cause by underflow.  For the current constant, value on [-0x1.0p-40f, 0) will return 0 instead of -1.

ceil is, well, broken.  But since ceil(x)=-floor(-x), it can be reworked as:
 1  2  3  4  5 `public static final int ceil(float x) {  //return (int)(x + BIG_ENOUGH_CEIL) - BIG_ENOUGH_INT;    return BIG_ENOUGH_INT - (int)(BIG_ENOUGH_FLOOR-x);}`
Pages: [1]
 ignore  |  Print

 nelsongames (14 views) 2018-04-24 18:15:36 nelsongames (11 views) 2018-04-24 18:14:32 ivj94 (587 views) 2018-03-24 14:47:39 ivj94 (49 views) 2018-03-24 14:46:31 ivj94 (384 views) 2018-03-24 14:43:53 Solater (64 views) 2018-03-17 05:04:08 nelsongames (110 views) 2018-03-05 17:56:34 Gornova (159 views) 2018-03-02 22:15:33 buddyBro (707 views) 2018-02-28 16:59:18 buddyBro (93 views) 2018-02-28 16:45:17
 KaiHH 13x ByerN 11x SHC 10x NuclearPixels 10x Zemlaynin 10x Guerra2442 8x Damocles 6x VaTTeRGeR 5x Spasi 4x orangepascal 4x philfrei 4x ags1 4x princec 3x ndnwarrior15 3x mesterh 3x Phased 2x
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org