What was your method for sqrt approximation?
-
-
Vastauksena käyttäjille @DefPriPub ja @Peter_shirley
I will have to go back and look. Been long enough that I don’t recall. I’ll follow up when I can get to my computer.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
It looks like I settled on a recursive newtonian approximation. Code is similar to this: https://gist.github.com/alexshtf/eb5128b3e3e143187794 … FWIW, I didn't bother trying to make it fast. I wanted constexpr and never got around to making a faster approximation.
1 vastaus 0 uudelleentwiittausta 1 tykkäys -
For a real-time approximation, I would probably unroll it to be non-recursive, and iterate a fixed number of times, probably templating on # of iterations so that I could have a few version. sqrt_fast and sqrt_precise, for instance.
2 vastausta 0 uudelleentwiittausta 0 tykkäystä -
Vastauksena käyttäjille @tloch14 ja @Peter_shirley
Do you have a fast sqrt() function (or approximation) for double precision floats? All the ones I'm finding online are for 32 bit, or for the inverse sq. root which is what I don't want to calculate.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
Vastauksena käyttäjille @DefPriPub ja @Peter_shirley
I don’t. You’re right about there being a lot of 32 bit ones, and they usually rely on UB, reinterpret-casting and bit shifting for their speed. Sorry.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
https://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi … This page has several implementations and some are double. Only one that's faster than std::sqrt. Could be a good starting place for implementing your own.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
For instance, if you iterate sqrt8 (babylonian method) a fixed number of times instead of comparing to see if it's close enough, you may get a reasonable speed/precision tradeoff.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
Vastauksena käyttäjille @tloch14 ja @Peter_shirley
After about two days I've trying, I'd like to report that I haven't been able to find an approximation that was faster. In most cases, I wasn't able to beat std::sqrt(). The times that I did, my approximation was horribly in correct. std::sqrt() is king. All shall praise it.
6 vastausta 1 uudelleentwiittaus 2 tykkäystä -
Well, FSQRT is good and std::sqrt() isn't screwing it up :)
1 vastaus 0 uudelleentwiittausta 0 tykkäystä
Isn’t the implementation of std::sqrt not easily inlineable without link time code generation? And therefore also harder to vectorize? I’m not saying this with authority on the subject. Just thinking through how std::sqrt might actually get in the way.
-
-
There is a SIMD instruction for computing a square root https://www.felixcloutier.com/x86/sqrtps I'm not sure what GCC exactly will do if you have multiple std::sqrt() calls right after another.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
Vastauksena käyttäjille @DefPriPub, @tloch14 ja
"-ps" means that it'll calculate in "p"arralel all the "s"ingle float values of the SIMD register. What you are interested in is "-sd" which runs on a "s"ingle "d"ouble value if the SIMD register: https://www.felixcloutier.com/x86/sqrtsd
0 vastausta 0 uudelleentwiittausta 0 tykkäystä
Keskustelun loppu
Uusi keskustelu -
Lataaminen näyttää kestävän hetken.
Twitter saattaa olla ruuhkautunut tai ongelma on muuten hetkellinen. Yritä uudelleen tai käy Twitterin tilasivulla saadaksesi lisätietoja.