Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (162) games submitted by our members Games in WIP (632) 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 4260 times) 0 Members and 1 Guest are viewing this topic.
Riven
« League of Dukes »

« JGO Overlord »

Medals: 966
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: 164
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
 « 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

You cannot reply to this message, because it is very, very old.

 MrMapcom (10 views) 2015-05-23 20:26:16 MrMapcom (10 views) 2015-05-23 20:23:34 Waterwolf (28 views) 2015-05-20 15:01:45 chrislo27 (34 views) 2015-05-20 03:42:21 BurntPizza (69 views) 2015-05-10 15:53:18 FrozenShade (55 views) 2015-05-07 09:11:21 TheLopais (217 views) 2015-05-06 13:36:48 TheLopais (201 views) 2015-05-06 13:35:14 TheLopais (206 views) 2015-05-06 13:33:39 TheLopais (226 views) 2015-05-06 13:32:48
 Spasi 32x BurntPizza 16x DavidBVal 12x ra4king 12x Riven 12x EgonOlsen 11x theagentd 11x Husk 10x KevinWorkman 9x princec 8x scanevaro 8x opiop65 7x KaiHH 7x revers 6x HeroesGraveDev 5x MrPork 5x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 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