Mesa (master): glsl: improve the accuracy of the atan(x, y) builtin function .

Paul Berry stereotype441 at kemper.freedesktop.org
Mon Aug 1 14:41:43 PDT 2011

```Module: Mesa
Branch: master
Commit: b1b4ea0b3679db0b8fddaa9663a10d4712bba3b7
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=b1b4ea0b3679db0b8fddaa9663a10d4712bba3b7

Author: Paul Berry <stereotype441 at gmail.com>
Date:   Wed Jul 27 15:53:31 2011 -0700

glsl: improve the accuracy of the atan(x,y) builtin function.

The previous formula for atan(x,y) returned a value of +/- pi whenever
|x|<0.0001, and used a formula based on atan(y/x) otherwise.  This
broke in cases where both x and y were small (e.g. atan(1e-5, 1e-5)).

This patch modifies the formula so that it returns a value of +/- pi
whenever |x|<1e-8*|y|, and uses the formula based on atan(y/x)
otherwise.

---

src/glsl/builtins/ir/atan |    4 +++-
1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/src/glsl/builtins/ir/atan b/src/glsl/builtins/ir/atan
index cfecc1f..7b5ea13 100644
--- a/src/glsl/builtins/ir/atan
+++ b/src/glsl/builtins/ir/atan
@@ -54,7 +54,9 @@
)
(
(declare () float r)
-      (if (expression bool > (expression float abs (var_ref x)) (constant float (0.000100))) (
+      (if (expression bool >
+           (expression float abs (var_ref x))
+           (expression float * (constant float (1.0e-8)) (expression float abs (var_ref y)))) (
(assign (x) (var_ref r) (call atan ((expression float / (var_ref y) (var_ref x)))))
(if (expression bool < (var_ref x) (constant float (0.000000)) ) (
(if (expression bool >= (var_ref y) (constant float (0.000000)) )

```