UniformSampleCone, x

Percentage Accurate: 57.5% → 98.8%
Time: 15.8s
Alternatives: 17
Speedup: 9.8×

Specification

?
\[\left(\left(2.328306437 \cdot 10^{-10} \leq ux \land ux \leq 1\right) \land \left(2.328306437 \cdot 10^{-10} \leq uy \land uy \leq 1\right)\right) \land \left(0 \leq maxCos \land maxCos \leq 1\right)\]
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(1 - ux\right) + ux \cdot maxCos\\ \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - t\_0 \cdot t\_0} \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos))))
   (* (cos (* (* uy 2.0) PI)) (sqrt (- 1.0 (* t_0 t_0))))))
float code(float ux, float uy, float maxCos) {
	float t_0 = (1.0f - ux) + (ux * maxCos);
	return cosf(((uy * 2.0f) * ((float) M_PI))) * sqrtf((1.0f - (t_0 * t_0)));
}
function code(ux, uy, maxCos)
	t_0 = Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos))
	return Float32(cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(Float32(1.0) - Float32(t_0 * t_0))))
end
function tmp = code(ux, uy, maxCos)
	t_0 = (single(1.0) - ux) + (ux * maxCos);
	tmp = cos(((uy * single(2.0)) * single(pi))) * sqrt((single(1.0) - (t_0 * t_0)));
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\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 17 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} \\ \begin{array}{l} t_0 := \left(1 - ux\right) + ux \cdot maxCos\\ \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - t\_0 \cdot t\_0} \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos))))
   (* (cos (* (* uy 2.0) PI)) (sqrt (- 1.0 (* t_0 t_0))))))
float code(float ux, float uy, float maxCos) {
	float t_0 = (1.0f - ux) + (ux * maxCos);
	return cosf(((uy * 2.0f) * ((float) M_PI))) * sqrtf((1.0f - (t_0 * t_0)));
}
function code(ux, uy, maxCos)
	t_0 = Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos))
	return Float32(cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(Float32(1.0) - Float32(t_0 * t_0))))
end
function tmp = code(ux, uy, maxCos)
	t_0 = (single(1.0) - ux) + (ux * maxCos);
	tmp = cos(((uy * single(2.0)) * single(pi))) * sqrt((single(1.0) - (t_0 * t_0)));
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}

Alternative 1: 98.8% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(uy \cdot \pi\right)\\ \left(t\_0 \cdot t\_0 - \sin \left(uy \cdot \left(\pi \cdot \log e\right)\right) \cdot \sin \left(uy \cdot \pi\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)} \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (cos (* uy PI))))
   (*
    (- (* t_0 t_0) (* (sin (* uy (* PI (log E)))) (sin (* uy PI))))
    (sqrt
     (*
      ux
      (fma maxCos -2.0 (fma (- 1.0 maxCos) (fma ux maxCos (- ux)) 2.0)))))))
float code(float ux, float uy, float maxCos) {
	float t_0 = cosf((uy * ((float) M_PI)));
	return ((t_0 * t_0) - (sinf((uy * (((float) M_PI) * logf(((float) M_E))))) * sinf((uy * ((float) M_PI))))) * sqrtf((ux * fmaf(maxCos, -2.0f, fmaf((1.0f - maxCos), fmaf(ux, maxCos, -ux), 2.0f))));
}
function code(ux, uy, maxCos)
	t_0 = cos(Float32(uy * Float32(pi)))
	return Float32(Float32(Float32(t_0 * t_0) - Float32(sin(Float32(uy * Float32(Float32(pi) * log(Float32(exp(1)))))) * sin(Float32(uy * Float32(pi))))) * sqrt(Float32(ux * fma(maxCos, Float32(-2.0), fma(Float32(Float32(1.0) - maxCos), fma(ux, maxCos, Float32(-ux)), Float32(2.0))))))
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(uy \cdot \pi\right)\\
\left(t\_0 \cdot t\_0 - \sin \left(uy \cdot \left(\pi \cdot \log e\right)\right) \cdot \sin \left(uy \cdot \pi\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)}
\end{array}
\end{array}
Derivation
  1. Initial program 57.5%

    \[\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in ux around 0

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}} \]
  4. Step-by-step derivation
    1. lower-*.f32N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}} \]
    2. cancel-sign-sub-invN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \left(\mathsf{neg}\left(2\right)\right) \cdot maxCos\right)}} \]
    3. metadata-evalN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \color{blue}{-2} \cdot maxCos\right)} \]
    4. +-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\left(-2 \cdot maxCos + \left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)\right)}} \]
    5. *-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\color{blue}{maxCos \cdot -2} + \left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)\right)} \]
    6. lower-fma.f32N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\mathsf{fma}\left(maxCos, -2, 2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)}} \]
    7. +-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{-1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right) + 2}\right)} \]
    8. mul-1-negN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(\mathsf{neg}\left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)} + 2\right)} \]
    9. *-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\color{blue}{{\left(maxCos - 1\right)}^{2} \cdot ux}\right)\right) + 2\right)} \]
    10. unpow2N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\color{blue}{\left(\left(maxCos - 1\right) \cdot \left(maxCos - 1\right)\right)} \cdot ux\right)\right) + 2\right)} \]
    11. associate-*l*N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\color{blue}{\left(maxCos - 1\right) \cdot \left(\left(maxCos - 1\right) \cdot ux\right)}\right)\right) + 2\right)} \]
    12. *-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\left(maxCos - 1\right) \cdot \color{blue}{\left(ux \cdot \left(maxCos - 1\right)\right)}\right)\right) + 2\right)} \]
    13. distribute-lft-neg-inN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(\mathsf{neg}\left(\left(maxCos - 1\right)\right)\right) \cdot \left(ux \cdot \left(maxCos - 1\right)\right)} + 2\right)} \]
    14. neg-sub0N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(0 - \left(maxCos - 1\right)\right)} \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
    15. associate-+l-N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(\left(0 - maxCos\right) + 1\right)} \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
    16. neg-sub0N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\color{blue}{\left(\mathsf{neg}\left(maxCos\right)\right)} + 1\right) \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
    17. mul-1-negN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\color{blue}{-1 \cdot maxCos} + 1\right) \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
    18. +-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(1 + -1 \cdot maxCos\right)} \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
  5. Applied rewrites98.8%

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\color{blue}{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)}} \]
  6. Step-by-step derivation
    1. lift-cos.f32N/A

      \[\leadsto \color{blue}{\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right)} \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    2. lift-*.f32N/A

      \[\leadsto \cos \color{blue}{\left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right)} \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    3. lift-*.f32N/A

      \[\leadsto \cos \left(\color{blue}{\left(uy \cdot 2\right)} \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    4. lift-PI.f32N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \color{blue}{\mathsf{PI}\left(\right)}\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    5. *-commutativeN/A

      \[\leadsto \cos \left(\color{blue}{\left(2 \cdot uy\right)} \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    6. lift-PI.f32N/A

      \[\leadsto \cos \left(\left(2 \cdot uy\right) \cdot \color{blue}{\mathsf{PI}\left(\right)}\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    7. associate-*l*N/A

      \[\leadsto \cos \color{blue}{\left(2 \cdot \left(uy \cdot \mathsf{PI}\left(\right)\right)\right)} \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    8. cos-2N/A

      \[\leadsto \color{blue}{\left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right)} \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    9. lower--.f32N/A

      \[\leadsto \color{blue}{\left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right)} \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    10. lower-*.f32N/A

      \[\leadsto \left(\color{blue}{\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right)} - \sin \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    11. lower-cos.f32N/A

      \[\leadsto \left(\color{blue}{\cos \left(uy \cdot \mathsf{PI}\left(\right)\right)} \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    12. lower-*.f32N/A

      \[\leadsto \left(\cos \color{blue}{\left(uy \cdot \mathsf{PI}\left(\right)\right)} \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    13. lower-cos.f32N/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \color{blue}{\cos \left(uy \cdot \mathsf{PI}\left(\right)\right)} - \sin \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    14. lower-*.f32N/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \color{blue}{\left(uy \cdot \mathsf{PI}\left(\right)\right)} - \sin \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    15. lower-*.f32N/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \color{blue}{\sin \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)}\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    16. lower-sin.f32N/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \color{blue}{\sin \left(uy \cdot \mathsf{PI}\left(\right)\right)} \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    17. lower-*.f32N/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \color{blue}{\left(uy \cdot \mathsf{PI}\left(\right)\right)} \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    18. lower-sin.f32N/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \color{blue}{\sin \left(uy \cdot \mathsf{PI}\left(\right)\right)}\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    19. lower-*.f3298.7

      \[\leadsto \left(\cos \left(uy \cdot \pi\right) \cdot \cos \left(uy \cdot \pi\right) - \sin \left(uy \cdot \pi\right) \cdot \sin \color{blue}{\left(uy \cdot \pi\right)}\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)} \]
  7. Applied rewrites98.7%

    \[\leadsto \color{blue}{\left(\cos \left(uy \cdot \pi\right) \cdot \cos \left(uy \cdot \pi\right) - \sin \left(uy \cdot \pi\right) \cdot \sin \left(uy \cdot \pi\right)\right)} \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)} \]
  8. Step-by-step derivation
    1. lift-PI.f32N/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \color{blue}{\mathsf{PI}\left(\right)}\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    2. add-log-expN/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \color{blue}{\log \left(e^{\mathsf{PI}\left(\right)}\right)}\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    3. *-un-lft-identityN/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \log \left(e^{\color{blue}{1 \cdot \mathsf{PI}\left(\right)}}\right)\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    4. lift-PI.f32N/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \log \left(e^{1 \cdot \color{blue}{\mathsf{PI}\left(\right)}}\right)\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    5. exp-prodN/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \log \color{blue}{\left({\left(e^{1}\right)}^{\mathsf{PI}\left(\right)}\right)}\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    6. log-powN/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \color{blue}{\left(\mathsf{PI}\left(\right) \cdot \log \left(e^{1}\right)\right)}\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    7. lower-*.f32N/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \color{blue}{\left(\mathsf{PI}\left(\right) \cdot \log \left(e^{1}\right)\right)}\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    8. lower-log.f32N/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \left(\mathsf{PI}\left(\right) \cdot \color{blue}{\log \left(e^{1}\right)}\right)\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    9. exp-1-eN/A

      \[\leadsto \left(\cos \left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \cos \left(uy \cdot \mathsf{PI}\left(\right)\right) - \sin \left(uy \cdot \left(\mathsf{PI}\left(\right) \cdot \log \color{blue}{\mathsf{E}\left(\right)}\right)\right) \cdot \sin \left(uy \cdot \mathsf{PI}\left(\right)\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
    10. lower-E.f3298.9

      \[\leadsto \left(\cos \left(uy \cdot \pi\right) \cdot \cos \left(uy \cdot \pi\right) - \sin \left(uy \cdot \left(\pi \cdot \log \color{blue}{e}\right)\right) \cdot \sin \left(uy \cdot \pi\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)} \]
  9. Applied rewrites98.9%

    \[\leadsto \left(\cos \left(uy \cdot \pi\right) \cdot \cos \left(uy \cdot \pi\right) - \sin \left(uy \cdot \color{blue}{\left(\pi \cdot \log e\right)}\right) \cdot \sin \left(uy \cdot \pi\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)} \]
  10. Add Preprocessing

Alternative 2: 99.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos \left(\pi \cdot \left(uy \cdot 2\right)\right) \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), maxCos \cdot -2\right), ux, ux \cdot 2\right)} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (*
  (cos (* PI (* uy 2.0)))
  (sqrt
   (fma
    (fma (- 1.0 maxCos) (fma ux maxCos (- ux)) (* maxCos -2.0))
    ux
    (* ux 2.0)))))
float code(float ux, float uy, float maxCos) {
	return cosf((((float) M_PI) * (uy * 2.0f))) * sqrtf(fmaf(fmaf((1.0f - maxCos), fmaf(ux, maxCos, -ux), (maxCos * -2.0f)), ux, (ux * 2.0f)));
}
function code(ux, uy, maxCos)
	return Float32(cos(Float32(Float32(pi) * Float32(uy * Float32(2.0)))) * sqrt(fma(fma(Float32(Float32(1.0) - maxCos), fma(ux, maxCos, Float32(-ux)), Float32(maxCos * Float32(-2.0))), ux, Float32(ux * Float32(2.0)))))
end
\begin{array}{l}

\\
\cos \left(\pi \cdot \left(uy \cdot 2\right)\right) \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), maxCos \cdot -2\right), ux, ux \cdot 2\right)}
\end{array}
Derivation
  1. Initial program 57.5%

    \[\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in ux around 0

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}} \]
  4. Step-by-step derivation
    1. lower-*.f32N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}} \]
    2. cancel-sign-sub-invN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \left(\mathsf{neg}\left(2\right)\right) \cdot maxCos\right)}} \]
    3. metadata-evalN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \color{blue}{-2} \cdot maxCos\right)} \]
    4. +-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\left(-2 \cdot maxCos + \left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)\right)}} \]
    5. *-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\color{blue}{maxCos \cdot -2} + \left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)\right)} \]
    6. lower-fma.f32N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\mathsf{fma}\left(maxCos, -2, 2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)}} \]
    7. +-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{-1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right) + 2}\right)} \]
    8. mul-1-negN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(\mathsf{neg}\left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)} + 2\right)} \]
    9. *-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\color{blue}{{\left(maxCos - 1\right)}^{2} \cdot ux}\right)\right) + 2\right)} \]
    10. unpow2N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\color{blue}{\left(\left(maxCos - 1\right) \cdot \left(maxCos - 1\right)\right)} \cdot ux\right)\right) + 2\right)} \]
    11. associate-*l*N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\color{blue}{\left(maxCos - 1\right) \cdot \left(\left(maxCos - 1\right) \cdot ux\right)}\right)\right) + 2\right)} \]
    12. *-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\left(maxCos - 1\right) \cdot \color{blue}{\left(ux \cdot \left(maxCos - 1\right)\right)}\right)\right) + 2\right)} \]
    13. distribute-lft-neg-inN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(\mathsf{neg}\left(\left(maxCos - 1\right)\right)\right) \cdot \left(ux \cdot \left(maxCos - 1\right)\right)} + 2\right)} \]
    14. neg-sub0N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(0 - \left(maxCos - 1\right)\right)} \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
    15. associate-+l-N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(\left(0 - maxCos\right) + 1\right)} \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
    16. neg-sub0N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\color{blue}{\left(\mathsf{neg}\left(maxCos\right)\right)} + 1\right) \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
    17. mul-1-negN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\color{blue}{-1 \cdot maxCos} + 1\right) \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
    18. +-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(1 + -1 \cdot maxCos\right)} \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
  5. Applied rewrites98.9%

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\color{blue}{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)}} \]
  6. Step-by-step derivation
    1. Applied rewrites99.0%

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), maxCos \cdot -2\right), \color{blue}{ux}, 2 \cdot ux\right)} \]
    2. Final simplification99.0%

      \[\leadsto \cos \left(\pi \cdot \left(uy \cdot 2\right)\right) \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), maxCos \cdot -2\right), ux, ux \cdot 2\right)} \]
    3. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 2024228 
    (FPCore (ux uy maxCos)
      :name "UniformSampleCone, x"
      :precision binary32
      :pre (and (and (and (<= 2.328306437e-10 ux) (<= ux 1.0)) (and (<= 2.328306437e-10 uy) (<= uy 1.0))) (and (<= 0.0 maxCos) (<= maxCos 1.0)))
      (* (cos (* (* uy 2.0) PI)) (sqrt (- 1.0 (* (+ (- 1.0 ux) (* ux maxCos)) (+ (- 1.0 ux) (* ux maxCos)))))))