Beckmann Sample, near normal, slope_y

Percentage Accurate: 57.8% → 98.2%
Time: 19.0s
Alternatives: 19
Speedup: 4.7×

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}

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 19 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.2% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;u1 \leq 0.03999999910593033:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1, u1, u1\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \left(2 \cdot \left(\sin \left(\pi \cdot u2\right) \cdot \cos \left(\pi \cdot u2\right)\right)\right)\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (if (<= u1 0.03999999910593033)
   (*
    (sqrt (fma (* (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1) u1 u1))
    (sin (* (+ PI PI) u2)))
   (*
    (sqrt (- (log (- 1.0 u1))))
    (* 2.0 (* (sin (* PI u2)) (cos (* PI u2)))))))
float code(float cosTheta_i, float u1, float u2) {
	float tmp;
	if (u1 <= 0.03999999910593033f) {
		tmp = sqrtf(fmaf((fmaf(fmaf(0.25f, u1, 0.3333333333333333f), u1, 0.5f) * u1), u1, u1)) * sinf(((((float) M_PI) + ((float) M_PI)) * u2));
	} else {
		tmp = sqrtf(-logf((1.0f - u1))) * (2.0f * (sinf((((float) M_PI) * u2)) * cosf((((float) M_PI) * u2))));
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	tmp = Float32(0.0)
	if (u1 <= Float32(0.03999999910593033))
		tmp = Float32(sqrt(fma(Float32(fma(fma(Float32(0.25), u1, Float32(0.3333333333333333)), u1, Float32(0.5)) * u1), u1, u1)) * sin(Float32(Float32(Float32(pi) + Float32(pi)) * u2)));
	else
		tmp = Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * Float32(Float32(2.0) * Float32(sin(Float32(Float32(pi) * u2)) * cos(Float32(Float32(pi) * u2)))));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;u1 \leq 0.03999999910593033:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1, u1, u1\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u1 < 0.0399999991

    1. Initial program 50.7%

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

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(\frac{1}{2} + u1 \cdot \left(\frac{1}{3} + \frac{1}{4} \cdot u1\right)\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites98.3%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Applied rewrites98.3%

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]
    5. Applied rewrites98.3%

      \[\leadsto \sqrt{\mathsf{fma}\left(u1, \color{blue}{1}, u1 \cdot \left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1\right)\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]
    6. Applied rewrites98.3%

      \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1, \color{blue}{u1}, u1\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]

    if 0.0399999991 < u1

    1. Initial program 97.3%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Applied rewrites97.2%

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(2 \cdot \left(\sin \left(\pi \cdot u2\right) \cdot \cos \left(\pi \cdot u2\right)\right)\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 98.2% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\ \mathbf{if}\;u1 \leq 0.03999999910593033:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1, u1, u1\right)} \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (sin (* (+ PI PI) u2))))
   (if (<= u1 0.03999999910593033)
     (*
      (sqrt (fma (* (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1) u1 u1))
      t_0)
     (* (sqrt (- (log (- 1.0 u1)))) t_0))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = sinf(((((float) M_PI) + ((float) M_PI)) * u2));
	float tmp;
	if (u1 <= 0.03999999910593033f) {
		tmp = sqrtf(fmaf((fmaf(fmaf(0.25f, u1, 0.3333333333333333f), u1, 0.5f) * u1), u1, u1)) * t_0;
	} else {
		tmp = sqrtf(-logf((1.0f - u1))) * t_0;
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = sin(Float32(Float32(Float32(pi) + Float32(pi)) * u2))
	tmp = Float32(0.0)
	if (u1 <= Float32(0.03999999910593033))
		tmp = Float32(sqrt(fma(Float32(fma(fma(Float32(0.25), u1, Float32(0.3333333333333333)), u1, Float32(0.5)) * u1), u1, u1)) * t_0);
	else
		tmp = Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * t_0);
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\
\mathbf{if}\;u1 \leq 0.03999999910593033:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1, u1, u1\right)} \cdot t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u1 < 0.0399999991

    1. Initial program 50.7%

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

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(\frac{1}{2} + u1 \cdot \left(\frac{1}{3} + \frac{1}{4} \cdot u1\right)\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites98.3%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Applied rewrites98.3%

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]
    5. Applied rewrites98.3%

      \[\leadsto \sqrt{\mathsf{fma}\left(u1, \color{blue}{1}, u1 \cdot \left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1\right)\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]
    6. Applied rewrites98.3%

      \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1, \color{blue}{u1}, u1\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]

    if 0.0399999991 < u1

    1. Initial program 97.3%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Applied rewrites97.3%

      \[\leadsto \color{blue}{\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 98.1% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\ \mathbf{if}\;u1 \leq 0.041999999433755875:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (sin (* (+ PI PI) u2))))
   (if (<= u1 0.041999999433755875)
     (*
      (sqrt (* (fma (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1 1.0) u1))
      t_0)
     (* (sqrt (- (log (- 1.0 u1)))) t_0))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = sinf(((((float) M_PI) + ((float) M_PI)) * u2));
	float tmp;
	if (u1 <= 0.041999999433755875f) {
		tmp = sqrtf((fmaf(fmaf(fmaf(0.25f, u1, 0.3333333333333333f), u1, 0.5f), u1, 1.0f) * u1)) * t_0;
	} else {
		tmp = sqrtf(-logf((1.0f - u1))) * t_0;
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = sin(Float32(Float32(Float32(pi) + Float32(pi)) * u2))
	tmp = Float32(0.0)
	if (u1 <= Float32(0.041999999433755875))
		tmp = Float32(sqrt(Float32(fma(fma(fma(Float32(0.25), u1, Float32(0.3333333333333333)), u1, Float32(0.5)), u1, Float32(1.0)) * u1)) * t_0);
	else
		tmp = Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * t_0);
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\
\mathbf{if}\;u1 \leq 0.041999999433755875:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u1 < 0.0419999994

    1. Initial program 50.8%

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

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(\frac{1}{2} + u1 \cdot \left(\frac{1}{3} + \frac{1}{4} \cdot u1\right)\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites98.3%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Applied rewrites98.3%

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]

    if 0.0419999994 < u1

    1. Initial program 97.4%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Applied rewrites97.4%

      \[\leadsto \color{blue}{\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 97.9% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\ \mathbf{if}\;u1 \leq 0.014999999664723873:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(u1, 1, u1 \cdot \left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right) \cdot u1\right)\right)} \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (sin (* (+ PI PI) u2))))
   (if (<= u1 0.014999999664723873)
     (* (sqrt (fma u1 1.0 (* u1 (* (fma 0.3333333333333333 u1 0.5) u1)))) t_0)
     (* (sqrt (- (log (- 1.0 u1)))) t_0))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = sinf(((((float) M_PI) + ((float) M_PI)) * u2));
	float tmp;
	if (u1 <= 0.014999999664723873f) {
		tmp = sqrtf(fmaf(u1, 1.0f, (u1 * (fmaf(0.3333333333333333f, u1, 0.5f) * u1)))) * t_0;
	} else {
		tmp = sqrtf(-logf((1.0f - u1))) * t_0;
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = sin(Float32(Float32(Float32(pi) + Float32(pi)) * u2))
	tmp = Float32(0.0)
	if (u1 <= Float32(0.014999999664723873))
		tmp = Float32(sqrt(fma(u1, Float32(1.0), Float32(u1 * Float32(fma(Float32(0.3333333333333333), u1, Float32(0.5)) * u1)))) * t_0);
	else
		tmp = Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * t_0);
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\
\mathbf{if}\;u1 \leq 0.014999999664723873:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(u1, 1, u1 \cdot \left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right) \cdot u1\right)\right)} \cdot t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u1 < 0.0149999997

    1. Initial program 48.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

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(\frac{1}{2} + u1 \cdot \left(\frac{1}{3} + \frac{1}{4} \cdot u1\right)\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites98.3%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Applied rewrites98.3%

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]
    5. Applied rewrites98.4%

      \[\leadsto \sqrt{\mathsf{fma}\left(u1, \color{blue}{1}, u1 \cdot \left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1\right)\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]
    6. Taylor expanded in u1 around 0

      \[\leadsto \sqrt{\mathsf{fma}\left(u1, 1, u1 \cdot \left(\mathsf{fma}\left(\frac{1}{3}, u1, \frac{1}{2}\right) \cdot u1\right)\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]
    7. Applied rewrites98.3%

      \[\leadsto \sqrt{\mathsf{fma}\left(u1, 1, u1 \cdot \left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right) \cdot u1\right)\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]

    if 0.0149999997 < u1

    1. Initial program 96.5%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Applied rewrites96.5%

      \[\leadsto \color{blue}{\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 97.9% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\ \mathbf{if}\;u1 \leq 0.014000000432133675:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (sin (* (+ PI PI) u2))))
   (if (<= u1 0.014000000432133675)
     (* (sqrt (* (fma (fma 0.3333333333333333 u1 0.5) u1 1.0) u1)) t_0)
     (* (sqrt (- (log (- 1.0 u1)))) t_0))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = sinf(((((float) M_PI) + ((float) M_PI)) * u2));
	float tmp;
	if (u1 <= 0.014000000432133675f) {
		tmp = sqrtf((fmaf(fmaf(0.3333333333333333f, u1, 0.5f), u1, 1.0f) * u1)) * t_0;
	} else {
		tmp = sqrtf(-logf((1.0f - u1))) * t_0;
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = sin(Float32(Float32(Float32(pi) + Float32(pi)) * u2))
	tmp = Float32(0.0)
	if (u1 <= Float32(0.014000000432133675))
		tmp = Float32(sqrt(Float32(fma(fma(Float32(0.3333333333333333), u1, Float32(0.5)), u1, Float32(1.0)) * u1)) * t_0);
	else
		tmp = Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * t_0);
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\
\mathbf{if}\;u1 \leq 0.014000000432133675:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u1 < 0.0140000004

    1. Initial program 48.2%

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

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(\frac{1}{2} + \frac{1}{3} \cdot u1\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites98.2%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Applied rewrites98.2%

      \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \color{blue}{\sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]

    if 0.0140000004 < u1

    1. Initial program 96.5%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Applied rewrites96.5%

      \[\leadsto \color{blue}{\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 96.9% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\ \mathbf{if}\;u1 \leq 0.002400000113993883:\\ \;\;\;\;\mathsf{fma}\left(\sqrt{\left(u1 \cdot u1\right) \cdot u1}, 0.25, \sqrt{u1}\right) \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (sin (* (+ PI PI) u2))))
   (if (<= u1 0.002400000113993883)
     (* (fma (sqrt (* (* u1 u1) u1)) 0.25 (sqrt u1)) t_0)
     (* (sqrt (- (log (- 1.0 u1)))) t_0))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = sinf(((((float) M_PI) + ((float) M_PI)) * u2));
	float tmp;
	if (u1 <= 0.002400000113993883f) {
		tmp = fmaf(sqrtf(((u1 * u1) * u1)), 0.25f, sqrtf(u1)) * t_0;
	} else {
		tmp = sqrtf(-logf((1.0f - u1))) * t_0;
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = sin(Float32(Float32(Float32(pi) + Float32(pi)) * u2))
	tmp = Float32(0.0)
	if (u1 <= Float32(0.002400000113993883))
		tmp = Float32(fma(sqrt(Float32(Float32(u1 * u1) * u1)), Float32(0.25), sqrt(u1)) * t_0);
	else
		tmp = Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * t_0);
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\
\mathbf{if}\;u1 \leq 0.002400000113993883:\\
\;\;\;\;\mathsf{fma}\left(\sqrt{\left(u1 \cdot u1\right) \cdot u1}, 0.25, \sqrt{u1}\right) \cdot t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u1 < 0.00240000011

    1. Initial program 43.8%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Applied rewrites43.8%

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

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

      \[\leadsto \color{blue}{\left(\sqrt{u1} + \frac{1}{4} \cdot \sqrt{{u1}^{3}}\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]
    5. Applied rewrites98.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\sqrt{\left(u1 \cdot u1\right) \cdot u1}, 0.25, \sqrt{u1}\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]

    if 0.00240000011 < u1

    1. Initial program 94.1%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Applied rewrites94.1%

      \[\leadsto \color{blue}{\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 7: 96.9% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \log \left(1 - u1\right)\\ t_1 := \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\ \mathbf{if}\;t\_0 \leq -0.002400000113993883:\\ \;\;\;\;\sqrt{-t\_0} \cdot t\_1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(0.5, u1, 1\right) \cdot u1} \cdot t\_1\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (log (- 1.0 u1))) (t_1 (sin (* (+ PI PI) u2))))
   (if (<= t_0 -0.002400000113993883)
     (* (sqrt (- t_0)) t_1)
     (* (sqrt (* (fma 0.5 u1 1.0) u1)) t_1))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = logf((1.0f - u1));
	float t_1 = sinf(((((float) M_PI) + ((float) M_PI)) * u2));
	float tmp;
	if (t_0 <= -0.002400000113993883f) {
		tmp = sqrtf(-t_0) * t_1;
	} else {
		tmp = sqrtf((fmaf(0.5f, u1, 1.0f) * u1)) * t_1;
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = log(Float32(Float32(1.0) - u1))
	t_1 = sin(Float32(Float32(Float32(pi) + Float32(pi)) * u2))
	tmp = Float32(0.0)
	if (t_0 <= Float32(-0.002400000113993883))
		tmp = Float32(sqrt(Float32(-t_0)) * t_1);
	else
		tmp = Float32(sqrt(Float32(fma(Float32(0.5), u1, Float32(1.0)) * u1)) * t_1);
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \log \left(1 - u1\right)\\
t_1 := \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\
\mathbf{if}\;t\_0 \leq -0.002400000113993883:\\
\;\;\;\;\sqrt{-t\_0} \cdot t\_1\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(0.5, u1, 1\right) \cdot u1} \cdot t\_1\\


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

    1. Initial program 94.1%

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    2. Applied rewrites94.1%

      \[\leadsto \color{blue}{\sqrt{-\log \left(1 - u1\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]

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

    1. Initial program 43.8%

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

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(\frac{1}{2} + u1 \cdot \left(\frac{1}{3} + \frac{1}{4} \cdot u1\right)\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites98.3%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Applied rewrites98.3%

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]
    5. Taylor expanded in u1 around 0

      \[\leadsto \sqrt{\mathsf{fma}\left(\frac{1}{2}, u1, 1\right) \cdot u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]
    6. Applied rewrites98.0%

      \[\leadsto \sqrt{\mathsf{fma}\left(0.5, u1, 1\right) \cdot u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 8: 94.6% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \log \left(1 - u1\right)\\ \mathbf{if}\;t\_0 \leq -0.005499999970197678:\\ \;\;\;\;\sqrt{-t\_0} \cdot \left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(0.5, u1, 1\right) \cdot u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (log (- 1.0 u1))))
   (if (<= t_0 -0.005499999970197678)
     (*
      (sqrt (- t_0))
      (*
       (fma (* -1.3333333333333333 (* u2 u2)) (* (* PI PI) PI) (+ PI PI))
       u2))
     (* (sqrt (* (fma 0.5 u1 1.0) u1)) (sin (* (+ PI PI) u2))))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = logf((1.0f - u1));
	float tmp;
	if (t_0 <= -0.005499999970197678f) {
		tmp = sqrtf(-t_0) * (fmaf((-1.3333333333333333f * (u2 * u2)), ((((float) M_PI) * ((float) M_PI)) * ((float) M_PI)), (((float) M_PI) + ((float) M_PI))) * u2);
	} else {
		tmp = sqrtf((fmaf(0.5f, u1, 1.0f) * u1)) * sinf(((((float) M_PI) + ((float) M_PI)) * u2));
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = log(Float32(Float32(1.0) - u1))
	tmp = Float32(0.0)
	if (t_0 <= Float32(-0.005499999970197678))
		tmp = Float32(sqrt(Float32(-t_0)) * Float32(fma(Float32(Float32(-1.3333333333333333) * Float32(u2 * u2)), Float32(Float32(Float32(pi) * Float32(pi)) * Float32(pi)), Float32(Float32(pi) + Float32(pi))) * u2));
	else
		tmp = Float32(sqrt(Float32(fma(Float32(0.5), u1, Float32(1.0)) * u1)) * sin(Float32(Float32(Float32(pi) + Float32(pi)) * u2)));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \log \left(1 - u1\right)\\
\mathbf{if}\;t\_0 \leq -0.005499999970197678:\\
\;\;\;\;\sqrt{-t\_0} \cdot \left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(0.5, u1, 1\right) \cdot u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\


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

    1. Initial program 95.3%

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

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(u2 \cdot \left(\frac{-4}{3} \cdot \left({u2}^{2} \cdot {\mathsf{PI}\left(\right)}^{3}\right) + 2 \cdot \mathsf{PI}\left(\right)\right)\right)} \]
    3. Applied rewrites86.0%

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)} \]

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

    1. Initial program 45.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

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(\frac{1}{2} + u1 \cdot \left(\frac{1}{3} + \frac{1}{4} \cdot u1\right)\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites98.3%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Applied rewrites98.3%

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]
    5. Taylor expanded in u1 around 0

      \[\leadsto \sqrt{\mathsf{fma}\left(\frac{1}{2}, u1, 1\right) \cdot u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]
    6. Applied rewrites97.3%

      \[\leadsto \sqrt{\mathsf{fma}\left(0.5, u1, 1\right) \cdot u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 9: 91.8% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\\ \mathbf{if}\;u1 \leq 8.179999895219225 \cdot 10^{-7}:\\ \;\;\;\;\sqrt{u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\ \mathbf{elif}\;u1 \leq 0.014000000432133675:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0
         (*
          (fma (* -1.3333333333333333 (* u2 u2)) (* (* PI PI) PI) (+ PI PI))
          u2)))
   (if (<= u1 8.179999895219225e-7)
     (* (sqrt u1) (sin (* (+ PI PI) u2)))
     (if (<= u1 0.014000000432133675)
       (* (sqrt (* (fma (fma 0.3333333333333333 u1 0.5) u1 1.0) u1)) t_0)
       (* (sqrt (- (log (- 1.0 u1)))) t_0)))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = fmaf((-1.3333333333333333f * (u2 * u2)), ((((float) M_PI) * ((float) M_PI)) * ((float) M_PI)), (((float) M_PI) + ((float) M_PI))) * u2;
	float tmp;
	if (u1 <= 8.179999895219225e-7f) {
		tmp = sqrtf(u1) * sinf(((((float) M_PI) + ((float) M_PI)) * u2));
	} else if (u1 <= 0.014000000432133675f) {
		tmp = sqrtf((fmaf(fmaf(0.3333333333333333f, u1, 0.5f), u1, 1.0f) * u1)) * t_0;
	} else {
		tmp = sqrtf(-logf((1.0f - u1))) * t_0;
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = Float32(fma(Float32(Float32(-1.3333333333333333) * Float32(u2 * u2)), Float32(Float32(Float32(pi) * Float32(pi)) * Float32(pi)), Float32(Float32(pi) + Float32(pi))) * u2)
	tmp = Float32(0.0)
	if (u1 <= Float32(8.179999895219225e-7))
		tmp = Float32(sqrt(u1) * sin(Float32(Float32(Float32(pi) + Float32(pi)) * u2)));
	elseif (u1 <= Float32(0.014000000432133675))
		tmp = Float32(sqrt(Float32(fma(fma(Float32(0.3333333333333333), u1, Float32(0.5)), u1, Float32(1.0)) * u1)) * t_0);
	else
		tmp = Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * t_0);
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\\
\mathbf{if}\;u1 \leq 8.179999895219225 \cdot 10^{-7}:\\
\;\;\;\;\sqrt{u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\

\mathbf{elif}\;u1 \leq 0.014000000432133675:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if u1 < 8.1799999e-7

    1. Initial program 21.3%

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

      \[\leadsto \sqrt{\color{blue}{u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites98.2%

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

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

    if 8.1799999e-7 < u1 < 0.0140000004

    1. Initial program 70.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

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(\frac{1}{2} + \frac{1}{3} \cdot u1\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites98.1%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Taylor expanded in u2 around 0

      \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{3}, u1, \frac{1}{2}\right), u1, 1\right) \cdot u1} \cdot \color{blue}{\left(u2 \cdot \left(\frac{-4}{3} \cdot \left({u2}^{2} \cdot {\mathsf{PI}\left(\right)}^{3}\right) + 2 \cdot \mathsf{PI}\left(\right)\right)\right)} \]
    5. Applied rewrites88.8%

      \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \color{blue}{\left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)} \]

    if 0.0140000004 < u1

    1. Initial program 96.5%

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

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(u2 \cdot \left(\frac{-4}{3} \cdot \left({u2}^{2} \cdot {\mathsf{PI}\left(\right)}^{3}\right) + 2 \cdot \mathsf{PI}\left(\right)\right)\right)} \]
    3. Applied rewrites86.7%

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 10: 90.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;u2 \leq 0.02199999988079071:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(u1, 1, u1 \cdot \left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1\right)\right)} \cdot \left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (if (<= u2 0.02199999988079071)
   (*
    (sqrt
     (fma u1 1.0 (* u1 (* (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1))))
    (* (fma (* -1.3333333333333333 (* u2 u2)) (* (* PI PI) PI) (+ PI PI)) u2))
   (* (sqrt u1) (sin (* (+ PI PI) u2)))))
float code(float cosTheta_i, float u1, float u2) {
	float tmp;
	if (u2 <= 0.02199999988079071f) {
		tmp = sqrtf(fmaf(u1, 1.0f, (u1 * (fmaf(fmaf(0.25f, u1, 0.3333333333333333f), u1, 0.5f) * u1)))) * (fmaf((-1.3333333333333333f * (u2 * u2)), ((((float) M_PI) * ((float) M_PI)) * ((float) M_PI)), (((float) M_PI) + ((float) M_PI))) * u2);
	} else {
		tmp = sqrtf(u1) * sinf(((((float) M_PI) + ((float) M_PI)) * u2));
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	tmp = Float32(0.0)
	if (u2 <= Float32(0.02199999988079071))
		tmp = Float32(sqrt(fma(u1, Float32(1.0), Float32(u1 * Float32(fma(fma(Float32(0.25), u1, Float32(0.3333333333333333)), u1, Float32(0.5)) * u1)))) * Float32(fma(Float32(Float32(-1.3333333333333333) * Float32(u2 * u2)), Float32(Float32(Float32(pi) * Float32(pi)) * Float32(pi)), Float32(Float32(pi) + Float32(pi))) * u2));
	else
		tmp = Float32(sqrt(u1) * sin(Float32(Float32(Float32(pi) + Float32(pi)) * u2)));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;u2 \leq 0.02199999988079071:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(u1, 1, u1 \cdot \left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1\right)\right)} \cdot \left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u2 < 0.0219999999

    1. Initial program 57.6%

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

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(\frac{1}{2} + u1 \cdot \left(\frac{1}{3} + \frac{1}{4} \cdot u1\right)\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites93.5%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Applied rewrites93.5%

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)} \]
    5. Applied rewrites93.6%

      \[\leadsto \sqrt{\mathsf{fma}\left(u1, \color{blue}{1}, u1 \cdot \left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1\right)\right)} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right) \]
    6. Taylor expanded in u2 around 0

      \[\leadsto \sqrt{\mathsf{fma}\left(u1, 1, u1 \cdot \left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{4}, u1, \frac{1}{3}\right), u1, \frac{1}{2}\right) \cdot u1\right)\right)} \cdot \color{blue}{\left(u2 \cdot \left(\frac{-4}{3} \cdot \left({u2}^{2} \cdot {\mathsf{PI}\left(\right)}^{3}\right) + 2 \cdot \mathsf{PI}\left(\right)\right)\right)} \]
    7. Applied rewrites93.3%

      \[\leadsto \sqrt{\mathsf{fma}\left(u1, 1, u1 \cdot \left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right) \cdot u1\right)\right)} \cdot \color{blue}{\left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)} \]

    if 0.0219999999 < u2

    1. Initial program 59.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

      \[\leadsto \sqrt{\color{blue}{u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites75.0%

      \[\leadsto \sqrt{\color{blue}{u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Applied rewrites75.0%

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

Alternative 11: 90.0% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;u2 \leq 0.02199999988079071:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{u1} \cdot \sin \left(\left(\pi + \pi\right) \cdot u2\right)\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (if (<= u2 0.02199999988079071)
   (*
    (sqrt (* (fma (fma (fma 0.25 u1 0.3333333333333333) u1 0.5) u1 1.0) u1))
    (* (fma (* -1.3333333333333333 (* u2 u2)) (* (* PI PI) PI) (+ PI PI)) u2))
   (* (sqrt u1) (sin (* (+ PI PI) u2)))))
float code(float cosTheta_i, float u1, float u2) {
	float tmp;
	if (u2 <= 0.02199999988079071f) {
		tmp = sqrtf((fmaf(fmaf(fmaf(0.25f, u1, 0.3333333333333333f), u1, 0.5f), u1, 1.0f) * u1)) * (fmaf((-1.3333333333333333f * (u2 * u2)), ((((float) M_PI) * ((float) M_PI)) * ((float) M_PI)), (((float) M_PI) + ((float) M_PI))) * u2);
	} else {
		tmp = sqrtf(u1) * sinf(((((float) M_PI) + ((float) M_PI)) * u2));
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	tmp = Float32(0.0)
	if (u2 <= Float32(0.02199999988079071))
		tmp = Float32(sqrt(Float32(fma(fma(fma(Float32(0.25), u1, Float32(0.3333333333333333)), u1, Float32(0.5)), u1, Float32(1.0)) * u1)) * Float32(fma(Float32(Float32(-1.3333333333333333) * Float32(u2 * u2)), Float32(Float32(Float32(pi) * Float32(pi)) * Float32(pi)), Float32(Float32(pi) + Float32(pi))) * u2));
	else
		tmp = Float32(sqrt(u1) * sin(Float32(Float32(Float32(pi) + Float32(pi)) * u2)));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;u2 \leq 0.02199999988079071:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u2 < 0.0219999999

    1. Initial program 57.6%

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

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(\frac{1}{2} + u1 \cdot \left(\frac{1}{3} + \frac{1}{4} \cdot u1\right)\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites93.5%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Taylor expanded in u2 around 0

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

      \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.25, u1, 0.3333333333333333\right), u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \color{blue}{\left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)} \]

    if 0.0219999999 < u2

    1. Initial program 59.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

      \[\leadsto \sqrt{\color{blue}{u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites75.0%

      \[\leadsto \sqrt{\color{blue}{u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Applied rewrites75.0%

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

Alternative 12: 88.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \log \left(1 - u1\right)\\ t_1 := \mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\\ \mathbf{if}\;t\_0 \leq -0.014999999664723873:\\ \;\;\;\;\sqrt{-t\_0} \cdot t\_1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot t\_1\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (log (- 1.0 u1)))
        (t_1
         (*
          (fma (* -1.3333333333333333 (* u2 u2)) (* (* PI PI) PI) (+ PI PI))
          u2)))
   (if (<= t_0 -0.014999999664723873)
     (* (sqrt (- t_0)) t_1)
     (* (sqrt (* (fma (fma 0.3333333333333333 u1 0.5) u1 1.0) u1)) t_1))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = logf((1.0f - u1));
	float t_1 = fmaf((-1.3333333333333333f * (u2 * u2)), ((((float) M_PI) * ((float) M_PI)) * ((float) M_PI)), (((float) M_PI) + ((float) M_PI))) * u2;
	float tmp;
	if (t_0 <= -0.014999999664723873f) {
		tmp = sqrtf(-t_0) * t_1;
	} else {
		tmp = sqrtf((fmaf(fmaf(0.3333333333333333f, u1, 0.5f), u1, 1.0f) * u1)) * t_1;
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = log(Float32(Float32(1.0) - u1))
	t_1 = Float32(fma(Float32(Float32(-1.3333333333333333) * Float32(u2 * u2)), Float32(Float32(Float32(pi) * Float32(pi)) * Float32(pi)), Float32(Float32(pi) + Float32(pi))) * u2)
	tmp = Float32(0.0)
	if (t_0 <= Float32(-0.014999999664723873))
		tmp = Float32(sqrt(Float32(-t_0)) * t_1);
	else
		tmp = Float32(sqrt(Float32(fma(fma(Float32(0.3333333333333333), u1, Float32(0.5)), u1, Float32(1.0)) * u1)) * t_1);
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \log \left(1 - u1\right)\\
t_1 := \mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\\
\mathbf{if}\;t\_0 \leq -0.014999999664723873:\\
\;\;\;\;\sqrt{-t\_0} \cdot t\_1\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot t\_1\\


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

    1. Initial program 96.5%

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

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(u2 \cdot \left(\frac{-4}{3} \cdot \left({u2}^{2} \cdot {\mathsf{PI}\left(\right)}^{3}\right) + 2 \cdot \mathsf{PI}\left(\right)\right)\right)} \]
    3. Applied rewrites86.7%

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)} \]

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

    1. Initial program 48.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

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(\frac{1}{2} + \frac{1}{3} \cdot u1\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites98.2%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Taylor expanded in u2 around 0

      \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{3}, u1, \frac{1}{2}\right), u1, 1\right) \cdot u1} \cdot \color{blue}{\left(u2 \cdot \left(\frac{-4}{3} \cdot \left({u2}^{2} \cdot {\mathsf{PI}\left(\right)}^{3}\right) + 2 \cdot \mathsf{PI}\left(\right)\right)\right)} \]
    5. Applied rewrites89.1%

      \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \color{blue}{\left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 13: 83.4% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;u1 \leq 0.00010800000018207356:\\ \;\;\;\;\sqrt{u1} \cdot \left(\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot \left(\pi \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (if (<= u1 0.00010800000018207356)
   (*
    (sqrt u1)
    (* (fma (* (* PI PI) (* PI -1.3333333333333333)) (* u2 u2) (+ PI PI)) u2))
   (*
    (sqrt (- (log (- 1.0 u1))))
    (*
     (fma (* -1.3333333333333333 (* u2 u2)) (* (* PI PI) PI) (+ PI PI))
     u2))))
float code(float cosTheta_i, float u1, float u2) {
	float tmp;
	if (u1 <= 0.00010800000018207356f) {
		tmp = sqrtf(u1) * (fmaf(((((float) M_PI) * ((float) M_PI)) * (((float) M_PI) * -1.3333333333333333f)), (u2 * u2), (((float) M_PI) + ((float) M_PI))) * u2);
	} else {
		tmp = sqrtf(-logf((1.0f - u1))) * (fmaf((-1.3333333333333333f * (u2 * u2)), ((((float) M_PI) * ((float) M_PI)) * ((float) M_PI)), (((float) M_PI) + ((float) M_PI))) * u2);
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	tmp = Float32(0.0)
	if (u1 <= Float32(0.00010800000018207356))
		tmp = Float32(sqrt(u1) * Float32(fma(Float32(Float32(Float32(pi) * Float32(pi)) * Float32(Float32(pi) * Float32(-1.3333333333333333))), Float32(u2 * u2), Float32(Float32(pi) + Float32(pi))) * u2));
	else
		tmp = Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * Float32(fma(Float32(Float32(-1.3333333333333333) * Float32(u2 * u2)), Float32(Float32(Float32(pi) * Float32(pi)) * Float32(pi)), Float32(Float32(pi) + Float32(pi))) * u2));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;u1 \leq 0.00010800000018207356:\\
\;\;\;\;\sqrt{u1} \cdot \left(\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot \left(\pi \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right)\\

\mathbf{else}:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u1 < 1.08e-4

    1. Initial program 35.6%

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

      \[\leadsto \sqrt{\color{blue}{u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites93.2%

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

      \[\leadsto \sqrt{u1} \cdot \color{blue}{\left(u2 \cdot \left(2 \cdot \mathsf{PI}\left(\right) + {u2}^{2} \cdot \left(\frac{-4}{3} \cdot {\mathsf{PI}\left(\right)}^{3} + \frac{4}{15} \cdot \left({u2}^{2} \cdot {\mathsf{PI}\left(\right)}^{5}\right)\right)\right)\right)} \]
    5. Applied rewrites87.3%

      \[\leadsto \sqrt{u1} \cdot \color{blue}{\left(\mathsf{fma}\left(\mathsf{fma}\left({\pi}^{5} \cdot \left(u2 \cdot u2\right), 0.26666666666666666, \left(\left(\pi \cdot \pi\right) \cdot \pi\right) \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right)} \]
    6. Taylor expanded in u2 around 0

      \[\leadsto \sqrt{u1} \cdot \left(\mathsf{fma}\left(\frac{-4}{3} \cdot {\mathsf{PI}\left(\right)}^{3}, u2 \cdot u2, \pi + \pi\right) \cdot u2\right) \]
    7. Applied rewrites85.1%

      \[\leadsto \sqrt{u1} \cdot \left(\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot \left(\pi \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right) \]

    if 1.08e-4 < u1

    1. Initial program 88.8%

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

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(u2 \cdot \left(\frac{-4}{3} \cdot \left({u2}^{2} \cdot {\mathsf{PI}\left(\right)}^{3}\right) + 2 \cdot \mathsf{PI}\left(\right)\right)\right)} \]
    3. Applied rewrites80.9%

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(\mathsf{fma}\left(-1.3333333333333333 \cdot \left(u2 \cdot u2\right), \left(\pi \cdot \pi\right) \cdot \pi, \pi + \pi\right) \cdot u2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 14: 81.3% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;u2 \leq 0.0007999999797903001:\\ \;\;\;\;\sqrt{-\left(\left(\left(-0.25 \cdot u1 - 0.3333333333333333\right) \cdot u1 - 0.5\right) \cdot u1 - 1\right) \cdot u1} \cdot \left(\left(\pi + \pi\right) \cdot u2\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{u1} \cdot \left(\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot \left(\pi \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right)\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (if (<= u2 0.0007999999797903001)
   (*
    (sqrt
     (-
      (*
       (- (* (- (* (- (* -0.25 u1) 0.3333333333333333) u1) 0.5) u1) 1.0)
       u1)))
    (* (+ PI PI) u2))
   (*
    (sqrt u1)
    (*
     (fma (* (* PI PI) (* PI -1.3333333333333333)) (* u2 u2) (+ PI PI))
     u2))))
float code(float cosTheta_i, float u1, float u2) {
	float tmp;
	if (u2 <= 0.0007999999797903001f) {
		tmp = sqrtf(-(((((((-0.25f * u1) - 0.3333333333333333f) * u1) - 0.5f) * u1) - 1.0f) * u1)) * ((((float) M_PI) + ((float) M_PI)) * u2);
	} else {
		tmp = sqrtf(u1) * (fmaf(((((float) M_PI) * ((float) M_PI)) * (((float) M_PI) * -1.3333333333333333f)), (u2 * u2), (((float) M_PI) + ((float) M_PI))) * u2);
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	tmp = Float32(0.0)
	if (u2 <= Float32(0.0007999999797903001))
		tmp = Float32(sqrt(Float32(-Float32(Float32(Float32(Float32(Float32(Float32(Float32(Float32(-0.25) * u1) - Float32(0.3333333333333333)) * u1) - Float32(0.5)) * u1) - Float32(1.0)) * u1))) * Float32(Float32(Float32(pi) + Float32(pi)) * u2));
	else
		tmp = Float32(sqrt(u1) * Float32(fma(Float32(Float32(Float32(pi) * Float32(pi)) * Float32(Float32(pi) * Float32(-1.3333333333333333))), Float32(u2 * u2), Float32(Float32(pi) + Float32(pi))) * u2));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;u2 \leq 0.0007999999797903001:\\
\;\;\;\;\sqrt{-\left(\left(\left(-0.25 \cdot u1 - 0.3333333333333333\right) \cdot u1 - 0.5\right) \cdot u1 - 1\right) \cdot u1} \cdot \left(\left(\pi + \pi\right) \cdot u2\right)\\

\mathbf{else}:\\
\;\;\;\;\sqrt{u1} \cdot \left(\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot \left(\pi \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u2 < 7.9999998e-4

    1. Initial program 57.9%

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

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(2 \cdot \left(u2 \cdot \mathsf{PI}\left(\right)\right)\right)} \]
    3. Applied rewrites57.7%

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(\left(\pi + \pi\right) \cdot u2\right)} \]
    4. Taylor expanded in u1 around 0

      \[\leadsto \sqrt{-\color{blue}{u1 \cdot \left(u1 \cdot \left(u1 \cdot \left(\frac{-1}{4} \cdot u1 - \frac{1}{3}\right) - \frac{1}{2}\right) - 1\right)}} \cdot \left(\left(\pi + \pi\right) \cdot u2\right) \]
    5. Applied rewrites92.6%

      \[\leadsto \sqrt{-\color{blue}{\left(\left(\left(-0.25 \cdot u1 - 0.3333333333333333\right) \cdot u1 - 0.5\right) \cdot u1 - 1\right) \cdot u1}} \cdot \left(\left(\pi + \pi\right) \cdot u2\right) \]

    if 7.9999998e-4 < u2

    1. Initial program 57.7%

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

      \[\leadsto \sqrt{\color{blue}{u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites76.0%

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

      \[\leadsto \sqrt{u1} \cdot \color{blue}{\left(u2 \cdot \left(2 \cdot \mathsf{PI}\left(\right) + {u2}^{2} \cdot \left(\frac{-4}{3} \cdot {\mathsf{PI}\left(\right)}^{3} + \frac{4}{15} \cdot \left({u2}^{2} \cdot {\mathsf{PI}\left(\right)}^{5}\right)\right)\right)\right)} \]
    5. Applied rewrites62.6%

      \[\leadsto \sqrt{u1} \cdot \color{blue}{\left(\mathsf{fma}\left(\mathsf{fma}\left({\pi}^{5} \cdot \left(u2 \cdot u2\right), 0.26666666666666666, \left(\left(\pi \cdot \pi\right) \cdot \pi\right) \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right)} \]
    6. Taylor expanded in u2 around 0

      \[\leadsto \sqrt{u1} \cdot \left(\mathsf{fma}\left(\frac{-4}{3} \cdot {\mathsf{PI}\left(\right)}^{3}, u2 \cdot u2, \pi + \pi\right) \cdot u2\right) \]
    7. Applied rewrites58.0%

      \[\leadsto \sqrt{u1} \cdot \left(\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot \left(\pi \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 15: 81.1% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;u1 \leq 0.0006000000284984708:\\ \;\;\;\;\sqrt{u1} \cdot \left(\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot \left(\pi \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \left(\left(\pi + \pi\right) \cdot u2\right)\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (if (<= u1 0.0006000000284984708)
   (*
    (sqrt u1)
    (* (fma (* (* PI PI) (* PI -1.3333333333333333)) (* u2 u2) (+ PI PI)) u2))
   (* (sqrt (- (log (- 1.0 u1)))) (* (+ PI PI) u2))))
float code(float cosTheta_i, float u1, float u2) {
	float tmp;
	if (u1 <= 0.0006000000284984708f) {
		tmp = sqrtf(u1) * (fmaf(((((float) M_PI) * ((float) M_PI)) * (((float) M_PI) * -1.3333333333333333f)), (u2 * u2), (((float) M_PI) + ((float) M_PI))) * u2);
	} else {
		tmp = sqrtf(-logf((1.0f - u1))) * ((((float) M_PI) + ((float) M_PI)) * u2);
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	tmp = Float32(0.0)
	if (u1 <= Float32(0.0006000000284984708))
		tmp = Float32(sqrt(u1) * Float32(fma(Float32(Float32(Float32(pi) * Float32(pi)) * Float32(Float32(pi) * Float32(-1.3333333333333333))), Float32(u2 * u2), Float32(Float32(pi) + Float32(pi))) * u2));
	else
		tmp = Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * Float32(Float32(Float32(pi) + Float32(pi)) * u2));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;u1 \leq 0.0006000000284984708:\\
\;\;\;\;\sqrt{u1} \cdot \left(\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot \left(\pi \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right)\\

\mathbf{else}:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \left(\left(\pi + \pi\right) \cdot u2\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u1 < 6.00000028e-4

    1. Initial program 40.2%

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

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

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

      \[\leadsto \sqrt{u1} \cdot \color{blue}{\left(u2 \cdot \left(2 \cdot \mathsf{PI}\left(\right) + {u2}^{2} \cdot \left(\frac{-4}{3} \cdot {\mathsf{PI}\left(\right)}^{3} + \frac{4}{15} \cdot \left({u2}^{2} \cdot {\mathsf{PI}\left(\right)}^{5}\right)\right)\right)\right)} \]
    5. Applied rewrites84.8%

      \[\leadsto \sqrt{u1} \cdot \color{blue}{\left(\mathsf{fma}\left(\mathsf{fma}\left({\pi}^{5} \cdot \left(u2 \cdot u2\right), 0.26666666666666666, \left(\left(\pi \cdot \pi\right) \cdot \pi\right) \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right)} \]
    6. Taylor expanded in u2 around 0

      \[\leadsto \sqrt{u1} \cdot \left(\mathsf{fma}\left(\frac{-4}{3} \cdot {\mathsf{PI}\left(\right)}^{3}, u2 \cdot u2, \pi + \pi\right) \cdot u2\right) \]
    7. Applied rewrites82.8%

      \[\leadsto \sqrt{u1} \cdot \left(\mathsf{fma}\left(\left(\pi \cdot \pi\right) \cdot \left(\pi \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right) \]

    if 6.00000028e-4 < u1

    1. Initial program 91.8%

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

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(2 \cdot \left(u2 \cdot \mathsf{PI}\left(\right)\right)\right)} \]
    3. Applied rewrites77.1%

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(\left(\pi + \pi\right) \cdot u2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 16: 80.8% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(\pi + \pi\right) \cdot u2\\ \mathbf{if}\;u1 \leq 0.014999999664723873:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (* (+ PI PI) u2)))
   (if (<= u1 0.014999999664723873)
     (* (sqrt (* (fma (fma 0.3333333333333333 u1 0.5) u1 1.0) u1)) t_0)
     (* (sqrt (- (log (- 1.0 u1)))) t_0))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = (((float) M_PI) + ((float) M_PI)) * u2;
	float tmp;
	if (u1 <= 0.014999999664723873f) {
		tmp = sqrtf((fmaf(fmaf(0.3333333333333333f, u1, 0.5f), u1, 1.0f) * u1)) * t_0;
	} else {
		tmp = sqrtf(-logf((1.0f - u1))) * t_0;
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = Float32(Float32(Float32(pi) + Float32(pi)) * u2)
	tmp = Float32(0.0)
	if (u1 <= Float32(0.014999999664723873))
		tmp = Float32(sqrt(Float32(fma(fma(Float32(0.3333333333333333), u1, Float32(0.5)), u1, Float32(1.0)) * u1)) * t_0);
	else
		tmp = Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * t_0);
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(\pi + \pi\right) \cdot u2\\
\mathbf{if}\;u1 \leq 0.014999999664723873:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u1 < 0.0149999997

    1. Initial program 48.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

      \[\leadsto \sqrt{\color{blue}{u1 \cdot \left(1 + u1 \cdot \left(\frac{1}{2} + \frac{1}{3} \cdot u1\right)\right)}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    3. Applied rewrites98.2%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Taylor expanded in u2 around 0

      \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{3}, u1, \frac{1}{2}\right), u1, 1\right) \cdot u1} \cdot \color{blue}{\left(u2 \cdot \left(2 \cdot \mathsf{PI}\left(\right) + {u2}^{2} \cdot \left(\frac{-4}{3} \cdot {\mathsf{PI}\left(\right)}^{3} + \frac{4}{15} \cdot \left({u2}^{2} \cdot {\mathsf{PI}\left(\right)}^{5}\right)\right)\right)\right)} \]
    5. Applied rewrites91.6%

      \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \color{blue}{\left(\mathsf{fma}\left(\mathsf{fma}\left({\pi}^{5} \cdot \left(u2 \cdot u2\right), 0.26666666666666666, \left(\left(\pi \cdot \pi\right) \cdot \pi\right) \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right)} \]
    6. Taylor expanded in u2 around 0

      \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{3}, u1, \frac{1}{2}\right), u1, 1\right) \cdot u1} \cdot \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
    7. Applied rewrites81.6%

      \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.3333333333333333, u1, 0.5\right), u1, 1\right) \cdot u1} \cdot \left(\left(\pi + \pi\right) \cdot u2\right) \]

    if 0.0149999997 < u1

    1. Initial program 96.5%

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

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(2 \cdot \left(u2 \cdot \mathsf{PI}\left(\right)\right)\right)} \]
    3. Applied rewrites79.3%

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(\left(\pi + \pi\right) \cdot u2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 17: 80.5% accurate, 2.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(\pi + \pi\right) \cdot u2\\ \mathbf{if}\;u1 \leq 0.002400000113993883:\\ \;\;\;\;\sqrt{-\left(-0.5 \cdot u1 - 1\right) \cdot u1} \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (* (+ PI PI) u2)))
   (if (<= u1 0.002400000113993883)
     (* (sqrt (- (* (- (* -0.5 u1) 1.0) u1))) t_0)
     (* (sqrt (- (log (- 1.0 u1)))) t_0))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = (((float) M_PI) + ((float) M_PI)) * u2;
	float tmp;
	if (u1 <= 0.002400000113993883f) {
		tmp = sqrtf(-(((-0.5f * u1) - 1.0f) * u1)) * t_0;
	} else {
		tmp = sqrtf(-logf((1.0f - u1))) * t_0;
	}
	return tmp;
}
function code(cosTheta_i, u1, u2)
	t_0 = Float32(Float32(Float32(pi) + Float32(pi)) * u2)
	tmp = Float32(0.0)
	if (u1 <= Float32(0.002400000113993883))
		tmp = Float32(sqrt(Float32(-Float32(Float32(Float32(Float32(-0.5) * u1) - Float32(1.0)) * u1))) * t_0);
	else
		tmp = Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * t_0);
	end
	return tmp
end
function tmp_2 = code(cosTheta_i, u1, u2)
	t_0 = (single(pi) + single(pi)) * u2;
	tmp = single(0.0);
	if (u1 <= single(0.002400000113993883))
		tmp = sqrt(-(((single(-0.5) * u1) - single(1.0)) * u1)) * t_0;
	else
		tmp = sqrt(-log((single(1.0) - u1))) * t_0;
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(\pi + \pi\right) \cdot u2\\
\mathbf{if}\;u1 \leq 0.002400000113993883:\\
\;\;\;\;\sqrt{-\left(-0.5 \cdot u1 - 1\right) \cdot u1} \cdot t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if u1 < 0.00240000011

    1. Initial program 43.8%

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

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(2 \cdot \left(u2 \cdot \mathsf{PI}\left(\right)\right)\right)} \]
    3. Applied rewrites39.8%

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(\left(\pi + \pi\right) \cdot u2\right)} \]
    4. Taylor expanded in u1 around 0

      \[\leadsto \sqrt{-\color{blue}{u1 \cdot \left(\frac{-1}{2} \cdot u1 - 1\right)}} \cdot \left(\left(\pi + \pi\right) \cdot u2\right) \]
    5. Applied rewrites81.2%

      \[\leadsto \sqrt{-\color{blue}{\left(-0.5 \cdot u1 - 1\right) \cdot u1}} \cdot \left(\left(\pi + \pi\right) \cdot u2\right) \]

    if 0.00240000011 < u1

    1. Initial program 94.1%

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

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(2 \cdot \left(u2 \cdot \mathsf{PI}\left(\right)\right)\right)} \]
    3. Applied rewrites78.6%

      \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(\left(\pi + \pi\right) \cdot u2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 18: 74.1% accurate, 2.6× speedup?

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

\\
\sqrt{-\left(-0.5 \cdot u1 - 1\right) \cdot u1} \cdot \left(\left(\pi + \pi\right) \cdot u2\right)
\end{array}
Derivation
  1. Initial program 57.8%

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

    \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(2 \cdot \left(u2 \cdot \mathsf{PI}\left(\right)\right)\right)} \]
  3. Applied rewrites50.6%

    \[\leadsto \sqrt{-\log \left(1 - u1\right)} \cdot \color{blue}{\left(\left(\pi + \pi\right) \cdot u2\right)} \]
  4. Taylor expanded in u1 around 0

    \[\leadsto \sqrt{-\color{blue}{u1 \cdot \left(\frac{-1}{2} \cdot u1 - 1\right)}} \cdot \left(\left(\pi + \pi\right) \cdot u2\right) \]
  5. Applied rewrites74.1%

    \[\leadsto \sqrt{-\color{blue}{\left(-0.5 \cdot u1 - 1\right) \cdot u1}} \cdot \left(\left(\pi + \pi\right) \cdot u2\right) \]
  6. Add Preprocessing

Alternative 19: 66.1% accurate, 4.7× speedup?

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

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

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

    \[\leadsto \sqrt{\color{blue}{u1}} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  3. Applied rewrites76.4%

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

    \[\leadsto \sqrt{u1} \cdot \color{blue}{\left(u2 \cdot \left(2 \cdot \mathsf{PI}\left(\right) + {u2}^{2} \cdot \left(\frac{-4}{3} \cdot {\mathsf{PI}\left(\right)}^{3} + \frac{4}{15} \cdot \left({u2}^{2} \cdot {\mathsf{PI}\left(\right)}^{5}\right)\right)\right)\right)} \]
  5. Applied rewrites72.0%

    \[\leadsto \sqrt{u1} \cdot \color{blue}{\left(\mathsf{fma}\left(\mathsf{fma}\left({\pi}^{5} \cdot \left(u2 \cdot u2\right), 0.26666666666666666, \left(\left(\pi \cdot \pi\right) \cdot \pi\right) \cdot -1.3333333333333333\right), u2 \cdot u2, \pi + \pi\right) \cdot u2\right)} \]
  6. Taylor expanded in u2 around 0

    \[\leadsto \sqrt{u1} \cdot \left(\left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u2\right) \]
  7. Applied rewrites66.1%

    \[\leadsto \sqrt{u1} \cdot \left(\left(\pi + \pi\right) \cdot u2\right) \]
  8. Add Preprocessing

Reproduce

?
herbie shell --seed 2025121 
(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))))