<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Dec 8, 2016 at 5:03 PM, Haixia Shi <span dir="ltr"><<a href="mailto:hshi@chromium.org" target="_blank">hshi@chromium.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Clamp input scalar value to range [-10, +10] to avoid precision problems<br>
when the absolute value of input is too large.<br>
<br>
Fixes dEQP-GLES3.functional.shaders.<wbr>builtin_functions.precision.<wbr>tanh.* test<br>
failures.<br>
<br>
Signed-off-by: Haixia Shi <<a href="mailto:hshi@chromium.org">hshi@chromium.org</a>><br>
Cc: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>>,<br>
Cc: Stéphane Marchesin <<a href="mailto:marcheu@chromium.org">marcheu@chromium.org</a>>,<br>
Cc: Kenneth Graunke <<a href="mailto:kenneth@whitecape.org">kenneth@whitecape.org</a>><br>
<br>
Change-Id: I324c948b3323ff8107127c42934f1<wbr>4459e124b95<br>
---<br>
src/compiler/glsl/builtin_<wbr>functions.cpp | 8 ++++++--<br>
1 file changed, 6 insertions(+), 2 deletions(-)<br>
<br>
diff --git a/src/compiler/glsl/builtin_<wbr>functions.cpp b/src/compiler/glsl/builtin_<wbr>functions.cpp<br>
index 3e4bcbb..4ede925 100644<br>
--- a/src/compiler/glsl/builtin_<wbr>functions.cpp<br>
+++ b/src/compiler/glsl/builtin_<wbr>functions.cpp<br>
@@ -3563,9 +3563,13 @@ builtin_builder::_tanh(const glsl_type *type)<br>
ir_variable *x = in_var(type, "x");<br>
MAKE_SIG(type, v130, 1, x);<br>
<br>
+ /* clamp x to [-10, +10] to avoid precision problems */<br></blockquote><div><br></div><div>Would you mind adding something like this to your comment:<br><br></div><div>When x > 10, e^-x is so small relative to e^x that it gets flushed to zero in the computation e^x + e^-x. The same happens in the other direction when x < -10.<br><br></div><div>With that added,<br><br></div><div>Reviewed-by: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>><br><br>Chatting with Ken a bit, I realized why you used 20
before. That's the threshold needed if you're doing the computation in
double-precision. However, GLSL doesn't define a double form of tanh so
that's not really a problem.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ ir_variable *t = body.make_temp(type, "tmp");<br>
+ body.emit(assign(t, min2(max2(x, imm(-10.0f)), imm(10.0f))));<br>
+<br>
/* (e^x - e^(-x)) / (e^x + e^(-x)) */<br>
- body.emit(ret(div(sub(exp(x), exp(neg(x))),<br>
- add(exp(x), exp(neg(x))))));<br>
+ body.emit(ret(div(sub(exp(t), exp(neg(t))),<br>
+ add(exp(t), exp(neg(t))))));<br>
<br>
return sig;<br>
}<br>
<span class="gmail-HOEnZb"><font color="#888888">--<br>
2.8.0.rc3.226.g39d4020<br>
<br>
</font></span></blockquote></div><br></div></div>