Beckmann Sample, near normal, slope_x

Percentage Accurate: 57.4% → 91.5%
Time: 9.4s
Alternatives: 6
Speedup: 14.4×

Specification

?
\[\left(\left(cosTheta\_i > 0.9999 \land cosTheta\_i \leq 1\right) \land \left(2.328306437 \cdot 10^{-10} \leq u1 \land u1 \leq 1\right)\right) \land \left(2.328306437 \cdot 10^{-10} \leq u2 \land u2 \leq 1\right)\]
\[\begin{array}{l} \\ \sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 (PI)) u2))))
\begin{array}{l}

\\
\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\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 6 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: 57.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 (PI)) u2))))
\begin{array}{l}

\\
\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right)
\end{array}

Alternative 1: 91.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right)\\ \mathbf{if}\;1 - u1 \leq 0.9998400211334229:\\ \;\;\;\;t\_0 \cdot \sqrt{-\log \left(1 - u1\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\left(-\left(-u1\right)\right) - \left(-u1\right) \cdot u1} \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (cos (* u2 (* (PI) 2.0)))))
   (if (<= (- 1.0 u1) 0.9998400211334229)
     (* t_0 (sqrt (- (log (- 1.0 u1)))))
     (* (sqrt (- (- (- u1)) (* (- u1) u1))) t_0))))
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right)\\
\mathbf{if}\;1 - u1 \leq 0.9998400211334229:\\
\;\;\;\;t\_0 \cdot \sqrt{-\log \left(1 - u1\right)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\left(-\left(-u1\right)\right) - \left(-u1\right) \cdot u1} \cdot t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f32 #s(literal 1 binary32) u1) < 0.999840021

    1. Initial program 91.2%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    2. Add Preprocessing

    if 0.999840021 < (-.f32 #s(literal 1 binary32) u1)

    1. Initial program 33.1%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    2. Add Preprocessing
    3. Applied rewrites20.5%

      \[\leadsto \sqrt{-\color{blue}{\left(\mathsf{log1p}\left(\left(-u1\right) \cdot u1\right) + \log \left(\frac{e^{-\mathsf{log1p}\left(u1\right)}}{1}\right)\right)}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    4. Taylor expanded in u1 around 0

      \[\leadsto \sqrt{-\left(\mathsf{log1p}\left(\left(-u1\right) \cdot u1\right) + \color{blue}{-1 \cdot u1}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    5. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \sqrt{-\left(\mathsf{log1p}\left(\left(-u1\right) \cdot u1\right) + \color{blue}{\left(\mathsf{neg}\left(u1\right)\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      2. lower-neg.f3257.9

        \[\leadsto \sqrt{-\left(\mathsf{log1p}\left(\left(-u1\right) \cdot u1\right) + \color{blue}{\left(-u1\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    6. Applied rewrites57.9%

      \[\leadsto \sqrt{-\left(\mathsf{log1p}\left(\left(-u1\right) \cdot u1\right) + \color{blue}{\left(-u1\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    7. Taylor expanded in u1 around 0

      \[\leadsto \sqrt{-\left(\color{blue}{-1 \cdot {u1}^{2}} + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    8. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto \sqrt{-\left(-1 \cdot \color{blue}{\left(u1 \cdot u1\right)} + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      2. associate-*r*N/A

        \[\leadsto \sqrt{-\left(\color{blue}{\left(-1 \cdot u1\right) \cdot u1} + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      3. lower-*.f32N/A

        \[\leadsto \sqrt{-\left(\color{blue}{\left(-1 \cdot u1\right) \cdot u1} + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      4. mul-1-negN/A

        \[\leadsto \sqrt{-\left(\color{blue}{\left(\mathsf{neg}\left(u1\right)\right)} \cdot u1 + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      5. lower-neg.f3293.1

        \[\leadsto \sqrt{-\left(\color{blue}{\left(-u1\right)} \cdot u1 + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    9. Applied rewrites93.1%

      \[\leadsto \sqrt{-\left(\color{blue}{\left(-u1\right) \cdot u1} + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification92.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;1 - u1 \leq 0.9998400211334229:\\ \;\;\;\;\cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right) \cdot \sqrt{-\log \left(1 - u1\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\left(-\left(-u1\right)\right) - \left(-u1\right) \cdot u1} \cdot \cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 86.9% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right)\\ t_1 := \sqrt{-\log \left(1 - u1\right)}\\ \mathbf{if}\;t\_0 \cdot t\_1 \leq 0.02199999988079071:\\ \;\;\;\;\sqrt{\left(-\left(-u1\right)\right) - \left(-u1\right) \cdot u1} \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;1 \cdot t\_1\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (cos (* u2 (* (PI) 2.0)))) (t_1 (sqrt (- (log (- 1.0 u1))))))
   (if (<= (* t_0 t_1) 0.02199999988079071)
     (* (sqrt (- (- (- u1)) (* (- u1) u1))) t_0)
     (* 1.0 t_1))))
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right)\\
t_1 := \sqrt{-\log \left(1 - u1\right)}\\
\mathbf{if}\;t\_0 \cdot t\_1 \leq 0.02199999988079071:\\
\;\;\;\;\sqrt{\left(-\left(-u1\right)\right) - \left(-u1\right) \cdot u1} \cdot t\_0\\

\mathbf{else}:\\
\;\;\;\;1 \cdot t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f32 (sqrt.f32 (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1)))) (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2))) < 0.0219999999

    1. Initial program 38.5%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    2. Add Preprocessing
    3. Applied rewrites20.6%

      \[\leadsto \sqrt{-\color{blue}{\left(\mathsf{log1p}\left(\left(-u1\right) \cdot u1\right) + \log \left(\frac{e^{-\mathsf{log1p}\left(u1\right)}}{1}\right)\right)}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    4. Taylor expanded in u1 around 0

      \[\leadsto \sqrt{-\left(\mathsf{log1p}\left(\left(-u1\right) \cdot u1\right) + \color{blue}{-1 \cdot u1}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    5. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \sqrt{-\left(\mathsf{log1p}\left(\left(-u1\right) \cdot u1\right) + \color{blue}{\left(\mathsf{neg}\left(u1\right)\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      2. lower-neg.f3255.9

        \[\leadsto \sqrt{-\left(\mathsf{log1p}\left(\left(-u1\right) \cdot u1\right) + \color{blue}{\left(-u1\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    6. Applied rewrites55.9%

      \[\leadsto \sqrt{-\left(\mathsf{log1p}\left(\left(-u1\right) \cdot u1\right) + \color{blue}{\left(-u1\right)}\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    7. Taylor expanded in u1 around 0

      \[\leadsto \sqrt{-\left(\color{blue}{-1 \cdot {u1}^{2}} + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    8. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto \sqrt{-\left(-1 \cdot \color{blue}{\left(u1 \cdot u1\right)} + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      2. associate-*r*N/A

        \[\leadsto \sqrt{-\left(\color{blue}{\left(-1 \cdot u1\right) \cdot u1} + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      3. lower-*.f32N/A

        \[\leadsto \sqrt{-\left(\color{blue}{\left(-1 \cdot u1\right) \cdot u1} + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      4. mul-1-negN/A

        \[\leadsto \sqrt{-\left(\color{blue}{\left(\mathsf{neg}\left(u1\right)\right)} \cdot u1 + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      5. lower-neg.f3289.6

        \[\leadsto \sqrt{-\left(\color{blue}{\left(-u1\right)} \cdot u1 + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    9. Applied rewrites89.6%

      \[\leadsto \sqrt{-\left(\color{blue}{\left(-u1\right) \cdot u1} + \left(-u1\right)\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]

    if 0.0219999999 < (*.f32 (sqrt.f32 (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1)))) (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2)))

    1. Initial program 92.4%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    2. Add Preprocessing
    3. Taylor expanded in u2 around 0

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{1} \]
    4. Step-by-step derivation
      1. Applied rewrites78.6%

        \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{1} \]
    5. Recombined 2 regimes into one program.
    6. Final simplification85.5%

      \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right) \cdot \sqrt{-\log \left(1 - u1\right)} \leq 0.02199999988079071:\\ \;\;\;\;\sqrt{\left(-\left(-u1\right)\right) - \left(-u1\right) \cdot u1} \cdot \cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;1 \cdot \sqrt{-\log \left(1 - u1\right)}\\ \end{array} \]
    7. Add Preprocessing

    Alternative 3: 86.9% accurate, 0.6× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right)\\ t_1 := \sqrt{-\log \left(1 - u1\right)}\\ \mathbf{if}\;t\_0 \cdot t\_1 \leq 0.02199999988079071:\\ \;\;\;\;\sqrt{u1} \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;1 \cdot t\_1\\ \end{array} \end{array} \]
    (FPCore (cosTheta_i u1 u2)
     :precision binary32
     (let* ((t_0 (cos (* u2 (* (PI) 2.0)))) (t_1 (sqrt (- (log (- 1.0 u1))))))
       (if (<= (* t_0 t_1) 0.02199999988079071) (* (sqrt u1) t_0) (* 1.0 t_1))))
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    t_0 := \cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right)\\
    t_1 := \sqrt{-\log \left(1 - u1\right)}\\
    \mathbf{if}\;t\_0 \cdot t\_1 \leq 0.02199999988079071:\\
    \;\;\;\;\sqrt{u1} \cdot t\_0\\
    
    \mathbf{else}:\\
    \;\;\;\;1 \cdot t\_1\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (*.f32 (sqrt.f32 (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1)))) (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2))) < 0.0219999999

      1. Initial program 38.5%

        \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      2. Add Preprocessing
      3. Applied rewrites44.3%

        \[\leadsto \color{blue}{{\left(e^{\log \left(\mathsf{log1p}\left(u1\right)\right)}\right)}^{0.5}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      4. Taylor expanded in u1 around 0

        \[\leadsto \color{blue}{\sqrt{u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      5. Step-by-step derivation
        1. lower-sqrt.f3289.5

          \[\leadsto \color{blue}{\sqrt{u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      6. Applied rewrites89.5%

        \[\leadsto \color{blue}{\sqrt{u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]

      if 0.0219999999 < (*.f32 (sqrt.f32 (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1)))) (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2)))

      1. Initial program 92.4%

        \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
      2. Add Preprocessing
      3. Taylor expanded in u2 around 0

        \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{1} \]
      4. Step-by-step derivation
        1. Applied rewrites78.6%

          \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{1} \]
      5. Recombined 2 regimes into one program.
      6. Final simplification85.5%

        \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right) \cdot \sqrt{-\log \left(1 - u1\right)} \leq 0.02199999988079071:\\ \;\;\;\;\sqrt{u1} \cdot \cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;1 \cdot \sqrt{-\log \left(1 - u1\right)}\\ \end{array} \]
      7. Add Preprocessing

      Alternative 4: 75.6% accurate, 0.6× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{-\log \left(1 - u1\right)}\\ \mathbf{if}\;\cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right) \cdot t\_0 \leq 0.013000000268220901:\\ \;\;\;\;{\left(u1 \cdot u1\right)}^{0.25} \cdot 1\\ \mathbf{else}:\\ \;\;\;\;1 \cdot t\_0\\ \end{array} \end{array} \]
      (FPCore (cosTheta_i u1 u2)
       :precision binary32
       (let* ((t_0 (sqrt (- (log (- 1.0 u1))))))
         (if (<= (* (cos (* u2 (* (PI) 2.0))) t_0) 0.013000000268220901)
           (* (pow (* u1 u1) 0.25) 1.0)
           (* 1.0 t_0))))
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      t_0 := \sqrt{-\log \left(1 - u1\right)}\\
      \mathbf{if}\;\cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right) \cdot t\_0 \leq 0.013000000268220901:\\
      \;\;\;\;{\left(u1 \cdot u1\right)}^{0.25} \cdot 1\\
      
      \mathbf{else}:\\
      \;\;\;\;1 \cdot t\_0\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if (*.f32 (sqrt.f32 (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1)))) (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2))) < 0.0130000003

        1. Initial program 36.7%

          \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
        2. Add Preprocessing
        3. Applied rewrites44.3%

          \[\leadsto \color{blue}{{\left(e^{\log \left(\mathsf{log1p}\left(u1\right)\right)}\right)}^{0.5}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
        4. Taylor expanded in u1 around 0

          \[\leadsto \color{blue}{\sqrt{u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
        5. Step-by-step derivation
          1. lower-sqrt.f3290.6

            \[\leadsto \color{blue}{\sqrt{u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
        6. Applied rewrites90.6%

          \[\leadsto \color{blue}{\sqrt{u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
        7. Taylor expanded in u2 around 0

          \[\leadsto \sqrt{u1} \cdot \color{blue}{1} \]
        8. Step-by-step derivation
          1. Applied rewrites69.6%

            \[\leadsto \sqrt{u1} \cdot \color{blue}{1} \]
          2. Step-by-step derivation
            1. Applied rewrites69.6%

              \[\leadsto {\left(u1 \cdot u1\right)}^{\color{blue}{0.25}} \cdot 1 \]

            if 0.0130000003 < (*.f32 (sqrt.f32 (neg.f32 (log.f32 (-.f32 #s(literal 1 binary32) u1)))) (cos.f32 (*.f32 (*.f32 #s(literal 2 binary32) (PI.f32)) u2)))

            1. Initial program 90.9%

              \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
            2. Add Preprocessing
            3. Taylor expanded in u2 around 0

              \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{1} \]
            4. Step-by-step derivation
              1. Applied rewrites77.5%

                \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{1} \]
            5. Recombined 2 regimes into one program.
            6. Final simplification72.8%

              \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(u2 \cdot \left(\mathsf{PI}\left(\right) \cdot 2\right)\right) \cdot \sqrt{-\log \left(1 - u1\right)} \leq 0.013000000268220901:\\ \;\;\;\;{\left(u1 \cdot u1\right)}^{0.25} \cdot 1\\ \mathbf{else}:\\ \;\;\;\;1 \cdot \sqrt{-\log \left(1 - u1\right)}\\ \end{array} \]
            7. Add Preprocessing

            Alternative 5: 64.9% accurate, 2.1× speedup?

            \[\begin{array}{l} \\ {\left(u1 \cdot u1\right)}^{0.25} \cdot 1 \end{array} \]
            (FPCore (cosTheta_i u1 u2) :precision binary32 (* (pow (* u1 u1) 0.25) 1.0))
            float code(float cosTheta_i, float u1, float u2) {
            	return powf((u1 * u1), 0.25f) * 1.0f;
            }
            
            real(4) function code(costheta_i, u1, u2)
                real(4), intent (in) :: costheta_i
                real(4), intent (in) :: u1
                real(4), intent (in) :: u2
                code = ((u1 * u1) ** 0.25e0) * 1.0e0
            end function
            
            function code(cosTheta_i, u1, u2)
            	return Float32((Float32(u1 * u1) ^ Float32(0.25)) * Float32(1.0))
            end
            
            function tmp = code(cosTheta_i, u1, u2)
            	tmp = ((u1 * u1) ^ single(0.25)) * single(1.0);
            end
            
            \begin{array}{l}
            
            \\
            {\left(u1 \cdot u1\right)}^{0.25} \cdot 1
            \end{array}
            
            Derivation
            1. Initial program 58.7%

              \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
            2. Add Preprocessing
            3. Applied rewrites39.2%

              \[\leadsto \color{blue}{{\left(e^{\log \left(\mathsf{log1p}\left(u1\right)\right)}\right)}^{0.5}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
            4. Taylor expanded in u1 around 0

              \[\leadsto \color{blue}{\sqrt{u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
            5. Step-by-step derivation
              1. lower-sqrt.f3274.4

                \[\leadsto \color{blue}{\sqrt{u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
            6. Applied rewrites74.4%

              \[\leadsto \color{blue}{\sqrt{u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
            7. Taylor expanded in u2 around 0

              \[\leadsto \sqrt{u1} \cdot \color{blue}{1} \]
            8. Step-by-step derivation
              1. Applied rewrites60.9%

                \[\leadsto \sqrt{u1} \cdot \color{blue}{1} \]
              2. Step-by-step derivation
                1. Applied rewrites60.9%

                  \[\leadsto {\left(u1 \cdot u1\right)}^{\color{blue}{0.25}} \cdot 1 \]
                2. Add Preprocessing

                Alternative 6: 64.9% accurate, 14.4× speedup?

                \[\begin{array}{l} \\ \sqrt{u1} \cdot 1 \end{array} \]
                (FPCore (cosTheta_i u1 u2) :precision binary32 (* (sqrt u1) 1.0))
                float code(float cosTheta_i, float u1, float u2) {
                	return sqrtf(u1) * 1.0f;
                }
                
                real(4) function code(costheta_i, u1, u2)
                    real(4), intent (in) :: costheta_i
                    real(4), intent (in) :: u1
                    real(4), intent (in) :: u2
                    code = sqrt(u1) * 1.0e0
                end function
                
                function code(cosTheta_i, u1, u2)
                	return Float32(sqrt(u1) * Float32(1.0))
                end
                
                function tmp = code(cosTheta_i, u1, u2)
                	tmp = sqrt(u1) * single(1.0);
                end
                
                \begin{array}{l}
                
                \\
                \sqrt{u1} \cdot 1
                \end{array}
                
                Derivation
                1. Initial program 58.7%

                  \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
                2. Add Preprocessing
                3. Applied rewrites39.2%

                  \[\leadsto \color{blue}{{\left(e^{\log \left(\mathsf{log1p}\left(u1\right)\right)}\right)}^{0.5}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
                4. Taylor expanded in u1 around 0

                  \[\leadsto \color{blue}{\sqrt{u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
                5. Step-by-step derivation
                  1. lower-sqrt.f3274.4

                    \[\leadsto \color{blue}{\sqrt{u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
                6. Applied rewrites74.4%

                  \[\leadsto \color{blue}{\sqrt{u1}} \cdot \cos \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
                7. Taylor expanded in u2 around 0

                  \[\leadsto \sqrt{u1} \cdot \color{blue}{1} \]
                8. Step-by-step derivation
                  1. Applied rewrites60.9%

                    \[\leadsto \sqrt{u1} \cdot \color{blue}{1} \]
                  2. Add Preprocessing

                  Reproduce

                  ?
                  herbie shell --seed 2024257 
                  (FPCore (cosTheta_i u1 u2)
                    :name "Beckmann Sample, near normal, slope_x"
                    :precision binary32
                    :pre (and (and (and (> cosTheta_i 0.9999) (<= cosTheta_i 1.0)) (and (<= 2.328306437e-10 u1) (<= u1 1.0))) (and (<= 2.328306437e-10 u2) (<= u2 1.0)))
                    (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 (PI)) u2))))