Beckmann Sample, near normal, slope_x

Percentage Accurate: 57.5% → 99.0%
Time: 12.8s
Alternatives: 7
Speedup: 4.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 \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 PI) u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-logf((1.0f - u1))) * cosf(((2.0f * ((float) M_PI)) * u2));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * cos(Float32(Float32(Float32(2.0) * Float32(pi)) * u2)))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt(-log((single(1.0) - u1))) * cos(((single(2.0) * single(pi)) * u2));
end
\begin{array}{l}

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

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

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

Alternative 1: 99.0% accurate, 0.6× speedup?

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

\\
\begin{array}{l}
t_0 := \sqrt{2 \cdot \pi}\\
\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(t_0 \cdot \left(u2 \cdot t_0\right)\right)
\end{array}
\end{array}
Derivation
  1. Initial program 53.7%

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

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

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

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

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\color{blue}{\sqrt[3]{\left(\left(2 \cdot \pi\right) \cdot \left(2 \cdot \pi\right)\right) \cdot \left(2 \cdot \pi\right)}} \cdot u2\right) \]
    2. add-cbrt-cube99.1%

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

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

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\sqrt[3]{\color{blue}{{\left(2 \cdot \pi\right)}^{3}} \cdot \left(\left(u2 \cdot u2\right) \cdot u2\right)}\right) \]
    5. pow399.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\sqrt[3]{{\left(2 \cdot \pi\right)}^{3} \cdot \color{blue}{{u2}^{3}}}\right) \]
  5. Applied egg-rr99.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \color{blue}{\left(\sqrt[3]{{\left(2 \cdot \pi\right)}^{3} \cdot {u2}^{3}}\right)} \]
  6. Step-by-step derivation
    1. pow-prod-down99.1%

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

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\sqrt[3]{{\color{blue}{\left(2 \cdot \left(\pi \cdot u2\right)\right)}}^{3}}\right) \]
    3. rem-cbrt-cube99.1%

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

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

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \color{blue}{\left(u2 \cdot \left(2 \cdot \pi\right)\right)} \]
    6. add-sqr-sqrt99.1%

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

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

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

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

Alternative 2: 99.0% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\sqrt[3]{{\left(2 \cdot \pi\right)}^{3} \cdot {u2}^{3}}\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (*
  (sqrt (- (log1p (- u1))))
  (cos (cbrt (* (pow (* 2.0 PI) 3.0) (pow u2 3.0))))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-log1pf(-u1)) * cosf(cbrtf((powf((2.0f * ((float) M_PI)), 3.0f) * powf(u2, 3.0f))));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(-log1p(Float32(-u1)))) * cos(cbrt(Float32((Float32(Float32(2.0) * Float32(pi)) ^ Float32(3.0)) * (u2 ^ Float32(3.0))))))
end
\begin{array}{l}

\\
\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\sqrt[3]{{\left(2 \cdot \pi\right)}^{3} \cdot {u2}^{3}}\right)
\end{array}
Derivation
  1. Initial program 53.7%

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

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

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

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

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\color{blue}{\sqrt[3]{\left(\left(2 \cdot \pi\right) \cdot \left(2 \cdot \pi\right)\right) \cdot \left(2 \cdot \pi\right)}} \cdot u2\right) \]
    2. add-cbrt-cube99.1%

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

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

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\sqrt[3]{\color{blue}{{\left(2 \cdot \pi\right)}^{3}} \cdot \left(\left(u2 \cdot u2\right) \cdot u2\right)}\right) \]
    5. pow399.1%

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\sqrt[3]{{\left(2 \cdot \pi\right)}^{3} \cdot \color{blue}{{u2}^{3}}}\right) \]
  5. Applied egg-rr99.1%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \color{blue}{\left(\sqrt[3]{{\left(2 \cdot \pi\right)}^{3} \cdot {u2}^{3}}\right)} \]
  6. Final simplification99.1%

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

Alternative 3: 94.8% accurate, 0.8× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\


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

    1. Initial program 51.4%

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

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

        \[\leadsto \sqrt{-\left(\color{blue}{u1 \cdot -1} + -0.5 \cdot {u1}^{2}\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      2. *-commutative90.3%

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

        \[\leadsto \sqrt{-\left(u1 \cdot -1 + \color{blue}{\left(u1 \cdot u1\right)} \cdot -0.5\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      4. associate-*l*90.3%

        \[\leadsto \sqrt{-\left(u1 \cdot -1 + \color{blue}{u1 \cdot \left(u1 \cdot -0.5\right)}\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
      5. distribute-lft-out90.2%

        \[\leadsto \sqrt{-\color{blue}{u1 \cdot \left(-1 + u1 \cdot -0.5\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Simplified90.2%

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

    if 0.999999583 < (cos.f32 (*.f32 (*.f32 2 (PI.f32)) u2))

    1. Initial program 55.3%

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

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

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

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

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification95.8%

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

Alternative 4: 90.9% accurate, 0.8× speedup?

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

\\
\begin{array}{l}
t_0 := \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right)\\
\mathbf{if}\;t_0 \leq 0.9999970197677612:\\
\;\;\;\;t_0 \cdot \sqrt{u1}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\


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

    1. Initial program 51.4%

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

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

        \[\leadsto \sqrt{-\color{blue}{\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Simplified80.8%

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

    if 0.99999702 < (cos.f32 (*.f32 (*.f32 2 (PI.f32)) u2))

    1. Initial program 55.0%

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

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

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

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

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

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

Alternative 5: 99.0% accurate, 1.0× speedup?

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

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

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

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

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

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

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

Alternative 6: 80.4% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \sqrt{-\mathsf{log1p}\left(-u1\right)} \end{array} \]
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt (- (log1p (- u1)))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-log1pf(-u1));
}
function code(cosTheta_i, u1, u2)
	return sqrt(Float32(-log1p(Float32(-u1))))
end
\begin{array}{l}

\\
\sqrt{-\mathsf{log1p}\left(-u1\right)}
\end{array}
Derivation
  1. Initial program 53.7%

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

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

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

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

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  5. Final simplification79.5%

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \]

Alternative 7: 65.1% accurate, 4.0× speedup?

\[\begin{array}{l} \\ \sqrt{u1} \end{array} \]
(FPCore (cosTheta_i u1 u2) :precision binary32 (sqrt u1))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(u1);
}
real(4) function code(costheta_i, u1, u2)
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = sqrt(u1)
end function
function code(cosTheta_i, u1, u2)
	return sqrt(u1)
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt(u1);
end
\begin{array}{l}

\\
\sqrt{u1}
\end{array}
Derivation
  1. Initial program 53.7%

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

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

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

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

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{1} \]
  5. Step-by-step derivation
    1. add-cbrt-cube79.5%

      \[\leadsto \color{blue}{\sqrt[3]{\left(\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sqrt{-\mathsf{log1p}\left(-u1\right)}\right) \cdot \sqrt{-\mathsf{log1p}\left(-u1\right)}}} \cdot 1 \]
    2. pow1/377.1%

      \[\leadsto \color{blue}{{\left(\left(\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sqrt{-\mathsf{log1p}\left(-u1\right)}\right) \cdot \sqrt{-\mathsf{log1p}\left(-u1\right)}\right)}^{0.3333333333333333}} \cdot 1 \]
  6. Applied egg-rr63.6%

    \[\leadsto \color{blue}{{\left({\left(\mathsf{log1p}\left(u1\right)\right)}^{1.5}\right)}^{0.3333333333333333}} \cdot 1 \]
  7. Taylor expanded in u1 around 0 66.2%

    \[\leadsto \color{blue}{\sqrt{u1}} \cdot 1 \]
  8. Final simplification66.2%

    \[\leadsto \sqrt{u1} \]

Reproduce

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