forked from taichi-dev/taichi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast_pow.glsl.h
55 lines (49 loc) · 979 Bytes
/
fast_pow.glsl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// vim: ft=glsl
// clang-format off
// NOLINTBEGIN(*)
#include "taichi/util/macros.h"
#ifdef TI_INSIDE_OPENGL_CODEGEN
#define OPENGL_BEGIN_FAST_POW_DEF constexpr auto kOpenGLFastPowSourceCode =
#define OPENGL_END_FAST_POW_DEF ;
#else
static_assert(false, "Do not include");
#define OPENGL_BEGIN_FAST_POW_DEF
#define OPENGL_END_FAST_POW_DEF
#endif
OPENGL_BEGIN_FAST_POW_DEF
STR(
int fast_pow_i32(int x, int y)
{
if (y > 512)
return int(pow(x, y));
bool neg = y < 0;
y = abs(y);
int ret = 1;
while (y != 0) {
if ((y & 1) != 0)
ret *= x;
x *= x;
y >>= 1;
}
return neg ? 1 / ret : ret;
}
float fast_pow_f32(float x, int y)
{
if (y > 512)
return pow(x, y);
bool neg = y < 0;
y = abs(y);
float ret = 1.0;
while (y != 0) {
if ((y & 1) != 0)
ret *= x;
x *= x;
y >>= 1;
}
return neg ? 1.0 / ret : ret;
}
)
OPENGL_END_FAST_POW_DEF
#undef OPENGL_BEGIN_FAST_POW_DEF
#undef OPENGL_END_FAST_POW_DEF
// NOLINTEND(*)