Rust f32::atanh

Percentage Accurate: 92.9% → 97.0%
Time: 5.4s
Alternatives: 2
Speedup: 125.0×

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 2 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: 92.9% 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: 97.0% accurate, 125.0× speedup?

\[\begin{array}{l} \\ x \end{array} \]
(FPCore (x) :precision binary32 x)
float code(float x) {
	return x;
}
real(4) function code(x)
    real(4), intent (in) :: x
    code = x
end function
function code(x)
	return x
end
function tmp = code(x)
	tmp = x;
end
\begin{array}{l}

\\
x
\end{array}
Derivation
  1. Initial program 91.4%

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

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)}{1 - {x}^{3}}}\right) \]
  4. Step-by-step derivation
    1. lift-/.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\color{blue}{\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)}{1 - {x}^{3}}}\right) \]
    2. lift--.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)}{\color{blue}{1 - {x}^{3}}}\right) \]
    3. sub-negN/A

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

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)}{\color{blue}{\left(\mathsf{neg}\left(-1\right)\right)} + \left(\mathsf{neg}\left({x}^{3}\right)\right)}\right) \]
    5. distribute-neg-inN/A

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

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)}{\mathsf{neg}\left(\color{blue}{\left(-1 + {x}^{3}\right)}\right)}\right) \]
    7. un-div-invN/A

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

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)\right) \cdot \frac{\color{blue}{\mathsf{neg}\left(-1\right)}}{\mathsf{neg}\left(\left(-1 + {x}^{3}\right)\right)}\right) \]
    9. frac-2negN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)\right) \cdot \color{blue}{\frac{-1}{-1 + {x}^{3}}}\right) \]
    10. associate-*r/N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\color{blue}{\frac{\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)\right) \cdot -1}{-1 + {x}^{3}}}\right) \]
    11. *-commutativeN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\color{blue}{-1 \cdot \left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)\right)}}{-1 + {x}^{3}}\right) \]
    12. neg-mul-1N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\color{blue}{\mathsf{neg}\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)\right)}}{-1 + {x}^{3}}\right) \]
    13. lift-*.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{neg}\left(\color{blue}{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)}\right)}{-1 + {x}^{3}}\right) \]
    14. lift-*.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{neg}\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \color{blue}{\left(x \cdot 2\right)}\right)}{-1 + {x}^{3}}\right) \]
    15. *-commutativeN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{neg}\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \color{blue}{\left(2 \cdot x\right)}\right)}{-1 + {x}^{3}}\right) \]
    16. lift-*.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{neg}\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \color{blue}{\left(2 \cdot x\right)}\right)}{-1 + {x}^{3}}\right) \]
    17. distribute-rgt-neg-inN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\color{blue}{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(\mathsf{neg}\left(2 \cdot x\right)\right)}}{-1 + {x}^{3}}\right) \]
    18. lift-+.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(\mathsf{neg}\left(2 \cdot x\right)\right)}{\color{blue}{-1 + {x}^{3}}}\right) \]
    19. +-commutativeN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(\mathsf{neg}\left(2 \cdot x\right)\right)}{\color{blue}{{x}^{3} + -1}}\right) \]
    20. lift-pow.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(\mathsf{neg}\left(2 \cdot x\right)\right)}{\color{blue}{{x}^{3}} + -1}\right) \]
    21. sqr-powN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(\mathsf{neg}\left(2 \cdot x\right)\right)}{\color{blue}{{x}^{\left(\frac{3}{2}\right)} \cdot {x}^{\left(\frac{3}{2}\right)}} + -1}\right) \]
  5. Applied rewrites22.2%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{\frac{\mathsf{fma}\left(1 - x, x, 1\right)}{{x}^{1.5} + 1} \cdot \frac{-2 \cdot x}{{x}^{1.5} - 1}}\right) \]
  6. Taylor expanded in x around 0

    \[\leadsto \color{blue}{x \cdot \left(1 + \frac{1}{4} \cdot \left(x \cdot \left(4 \cdot \left(1 + \left(\sqrt{x} + -1 \cdot \sqrt{x}\right)\right) - 4\right)\right)\right)} \]
  7. Step-by-step derivation
    1. distribute-rgt-inN/A

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

      \[\leadsto \color{blue}{x} + \left(\frac{1}{4} \cdot \left(x \cdot \left(4 \cdot \left(1 + \left(\sqrt{x} + -1 \cdot \sqrt{x}\right)\right) - 4\right)\right)\right) \cdot x \]
    3. associate-*r*N/A

      \[\leadsto x + \color{blue}{\left(\left(\frac{1}{4} \cdot x\right) \cdot \left(4 \cdot \left(1 + \left(\sqrt{x} + -1 \cdot \sqrt{x}\right)\right) - 4\right)\right)} \cdot x \]
    4. associate-*l*N/A

      \[\leadsto x + \color{blue}{\left(\frac{1}{4} \cdot x\right) \cdot \left(\left(4 \cdot \left(1 + \left(\sqrt{x} + -1 \cdot \sqrt{x}\right)\right) - 4\right) \cdot x\right)} \]
    5. *-commutativeN/A

      \[\leadsto x + \color{blue}{\left(x \cdot \frac{1}{4}\right)} \cdot \left(\left(4 \cdot \left(1 + \left(\sqrt{x} + -1 \cdot \sqrt{x}\right)\right) - 4\right) \cdot x\right) \]
    6. *-commutativeN/A

      \[\leadsto x + \left(x \cdot \frac{1}{4}\right) \cdot \color{blue}{\left(x \cdot \left(4 \cdot \left(1 + \left(\sqrt{x} + -1 \cdot \sqrt{x}\right)\right) - 4\right)\right)} \]
    7. associate-*r*N/A

      \[\leadsto x + \color{blue}{x \cdot \left(\frac{1}{4} \cdot \left(x \cdot \left(4 \cdot \left(1 + \left(\sqrt{x} + -1 \cdot \sqrt{x}\right)\right) - 4\right)\right)\right)} \]
    8. associate-*r*N/A

      \[\leadsto x + x \cdot \color{blue}{\left(\left(\frac{1}{4} \cdot x\right) \cdot \left(4 \cdot \left(1 + \left(\sqrt{x} + -1 \cdot \sqrt{x}\right)\right) - 4\right)\right)} \]
    9. associate-*r*N/A

      \[\leadsto x + \color{blue}{\left(x \cdot \left(\frac{1}{4} \cdot x\right)\right) \cdot \left(4 \cdot \left(1 + \left(\sqrt{x} + -1 \cdot \sqrt{x}\right)\right) - 4\right)} \]
  8. Applied rewrites97.1%

    \[\leadsto \color{blue}{x} \]
  9. Add Preprocessing

Alternative 2: 7.7% accurate, 125.0× speedup?

\[\begin{array}{l} \\ 1 \end{array} \]
(FPCore (x) :precision binary32 1.0)
float code(float x) {
	return 1.0f;
}
real(4) function code(x)
    real(4), intent (in) :: x
    code = 1.0e0
end function
function code(x)
	return Float32(1.0)
end
function tmp = code(x)
	tmp = single(1.0);
end
\begin{array}{l}

\\
1
\end{array}
Derivation
  1. Initial program 92.2%

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

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)}{1 - {x}^{3}}}\right) \]
  4. Step-by-step derivation
    1. lift-/.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\color{blue}{\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)}{1 - {x}^{3}}}\right) \]
    2. lift--.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)}{\color{blue}{1 - {x}^{3}}}\right) \]
    3. sub-negN/A

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

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)}{\color{blue}{\left(\mathsf{neg}\left(-1\right)\right)} + \left(\mathsf{neg}\left({x}^{3}\right)\right)}\right) \]
    5. distribute-neg-inN/A

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

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)}{\mathsf{neg}\left(\color{blue}{\left(-1 + {x}^{3}\right)}\right)}\right) \]
    7. un-div-invN/A

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

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)\right) \cdot \frac{\color{blue}{\mathsf{neg}\left(-1\right)}}{\mathsf{neg}\left(\left(-1 + {x}^{3}\right)\right)}\right) \]
    9. frac-2negN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)\right) \cdot \color{blue}{\frac{-1}{-1 + {x}^{3}}}\right) \]
    10. associate-*r/N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\color{blue}{\frac{\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)\right) \cdot -1}{-1 + {x}^{3}}}\right) \]
    11. *-commutativeN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\color{blue}{-1 \cdot \left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)\right)}}{-1 + {x}^{3}}\right) \]
    12. neg-mul-1N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\color{blue}{\mathsf{neg}\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)\right)}}{-1 + {x}^{3}}\right) \]
    13. lift-*.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{neg}\left(\color{blue}{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(x \cdot 2\right)}\right)}{-1 + {x}^{3}}\right) \]
    14. lift-*.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{neg}\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \color{blue}{\left(x \cdot 2\right)}\right)}{-1 + {x}^{3}}\right) \]
    15. *-commutativeN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{neg}\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \color{blue}{\left(2 \cdot x\right)}\right)}{-1 + {x}^{3}}\right) \]
    16. lift-*.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{neg}\left(\mathsf{fma}\left(1 - x, x, 1\right) \cdot \color{blue}{\left(2 \cdot x\right)}\right)}{-1 + {x}^{3}}\right) \]
    17. distribute-rgt-neg-inN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\color{blue}{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(\mathsf{neg}\left(2 \cdot x\right)\right)}}{-1 + {x}^{3}}\right) \]
    18. lift-+.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(\mathsf{neg}\left(2 \cdot x\right)\right)}{\color{blue}{-1 + {x}^{3}}}\right) \]
    19. +-commutativeN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(\mathsf{neg}\left(2 \cdot x\right)\right)}{\color{blue}{{x}^{3} + -1}}\right) \]
    20. lift-pow.f32N/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(\mathsf{neg}\left(2 \cdot x\right)\right)}{\color{blue}{{x}^{3}} + -1}\right) \]
    21. sqr-powN/A

      \[\leadsto \frac{1}{2} \cdot \mathsf{log1p}\left(\frac{\mathsf{fma}\left(1 - x, x, 1\right) \cdot \left(\mathsf{neg}\left(2 \cdot x\right)\right)}{\color{blue}{{x}^{\left(\frac{3}{2}\right)} \cdot {x}^{\left(\frac{3}{2}\right)}} + -1}\right) \]
  5. Applied rewrites21.2%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{\frac{\mathsf{fma}\left(1 - x, x, 1\right)}{{x}^{1.5} + 1} \cdot \frac{-2 \cdot x}{{x}^{1.5} - 1}}\right) \]
  6. Taylor expanded in x around -inf

    \[\leadsto \color{blue}{\frac{-1}{{\left(\sqrt{-1}\right)}^{2}}} \]
  7. Step-by-step derivation
    1. unpow2N/A

      \[\leadsto \frac{-1}{\color{blue}{\sqrt{-1} \cdot \sqrt{-1}}} \]
    2. rem-square-sqrtN/A

      \[\leadsto \frac{-1}{\color{blue}{-1}} \]
    3. metadata-eval7.5

      \[\leadsto \color{blue}{1} \]
  8. Applied rewrites7.5%

    \[\leadsto \color{blue}{1} \]
  9. Add Preprocessing

Reproduce

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