Cycles: Use more stable version of integer square root function

Old code was working quite unreliable in combination with fast math
flag, especially when compiling with Clang. It seems we were hitting
result of the following bug submitted to Clang [1].

Basically, it was happening so that (int)sqrtf(64) was 7 when Cycles
is built with Clang but was correct 8 when built with GCC.

This commit works this around. Annoying, but don't see other way to
keep sampling pattern the same for Clang and GCC.

[1] https://bugs.llvm.org//show_bug.cgi?id=24063
This commit is contained in:
Sergey Sharybin 2017-05-09 16:12:06 +02:00
parent e20eb2dec0
commit 4d38932cb4

@ -175,15 +175,26 @@ ccl_device float cmj_sample_1D(int s, int N, int p)
return (x + jx)*invN;
}
/* TODO(sergey): Do some extra tests and consider moving to util_math.h. */
ccl_device_inline int cmj_isqrt(int value)
{
#if defined(__KERNEL_CUDA__)
return float_to_int(__fsqrt_ru(value));
#elif defined(__KERNEL_GPU__)
return float_to_int(sqrtf(value));
#else
/* This is a work around for fast-math on CPU which might replace sqrtf()
* with am approximated version.
*/
return float_to_int(sqrtf(value) + 1e-6f);
#endif
}
ccl_device void cmj_sample_2D(int s, int N, int p, float *fx, float *fy)
{
kernel_assert(s < N);
#if defined(__KERNEL_CUDA__)
int m = float_to_int(__fsqrt_ru(N));
#else
int m = float_to_int(sqrtf(N));
#endif
int m = cmj_isqrt(N);
int n = (N - 1)/m + 1;
float invN = 1.0f/N;
float invm = 1.0f/m;