Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  Math.abs() discussion  (Read 7682 times)
0 Members and 1 Guest are viewing this topic.
Offline Mr_Light

Senior Member




shiny.


« Reply #30 - Posted 2006-02-22 04:55:41 »

I know damn little about bytecode but I'm guessing in that example both compile to the same.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Raghar

Junior Member




Ue ni taete 'ru hitomi ni kono mi wa dou utsuru


« Reply #31 - Posted 2006-02-23 03:17:29 »

I'm willing to bet my left lung that Math.abs() has never in the history of mankind even been close to being a bottleneck in any kind of program.

...
Optimisations such as these are dangerous, pointless and a huge waste of time.
"res += vals/(i+1);"

Addition and (integer) division.
It's SLOWER than Math.abs().

Video encoder / decoder.



...

Of course the div and the idiv will be slower than abs. These are, with the sqrt, the slowest instructions on CPU. 80 cycles each (in full precision).

If you'd like to see the reason, you could look into my 256 bit library (It alows 256 bit integer register based computation on 32/64+ bit CPUs.) FFT based division algorithms are better at aprox 1024 bits, and hardware based FFT like implementations could be unnecessary complex for only 64 bit registers.

Of course assembly programmers could use aproximate algorithm at cost of aprox 3 multiplies. In addition situation for small divisors isn't that bad, because recent CPUs have division table, so small divisors are resolved more easily.
Offline Raghar

Junior Member




Ue ni taete 'ru hitomi ni kono mi wa dou utsuru


« Reply #32 - Posted 2006-02-23 03:18:58 »


PS>OT how do u test the accuracy of a trig function like java apparently without cause extra trig functions that are known to be accurate?


I would guess that answer on you question would be Taylor series/aproximations.

I remember how I used words "Taylor series" and "that number with exclamation mark" in one sentence.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #33 - Posted 2006-02-24 19:38:28 »

A related function to Math.abs() - How often have you wanted a value capped at zero Wink

1  
2  
3  
4  
5  
6  
7  
8  
9  
   /**
    *
    * performs the equivalent of Math.max(a,0),
    * though alot faster.
    *
    */

   public static final int max0(int a) {
      return a&~(a>>31);
   }
Offline chaosdeathfish

Junior Member


Projects: 1



« Reply #34 - Posted 2006-02-27 17:43:29 »

OK, slightly off-topic, but while we're talking about wierd optimisations.. Anyone know a decent capping algorithm? My current code is:

1  
2  
3  
int cap( int value ) {
    return value > 0 ? value < 0xFF ? value : 0xFF : 0;
}


Any wierd bitshifting things that can do this? Smiley
Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #35 - Posted 2006-02-27 19:29:18 »

This is about 10-20% faster for me:

1  
return value > 255 ? 255 : value&~(value>>31);


edit:
Ooh, a much better cap to zero was already posted.
I really need to start reading the posts one of these days. Cheesy

Play Minecraft!
Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #36 - Posted 2006-02-27 19:35:48 »

There we go:

1  
return value & ~(value >> 31) & ~((255-value)>>31) | (((255-value)>>31)&0xff);



Original method with two ifs (ives?): 10.237750000000005 ns
This method: 4.809813399999991 ns

Play Minecraft!
Offline g666

Junior Member





« Reply #37 - Posted 2006-02-27 19:44:26 »

There we go:

1  
return value & ~(value >> 31) & ~((255-value)>>31) | (((255-value)>>31)&0xff);



Original method with two ifs (ives?): 10.237750000000005 ns
This method: 4.809813399999991 ns

Does an if compile to the same instructions as ?: i think i read somewherer that it doesnt, and that :? is faster.

desperately seeking sanity
Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #38 - Posted 2006-02-27 19:54:29 »

1  
2  
3  
4  
    private static int cap(int value)
    {
        return value > 0 ? value < 0xFF ? value : 0xFF : 0;
    }


10.895770499999994 ns

1  
2  
3  
4  
5  
6  
7  
8  
9  
    private static int cap2(int value)
    {
        if (value>0)
            if (value<255)
                return value;
            else
                return 255;
        else return 0;
    }


9.652298100000003 ns

Play Minecraft!
Offline chaosdeathfish

Junior Member


Projects: 1



« Reply #39 - Posted 2006-02-27 21:31:18 »

Ah! Thanks for that! I've tried that out in my code, and.. I'm afraid it was slower Sad Even using if instead of ?: was slightly slower.

I guess that proves the maxim that you should always benchmark in your own working code..
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #40 - Posted 2006-02-28 23:54:36 »

There we go:

1  
return value & ~(value >> 31) & ~((255-value)>>31) | (((255-value)>>31)&0xff);



Original method with two ifs (ives?): 10.237750000000005 ns
This method: 4.809813399999991 ns

mucho kudos for that algorithm, When I saw your usage of 0xFF at the end, I expected it to only work for the specific case of the cap being 2^n-1.
However, on closer inspection, it works in all cases.

if you replace the 255's, and 0xFF with 'max', you have yourself a pretty complete method implementation!

Very funky.
Offline zingbat

Senior Member




Java games rock!


« Reply #41 - Posted 2006-03-01 02:31:27 »

Is there any documentation that shows what are the optimizations done by the latest java compiler? I want to know the exact conditions that will trigger an optimization if this is possible. Since the JIT optimizes the code at runtime this is not as easy as a static compiler that optimizes the code in compilation time.

Offline shawnkendall

Senior Member





« Reply #42 - Posted 2006-04-27 06:39:54 »

I'm willing to bet my left lung that Math.abs() has never in the history of mankind even been close to being a bottleneck in any kind of program.
Optimisations such as these are dangerous, pointless and a huge waste of time.

I know it's hard to believe, but I have just come across a Math.abs() bottleneck in my game code, specifically when I have lots of physics obejcts bouncing around, the object to object collision check does a first/quick Manhattan (Lm) distance check.  This is basically like an Axis-Aligned Bounding Box check but without the box.  Anyway, Manhattan distance uses the absolute value difference between points.  I'm doing allot to keep from doing more expensive tests and BOOM! profiler shows the test as second highest method call!

I should mention I am using floats, so the little bit hacks don't help here, but I am trying several tests...

Well at least in the NetBeans profiler...
x = ( x > 0 ) ? x : -x;

is faster than

x = Math.abs(x);

Could be a lie though because the profiling might prevent the VM optimizations for Math.abs()...

[Last mod of the night]

of course...
if ( x < 0 ) { x = -x; }

is faster than

x = ( x > 0 ) ? x : -x;

Because half the time (in general) there will be no asignment done (i.e. x > 0) so i ended with that.

Shawn Kendall
Cosmic Interactive, LLC
http://www.facebook.com/BermudaDash
Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #43 - Posted 2006-04-27 13:48:35 »

I've GOT to stop betting my lungs. Undecided

Play Minecraft!
Offline Raghar

Junior Member




Ue ni taete 'ru hitomi ni kono mi wa dou utsuru


« Reply #44 - Posted 2006-04-30 01:34:02 »

This is basically like an Axis-Aligned Bounding Box check but without the box.  Anyway, Manhattan distance uses the absolute value difference between points.  I'm doing allot to keep from doing more expensive tests and BOOM! profiler shows the test as second highest method call!

I should mention I am using floats, so the little bit hacks don't help here, but I am trying several tests...

Well at least in the NetBeans profiler...
x = ( x > 0 ) ? x : -x;

is faster than

x = Math.abs(x);

Could be a lie though because the profiling might prevent the VM optimizations for Math.abs()...

[Last mod of the night]

of course...
if ( x < 0 ) { x = -x; }

is faster than

x = ( x > 0 ) ? x : -x;

Because half the time (in general) there will be no asignment done (i.e. x > 0) so i ended with that.

And what about

if(a < 0) {
      a *= -1;}

Of course there are bit hacks that would change sign of float point number on SSE2 registers. The bad thing is they would need support from JVM.

and XMM0, const1 // change sign of 4 floats, or 2 doubles to positive.
xor XMM0, const2

Offline abies

Senior Member





« Reply #45 - Posted 2006-04-30 12:59:46 »

Could be a lie though because the profiling might prevent the VM optimizations for Math.abs()...

I'm 95% sure that this is the case. I have seen getters/setters to be reported by profilers as a bottlenecks, while in real run, they were perfectly inlined. I'll doublecheck that, but I think that Math.abs should be intrinsic (sp?) and do not invoke any method call at all during normal run.

Edit:
I have doublechecked in mustang code and abs is generated inline - as one single fabs opcode under i486. I'm afraid that any profiling reporting it as a method call is not really right...

Artur Biesiadowski
Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #46 - Posted 2006-05-02 16:58:17 »

I wasn't using any profilers in any of my tests..

Play Minecraft!
Pages: 1 [2]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (6 views)
2014-04-24 23:00:44

xsi3rr4x (83 views)
2014-04-15 18:08:23

BurntPizza (75 views)
2014-04-15 03:46:01

UprightPath (86 views)
2014-04-14 17:39:50

UprightPath (69 views)
2014-04-14 17:35:47

Porlus (86 views)
2014-04-14 15:48:38

tom_mai78101 (109 views)
2014-04-10 04:04:31

BurntPizza (169 views)
2014-04-08 23:06:04

tom_mai78101 (265 views)
2014-04-05 13:34:39

trollwarrior1 (217 views)
2014-04-04 12:06:45
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!