Beckmann Sample, near normal, slope_y

Percentage Accurate: 58.5% → 98.4%
Time: 22.9s
Alternatives: 7
Speedup: 2.9×

Specification

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

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

Sampling outcomes in binary32 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 7 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 58.5% 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.4% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{\pi \cdot 2}\\ \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \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 (* PI 2.0))))
   (* (sqrt (- (log1p (- u1)))) (sin (* t_0 (* u2 t_0))))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = sqrtf((((float) M_PI) * 2.0f));
	return sqrtf(-log1pf(-u1)) * sinf((t_0 * (u2 * t_0)));
}
function code(cosTheta_i, u1, u2)
	t_0 = sqrt(Float32(Float32(pi) * Float32(2.0)))
	return Float32(sqrt(Float32(-log1p(Float32(-u1)))) * sin(Float32(t_0 * Float32(u2 * t_0))))
end
\begin{array}{l}

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

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

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

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

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

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

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

      \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(e^{\mathsf{log1p}\left(\color{blue}{2 \cdot \left(\pi \cdot u2\right)}\right)} - 1\right) \]
  6. Applied egg-rr64.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 98.4% accurate, 0.6× speedup?

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

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

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

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

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

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

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

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

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

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

Alternative 3: 98.4% accurate, 1.0× speedup?

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

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

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

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

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

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

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

Alternative 4: 90.4% accurate, 1.4× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;\sqrt{u1} \cdot \sin t\_0\\


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

    1. Initial program 63.0%

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

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

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

      \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \sin \left(\left(2 \cdot \pi\right) \cdot u2\right)} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. add-exp-log94.4%

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

        \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot e^{\log \sin \color{blue}{\left(2 \cdot \left(\pi \cdot u2\right)\right)}} \]
    6. Applied egg-rr94.4%

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

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

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

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

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

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

    1. Initial program 57.0%

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

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

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

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

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

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

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

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

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

Alternative 5: 76.0% accurate, 1.5× speedup?

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

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

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

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

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

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

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

      \[\leadsto \sqrt{u1} \cdot \sin \color{blue}{\left(\left(2 \cdot u2\right) \cdot \pi\right)} \]
    2. *-commutative74.0%

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

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

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

Alternative 6: 65.7% accurate, 2.9× speedup?

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

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

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

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

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

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

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

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

      \[\leadsto 2 \cdot \color{blue}{\left(\pi \cdot \left(\sqrt{u1} \cdot u2\right)\right)} \]
    3. *-commutative63.2%

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

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

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

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

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

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

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

Alternative 7: 65.7% accurate, 2.9× speedup?

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

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

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

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

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

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

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

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

      \[\leadsto 2 \cdot \color{blue}{\left(\pi \cdot \left(\sqrt{u1} \cdot u2\right)\right)} \]
    3. *-commutative63.2%

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

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

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

Reproduce

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