Beckmann Sample, near normal, slope_y

Percentage Accurate: 57.8% → 98.3%
Time: 11.2s
Alternatives: 7
Speedup: 2.0×

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 \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (- (log (- 1.0 u1)))) (sin (* (* 2.0 PI) u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-logf((1.0f - u1))) * sinf(((2.0f * ((float) M_PI)) * u2));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * sin(Float32(Float32(Float32(2.0) * Float32(pi)) * u2)))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt(-log((single(1.0) - u1))) * sin(((single(2.0) * single(pi)) * u2));
end
\begin{array}{l}

\\
\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\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 7 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.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (- (log (- 1.0 u1)))) (sin (* (* 2.0 PI) u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-logf((1.0f - u1))) * sinf(((2.0f * ((float) M_PI)) * u2));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * sin(Float32(Float32(Float32(2.0) * Float32(pi)) * u2)))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt(-log((single(1.0) - u1))) * sin(((single(2.0) * single(pi)) * u2));
end
\begin{array}{l}

\\
\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right)
\end{array}

Alternative 1: 98.3% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\sin \left(\pi \cdot \left(u2 \cdot 2\right)\right)\right)\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (- (log1p (- u1)))) (log1p (expm1 (sin (* PI (* u2 2.0)))))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-log1pf(-u1)) * log1pf(expm1f(sinf((((float) M_PI) * (u2 * 2.0f)))));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(-log1p(Float32(-u1)))) * log1p(expm1(sin(Float32(Float32(pi) * Float32(u2 * Float32(2.0)))))))
end
\begin{array}{l}

\\
\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\sin \left(\pi \cdot \left(u2 \cdot 2\right)\right)\right)\right)
\end{array}
Derivation
  1. Initial program 63.4%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg63.4%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-def98.3%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. associate-*l*98.3%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \color{blue}{\left(2 \cdot \left(\pi \cdot u2\right)\right)} \]
  3. Simplified98.3%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \left(\pi \cdot u2\right)\right)} \]
  4. Step-by-step derivation
    1. log1p-expm1-u98.3%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\pi \cdot u2\right)\right)}\right) \]
  5. Applied egg-rr98.3%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\pi \cdot u2\right)\right)}\right) \]
  6. Step-by-step derivation
    1. log1p-expm1-u98.3%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\left(\pi \cdot u2\right)}\right) \]
    2. add-sqr-sqrt97.7%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \left(\pi \cdot \color{blue}{\left(\sqrt{u2} \cdot \sqrt{u2}\right)}\right)\right) \]
    3. associate-*r*97.7%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\left(\left(\pi \cdot \sqrt{u2}\right) \cdot \sqrt{u2}\right)}\right) \]
  7. Applied egg-rr97.7%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\left(\left(\pi \cdot \sqrt{u2}\right) \cdot \sqrt{u2}\right)}\right) \]
  8. Step-by-step derivation
    1. pow197.7%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{{\sin \left(2 \cdot \left(\left(\pi \cdot \sqrt{u2}\right) \cdot \sqrt{u2}\right)\right)}^{1}} \]
    2. metadata-eval97.7%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot {\sin \left(2 \cdot \left(\left(\pi \cdot \sqrt{u2}\right) \cdot \sqrt{u2}\right)\right)}^{\color{blue}{\left(\frac{2}{2}\right)}} \]
    3. sqrt-pow195.3%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\sqrt{{\sin \left(2 \cdot \left(\left(\pi \cdot \sqrt{u2}\right) \cdot \sqrt{u2}\right)\right)}^{2}}} \]
    4. associate-*l*95.3%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sqrt{{\sin \left(2 \cdot \color{blue}{\left(\pi \cdot \left(\sqrt{u2} \cdot \sqrt{u2}\right)\right)}\right)}^{2}} \]
    5. add-sqr-sqrt95.9%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sqrt{{\sin \left(2 \cdot \left(\pi \cdot \color{blue}{u2}\right)\right)}^{2}} \]
    6. *-commutative95.9%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sqrt{{\sin \left(2 \cdot \color{blue}{\left(u2 \cdot \pi\right)}\right)}^{2}} \]
    7. associate-*l*95.9%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sqrt{{\sin \color{blue}{\left(\left(2 \cdot u2\right) \cdot \pi\right)}}^{2}} \]
    8. log1p-expm1-u95.9%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\sqrt{{\sin \left(\left(2 \cdot u2\right) \cdot \pi\right)}^{2}}\right)\right)} \]
    9. associate-*l*95.9%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\sqrt{{\sin \color{blue}{\left(2 \cdot \left(u2 \cdot \pi\right)\right)}}^{2}}\right)\right) \]
    10. *-commutative95.9%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\sqrt{{\sin \left(2 \cdot \color{blue}{\left(\pi \cdot u2\right)}\right)}^{2}}\right)\right) \]
    11. add-sqr-sqrt95.3%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\sqrt{{\sin \left(2 \cdot \left(\pi \cdot \color{blue}{\left(\sqrt{u2} \cdot \sqrt{u2}\right)}\right)\right)}^{2}}\right)\right) \]
    12. associate-*l*95.3%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\sqrt{{\sin \left(2 \cdot \color{blue}{\left(\left(\pi \cdot \sqrt{u2}\right) \cdot \sqrt{u2}\right)}\right)}^{2}}\right)\right) \]
  9. Applied egg-rr98.3%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\sin \left(\pi \cdot \left(u2 \cdot 2\right)\right)\right)\right)} \]
  10. Final simplification98.3%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(\sin \left(\pi \cdot \left(u2 \cdot 2\right)\right)\right)\right) \]

Alternative 2: 94.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := u2 \cdot \left(\pi \cdot 2\right)\\ \mathbf{if}\;t_0 \leq 0.003000000026077032:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot t_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{u1 - u1 \cdot \left(u1 \cdot -0.5\right)} \cdot \sin t_0\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (* u2 (* PI 2.0))))
   (if (<= t_0 0.003000000026077032)
     (* (sqrt (- (log1p (- u1)))) t_0)
     (* (sqrt (- u1 (* u1 (* u1 -0.5)))) (sin t_0)))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = u2 * (((float) M_PI) * 2.0f);
	float tmp;
	if (t_0 <= 0.003000000026077032f) {
		tmp = sqrtf(-log1pf(-u1)) * t_0;
	} else {
		tmp = sqrtf((u1 - (u1 * (u1 * -0.5f)))) * sinf(t_0);
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = Float32(u2 * Float32(Float32(pi) * Float32(2.0)))
	tmp = Float32(0.0)
	if (t_0 <= Float32(0.003000000026077032))
		tmp = Float32(sqrt(Float32(-log1p(Float32(-u1)))) * t_0);
	else
		tmp = Float32(sqrt(Float32(u1 - Float32(u1 * Float32(u1 * Float32(-0.5))))) * sin(t_0));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := u2 \cdot \left(\pi \cdot 2\right)\\
\mathbf{if}\;t_0 \leq 0.003000000026077032:\\
\;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot t_0\\

\mathbf{else}:\\
\;\;\;\;\sqrt{u1 - u1 \cdot \left(u1 \cdot -0.5\right)} \cdot \sin t_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f32 (*.f32 2 (PI.f32)) u2) < 0.00300000003

    1. Initial program 65.1%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Step-by-step derivation
      1. sub-neg65.1%

        \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. log1p-def98.4%

        \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      3. associate-*l*98.4%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \color{blue}{\left(2 \cdot \left(\pi \cdot u2\right)\right)} \]
    3. Simplified98.4%

      \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \left(\pi \cdot u2\right)\right)} \]
    4. Step-by-step derivation
      1. log1p-expm1-u98.4%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\pi \cdot u2\right)\right)}\right) \]
    5. Applied egg-rr98.4%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\pi \cdot u2\right)\right)}\right) \]
    6. Step-by-step derivation
      1. log1p-expm1-u98.4%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\left(\pi \cdot u2\right)}\right) \]
      2. add-sqr-sqrt97.9%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \left(\pi \cdot \color{blue}{\left(\sqrt{u2} \cdot \sqrt{u2}\right)}\right)\right) \]
      3. associate-*r*97.9%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\left(\left(\pi \cdot \sqrt{u2}\right) \cdot \sqrt{u2}\right)}\right) \]
    7. Applied egg-rr97.9%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\left(\left(\pi \cdot \sqrt{u2}\right) \cdot \sqrt{u2}\right)}\right) \]
    8. Taylor expanded in u2 around 0 97.8%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(2 \cdot \left(u2 \cdot \pi\right)\right)} \]
    9. Step-by-step derivation
      1. associate-*r*97.8%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(\left(2 \cdot u2\right) \cdot \pi\right)} \]
      2. *-commutative97.8%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\color{blue}{\left(u2 \cdot 2\right)} \cdot \pi\right) \]
      3. associate-*l*97.8%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(u2 \cdot \left(2 \cdot \pi\right)\right)} \]
    10. Simplified97.8%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(u2 \cdot \left(2 \cdot \pi\right)\right)} \]

    if 0.00300000003 < (*.f32 (*.f32 2 (PI.f32)) u2)

    1. Initial program 59.9%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Taylor expanded in u1 around 0 85.2%

      \[\leadsto \sqrt{-\color{blue}{\left(-1 \cdot u1 + -0.5 \cdot {u1}^{2}\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Step-by-step derivation
      1. +-commutative85.2%

        \[\leadsto \sqrt{-\color{blue}{\left(-0.5 \cdot {u1}^{2} + -1 \cdot u1\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. mul-1-neg85.2%

        \[\leadsto \sqrt{-\left(-0.5 \cdot {u1}^{2} + \color{blue}{\left(-u1\right)}\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      3. unsub-neg85.2%

        \[\leadsto \sqrt{-\color{blue}{\left(-0.5 \cdot {u1}^{2} - u1\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      4. unpow285.2%

        \[\leadsto \sqrt{-\left(-0.5 \cdot \color{blue}{\left(u1 \cdot u1\right)} - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      5. associate-*r*85.2%

        \[\leadsto \sqrt{-\left(\color{blue}{\left(-0.5 \cdot u1\right) \cdot u1} - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Simplified85.2%

      \[\leadsto \sqrt{-\color{blue}{\left(\left(-0.5 \cdot u1\right) \cdot u1 - u1\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification93.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;u2 \cdot \left(\pi \cdot 2\right) \leq 0.003000000026077032:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(u2 \cdot \left(\pi \cdot 2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{u1 - u1 \cdot \left(u1 \cdot -0.5\right)} \cdot \sin \left(u2 \cdot \left(\pi \cdot 2\right)\right)\\ \end{array} \]

Alternative 3: 90.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := u2 \cdot \left(\pi \cdot 2\right)\\ \mathbf{if}\;t_0 \leq 0.014499999582767487:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot t_0\\ \mathbf{else}:\\ \;\;\;\;\sin \left(2 \cdot \left(\pi \cdot u2\right)\right) \cdot \sqrt{u1}\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (* u2 (* PI 2.0))))
   (if (<= t_0 0.014499999582767487)
     (* (sqrt (- (log1p (- u1)))) t_0)
     (* (sin (* 2.0 (* PI u2))) (sqrt u1)))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = u2 * (((float) M_PI) * 2.0f);
	float tmp;
	if (t_0 <= 0.014499999582767487f) {
		tmp = sqrtf(-log1pf(-u1)) * t_0;
	} else {
		tmp = sinf((2.0f * (((float) M_PI) * u2))) * sqrtf(u1);
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = Float32(u2 * Float32(Float32(pi) * Float32(2.0)))
	tmp = Float32(0.0)
	if (t_0 <= Float32(0.014499999582767487))
		tmp = Float32(sqrt(Float32(-log1p(Float32(-u1)))) * t_0);
	else
		tmp = Float32(sin(Float32(Float32(2.0) * Float32(Float32(pi) * u2))) * sqrt(u1));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := u2 \cdot \left(\pi \cdot 2\right)\\
\mathbf{if}\;t_0 \leq 0.014499999582767487:\\
\;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot t_0\\

\mathbf{else}:\\
\;\;\;\;\sin \left(2 \cdot \left(\pi \cdot u2\right)\right) \cdot \sqrt{u1}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f32 (*.f32 2 (PI.f32)) u2) < 0.0144999996

    1. Initial program 64.7%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Step-by-step derivation
      1. sub-neg64.7%

        \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. log1p-def98.4%

        \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      3. associate-*l*98.4%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \color{blue}{\left(2 \cdot \left(\pi \cdot u2\right)\right)} \]
    3. Simplified98.4%

      \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \left(\pi \cdot u2\right)\right)} \]
    4. Step-by-step derivation
      1. log1p-expm1-u98.4%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\pi \cdot u2\right)\right)}\right) \]
    5. Applied egg-rr98.4%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\pi \cdot u2\right)\right)}\right) \]
    6. Step-by-step derivation
      1. log1p-expm1-u98.4%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\left(\pi \cdot u2\right)}\right) \]
      2. add-sqr-sqrt97.8%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \left(\pi \cdot \color{blue}{\left(\sqrt{u2} \cdot \sqrt{u2}\right)}\right)\right) \]
      3. associate-*r*97.8%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\left(\left(\pi \cdot \sqrt{u2}\right) \cdot \sqrt{u2}\right)}\right) \]
    7. Applied egg-rr97.8%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \color{blue}{\left(\left(\pi \cdot \sqrt{u2}\right) \cdot \sqrt{u2}\right)}\right) \]
    8. Taylor expanded in u2 around 0 96.4%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(2 \cdot \left(u2 \cdot \pi\right)\right)} \]
    9. Step-by-step derivation
      1. associate-*r*96.4%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(\left(2 \cdot u2\right) \cdot \pi\right)} \]
      2. *-commutative96.4%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\color{blue}{\left(u2 \cdot 2\right)} \cdot \pi\right) \]
      3. associate-*l*96.4%

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(u2 \cdot \left(2 \cdot \pi\right)\right)} \]
    10. Simplified96.4%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(u2 \cdot \left(2 \cdot \pi\right)\right)} \]

    if 0.0144999996 < (*.f32 (*.f32 2 (PI.f32)) u2)

    1. Initial program 60.0%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Taylor expanded in u1 around 0 73.9%

      \[\leadsto \sqrt{-\color{blue}{-1 \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Step-by-step derivation
      1. mul-1-neg73.9%

        \[\leadsto \sqrt{-\color{blue}{\left(-u1\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Simplified73.9%

      \[\leadsto \sqrt{-\color{blue}{\left(-u1\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    5. Taylor expanded in u2 around inf 73.9%

      \[\leadsto \color{blue}{\sin \left(2 \cdot \left(u2 \cdot \pi\right)\right) \cdot \sqrt{u1}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification90.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;u2 \cdot \left(\pi \cdot 2\right) \leq 0.014499999582767487:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(u2 \cdot \left(\pi \cdot 2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\sin \left(2 \cdot \left(\pi \cdot u2\right)\right) \cdot \sqrt{u1}\\ \end{array} \]

Alternative 4: 98.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \left(\pi \cdot u2\right)\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (- (log1p (- u1)))) (sin (* 2.0 (* PI u2)))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-log1pf(-u1)) * sinf((2.0f * (((float) M_PI) * u2)));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(-log1p(Float32(-u1)))) * sin(Float32(Float32(2.0) * Float32(Float32(pi) * u2))))
end
\begin{array}{l}

\\
\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \left(\pi \cdot u2\right)\right)
\end{array}
Derivation
  1. Initial program 63.4%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. sub-neg63.4%

      \[\leadsto \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. log1p-def98.3%

      \[\leadsto \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. associate-*l*98.3%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \color{blue}{\left(2 \cdot \left(\pi \cdot u2\right)\right)} \]
  3. Simplified98.3%

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \left(\pi \cdot u2\right)\right)} \]
  4. Final simplification98.3%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(2 \cdot \left(\pi \cdot u2\right)\right) \]

Alternative 5: 76.3% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \sin \left(2 \cdot \left(\pi \cdot u2\right)\right) \cdot \sqrt{u1} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sin (* 2.0 (* PI u2))) (sqrt u1)))
float code(float cosTheta_i, float u1, float u2) {
	return sinf((2.0f * (((float) M_PI) * u2))) * sqrtf(u1);
}
function code(cosTheta_i, u1, u2)
	return Float32(sin(Float32(Float32(2.0) * Float32(Float32(pi) * u2))) * sqrt(u1))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sin((single(2.0) * (single(pi) * u2))) * sqrt(u1);
end
\begin{array}{l}

\\
\sin \left(2 \cdot \left(\pi \cdot u2\right)\right) \cdot \sqrt{u1}
\end{array}
Derivation
  1. Initial program 63.4%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Taylor expanded in u1 around 0 72.6%

    \[\leadsto \sqrt{-\color{blue}{-1 \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Step-by-step derivation
    1. mul-1-neg72.6%

      \[\leadsto \sqrt{-\color{blue}{\left(-u1\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  4. Simplified72.6%

    \[\leadsto \sqrt{-\color{blue}{\left(-u1\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  5. Taylor expanded in u2 around inf 72.6%

    \[\leadsto \color{blue}{\sin \left(2 \cdot \left(u2 \cdot \pi\right)\right) \cdot \sqrt{u1}} \]
  6. Final simplification72.6%

    \[\leadsto \sin \left(2 \cdot \left(\pi \cdot u2\right)\right) \cdot \sqrt{u1} \]

Alternative 6: 66.2% accurate, 2.0× speedup?

\[\begin{array}{l} \\ 2 \cdot \left(u2 \cdot \left(\pi \cdot \sqrt{u1}\right)\right) \end{array} \]
(FPCore (cosTheta_i u1 u2) :precision binary32 (* 2.0 (* u2 (* PI (sqrt u1)))))
float code(float cosTheta_i, float u1, float u2) {
	return 2.0f * (u2 * (((float) M_PI) * sqrtf(u1)));
}
function code(cosTheta_i, u1, u2)
	return Float32(Float32(2.0) * Float32(u2 * Float32(Float32(pi) * sqrt(u1))))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = single(2.0) * (u2 * (single(pi) * sqrt(u1)));
end
\begin{array}{l}

\\
2 \cdot \left(u2 \cdot \left(\pi \cdot \sqrt{u1}\right)\right)
\end{array}
Derivation
  1. Initial program 63.4%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Taylor expanded in u1 around 0 72.6%

    \[\leadsto \sqrt{-\color{blue}{-1 \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Step-by-step derivation
    1. mul-1-neg72.6%

      \[\leadsto \sqrt{-\color{blue}{\left(-u1\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  4. Simplified72.6%

    \[\leadsto \sqrt{-\color{blue}{\left(-u1\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  5. Taylor expanded in u2 around 0 63.4%

    \[\leadsto \color{blue}{2 \cdot \left(\left(u2 \cdot \pi\right) \cdot \sqrt{u1}\right)} \]
  6. Step-by-step derivation
    1. associate-*r*63.4%

      \[\leadsto \color{blue}{\left(2 \cdot \left(u2 \cdot \pi\right)\right) \cdot \sqrt{u1}} \]
  7. Simplified63.4%

    \[\leadsto \color{blue}{\left(2 \cdot \left(u2 \cdot \pi\right)\right) \cdot \sqrt{u1}} \]
  8. Taylor expanded in u2 around 0 63.4%

    \[\leadsto \color{blue}{2 \cdot \left(\left(u2 \cdot \pi\right) \cdot \sqrt{u1}\right)} \]
  9. Step-by-step derivation
    1. associate-*l*63.4%

      \[\leadsto 2 \cdot \color{blue}{\left(u2 \cdot \left(\pi \cdot \sqrt{u1}\right)\right)} \]
  10. Simplified63.4%

    \[\leadsto \color{blue}{2 \cdot \left(u2 \cdot \left(\pi \cdot \sqrt{u1}\right)\right)} \]
  11. Final simplification63.4%

    \[\leadsto 2 \cdot \left(u2 \cdot \left(\pi \cdot \sqrt{u1}\right)\right) \]

Alternative 7: 7.1% accurate, 409.0× speedup?

\[\begin{array}{l} \\ 0 \end{array} \]
(FPCore (cosTheta_i u1 u2) :precision binary32 0.0)
float code(float cosTheta_i, float u1, float u2) {
	return 0.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 = 0.0e0
end function
function code(cosTheta_i, u1, u2)
	return Float32(0.0)
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = single(0.0);
end
\begin{array}{l}

\\
0
\end{array}
Derivation
  1. Initial program 63.4%

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Step-by-step derivation
    1. associate-*r*63.4%

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \sin \color{blue}{\left(2 \cdot \left(\pi \cdot u2\right)\right)} \]
    2. add-cube-cbrt63.2%

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \sin \color{blue}{\left(\left(\sqrt[3]{2 \cdot \left(\pi \cdot u2\right)} \cdot \sqrt[3]{2 \cdot \left(\pi \cdot u2\right)}\right) \cdot \sqrt[3]{2 \cdot \left(\pi \cdot u2\right)}\right)} \]
    3. pow363.2%

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \sin \color{blue}{\left({\left(\sqrt[3]{2 \cdot \left(\pi \cdot u2\right)}\right)}^{3}\right)} \]
  3. Applied egg-rr63.2%

    \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \sin \color{blue}{\left({\left(\sqrt[3]{2 \cdot \left(\pi \cdot u2\right)}\right)}^{3}\right)} \]
  4. Taylor expanded in u2 around 0 7.1%

    \[\leadsto \color{blue}{0} \]
  5. Final simplification7.1%

    \[\leadsto 0 \]

Reproduce

?
herbie shell --seed 2023188 
(FPCore (cosTheta_i u1 u2)
  :name "Beckmann Sample, near normal, slope_y"
  :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)))) (sin (* (* 2.0 PI) u2))))