UniformSampleCone, x

Percentage Accurate: 56.9% → 99.0%
Time: 16.9s
Alternatives: 19
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 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: 56.9% 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: 99.0% accurate, 1.0× speedup?

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

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

    \[\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. +-commutativeN/A

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

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

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

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

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\color{blue}{ux \cdot \left(\mathsf{neg}\left({\left(maxCos - 1\right)}^{2}\right)\right)} + \left(2 + -2 \cdot maxCos\right)\right)} \]
    8. 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(ux, \mathsf{neg}\left({\left(maxCos - 1\right)}^{2}\right), 2 + -2 \cdot maxCos\right)}} \]
    9. unpow2N/A

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

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

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(ux, \left(maxCos - 1\right) \cdot \color{blue}{\left(0 - \left(maxCos - 1\right)\right)}, 2 + -2 \cdot maxCos\right)} \]
    12. 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(ux, \left(maxCos - 1\right) \cdot \color{blue}{\left(\left(0 - maxCos\right) + 1\right)}, 2 + -2 \cdot maxCos\right)} \]
    13. neg-sub0N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(ux, \left(maxCos - 1\right) \cdot \left(\color{blue}{\left(\mathsf{neg}\left(maxCos\right)\right)} + 1\right), 2 + -2 \cdot maxCos\right)} \]
    14. 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(ux, \left(maxCos - 1\right) \cdot \left(\color{blue}{-1 \cdot maxCos} + 1\right), 2 + -2 \cdot maxCos\right)} \]
    15. +-commutativeN/A

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

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

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

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

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(ux, \color{blue}{\left(maxCos + -1\right)} \cdot \left(1 + -1 \cdot maxCos\right), 2 + -2 \cdot maxCos\right)} \]
    20. 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(ux, \left(maxCos + -1\right) \cdot \left(1 + \color{blue}{\left(\mathsf{neg}\left(maxCos\right)\right)}\right), 2 + -2 \cdot maxCos\right)} \]
    21. unsub-negN/A

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

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \color{blue}{\left(1 - maxCos\right)}, 2 + -2 \cdot maxCos\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(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right)}} \]
  6. Step-by-step derivation
    1. Applied rewrites98.9%

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

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

    Alternative 2: 97.5% accurate, 0.6× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\left(uy \cdot 2\right) \cdot \pi\right)\\ \mathbf{if}\;t\_0 \leq 0.996399998664856:\\ \;\;\;\;t\_0 \cdot \sqrt{ux \cdot \left(2 - ux\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(maxCos, -2, 2\right), ux, \left(ux \cdot \left(maxCos + -1\right)\right) \cdot \left(ux \cdot \left(1 - maxCos\right)\right)\right)} \cdot \mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \pi \cdot \pi, 1\right)\\ \end{array} \end{array} \]
    (FPCore (ux uy maxCos)
     :precision binary32
     (let* ((t_0 (cos (* (* uy 2.0) PI))))
       (if (<= t_0 0.996399998664856)
         (* t_0 (sqrt (* ux (- 2.0 ux))))
         (*
          (sqrt
           (fma
            (fma maxCos -2.0 2.0)
            ux
            (* (* ux (+ maxCos -1.0)) (* ux (- 1.0 maxCos)))))
          (fma (* -2.0 (* uy uy)) (* PI PI) 1.0)))))
    float code(float ux, float uy, float maxCos) {
    	float t_0 = cosf(((uy * 2.0f) * ((float) M_PI)));
    	float tmp;
    	if (t_0 <= 0.996399998664856f) {
    		tmp = t_0 * sqrtf((ux * (2.0f - ux)));
    	} else {
    		tmp = sqrtf(fmaf(fmaf(maxCos, -2.0f, 2.0f), ux, ((ux * (maxCos + -1.0f)) * (ux * (1.0f - maxCos))))) * fmaf((-2.0f * (uy * uy)), (((float) M_PI) * ((float) M_PI)), 1.0f);
    	}
    	return tmp;
    }
    
    function code(ux, uy, maxCos)
    	t_0 = cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi)))
    	tmp = Float32(0.0)
    	if (t_0 <= Float32(0.996399998664856))
    		tmp = Float32(t_0 * sqrt(Float32(ux * Float32(Float32(2.0) - ux))));
    	else
    		tmp = Float32(sqrt(fma(fma(maxCos, Float32(-2.0), Float32(2.0)), ux, Float32(Float32(ux * Float32(maxCos + Float32(-1.0))) * Float32(ux * Float32(Float32(1.0) - maxCos))))) * fma(Float32(Float32(-2.0) * Float32(uy * uy)), Float32(Float32(pi) * Float32(pi)), Float32(1.0)));
    	end
    	return tmp
    end
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    t_0 := \cos \left(\left(uy \cdot 2\right) \cdot \pi\right)\\
    \mathbf{if}\;t\_0 \leq 0.996399998664856:\\
    \;\;\;\;t\_0 \cdot \sqrt{ux \cdot \left(2 - ux\right)}\\
    
    \mathbf{else}:\\
    \;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(maxCos, -2, 2\right), ux, \left(ux \cdot \left(maxCos + -1\right)\right) \cdot \left(ux \cdot \left(1 - maxCos\right)\right)\right)} \cdot \mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \pi \cdot \pi, 1\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (cos.f32 (*.f32 (*.f32 uy #s(literal 2 binary32)) (PI.f32))) < 0.996399999

      1. Initial program 57.7%

        \[\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. +-commutativeN/A

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

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

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

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

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\color{blue}{ux \cdot \left(\mathsf{neg}\left({\left(maxCos - 1\right)}^{2}\right)\right)} + \left(2 + -2 \cdot maxCos\right)\right)} \]
        8. 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(ux, \mathsf{neg}\left({\left(maxCos - 1\right)}^{2}\right), 2 + -2 \cdot maxCos\right)}} \]
        9. unpow2N/A

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

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

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(ux, \left(maxCos - 1\right) \cdot \color{blue}{\left(0 - \left(maxCos - 1\right)\right)}, 2 + -2 \cdot maxCos\right)} \]
        12. 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(ux, \left(maxCos - 1\right) \cdot \color{blue}{\left(\left(0 - maxCos\right) + 1\right)}, 2 + -2 \cdot maxCos\right)} \]
        13. neg-sub0N/A

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(ux, \left(maxCos - 1\right) \cdot \left(\color{blue}{\left(\mathsf{neg}\left(maxCos\right)\right)} + 1\right), 2 + -2 \cdot maxCos\right)} \]
        14. 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(ux, \left(maxCos - 1\right) \cdot \left(\color{blue}{-1 \cdot maxCos} + 1\right), 2 + -2 \cdot maxCos\right)} \]
        15. +-commutativeN/A

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

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

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

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

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(ux, \color{blue}{\left(maxCos + -1\right)} \cdot \left(1 + -1 \cdot maxCos\right), 2 + -2 \cdot maxCos\right)} \]
        20. 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(ux, \left(maxCos + -1\right) \cdot \left(1 + \color{blue}{\left(\mathsf{neg}\left(maxCos\right)\right)}\right), 2 + -2 \cdot maxCos\right)} \]
        21. unsub-negN/A

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

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

        \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\color{blue}{ux \cdot \mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right)}} \]
      6. Taylor expanded in maxCos around 0

        \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\left(2 + -1 \cdot ux\right)}} \]
      7. Step-by-step derivation
        1. Applied rewrites91.2%

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

        if 0.996399999 < (cos.f32 (*.f32 (*.f32 uy #s(literal 2 binary32)) (PI.f32)))

        1. Initial program 56.7%

          \[\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. +-commutativeN/A

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

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

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

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

            \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\color{blue}{ux \cdot \left(\mathsf{neg}\left({\left(maxCos - 1\right)}^{2}\right)\right)} + \left(2 + -2 \cdot maxCos\right)\right)} \]
          8. 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(ux, \mathsf{neg}\left({\left(maxCos - 1\right)}^{2}\right), 2 + -2 \cdot maxCos\right)}} \]
          9. unpow2N/A

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

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

            \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(ux, \left(maxCos - 1\right) \cdot \color{blue}{\left(0 - \left(maxCos - 1\right)\right)}, 2 + -2 \cdot maxCos\right)} \]
          12. 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(ux, \left(maxCos - 1\right) \cdot \color{blue}{\left(\left(0 - maxCos\right) + 1\right)}, 2 + -2 \cdot maxCos\right)} \]
          13. neg-sub0N/A

            \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(ux, \left(maxCos - 1\right) \cdot \left(\color{blue}{\left(\mathsf{neg}\left(maxCos\right)\right)} + 1\right), 2 + -2 \cdot maxCos\right)} \]
          14. 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(ux, \left(maxCos - 1\right) \cdot \left(\color{blue}{-1 \cdot maxCos} + 1\right), 2 + -2 \cdot maxCos\right)} \]
          15. +-commutativeN/A

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

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

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

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

            \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(ux, \color{blue}{\left(maxCos + -1\right)} \cdot \left(1 + -1 \cdot maxCos\right), 2 + -2 \cdot maxCos\right)} \]
          20. 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(ux, \left(maxCos + -1\right) \cdot \left(1 + \color{blue}{\left(\mathsf{neg}\left(maxCos\right)\right)}\right), 2 + -2 \cdot maxCos\right)} \]
          21. unsub-negN/A

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

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

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

            \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(maxCos, -2, 2\right), \color{blue}{ux}, \left(ux \cdot \left(maxCos + -1\right)\right) \cdot \left(\left(1 - maxCos\right) \cdot ux\right)\right)} \]
          2. Taylor expanded in uy around 0

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

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

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

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

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

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

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

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

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

              \[\leadsto \mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \color{blue}{\mathsf{PI}\left(\right)} \cdot \mathsf{PI}\left(\right), 1\right) \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(maxCos, -2, 2\right), ux, \left(ux \cdot \left(maxCos + -1\right)\right) \cdot \left(\left(1 - maxCos\right) \cdot ux\right)\right)} \]
            10. lower-PI.f3299.0

              \[\leadsto \mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \pi \cdot \color{blue}{\pi}, 1\right) \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(maxCos, -2, 2\right), ux, \left(ux \cdot \left(maxCos + -1\right)\right) \cdot \left(\left(1 - maxCos\right) \cdot ux\right)\right)} \]
          4. Applied rewrites99.0%

            \[\leadsto \color{blue}{\mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \pi \cdot \pi, 1\right)} \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(maxCos, -2, 2\right), ux, \left(ux \cdot \left(maxCos + -1\right)\right) \cdot \left(\left(1 - maxCos\right) \cdot ux\right)\right)} \]
        7. Recombined 2 regimes into one program.
        8. Final simplification97.5%

          \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \leq 0.996399998664856:\\ \;\;\;\;\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 - ux\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(maxCos, -2, 2\right), ux, \left(ux \cdot \left(maxCos + -1\right)\right) \cdot \left(ux \cdot \left(1 - maxCos\right)\right)\right)} \cdot \mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \pi \cdot \pi, 1\right)\\ \end{array} \]
        9. Add Preprocessing

        Reproduce

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