• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece!🔗 Click here to enter!

[JASS] Alternative to SquareRoot?

Status
Not open for further replies.
Level 24
Joined
Aug 1, 2013
Messages
4,657
If you want to avoid it because of its required processing time, you could do the calculations on the squared values instead.

Aka:
MaxDistance = 600
DistanceX = 400
DistanceY = 300

IsInRange = MaxDistance >= sqrt(DistanceX*DistanceX + DistanceY*DistanceY)
Or
IsInRange = MaxDistance*MaxDistance >= DistanceX*DistanceX + DistanceY*DistanceY
 
Level 39
Joined
Feb 27, 2007
Messages
5,031
So... The formula to apply this in jass is?
What is the problem with sqrt()? Is there a specific situation which should not use it?
If you're doing a lot of sqrt() calls in a short time (physic engine, for example) it could potentially be beneficial to optimize a lot and use something like the fast inverse sqrt trick DracoL1ch posted. However, for most WC3 code sqrt() is fine and if it really matters you can achieve an acceptable increase in speed/efficiency by working with the square of the number you want to compute the sqrt of exactly as Wietlol said.

The answers to these questions depend a lot on why you don't want to use sqrt() in the first place.
 
The answers to these questions depend a lot on why you don't want to use sqrt() in the first place.
Exactly. What Wietlol explained is a nice trick to avoid the sqrt function call, but it only should be used privatly by a system, and only works if you have a reference value, so like MaxDistance for example.

There can be done other approaches in other cases, like caching sqrt values onInit + a table lookup later, or just to use other maths forumlas maybe if it is for certain calculations.
So it's really hard to say anything how to genealy "avoid" sqrt, because by itself as standalone, it is a very useful function.
 
Level 9
Joined
Jun 21, 2012
Messages
432
What is the problem with sqrt? Is there a specific situation which should not use it?
The problem is the processing timer as wietlol said, but neither is it a serious problem. I just wanted to know if there is another way to calculate the sqrt().

in my tests fast inverse square root was slower in jass.
You can post a test code?
 

Deleted member 219079

D

Deleted member 219079

I think Wietlol's answer is the solution.
 

LeP

LeP

Level 13
Joined
Feb 13, 2008
Messages
539
You can post a test code?

JASS:
function rsqrt takes real r returns real
   local real x = r * 0.5
   set r = i2r(0x5f3759df - ( r2i(r) / 2 ))
   return r * ( 1.5 - ( x * r * r ) )
endfunction

Go wild. Needs an r2i /i2r return-bug function of some sort.
The performance killer here is ofc the r2i/i2r calls.
 

LeP

LeP

Level 13
Joined
Feb 13, 2008
Messages
539
I assume that i2r() and r2i() are I2R() and R2I().
Or are they typecasts (without changing the bytedata behind the screen)?
Typecasts like here or here.

I took crigges approach.
Here's the full script i guess.
JASS:
//# +nosemanticerror
private function indexToReal takes integer i returns real
  return i
endfunction

//# +nosemanticerror
private function realToIndex takes real i returns integer
  return i
endfunction

private function cleanR takes real r returns real
  return r
  return 0.0 // dont let jh inlines this
endfunction

private function cleanI takes integer i returns integer
  return i
  return 0 // dont let jh inlines this
endfunction

function i2r takes integer i returns real
  return cleanR(indexToReal(i)) // this gets inlined
endfunction

function r2i takes real i returns integer
  return cleanI(realToIndex(i)) // this gets inlined
endfunction

function rsqrt takes real r returns real
   local real x = r * 0.5
   set r = i2r(0x5f3759df - ( r2i(r) / 2 ))
   return r * ( 1.5 - ( x * r * r ) )
endfunction
 

Deleted member 219079

D

Deleted member 219079

And you're confident it's the most efficient with 2 casts?
 

Deleted member 219079

D

Deleted member 219079

I2R < *1.

I think the hexadecimal number is there to make it look cooler.
 
Level 9
Joined
Jun 21, 2012
Messages
432
JASS:
function rsqrt takes real r returns real
   local real x = r * 0.5
   set r = i2r(0x5f3759df - ( r2i(r) / 2 ))
   return r * ( 1.5 - ( x * r * r ) )
endfunction

Go wild. Needs an r2i /i2r return-bug function of some sort.
The performance killer here is ofc the r2i/i2r calls.
Thanks... you say that it is slow that SquareRoot however I find it interesting.

If you want to avoid it because of its required processing time, you could do the calculations on the squared values instead.

Aka:
MaxDistance = 600
DistanceX = 400
DistanceY = 300

IsInRange = MaxDistance >= sqrt(DistanceX*DistanceX + DistanceY*DistanceY)
Or
IsInRange = MaxDistance*MaxDistance >= DistanceX*DistanceX + DistanceY*DistanceY

I think Wietlol's answer is the solution.
I agree with you.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
To clarify, SquareRoot function is not slow. Most of its execution time is the JASS overhead of making a function call (name lookup + stack stuff including new local hashtable). This is why a faster solution cannot exist because such a solution would need to be written in JASS and as such the JASS overhead already makes it slower than SquareRoot. This is also why there is as good as no speed difference between using integer and real types in JASS, despite most processors having integer throughput several times larger than their floating point throughput.

Every named element in a JASS script is looked up at run time using a hash table mapping element name string to element data. Even doing something as simple as "set i = i + i" performs 3 name lookups. Trying to write any sort of optimized mathematical code will end up executing painfully slowly when compared with native code.

Why they did not static link variables I do not know. That alone would make JASS an order of magnitude or more faster.
 
Status
Not open for further replies.
Top