Blender V5.3
gpu_shader_math_fast.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
9
10/* [Drobot2014a] Low Level Optimizations for GCN. */
11float sqrt_fast(float v)
12{
13 return intBitsToFloat(0x1fbd1df5 + (floatBitsToInt(v) >> 1));
14}
16{
17 return intBitsToFloat(0x1fbd1df5 + (floatBitsToInt(v) >> 1));
18}
19
20/* [Eberly2014] GPGPU Programming for Games and Science. */
21float acos_fast(float v)
22{
23 float res = -0.156583f * abs(v) + M_PI_2;
24 res *= sqrt_fast(1.0f - abs(v));
25 return (v >= 0) ? res : M_PI - res;
26}
28{
29 float2 res = -0.156583f * abs(v) + M_PI_2;
30 res *= sqrt_fast(1.0f - abs(v));
31 v.x = (v.x >= 0) ? res.x : M_PI - res.x;
32 v.y = (v.y >= 0) ? res.y : M_PI - res.y;
33 return v;
34}
35
36float atan_fast(float x)
37{
38 float a = abs(x);
39 float k = a > 1.0f ? (1.0f / a) : a;
40 float s = 1.0f - (1.0f - k); /* Crush denormals. */
41 float t = s * s;
42 /* http://mathforum.org/library/drmath/view/62672.html
43 * Examined 4278190080 values of atan:
44 * 2.36864877 avg ULP diff, 302 max ULP, 6.55651e-06f max error // (with denormals)
45 * Examined 4278190080 values of atan:
46 * 171160502 avg ULP diff, 855638016 max ULP, 6.55651e-06f max error // (crush denormals)
47 */
48 float r = s * fma(0.43157974f, t, 1.0f) / fma(fma(0.05831938f, t, 0.76443945f), t, 1.0f);
49 if (a > 1.0f) {
50 r = 1.57079632679489661923f - r;
51 }
52 return (x < 0.0f) ? -r : r;
53}
#define M_PI_2
#define M_PI
VecBase< int, D > floatBitsToInt(VecOp< float, D >) RET
VecBase< float, D > intBitsToFloat(VecOp< int, D >) RET
#define abs
#define fma
float atan_fast(float x)
float sqrt_fast(float v)
float acos_fast(float v)
float x
float y