Archived

This topic is now archived and is closed to further replies.

DV8D

[java] fast sqrt in java

Recommended Posts

DV8D    122
Has anyone a fast float sqrt function for java. I have been looking at nvidias fast math routine, a LUT (found here http://developer.nvidia.com/view.asp?IO=fast_math_routines ) but I didn´t manage to port that to java. Anyone?

Share this post


Link to post
Share on other sites
pirate_dau    151
I may be wrong, but this type of fast math routine doesn''t really fit into the paradigm of Java. That paradigm being OOP and platform independence over absolute speed (such the processor speed is fast becoming... well... um.. fast). So no, I''ve never seen this done personally with Java. You will likely also run into problems considering that many fast math routines depend somewhat on the preprocessor (which Java does not have).

This is one reason that I feel Java isn''t really in a position to take over game programming (specifically bleeding edge graphics game programming) -- because of its focus on structure rather than outright speed. In this arena, I would give C++ the obvious advantage. Java works for many other applications, including less demanding (in terms of math/graphics) games, of which there are many. It''s all about the right tool for the right job -- if you find yourself needing fast math routines, I would seriously think about changing languages. Or, mixing and matching languages so that you have enough control over the low-level stuff where you need it performance-wise (using a different language).

Sorry for letting your thread sit for so long, but I was half-wondering if somebody else would respond that they had something like this. I never like responding "nope, I can''t help ya there" -- but in this case, I still think that I had something worthwhile to say on the matter.

-pirate_dau

Share this post


Link to post
Share on other sites
DV8D    122
Thanks guys I was beginning to feel all alone here

pirate_dau, I agree with most of what you said but not all. Sure you have to sacrife some speed for platform indep. and OOP but why shouldn´t you make it as fast as possible? I am making an applet, a raytracer and don´t want the user to sit on his/her ass all day waiting for his/her computer crunching all those sqrt´s I do agree with your little rant about gameprogramming. But what theres is something that I don´t get, might be ´cause I´m not very good at english. What is your conclusion? If I need fast math routines then what?

mill-o do you mean that I should compile it in c than link to it with JNI rather than trying to implement it in java? Or was it meant to pirate_dau?

Share this post


Link to post
Share on other sites
GKW    200
There isn''t any reason you couldn''t implement this in java. I would suggest you use the StrictMath api if you want everyone to get the exact same LUT. You would take a serious performance hit if you made this call out through JNI on every call though. That being said I owuld do your raytracer with straight Math.sqrt or StrictMath.sqrt calls and then with the LUT and see which comes out ahead. With today processors it may be faster to actually compute the square root on the fly rather than saturating the bus with lookups.

The fanatic is incorruptible: if he kills for an idea, he can just as well get himself killed for one; in either case, tyrant or martyr, he is a monster.
--EM Cioran

Opere Citato

Share this post


Link to post
Share on other sites
pirate_dau    151
I realize that you''re concerned about performance, but there is a popular saying here at GameDev: Premature optimization is the root of all evil. GKW''s comment reminded me about benchmarking similar routines a while back and finding that the "non-optimized" routine actually ran faster, so definitely consider using the normal sqrt calls, as they may end up being sufficiently fast.

If you were to use the JNI, I would not use it ONLY for the sqrt -- that would be some ugly overhead, as GKW also said. If you were to use the JNI, I would probably move a great deal of the rendering while you''re at it.

What was my conclusion from earlier? I''m not sure I really had one. My conclusion for this post, though, would be to try it with the original math sqrt -- if that isn''t fast enough, then look for other solutions.

-pirate_dau

Share this post


Link to post
Share on other sites
OrangyTang    1298
Here''s an idea: create your own math library, a class like FastMath or something. Then use this in your game for any maths that you dont mind as much about accuracy. Then make your FastMath class mainly a wrapper around standard Math class functions to start with and when you need to optimise simply change your FastMath to use a LUT or whatever, and you dont need to change anything else in your code

Share this post


Link to post
Share on other sites
DV8D    122
Hmmz I must have dozed off..
Well I´ve been trying to implement some fastsqrt routines myself here but havn´t been able to "out run" Math.sqrt Thanks for the pointers/tips anyhow.
Until next time.

Share this post


Link to post
Share on other sites