Skip to main content
gamedev.net
🔒 Locked

Fast Math Needed

Started by TheKrust 5 replies 1.2k views
Original Post
TheKrust
TheKrust
I have a custom built lighting engine in visual basic. It runs pretty fast, but I need to get it as fast as possible. Is there an easy replacement for the sqare root function that uses less CPU time? It doesn't need to be %100 accurate
---------------------------------------- There's a steering wheel in my pants and it's drivin me nuts
coderchris
coderchris
you could always make a lookup table (if the range of numbers you need to sqrt is not too large)

Im not sure how fast that would actually be though, since nowadays functions like sqrt are fairly efficient

All you can do is try it and see how fast it would work. A lookup table wouldnt be near as accurate also
Rockoon1
Rockoon1
What version of VB?

If its VB6, you are (almost) stuck with Sqr() or a lookup table (make sure its a static array for performance)

If its VB.NET, you might be able to get the compiler to inline a call to the bitconverter so that you can extract the exponent efficiently and thus be able to get a good fast estimate.

If you have any division operations going on, then your bottleneck may infact be the divisions and not the sqrt.
TheKrust
TheKrust
actually, I just found a way. Really simple. Not accurate, but it does what I need it to do.
---------------------------------------- There's a steering wheel in my pants and it's drivin me nuts
Raghar
Raghar
Quote:
Original post by TheKrust
I have a custom built lighting engine in visual basic. It runs pretty fast, but I need to get it as fast as possible. Is there an easy replacement for the sqare root function that uses less CPU time? It doesn't need to be %100 accurate


Yes.
1. Don't use sqrt.
2. Calculate by fast way inverse sqrt.
3. Use asm instruction for fast incorrect inverse sqrt (if present on your CPU).
4. GFX card could do fast inverse incorrect sqrt.
Numsgil
Numsgil
On modern platforms, the sqrt function call has been so streamlined by the CPU that any fancy tricks are actually going to be slower. Be sure you test the speed of your code before and after you tinker. You should find that the sqrt operation is easily 30% faster than any math tricks you can do yourself.

The bit hacks are really only necessary on embedded systems anymore, to be honest.
[size=2]Darwinbots - [size=2]Artificial life simulation

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.