Rust f32::atanh

Percentage Accurate: 99.8% → 99.8%
Time: 3.1s
Alternatives: 3
Speedup: N/A×

Specification

?
\[\begin{array}{l} \\ \tanh^{-1} x \end{array} \]
(FPCore (x) :precision binary32 (atanh x))
float code(float x) {
	return atanhf(x);
}
function code(x)
	return atanh(x)
end
function tmp = code(x)
	tmp = atanh(x);
end
\begin{array}{l}

\\
\tanh^{-1} x
\end{array}

Sampling outcomes in binary32 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 3 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \end{array} \]
(FPCore (x) :precision binary32 (* 0.5 (log1p (/ (* 2.0 x) (- 1.0 x)))))
float code(float x) {
	return 0.5f * log1pf(((2.0f * x) / (1.0f - x)));
}
function code(x)
	return Float32(Float32(0.5) * log1p(Float32(Float32(Float32(2.0) * x) / Float32(Float32(1.0) - x))))
end
\begin{array}{l}

\\
0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right)
\end{array}

Alternative 1: 99.8% accurate, N/A× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 1 - -1 \cdot x\\ 0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{t\_0} - \frac{\left(-1 \cdot x\right) \cdot x}{-1 \cdot t\_0}}\right) \end{array} \end{array} \]
(FPCore (x)
 :precision binary32
 (let* ((t_0 (- 1.0 (* -1.0 x))))
   (*
    0.5
    (log1p (/ (* 2.0 x) (- (/ 1.0 t_0) (/ (* (* -1.0 x) x) (* -1.0 t_0))))))))
float code(float x) {
	float t_0 = 1.0f - (-1.0f * x);
	return 0.5f * log1pf(((2.0f * x) / ((1.0f / t_0) - (((-1.0f * x) * x) / (-1.0f * t_0)))));
}
function code(x)
	t_0 = Float32(Float32(1.0) - Float32(Float32(-1.0) * x))
	return Float32(Float32(0.5) * log1p(Float32(Float32(Float32(2.0) * x) / Float32(Float32(Float32(1.0) / t_0) - Float32(Float32(Float32(Float32(-1.0) * x) * x) / Float32(Float32(-1.0) * t_0))))))
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 1 - -1 \cdot x\\
0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{t\_0} - \frac{\left(-1 \cdot x\right) \cdot x}{-1 \cdot t\_0}}\right)
\end{array}
\end{array}
Derivation
  1. Initial program 99.8%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\color{blue}{1 - x}}\right) \]
    2. flip--N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\color{blue}{\frac{1 \cdot 1 - x \cdot x}{1 + x}}}\right) \]
    3. metadata-evalN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{\color{blue}{1} - x \cdot x}{1 + x}}\right) \]
    4. unpow2N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1 - \color{blue}{{x}^{2}}}{1 + x}}\right) \]
    5. div-subN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\color{blue}{\frac{1}{1 + x} - \frac{{x}^{2}}{1 + x}}}\right) \]
    6. lower--.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\color{blue}{\frac{1}{1 + x} - \frac{{x}^{2}}{1 + x}}}\right) \]
    7. lower-/.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\color{blue}{\frac{1}{1 + x}} - \frac{{x}^{2}}{1 + x}}\right) \]
    8. *-lft-identityN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{1 + \color{blue}{1 \cdot x}} - \frac{{x}^{2}}{1 + x}}\right) \]
    9. metadata-evalN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{1 + \color{blue}{\left(\mathsf{neg}\left(-1\right)\right)} \cdot x} - \frac{{x}^{2}}{1 + x}}\right) \]
    10. fp-cancel-sub-sign-invN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{\color{blue}{1 - -1 \cdot x}} - \frac{{x}^{2}}{1 + x}}\right) \]
    11. lower--.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{\color{blue}{1 - -1 \cdot x}} - \frac{{x}^{2}}{1 + x}}\right) \]
    12. lower-*.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{1 - \color{blue}{-1 \cdot x}} - \frac{{x}^{2}}{1 + x}}\right) \]
    13. lower-/.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{1 - -1 \cdot x} - \color{blue}{\frac{{x}^{2}}{1 + x}}}\right) \]
    14. unpow2N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{1 - -1 \cdot x} - \frac{\color{blue}{x \cdot x}}{1 + x}}\right) \]
    15. lower-*.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{1 - -1 \cdot x} - \frac{\color{blue}{x \cdot x}}{1 + x}}\right) \]
    16. *-lft-identityN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{1 - -1 \cdot x} - \frac{x \cdot x}{1 + \color{blue}{1 \cdot x}}}\right) \]
    17. metadata-evalN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{1 - -1 \cdot x} - \frac{x \cdot x}{1 + \color{blue}{\left(\mathsf{neg}\left(-1\right)\right)} \cdot x}}\right) \]
    18. fp-cancel-sub-sign-invN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{1 - -1 \cdot x} - \frac{x \cdot x}{\color{blue}{1 - -1 \cdot x}}}\right) \]
    19. lower--.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{1 - -1 \cdot x} - \frac{x \cdot x}{\color{blue}{1 - -1 \cdot x}}}\right) \]
    20. lower-*.f3299.8

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{1 - -1 \cdot x} - \frac{x \cdot x}{1 - \color{blue}{-1 \cdot x}}}\right) \]
  4. Applied rewrites99.8%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\color{blue}{\frac{1}{1 - -1 \cdot x} - \frac{x \cdot x}{1 - -1 \cdot x}}}\right) \]
  5. Final simplification99.8%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{\frac{1}{1 - -1 \cdot x} - \frac{\left(-1 \cdot x\right) \cdot x}{-1 \cdot \left(1 - -1 \cdot x\right)}}\right) \]
  6. Add Preprocessing

Alternative 2: 99.7% accurate, N/A× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \end{array} \]
(FPCore (x) :precision binary32 (* 0.5 (log1p (/ (* 2.0 x) (- 1.0 x)))))
float code(float x) {
	return 0.5f * log1pf(((2.0f * x) / (1.0f - x)));
}
function code(x)
	return Float32(Float32(0.5) * log1p(Float32(Float32(Float32(2.0) * x) / Float32(Float32(1.0) - x))))
end
\begin{array}{l}

\\
0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right)
\end{array}
Derivation
  1. Initial program 99.8%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 3: 99.2% accurate, N/A× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(\left(x \cdot x\right) \cdot 0.2857142857142857 - -0.4, x \cdot x, 0.6666666666666666\right) \cdot x\\ t_1 := t\_0 \cdot x\\ t_2 := -1 \cdot t\_1\\ 0.5 \cdot \left(\frac{\mathsf{fma}\left({t\_0}^{3}, \left(x \cdot x\right) \cdot x, 8\right)}{\mathsf{fma}\left(t\_2, t\_2, 4 - t\_1 \cdot 2\right)} \cdot x\right) \end{array} \end{array} \]
(FPCore (x)
 :precision binary32
 (let* ((t_0
         (*
          (fma
           (- (* (* x x) 0.2857142857142857) -0.4)
           (* x x)
           0.6666666666666666)
          x))
        (t_1 (* t_0 x))
        (t_2 (* -1.0 t_1)))
   (*
    0.5
    (*
     (/
      (fma (pow t_0 3.0) (* (* x x) x) 8.0)
      (fma t_2 t_2 (- 4.0 (* t_1 2.0))))
     x))))
float code(float x) {
	float t_0 = fmaf((((x * x) * 0.2857142857142857f) - -0.4f), (x * x), 0.6666666666666666f) * x;
	float t_1 = t_0 * x;
	float t_2 = -1.0f * t_1;
	return 0.5f * ((fmaf(powf(t_0, 3.0f), ((x * x) * x), 8.0f) / fmaf(t_2, t_2, (4.0f - (t_1 * 2.0f)))) * x);
}
function code(x)
	t_0 = Float32(fma(Float32(Float32(Float32(x * x) * Float32(0.2857142857142857)) - Float32(-0.4)), Float32(x * x), Float32(0.6666666666666666)) * x)
	t_1 = Float32(t_0 * x)
	t_2 = Float32(Float32(-1.0) * t_1)
	return Float32(Float32(0.5) * Float32(Float32(fma((t_0 ^ Float32(3.0)), Float32(Float32(x * x) * x), Float32(8.0)) / fma(t_2, t_2, Float32(Float32(4.0) - Float32(t_1 * Float32(2.0))))) * x))
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\left(x \cdot x\right) \cdot 0.2857142857142857 - -0.4, x \cdot x, 0.6666666666666666\right) \cdot x\\
t_1 := t\_0 \cdot x\\
t_2 := -1 \cdot t\_1\\
0.5 \cdot \left(\frac{\mathsf{fma}\left({t\_0}^{3}, \left(x \cdot x\right) \cdot x, 8\right)}{\mathsf{fma}\left(t\_2, t\_2, 4 - t\_1 \cdot 2\right)} \cdot x\right)
\end{array}
\end{array}
Derivation
  1. Initial program 99.8%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(x \cdot \left(2 + {x}^{2} \cdot \left(\frac{2}{3} + {x}^{2} \cdot \left(\frac{2}{5} + \frac{2}{7} \cdot {x}^{2}\right)\right)\right)\right)} \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \frac{1}{2} \cdot \left(\left(2 + {x}^{2} \cdot \left(\frac{2}{3} + {x}^{2} \cdot \left(\frac{2}{5} + \frac{2}{7} \cdot {x}^{2}\right)\right)\right) \cdot \color{blue}{x}\right) \]
    2. lower-*.f32N/A

      \[\leadsto \frac{1}{2} \cdot \left(\left(2 + {x}^{2} \cdot \left(\frac{2}{3} + {x}^{2} \cdot \left(\frac{2}{5} + \frac{2}{7} \cdot {x}^{2}\right)\right)\right) \cdot \color{blue}{x}\right) \]
  5. Applied rewrites99.1%

    \[\leadsto 0.5 \cdot \color{blue}{\left(\mathsf{fma}\left(\mathsf{fma}\left(0.2857142857142857 \cdot \left(x \cdot x\right) - -0.4, x \cdot x, 0.6666666666666666\right), x \cdot x, 2\right) \cdot x\right)} \]
  6. Applied rewrites99.1%

    \[\leadsto 0.5 \cdot \left(\frac{\mathsf{fma}\left({\left(\mathsf{fma}\left(\left(x \cdot x\right) \cdot 0.2857142857142857 - -0.4, x \cdot x, 0.6666666666666666\right) \cdot x\right)}^{3}, \left(x \cdot x\right) \cdot x, 8\right)}{\mathsf{fma}\left(-\left(\mathsf{fma}\left(\left(x \cdot x\right) \cdot 0.2857142857142857 - -0.4, x \cdot x, 0.6666666666666666\right) \cdot x\right) \cdot x, -\left(\mathsf{fma}\left(\left(x \cdot x\right) \cdot 0.2857142857142857 - -0.4, x \cdot x, 0.6666666666666666\right) \cdot x\right) \cdot x, 4 - \left(\left(\mathsf{fma}\left(\left(x \cdot x\right) \cdot 0.2857142857142857 - -0.4, x \cdot x, 0.6666666666666666\right) \cdot x\right) \cdot x\right) \cdot 2\right)} \cdot x\right) \]
  7. Final simplification99.1%

    \[\leadsto 0.5 \cdot \left(\frac{\mathsf{fma}\left({\left(\mathsf{fma}\left(\left(x \cdot x\right) \cdot 0.2857142857142857 - -0.4, x \cdot x, 0.6666666666666666\right) \cdot x\right)}^{3}, \left(x \cdot x\right) \cdot x, 8\right)}{\mathsf{fma}\left(-1 \cdot \left(\left(\mathsf{fma}\left(\left(x \cdot x\right) \cdot 0.2857142857142857 - -0.4, x \cdot x, 0.6666666666666666\right) \cdot x\right) \cdot x\right), -1 \cdot \left(\left(\mathsf{fma}\left(\left(x \cdot x\right) \cdot 0.2857142857142857 - -0.4, x \cdot x, 0.6666666666666666\right) \cdot x\right) \cdot x\right), 4 - \left(\left(\mathsf{fma}\left(\left(x \cdot x\right) \cdot 0.2857142857142857 - -0.4, x \cdot x, 0.6666666666666666\right) \cdot x\right) \cdot x\right) \cdot 2\right)} \cdot x\right) \]
  8. Add Preprocessing

Reproduce

?
herbie shell --seed 2025065 
(FPCore (x)
  :name "Rust f32::atanh"
  :precision binary32
  (* 0.5 (log1p (/ (* 2.0 x) (- 1.0 x)))))