Rust f32::acosh

Percentage Accurate: 53.1% → 99.4%
Time: 6.6s
Alternatives: 4
Speedup: 1.2×

Specification

?
\[x \geq 1\]
\[\begin{array}{l} \\ \cosh^{-1} x \end{array} \]
(FPCore (x) :precision binary32 (acosh x))
float code(float x) {
	return acoshf(x);
}
function code(x)
	return acosh(x)
end
function tmp = code(x)
	tmp = acosh(x);
end
\begin{array}{l}

\\
\cosh^{-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 4 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: 53.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(x + \sqrt{x \cdot x - 1}\right) \end{array} \]
(FPCore (x) :precision binary32 (log (+ x (sqrt (- (* x x) 1.0)))))
float code(float x) {
	return logf((x + sqrtf(((x * x) - 1.0f))));
}
real(4) function code(x)
    real(4), intent (in) :: x
    code = log((x + sqrt(((x * x) - 1.0e0))))
end function
function code(x)
	return log(Float32(x + sqrt(Float32(Float32(x * x) - Float32(1.0)))))
end
function tmp = code(x)
	tmp = log((x + sqrt(((x * x) - single(1.0)))));
end
\begin{array}{l}

\\
\log \left(x + \sqrt{x \cdot x - 1}\right)
\end{array}

Alternative 1: 99.4% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \log \left(\mathsf{fma}\left(\sqrt{x + 1}, \sqrt{x + -1}, x\right)\right) \end{array} \]
(FPCore (x)
 :precision binary32
 (log (fma (sqrt (+ x 1.0)) (sqrt (+ x -1.0)) x)))
float code(float x) {
	return logf(fmaf(sqrtf((x + 1.0f)), sqrtf((x + -1.0f)), x));
}
function code(x)
	return log(fma(sqrt(Float32(x + Float32(1.0))), sqrt(Float32(x + Float32(-1.0))), x))
end
\begin{array}{l}

\\
\log \left(\mathsf{fma}\left(\sqrt{x + 1}, \sqrt{x + -1}, x\right)\right)
\end{array}
Derivation
  1. Initial program 55.4%

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-+.f32N/A

      \[\leadsto \log \color{blue}{\left(x + \sqrt{x \cdot x - 1}\right)} \]
    2. +-commutativeN/A

      \[\leadsto \log \color{blue}{\left(\sqrt{x \cdot x - 1} + x\right)} \]
    3. lift-sqrt.f32N/A

      \[\leadsto \log \left(\color{blue}{\sqrt{x \cdot x - 1}} + x\right) \]
    4. pow1/2N/A

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

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

      \[\leadsto \log \left({\left(\color{blue}{x \cdot x} - 1\right)}^{\frac{1}{2}} + x\right) \]
    7. difference-of-sqr-1N/A

      \[\leadsto \log \left({\color{blue}{\left(\left(x + 1\right) \cdot \left(x - 1\right)\right)}}^{\frac{1}{2}} + x\right) \]
    8. unpow-prod-downN/A

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

      \[\leadsto \log \color{blue}{\left(\mathsf{fma}\left({\left(x + 1\right)}^{\frac{1}{2}}, {\left(x - 1\right)}^{\frac{1}{2}}, x\right)\right)} \]
    10. pow1/2N/A

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

      \[\leadsto \log \left(\mathsf{fma}\left(\color{blue}{\sqrt{x + 1}}, {\left(x - 1\right)}^{\frac{1}{2}}, x\right)\right) \]
    12. lower-+.f32N/A

      \[\leadsto \log \left(\mathsf{fma}\left(\sqrt{\color{blue}{x + 1}}, {\left(x - 1\right)}^{\frac{1}{2}}, x\right)\right) \]
    13. pow1/2N/A

      \[\leadsto \log \left(\mathsf{fma}\left(\sqrt{x + 1}, \color{blue}{\sqrt{x - 1}}, x\right)\right) \]
    14. lower-sqrt.f32N/A

      \[\leadsto \log \left(\mathsf{fma}\left(\sqrt{x + 1}, \color{blue}{\sqrt{x - 1}}, x\right)\right) \]
    15. sub-negN/A

      \[\leadsto \log \left(\mathsf{fma}\left(\sqrt{x + 1}, \sqrt{\color{blue}{x + \left(\mathsf{neg}\left(1\right)\right)}}, x\right)\right) \]
    16. lower-+.f32N/A

      \[\leadsto \log \left(\mathsf{fma}\left(\sqrt{x + 1}, \sqrt{\color{blue}{x + \left(\mathsf{neg}\left(1\right)\right)}}, x\right)\right) \]
    17. metadata-eval98.9

      \[\leadsto \log \left(\mathsf{fma}\left(\sqrt{x + 1}, \sqrt{x + \color{blue}{-1}}, x\right)\right) \]
  4. Applied rewrites98.9%

    \[\leadsto \log \color{blue}{\left(\mathsf{fma}\left(\sqrt{x + 1}, \sqrt{x + -1}, x\right)\right)} \]
  5. Add Preprocessing

Alternative 2: 98.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(x + \left(x + \frac{-0.5}{x}\right)\right) \end{array} \]
(FPCore (x) :precision binary32 (log (+ x (+ x (/ -0.5 x)))))
float code(float x) {
	return logf((x + (x + (-0.5f / x))));
}
real(4) function code(x)
    real(4), intent (in) :: x
    code = log((x + (x + ((-0.5e0) / x))))
end function
function code(x)
	return log(Float32(x + Float32(x + Float32(Float32(-0.5) / x))))
end
function tmp = code(x)
	tmp = log((x + (x + (single(-0.5) / x))));
end
\begin{array}{l}

\\
\log \left(x + \left(x + \frac{-0.5}{x}\right)\right)
\end{array}
Derivation
  1. Initial program 53.1%

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf

    \[\leadsto \log \left(x + \color{blue}{x \cdot \left(1 - \frac{1}{2} \cdot \frac{1}{{x}^{2}}\right)}\right) \]
  4. Step-by-step derivation
    1. sub-negN/A

      \[\leadsto \log \left(x + x \cdot \color{blue}{\left(1 + \left(\mathsf{neg}\left(\frac{1}{2} \cdot \frac{1}{{x}^{2}}\right)\right)\right)}\right) \]
    2. distribute-lft-inN/A

      \[\leadsto \log \left(x + \color{blue}{\left(x \cdot 1 + x \cdot \left(\mathsf{neg}\left(\frac{1}{2} \cdot \frac{1}{{x}^{2}}\right)\right)\right)}\right) \]
    3. *-rgt-identityN/A

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

      \[\leadsto \log \left(x + \color{blue}{\left(x + x \cdot \left(\mathsf{neg}\left(\frac{1}{2} \cdot \frac{1}{{x}^{2}}\right)\right)\right)}\right) \]
    5. *-commutativeN/A

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

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

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

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

      \[\leadsto \log \left(x + \left(x + \color{blue}{\frac{x \cdot 1}{{x}^{2}}} \cdot \frac{-1}{2}\right)\right) \]
    10. *-rgt-identityN/A

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

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

      \[\leadsto \log \left(x + \left(x + \color{blue}{\frac{\frac{x}{x}}{x}} \cdot \frac{-1}{2}\right)\right) \]
    13. *-inversesN/A

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

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

      \[\leadsto \log \left(x + \left(x + \frac{\color{blue}{\frac{-1}{2}}}{x}\right)\right) \]
    16. lower-/.f3298.5

      \[\leadsto \log \left(x + \left(x + \color{blue}{\frac{-0.5}{x}}\right)\right) \]
  5. Applied rewrites98.5%

    \[\leadsto \log \left(x + \color{blue}{\left(x + \frac{-0.5}{x}\right)}\right) \]
  6. Add Preprocessing

Developer Target 1: 99.3% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \log \left(x + \sqrt{x - 1} \cdot \sqrt{x + 1}\right) \end{array} \]
(FPCore (x)
 :precision binary32
 (log (+ x (* (sqrt (- x 1.0)) (sqrt (+ x 1.0))))))
float code(float x) {
	return logf((x + (sqrtf((x - 1.0f)) * sqrtf((x + 1.0f)))));
}
real(4) function code(x)
    real(4), intent (in) :: x
    code = log((x + (sqrt((x - 1.0e0)) * sqrt((x + 1.0e0)))))
end function
function code(x)
	return log(Float32(x + Float32(sqrt(Float32(x - Float32(1.0))) * sqrt(Float32(x + Float32(1.0))))))
end
function tmp = code(x)
	tmp = log((x + (sqrt((x - single(1.0))) * sqrt((x + single(1.0))))));
end
\begin{array}{l}

\\
\log \left(x + \sqrt{x - 1} \cdot \sqrt{x + 1}\right)
\end{array}

Reproduce

?
herbie shell --seed 2024230 
(FPCore (x)
  :name "Rust f32::acosh"
  :precision binary32
  :pre (>= x 1.0)

  :alt
  (! :herbie-platform default (log (+ x (* (sqrt (- x 1)) (sqrt (+ x 1))))))

  (log (+ x (sqrt (- (* x x) 1.0)))))