Beckmann Distribution sample, tan2theta, alphax == alphay

Percentage Accurate: 55.8% → 99.0%
Time: 10.3s
Alternatives: 13
Speedup: 10.5×

Specification

?
\[\left(0.0001 \leq \alpha \land \alpha \leq 1\right) \land \left(2.328306437 \cdot 10^{-10} \leq u0 \land u0 \leq 1\right)\]
\[\begin{array}{l} \\ \left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right) \end{array} \]
(FPCore (alpha u0)
 :precision binary32
 (* (* (- alpha) alpha) (log (- 1.0 u0))))
float code(float alpha, float u0) {
	return (-alpha * alpha) * logf((1.0f - u0));
}
real(4) function code(alpha, u0)
    real(4), intent (in) :: alpha
    real(4), intent (in) :: u0
    code = (-alpha * alpha) * log((1.0e0 - u0))
end function
function code(alpha, u0)
	return Float32(Float32(Float32(-alpha) * alpha) * log(Float32(Float32(1.0) - u0)))
end
function tmp = code(alpha, u0)
	tmp = (-alpha * alpha) * log((single(1.0) - u0));
end
\begin{array}{l}

\\
\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right)
\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 13 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: 55.8% accurate, 1.0× speedup?

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

\\
\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right)
\end{array}

Alternative 1: 99.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \alpha \cdot \left(\mathsf{log1p}\left(-u0\right) \cdot \left(-\alpha\right)\right) \end{array} \]
(FPCore (alpha u0) :precision binary32 (* alpha (* (log1p (- u0)) (- alpha))))
float code(float alpha, float u0) {
	return alpha * (log1pf(-u0) * -alpha);
}
function code(alpha, u0)
	return Float32(alpha * Float32(log1p(Float32(-u0)) * Float32(-alpha)))
end
\begin{array}{l}

\\
\alpha \cdot \left(\mathsf{log1p}\left(-u0\right) \cdot \left(-\alpha\right)\right)
\end{array}
Derivation
  1. Initial program 58.1%

    \[\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-log.f32N/A

      \[\leadsto \left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha\right) \cdot \color{blue}{\log \left(1 - u0\right)} \]
    2. lift--.f32N/A

      \[\leadsto \left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha\right) \cdot \log \color{blue}{\left(1 - u0\right)} \]
    3. sub-negN/A

      \[\leadsto \left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha\right) \cdot \log \color{blue}{\left(1 + \left(\mathsf{neg}\left(u0\right)\right)\right)} \]
    4. lower-log1p.f32N/A

      \[\leadsto \left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha\right) \cdot \color{blue}{\mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right)} \]
    5. lower-neg.f3298.9

      \[\leadsto \left(\left(-\alpha\right) \cdot \alpha\right) \cdot \mathsf{log1p}\left(\color{blue}{-u0}\right) \]
  4. Applied rewrites98.9%

    \[\leadsto \left(\left(-\alpha\right) \cdot \alpha\right) \cdot \color{blue}{\mathsf{log1p}\left(-u0\right)} \]
  5. Step-by-step derivation
    1. lift-neg.f32N/A

      \[\leadsto \left(\color{blue}{\left(\mathsf{neg}\left(\alpha\right)\right)} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    2. neg-mul-1N/A

      \[\leadsto \left(\color{blue}{\left(-1 \cdot \alpha\right)} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    3. metadata-evalN/A

      \[\leadsto \left(\left(\color{blue}{\left(\mathsf{neg}\left(1\right)\right)} \cdot \alpha\right) \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    4. lft-mult-inverseN/A

      \[\leadsto \left(\left(\left(\mathsf{neg}\left(\color{blue}{\frac{1}{\alpha} \cdot \alpha}\right)\right) \cdot \alpha\right) \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    5. lift-/.f32N/A

      \[\leadsto \left(\left(\left(\mathsf{neg}\left(\color{blue}{\frac{1}{\alpha}} \cdot \alpha\right)\right) \cdot \alpha\right) \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    6. *-commutativeN/A

      \[\leadsto \left(\left(\left(\mathsf{neg}\left(\color{blue}{\alpha \cdot \frac{1}{\alpha}}\right)\right) \cdot \alpha\right) \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    7. distribute-lft-neg-outN/A

      \[\leadsto \left(\left(\color{blue}{\left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \frac{1}{\alpha}\right)} \cdot \alpha\right) \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    8. lift-neg.f32N/A

      \[\leadsto \left(\left(\left(\color{blue}{\left(\mathsf{neg}\left(\alpha\right)\right)} \cdot \frac{1}{\alpha}\right) \cdot \alpha\right) \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    9. *-commutativeN/A

      \[\leadsto \left(\left(\color{blue}{\left(\frac{1}{\alpha} \cdot \left(\mathsf{neg}\left(\alpha\right)\right)\right)} \cdot \alpha\right) \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    10. associate-*r*N/A

      \[\leadsto \left(\color{blue}{\left(\frac{1}{\alpha} \cdot \left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha\right)\right)} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    11. lift-*.f32N/A

      \[\leadsto \left(\left(\frac{1}{\alpha} \cdot \color{blue}{\left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha\right)}\right) \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    12. *-commutativeN/A

      \[\leadsto \left(\color{blue}{\left(\left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha\right) \cdot \frac{1}{\alpha}\right)} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    13. lift-/.f32N/A

      \[\leadsto \left(\left(\left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha\right) \cdot \color{blue}{\frac{1}{\alpha}}\right) \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    14. div-invN/A

      \[\leadsto \left(\color{blue}{\frac{\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha}{\alpha}} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    15. clear-numN/A

      \[\leadsto \left(\color{blue}{\frac{1}{\frac{\alpha}{\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha}}} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    16. lower-/.f32N/A

      \[\leadsto \left(\color{blue}{\frac{1}{\frac{\alpha}{\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha}}} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    17. clear-numN/A

      \[\leadsto \left(\frac{1}{\color{blue}{\frac{1}{\frac{\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha}{\alpha}}}} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    18. metadata-evalN/A

      \[\leadsto \left(\frac{1}{\frac{\color{blue}{\mathsf{neg}\left(-1\right)}}{\frac{\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha}{\alpha}}} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    19. div-invN/A

      \[\leadsto \left(\frac{1}{\frac{\mathsf{neg}\left(-1\right)}{\color{blue}{\left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha\right) \cdot \frac{1}{\alpha}}}} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    20. lift-/.f32N/A

      \[\leadsto \left(\frac{1}{\frac{\mathsf{neg}\left(-1\right)}{\left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha\right) \cdot \color{blue}{\frac{1}{\alpha}}}} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    21. *-commutativeN/A

      \[\leadsto \left(\frac{1}{\frac{\mathsf{neg}\left(-1\right)}{\color{blue}{\frac{1}{\alpha} \cdot \left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha\right)}}} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    22. lift-*.f32N/A

      \[\leadsto \left(\frac{1}{\frac{\mathsf{neg}\left(-1\right)}{\frac{1}{\alpha} \cdot \color{blue}{\left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \alpha\right)}}} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    23. associate-*r*N/A

      \[\leadsto \left(\frac{1}{\frac{\mathsf{neg}\left(-1\right)}{\color{blue}{\left(\frac{1}{\alpha} \cdot \left(\mathsf{neg}\left(\alpha\right)\right)\right) \cdot \alpha}}} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
    24. *-commutativeN/A

      \[\leadsto \left(\frac{1}{\frac{\mathsf{neg}\left(-1\right)}{\color{blue}{\left(\left(\mathsf{neg}\left(\alpha\right)\right) \cdot \frac{1}{\alpha}\right)} \cdot \alpha}} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \]
  6. Applied rewrites98.7%

    \[\leadsto \left(\color{blue}{\frac{1}{\frac{-1}{\alpha}}} \cdot \alpha\right) \cdot \mathsf{log1p}\left(-u0\right) \]
  7. Step-by-step derivation
    1. lift-*.f32N/A

      \[\leadsto \color{blue}{\left(\frac{1}{\frac{-1}{\alpha}} \cdot \alpha\right) \cdot \mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right)} \]
    2. *-commutativeN/A

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right) \cdot \left(\frac{1}{\frac{-1}{\alpha}} \cdot \alpha\right)} \]
    3. lift-log1p.f32N/A

      \[\leadsto \color{blue}{\log \left(1 + \left(\mathsf{neg}\left(u0\right)\right)\right)} \cdot \left(\frac{1}{\frac{-1}{\alpha}} \cdot \alpha\right) \]
    4. lift-neg.f32N/A

      \[\leadsto \log \left(1 + \color{blue}{\left(\mathsf{neg}\left(u0\right)\right)}\right) \cdot \left(\frac{1}{\frac{-1}{\alpha}} \cdot \alpha\right) \]
    5. sub-negN/A

      \[\leadsto \log \color{blue}{\left(1 - u0\right)} \cdot \left(\frac{1}{\frac{-1}{\alpha}} \cdot \alpha\right) \]
    6. lift--.f32N/A

      \[\leadsto \log \color{blue}{\left(1 - u0\right)} \cdot \left(\frac{1}{\frac{-1}{\alpha}} \cdot \alpha\right) \]
    7. lift-log.f32N/A

      \[\leadsto \color{blue}{\log \left(1 - u0\right)} \cdot \left(\frac{1}{\frac{-1}{\alpha}} \cdot \alpha\right) \]
    8. lift-*.f32N/A

      \[\leadsto \log \left(1 - u0\right) \cdot \color{blue}{\left(\frac{1}{\frac{-1}{\alpha}} \cdot \alpha\right)} \]
    9. lift-/.f32N/A

      \[\leadsto \log \left(1 - u0\right) \cdot \left(\color{blue}{\frac{1}{\frac{-1}{\alpha}}} \cdot \alpha\right) \]
    10. lift-/.f32N/A

      \[\leadsto \log \left(1 - u0\right) \cdot \left(\frac{1}{\color{blue}{\frac{-1}{\alpha}}} \cdot \alpha\right) \]
    11. associate-/r/N/A

      \[\leadsto \log \left(1 - u0\right) \cdot \left(\color{blue}{\left(\frac{1}{-1} \cdot \alpha\right)} \cdot \alpha\right) \]
    12. metadata-evalN/A

      \[\leadsto \log \left(1 - u0\right) \cdot \left(\left(\color{blue}{-1} \cdot \alpha\right) \cdot \alpha\right) \]
    13. neg-mul-1N/A

      \[\leadsto \log \left(1 - u0\right) \cdot \left(\color{blue}{\left(\mathsf{neg}\left(\alpha\right)\right)} \cdot \alpha\right) \]
    14. associate-*r*N/A

      \[\leadsto \color{blue}{\left(\log \left(1 - u0\right) \cdot \left(\mathsf{neg}\left(\alpha\right)\right)\right) \cdot \alpha} \]
    15. lower-*.f32N/A

      \[\leadsto \color{blue}{\left(\log \left(1 - u0\right) \cdot \left(\mathsf{neg}\left(\alpha\right)\right)\right) \cdot \alpha} \]
    16. lower-*.f32N/A

      \[\leadsto \color{blue}{\left(\log \left(1 - u0\right) \cdot \left(\mathsf{neg}\left(\alpha\right)\right)\right)} \cdot \alpha \]
    17. lift-log.f32N/A

      \[\leadsto \left(\color{blue}{\log \left(1 - u0\right)} \cdot \left(\mathsf{neg}\left(\alpha\right)\right)\right) \cdot \alpha \]
    18. lift--.f32N/A

      \[\leadsto \left(\log \color{blue}{\left(1 - u0\right)} \cdot \left(\mathsf{neg}\left(\alpha\right)\right)\right) \cdot \alpha \]
    19. sub-negN/A

      \[\leadsto \left(\log \color{blue}{\left(1 + \left(\mathsf{neg}\left(u0\right)\right)\right)} \cdot \left(\mathsf{neg}\left(\alpha\right)\right)\right) \cdot \alpha \]
    20. lift-neg.f32N/A

      \[\leadsto \left(\log \left(1 + \color{blue}{\left(\mathsf{neg}\left(u0\right)\right)}\right) \cdot \left(\mathsf{neg}\left(\alpha\right)\right)\right) \cdot \alpha \]
    21. lift-log1p.f32N/A

      \[\leadsto \left(\color{blue}{\mathsf{log1p}\left(\mathsf{neg}\left(u0\right)\right)} \cdot \left(\mathsf{neg}\left(\alpha\right)\right)\right) \cdot \alpha \]
    22. lift-neg.f3299.0

      \[\leadsto \left(\mathsf{log1p}\left(-u0\right) \cdot \color{blue}{\left(-\alpha\right)}\right) \cdot \alpha \]
  8. Applied rewrites99.0%

    \[\leadsto \color{blue}{\left(\mathsf{log1p}\left(-u0\right) \cdot \left(-\alpha\right)\right) \cdot \alpha} \]
  9. Final simplification99.0%

    \[\leadsto \alpha \cdot \left(\mathsf{log1p}\left(-u0\right) \cdot \left(-\alpha\right)\right) \]
  10. Add Preprocessing

Alternative 2: 93.7% accurate, 2.6× speedup?

\[\begin{array}{l} \\ u0 \cdot \mathsf{fma}\left(\alpha, \alpha, \left(u0 \cdot \alpha\right) \cdot \mathsf{fma}\left(\alpha, 0.5, u0 \cdot \left(\alpha \cdot \mathsf{fma}\left(u0, 0.25, 0.3333333333333333\right)\right)\right)\right) \end{array} \]
(FPCore (alpha u0)
 :precision binary32
 (*
  u0
  (fma
   alpha
   alpha
   (*
    (* u0 alpha)
    (fma alpha 0.5 (* u0 (* alpha (fma u0 0.25 0.3333333333333333))))))))
float code(float alpha, float u0) {
	return u0 * fmaf(alpha, alpha, ((u0 * alpha) * fmaf(alpha, 0.5f, (u0 * (alpha * fmaf(u0, 0.25f, 0.3333333333333333f))))));
}
function code(alpha, u0)
	return Float32(u0 * fma(alpha, alpha, Float32(Float32(u0 * alpha) * fma(alpha, Float32(0.5), Float32(u0 * Float32(alpha * fma(u0, Float32(0.25), Float32(0.3333333333333333))))))))
end
\begin{array}{l}

\\
u0 \cdot \mathsf{fma}\left(\alpha, \alpha, \left(u0 \cdot \alpha\right) \cdot \mathsf{fma}\left(\alpha, 0.5, u0 \cdot \left(\alpha \cdot \mathsf{fma}\left(u0, 0.25, 0.3333333333333333\right)\right)\right)\right)
\end{array}
Derivation
  1. Initial program 55.8%

    \[\left(\left(-\alpha\right) \cdot \alpha\right) \cdot \log \left(1 - u0\right) \]
  2. Add Preprocessing
  3. Taylor expanded in u0 around 0

    \[\leadsto \color{blue}{u0 \cdot \left(u0 \cdot \left(\frac{1}{2} \cdot {\alpha}^{2} + u0 \cdot \left(\frac{1}{4} \cdot \left({\alpha}^{2} \cdot u0\right) + \frac{1}{3} \cdot {\alpha}^{2}\right)\right) + {\alpha}^{2}\right)} \]
  4. Step-by-step derivation
    1. lower-*.f32N/A

      \[\leadsto \color{blue}{u0 \cdot \left(u0 \cdot \left(\frac{1}{2} \cdot {\alpha}^{2} + u0 \cdot \left(\frac{1}{4} \cdot \left({\alpha}^{2} \cdot u0\right) + \frac{1}{3} \cdot {\alpha}^{2}\right)\right) + {\alpha}^{2}\right)} \]
    2. lower-fma.f32N/A

      \[\leadsto u0 \cdot \color{blue}{\mathsf{fma}\left(u0, \frac{1}{2} \cdot {\alpha}^{2} + u0 \cdot \left(\frac{1}{4} \cdot \left({\alpha}^{2} \cdot u0\right) + \frac{1}{3} \cdot {\alpha}^{2}\right), {\alpha}^{2}\right)} \]
  5. Applied rewrites93.5%

    \[\leadsto \color{blue}{u0 \cdot \mathsf{fma}\left(u0, \left(\alpha \cdot \alpha\right) \cdot \mathsf{fma}\left(u0, u0 \cdot 0.25, \mathsf{fma}\left(u0, 0.3333333333333333, 0.5\right)\right), \alpha \cdot \alpha\right)} \]
  6. Step-by-step derivation
    1. Applied rewrites93.7%

      \[\leadsto u0 \cdot \mathsf{fma}\left(\alpha, \color{blue}{\alpha}, \left(\alpha \cdot u0\right) \cdot \left(\alpha \cdot \mathsf{fma}\left(u0, \mathsf{fma}\left(u0, 0.25, 0.3333333333333333\right), 0.5\right)\right)\right) \]
    2. Step-by-step derivation
      1. Applied rewrites93.7%

        \[\leadsto u0 \cdot \mathsf{fma}\left(\alpha, \alpha, \left(\alpha \cdot u0\right) \cdot \mathsf{fma}\left(\alpha, 0.5, u0 \cdot \left(\mathsf{fma}\left(u0, 0.25, 0.3333333333333333\right) \cdot \alpha\right)\right)\right) \]
      2. Final simplification93.7%

        \[\leadsto u0 \cdot \mathsf{fma}\left(\alpha, \alpha, \left(u0 \cdot \alpha\right) \cdot \mathsf{fma}\left(\alpha, 0.5, u0 \cdot \left(\alpha \cdot \mathsf{fma}\left(u0, 0.25, 0.3333333333333333\right)\right)\right)\right) \]
      3. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024230 
      (FPCore (alpha u0)
        :name "Beckmann Distribution sample, tan2theta, alphax == alphay"
        :precision binary32
        :pre (and (and (<= 0.0001 alpha) (<= alpha 1.0)) (and (<= 2.328306437e-10 u0) (<= u0 1.0)))
        (* (* (- alpha) alpha) (log (- 1.0 u0))))