Ticket #3368: maths.patch

File maths.patch, 1.5 KB (added by Bruno Manganelli, 9 years ago)
  • Sqrt.cpp

     
    1919
    2020#include "Sqrt.h"
    2121
     22#if !(defined(_MSC_VER) || defined(__GNUC__) || defined(__clang__))
     23
     24// NOTE: this handwritten sqrt function has been found to be much slower than the
     25// one provided by the standard library on the MSVC, GCC and Clang compilers on
     26// x86 and x86-64 architectures. Other compilers and architectures have not been
     27// tested but the performance penalty is likely to be comparable.
     28
    2229// Based on http://freaknet.org/martin/tape/gos/misc/personal/msc/sqrt/sqrt.html
    2330u32 isqrt64(u64 n)
    2431{
     
    4249    return (u32)res;
    4350}
    4451
    45 // TODO: This should be equivalent to (u32)sqrt((double)n), and in practice
    46 // that seems to be true for all input, so do we actually need this integer-only
    47 // implementation? i.e. are there any platforms / compiler settings where
    48 // sqrt(double) won't give the correct answer? and is sqrt(double) faster?
     52#endif
  • Sqrt.h

     
    2222 * 64-bit integer square root.
    2323 * Returns r such that r^2 <= n < (r+1)^2, for the complete u64 range.
    2424 */
     25//NOTE: see Sqrt.cpp note
     26#if defined(_MSC_VER) || defined(__GNUC__) || defined(__clang__)
     27
     28#include <cmath>
     29inline u32 isqrt64(u64 n) { return (u32)sqrt((double)n); }
     30
     31#else
     32
    2533u32 isqrt64(u64 n);
    2634
     35#endif
     36
    2737#endif // INCLUDED_MATH_SQRT