Rust f32::asinh

Percentage Accurate: 38.1% → 99.5%
Time: 3.5s
Alternatives: 2
Speedup: 2.1×

Specification

?
\[\sinh^{-1} x \]
(FPCore (x)
  :precision binary32
  (asinh x))
float code(float x) {
	return asinhf(x);
}
function code(x)
	return asinh(x)
end
function tmp = code(x)
	tmp = asinh(x);
end
\sinh^{-1} x

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: 38.1% accurate, 1.0× speedup?

\[\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \]
(FPCore (x)
  :precision binary32
  (copysign (log (+ (fabs x) (sqrt (+ (* x x) 1.0)))) x))
float code(float x) {
	return copysignf(logf((fabsf(x) + sqrtf(((x * x) + 1.0f)))), x);
}
function code(x)
	return copysign(log(Float32(abs(x) + sqrt(Float32(Float32(x * x) + Float32(1.0))))), x)
end
function tmp = code(x)
	tmp = sign(x) * abs(log((abs(x) + sqrt(((x * x) + single(1.0))))));
end
\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right)

Alternative 1: 99.5% accurate, 1.1× speedup?

\[\mathsf{copysign}\left(\sinh^{-1} \left(\left|x\right|\right), x\right) \]
(FPCore (x)
  :precision binary32
  (copysign (asinh (fabs x)) x))
float code(float x) {
	return copysignf(asinhf(fabsf(x)), x);
}
function code(x)
	return copysign(asinh(abs(x)), x)
end
function tmp = code(x)
	tmp = sign(x) * abs(asinh(abs(x)));
end
\mathsf{copysign}\left(\sinh^{-1} \left(\left|x\right|\right), x\right)
Derivation
  1. Initial program 38.1%

    \[\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \]
  2. Step-by-step derivation
    1. lift-log.f32N/A

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

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

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

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{\color{blue}{x \cdot x + 1}}\right), x\right) \]
    5. lift-*.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{\color{blue}{x \cdot x} + 1}\right), x\right) \]
    6. sqr-abs-revN/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{\color{blue}{\left|x\right| \cdot \left|x\right|} + 1}\right), x\right) \]
    7. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{\color{blue}{\left|x\right|} \cdot \left|x\right| + 1}\right), x\right) \]
    8. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{\left|x\right| \cdot \color{blue}{\left|x\right|} + 1}\right), x\right) \]
    9. asinh-def-revN/A

      \[\leadsto \mathsf{copysign}\left(\color{blue}{\sinh^{-1} \left(\left|x\right|\right)}, x\right) \]
    10. lower-asinh.f3299.5%

      \[\leadsto \mathsf{copysign}\left(\color{blue}{\sinh^{-1} \left(\left|x\right|\right)}, x\right) \]
  3. Applied rewrites99.5%

    \[\leadsto \mathsf{copysign}\left(\color{blue}{\sinh^{-1} \left(\left|x\right|\right)}, x\right) \]
  4. Add Preprocessing

Alternative 2: 54.8% accurate, 2.1× speedup?

\[\mathsf{copysign}\left(-1 \cdot x, x\right) \]
(FPCore (x)
  :precision binary32
  (copysign (* -1.0 x) x))
float code(float x) {
	return copysignf((-1.0f * x), x);
}
function code(x)
	return copysign(Float32(Float32(-1.0) * x), x)
end
function tmp = code(x)
	tmp = sign(x) * abs((single(-1.0) * x));
end
\mathsf{copysign}\left(-1 \cdot x, x\right)
Derivation
  1. Initial program 38.1%

    \[\mathsf{copysign}\left(\log \left(\left|x\right| + \sqrt{x \cdot x + 1}\right), x\right) \]
  2. Taylor expanded in x around -inf

    \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \color{blue}{-1 \cdot x}\right), x\right) \]
  3. Step-by-step derivation
    1. lower-*.f3229.5%

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + -1 \cdot \color{blue}{x}\right), x\right) \]
  4. Applied rewrites29.5%

    \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \color{blue}{-1 \cdot x}\right), x\right) \]
  5. Step-by-step derivation
    1. lift-*.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + -1 \cdot \color{blue}{x}\right), x\right) \]
    2. mul-1-negN/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \left(\mathsf{neg}\left(x\right)\right)\right), x\right) \]
    3. lower-neg.f3229.5%

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \left(-x\right)\right), x\right) \]
  6. Applied rewrites29.5%

    \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| + \left(-x\right)\right), x\right) \]
  7. Step-by-step derivation
    1. lift-+.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \color{blue}{\left(\left|x\right| + \left(-x\right)\right)}, x\right) \]
    2. +-commutativeN/A

      \[\leadsto \mathsf{copysign}\left(\log \color{blue}{\left(\left(-x\right) + \left|x\right|\right)}, x\right) \]
    3. add-flipN/A

      \[\leadsto \mathsf{copysign}\left(\log \color{blue}{\left(\left(-x\right) - \left(\mathsf{neg}\left(\left|x\right|\right)\right)\right)}, x\right) \]
    4. lower--.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \color{blue}{\left(\left(-x\right) - \left(\mathsf{neg}\left(\left|x\right|\right)\right)\right)}, x\right) \]
    5. rem-square-sqrtN/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\sqrt{-x} \cdot \color{blue}{\sqrt{-x}} - \left(\mathsf{neg}\left(\left|x\right|\right)\right)\right), x\right) \]
    6. sqrt-unprodN/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\sqrt{\left(-x\right) \cdot \left(-x\right)} - \left(\mathsf{neg}\left(\left|x\right|\right)\right)\right), x\right) \]
    7. lift-neg.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\sqrt{\left(\mathsf{neg}\left(x\right)\right) \cdot \left(-x\right)} - \left(\mathsf{neg}\left(\left|x\right|\right)\right)\right), x\right) \]
    8. lift-neg.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\sqrt{\left(\mathsf{neg}\left(x\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} - \left(\mathsf{neg}\left(\left|x\right|\right)\right)\right), x\right) \]
    9. sqr-neg-revN/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\sqrt{x \cdot x} - \left(\mathsf{neg}\left(\left|x\right|\right)\right)\right), x\right) \]
    10. rem-sqrt-square-revN/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \left(\mathsf{neg}\left(\left|x\right|\right)\right)\right), x\right) \]
    11. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \left(\mathsf{neg}\left(\left|x\right|\right)\right)\right), x\right) \]
    12. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \left(\mathsf{neg}\left(\mathsf{Rewrite<=}\left(*-rgt-identity, \left(\left|x\right| \cdot 1\right)\right)\right)\right)\right), x\right) \]
    13. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \mathsf{Rewrite<=}\left(distribute-rgt-neg-out, \left(\left|x\right| \cdot \left(\mathsf{neg}\left(1\right)\right)\right)\right)\right), x\right) \]
    14. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \left|x\right| \cdot \mathsf{Rewrite=>}\left(metadata-eval, -1\right)\right), x\right) \]
    15. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \mathsf{Rewrite=>}\left(lift-fabs.f32, \left(\left|x\right|\right)\right) \cdot -1\right), x\right) \]
    16. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \mathsf{Rewrite=>}\left(rem-sqrt-square-rev, \left(\sqrt{x \cdot x}\right)\right) \cdot -1\right), x\right) \]
    17. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \sqrt{\mathsf{Rewrite=>}\left(sqr-neg-rev, \left(\left(\mathsf{neg}\left(x\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)\right)\right)} \cdot -1\right), x\right) \]
    18. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \sqrt{\mathsf{Rewrite<=}\left(lift-neg.f32, \left(-x\right)\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} \cdot -1\right), x\right) \]
    19. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \sqrt{\left(-x\right) \cdot \mathsf{Rewrite<=}\left(lift-neg.f32, \left(-x\right)\right)} \cdot -1\right), x\right) \]
    20. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \mathsf{Rewrite<=}\left(sqrt-unprod, \left(\sqrt{-x} \cdot \sqrt{-x}\right)\right) \cdot -1\right), x\right) \]
    21. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \mathsf{Rewrite=>}\left(rem-square-sqrt, \left(-x\right)\right) \cdot -1\right), x\right) \]
    22. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \mathsf{Rewrite=>}\left(lift-neg.f32, \left(\mathsf{neg}\left(x\right)\right)\right) \cdot -1\right), x\right) \]
    23. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \mathsf{Rewrite=>}\left(distribute-lft-neg-out, \left(\mathsf{neg}\left(x \cdot -1\right)\right)\right)\right), x\right) \]
    24. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \mathsf{Rewrite<=}\left(distribute-rgt-neg-out, \left(x \cdot \left(\mathsf{neg}\left(-1\right)\right)\right)\right)\right), x\right) \]
    25. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - x \cdot \mathsf{Rewrite=>}\left(metadata-eval, 1\right)\right), x\right) \]
    26. lift-fabs.f32N/A

      \[\leadsto \mathsf{copysign}\left(\log \left(\left|x\right| - \mathsf{Rewrite=>}\left(*-rgt-identity, x\right)\right), x\right) \]
  8. Applied rewrites29.5%

    \[\leadsto \mathsf{copysign}\left(\log \color{blue}{\left(\left|x\right| - x\right)}, x\right) \]
  9. Taylor expanded in x around 0

    \[\leadsto \mathsf{copysign}\left(\color{blue}{-1 \cdot x}, x\right) \]
  10. Step-by-step derivation
    1. lower-*.f3254.8%

      \[\leadsto \mathsf{copysign}\left(-1 \cdot \color{blue}{x}, x\right) \]
  11. Applied rewrites54.8%

    \[\leadsto \mathsf{copysign}\left(\color{blue}{-1 \cdot x}, x\right) \]
  12. Add Preprocessing

Reproduce

?
herbie shell --seed 2025258 
(FPCore (x)
  :name "Rust f32::asinh"
  :precision binary32
  (copysign (log (+ (fabs x) (sqrt (+ (* x x) 1.0)))) x))