[Mesa-dev] [PATCH 1/2] glsl: Use a simpler formula for tanh
Kenneth Graunke
kenneth at whitecape.org
Fri Dec 9 18:25:30 UTC 2016
On Friday, December 9, 2016 9:41:51 AM PST Jason Ekstrand wrote:
> The formula we have used in the past is a trivial reduction from the
> definition by simply multiplying both the numerator and denominator of the
> formula by 2. However, multiplying by e^x, you can further reduce it.
> This allows us to get rid of one side of the clamp and two of exponential
> functions which should make it faster. The new formula still passes the
> dEQP precision tests for tanh so it should be fine.
> ---
> src/compiler/glsl/builtin_functions.cpp | 18 ++++++++++--------
> 1 file changed, 10 insertions(+), 8 deletions(-)
>
> diff --git a/src/compiler/glsl/builtin_functions.cpp b/src/compiler/glsl/builtin_functions.cpp
> index 3dead1a..94e8279 100644
> --- a/src/compiler/glsl/builtin_functions.cpp
> +++ b/src/compiler/glsl/builtin_functions.cpp
> @@ -3563,17 +3563,19 @@ builtin_builder::_tanh(const glsl_type *type)
> ir_variable *x = in_var(type, "x");
> MAKE_SIG(type, v130, 1, x);
>
> - /* Clamp x to [-10, +10] to avoid precision problems.
> - * 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.
> + /* tanh(x) := (0.5 * (e^x - e^(-x))) / (0.5 * (e^x + e^(-x)))
> + *
> + * With a little algebra this reduces to (e^2x - 1) / (e^2x + 1)
> + *
> + * Clamp x to (-inf, +10] to avoid precision problems. When x > 10, e^x is
> + * so much larger than 1.0 that 1.0 gets flushed to zero in the computation
> + * e^x +- 1 so it can be ignored.
e^2x (you say e^x here and e^2x in the spirv patch). I'd also normally
write +/- instead of +-.
Both are
Reviewed-by: Kenneth Graunke <kenneth at whitecape.org>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20161209/36e16ecb/attachment.sig>
More information about the mesa-dev
mailing list