UniformSampleCone 2

Percentage Accurate: 98.9% → 98.8%
Time: 25.3s
Alternatives: 7
Speedup: N/A×

Specification

?
\[\left(\left(\left(\left(\left(-10000 \leq xi \land xi \leq 10000\right) \land \left(-10000 \leq yi \land yi \leq 10000\right)\right) \land \left(-10000 \leq zi \land zi \leq 10000\right)\right) \land \left(2.328306437 \cdot 10^{-10} \leq ux \land ux \leq 1\right)\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(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\\ t_1 := \sqrt{1 - t\_0 \cdot t\_0}\\ t_2 := \left(uy \cdot 2\right) \cdot \pi\\ \left(\left(\cos t\_2 \cdot t\_1\right) \cdot xi + \left(\sin t\_2 \cdot t\_1\right) \cdot yi\right) + t\_0 \cdot zi \end{array} \end{array} \]
(FPCore (xi yi zi ux uy maxCos)
 :precision binary32
 (let* ((t_0 (* (* (- 1.0 ux) maxCos) ux))
        (t_1 (sqrt (- 1.0 (* t_0 t_0))))
        (t_2 (* (* uy 2.0) PI)))
   (+ (+ (* (* (cos t_2) t_1) xi) (* (* (sin t_2) t_1) yi)) (* t_0 zi))))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
	float t_0 = ((1.0f - ux) * maxCos) * ux;
	float t_1 = sqrtf((1.0f - (t_0 * t_0)));
	float t_2 = (uy * 2.0f) * ((float) M_PI);
	return (((cosf(t_2) * t_1) * xi) + ((sinf(t_2) * t_1) * yi)) + (t_0 * zi);
}
function code(xi, yi, zi, ux, uy, maxCos)
	t_0 = Float32(Float32(Float32(Float32(1.0) - ux) * maxCos) * ux)
	t_1 = sqrt(Float32(Float32(1.0) - Float32(t_0 * t_0)))
	t_2 = Float32(Float32(uy * Float32(2.0)) * Float32(pi))
	return Float32(Float32(Float32(Float32(cos(t_2) * t_1) * xi) + Float32(Float32(sin(t_2) * t_1) * yi)) + Float32(t_0 * zi))
end
function tmp = code(xi, yi, zi, ux, uy, maxCos)
	t_0 = ((single(1.0) - ux) * maxCos) * ux;
	t_1 = sqrt((single(1.0) - (t_0 * t_0)));
	t_2 = (uy * single(2.0)) * single(pi);
	tmp = (((cos(t_2) * t_1) * xi) + ((sin(t_2) * t_1) * yi)) + (t_0 * zi);
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\\
t_1 := \sqrt{1 - t\_0 \cdot t\_0}\\
t_2 := \left(uy \cdot 2\right) \cdot \pi\\
\left(\left(\cos t\_2 \cdot t\_1\right) \cdot xi + \left(\sin t\_2 \cdot t\_1\right) \cdot yi\right) + t\_0 \cdot zi
\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 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: 98.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\\ t_1 := \sqrt{1 - t\_0 \cdot t\_0}\\ t_2 := \left(uy \cdot 2\right) \cdot \pi\\ \left(\left(\cos t\_2 \cdot t\_1\right) \cdot xi + \left(\sin t\_2 \cdot t\_1\right) \cdot yi\right) + t\_0 \cdot zi \end{array} \end{array} \]
(FPCore (xi yi zi ux uy maxCos)
 :precision binary32
 (let* ((t_0 (* (* (- 1.0 ux) maxCos) ux))
        (t_1 (sqrt (- 1.0 (* t_0 t_0))))
        (t_2 (* (* uy 2.0) PI)))
   (+ (+ (* (* (cos t_2) t_1) xi) (* (* (sin t_2) t_1) yi)) (* t_0 zi))))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
	float t_0 = ((1.0f - ux) * maxCos) * ux;
	float t_1 = sqrtf((1.0f - (t_0 * t_0)));
	float t_2 = (uy * 2.0f) * ((float) M_PI);
	return (((cosf(t_2) * t_1) * xi) + ((sinf(t_2) * t_1) * yi)) + (t_0 * zi);
}
function code(xi, yi, zi, ux, uy, maxCos)
	t_0 = Float32(Float32(Float32(Float32(1.0) - ux) * maxCos) * ux)
	t_1 = sqrt(Float32(Float32(1.0) - Float32(t_0 * t_0)))
	t_2 = Float32(Float32(uy * Float32(2.0)) * Float32(pi))
	return Float32(Float32(Float32(Float32(cos(t_2) * t_1) * xi) + Float32(Float32(sin(t_2) * t_1) * yi)) + Float32(t_0 * zi))
end
function tmp = code(xi, yi, zi, ux, uy, maxCos)
	t_0 = ((single(1.0) - ux) * maxCos) * ux;
	t_1 = sqrt((single(1.0) - (t_0 * t_0)));
	t_2 = (uy * single(2.0)) * single(pi);
	tmp = (((cos(t_2) * t_1) * xi) + ((sin(t_2) * t_1) * yi)) + (t_0 * zi);
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\\
t_1 := \sqrt{1 - t\_0 \cdot t\_0}\\
t_2 := \left(uy \cdot 2\right) \cdot \pi\\
\left(\left(\cos t\_2 \cdot t\_1\right) \cdot xi + \left(\sin t\_2 \cdot t\_1\right) \cdot yi\right) + t\_0 \cdot zi
\end{array}
\end{array}

Alternative 1: 98.8% accurate, 1.0× speedup?

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

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

    \[\left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
  2. Step-by-step derivation
    1. associate-+l+99.3%

      \[\leadsto \color{blue}{\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right)} \]
    2. associate-*l*99.3%

      \[\leadsto \color{blue}{\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \left(\sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)} \cdot xi\right)} + \left(\left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right) \]
    3. fma-define99.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right), \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)} \cdot xi, \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right)} \]
  3. Simplified99.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, \mathsf{fma}\left(\sin \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot yi, \left(1 - ux\right) \cdot \left(zi \cdot \left(ux \cdot maxCos\right)\right)\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in maxCos around 0 99.3%

    \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, \color{blue}{maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + yi \cdot \sin \left(2 \cdot \left(uy \cdot \pi\right)\right)}\right) \]
  6. Final simplification99.3%

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

Alternative 2: 98.8% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := ux \cdot \left(\left(1 - ux\right) \cdot maxCos\right)\\ t_1 := \pi \cdot \left(uy \cdot 2\right)\\ \left(xi \cdot \left(\cos t\_1 \cdot \sqrt{1 + t\_0 \cdot \left(ux \cdot \left(maxCos \cdot \left(ux + -1\right)\right)\right)}\right) + yi \cdot \sin t\_1\right) + zi \cdot t\_0 \end{array} \end{array} \]
(FPCore (xi yi zi ux uy maxCos)
 :precision binary32
 (let* ((t_0 (* ux (* (- 1.0 ux) maxCos))) (t_1 (* PI (* uy 2.0))))
   (+
    (+
     (* xi (* (cos t_1) (sqrt (+ 1.0 (* t_0 (* ux (* maxCos (+ ux -1.0))))))))
     (* yi (sin t_1)))
    (* zi t_0))))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
	float t_0 = ux * ((1.0f - ux) * maxCos);
	float t_1 = ((float) M_PI) * (uy * 2.0f);
	return ((xi * (cosf(t_1) * sqrtf((1.0f + (t_0 * (ux * (maxCos * (ux + -1.0f)))))))) + (yi * sinf(t_1))) + (zi * t_0);
}
function code(xi, yi, zi, ux, uy, maxCos)
	t_0 = Float32(ux * Float32(Float32(Float32(1.0) - ux) * maxCos))
	t_1 = Float32(Float32(pi) * Float32(uy * Float32(2.0)))
	return Float32(Float32(Float32(xi * Float32(cos(t_1) * sqrt(Float32(Float32(1.0) + Float32(t_0 * Float32(ux * Float32(maxCos * Float32(ux + Float32(-1.0))))))))) + Float32(yi * sin(t_1))) + Float32(zi * t_0))
end
function tmp = code(xi, yi, zi, ux, uy, maxCos)
	t_0 = ux * ((single(1.0) - ux) * maxCos);
	t_1 = single(pi) * (uy * single(2.0));
	tmp = ((xi * (cos(t_1) * sqrt((single(1.0) + (t_0 * (ux * (maxCos * (ux + single(-1.0))))))))) + (yi * sin(t_1))) + (zi * t_0);
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := ux \cdot \left(\left(1 - ux\right) \cdot maxCos\right)\\
t_1 := \pi \cdot \left(uy \cdot 2\right)\\
\left(xi \cdot \left(\cos t\_1 \cdot \sqrt{1 + t\_0 \cdot \left(ux \cdot \left(maxCos \cdot \left(ux + -1\right)\right)\right)}\right) + yi \cdot \sin t\_1\right) + zi \cdot t\_0
\end{array}
\end{array}
Derivation
  1. Initial program 99.2%

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

    \[\leadsto \left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \color{blue}{yi \cdot \sin \left(2 \cdot \left(uy \cdot \pi\right)\right)}\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
  4. Step-by-step derivation
    1. associate-*r*99.2%

      \[\leadsto \left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + yi \cdot \sin \color{blue}{\left(\left(2 \cdot uy\right) \cdot \pi\right)}\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
  5. Simplified99.2%

    \[\leadsto \left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \color{blue}{yi \cdot \sin \left(\left(2 \cdot uy\right) \cdot \pi\right)}\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
  6. Final simplification99.2%

    \[\leadsto \left(xi \cdot \left(\cos \left(\pi \cdot \left(uy \cdot 2\right)\right) \cdot \sqrt{1 + \left(ux \cdot \left(\left(1 - ux\right) \cdot maxCos\right)\right) \cdot \left(ux \cdot \left(maxCos \cdot \left(ux + -1\right)\right)\right)}\right) + yi \cdot \sin \left(\pi \cdot \left(uy \cdot 2\right)\right)\right) + zi \cdot \left(ux \cdot \left(\left(1 - ux\right) \cdot maxCos\right)\right) \]
  7. Add Preprocessing

Alternative 3: 95.9% accurate, 1.4× speedup?

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

\\
\begin{array}{l}
t_0 := \pi \cdot \left(uy \cdot 2\right)\\
\left(xi \cdot \left(\cos t\_0 \cdot \sqrt{1 + \left(ux \cdot \left(\left(1 - ux\right) \cdot maxCos\right)\right) \cdot \left(ux \cdot \left(maxCos \cdot \left(ux + -1\right)\right)\right)}\right) + yi \cdot \sin t\_0\right) + zi \cdot \left(ux \cdot maxCos\right)
\end{array}
\end{array}
Derivation
  1. Initial program 99.2%

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

    \[\leadsto \left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \color{blue}{yi \cdot \sin \left(2 \cdot \left(uy \cdot \pi\right)\right)}\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
  4. Step-by-step derivation
    1. associate-*r*99.2%

      \[\leadsto \left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + yi \cdot \sin \color{blue}{\left(\left(2 \cdot uy\right) \cdot \pi\right)}\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
  5. Simplified99.2%

    \[\leadsto \left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \color{blue}{yi \cdot \sin \left(\left(2 \cdot uy\right) \cdot \pi\right)}\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
  6. Taylor expanded in ux around 0 97.2%

    \[\leadsto \left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + yi \cdot \sin \left(\left(2 \cdot uy\right) \cdot \pi\right)\right) + \color{blue}{\left(maxCos \cdot ux\right)} \cdot zi \]
  7. Final simplification97.2%

    \[\leadsto \left(xi \cdot \left(\cos \left(\pi \cdot \left(uy \cdot 2\right)\right) \cdot \sqrt{1 + \left(ux \cdot \left(\left(1 - ux\right) \cdot maxCos\right)\right) \cdot \left(ux \cdot \left(maxCos \cdot \left(ux + -1\right)\right)\right)}\right) + yi \cdot \sin \left(\pi \cdot \left(uy \cdot 2\right)\right)\right) + zi \cdot \left(ux \cdot maxCos\right) \]
  8. Add Preprocessing

Alternative 4: 89.4% accurate, 1.4× speedup?

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

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

    \[\left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
  2. Step-by-step derivation
    1. associate-+l+99.3%

      \[\leadsto \color{blue}{\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right)} \]
    2. associate-*l*99.3%

      \[\leadsto \color{blue}{\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \left(\sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)} \cdot xi\right)} + \left(\left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right) \]
    3. fma-define99.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right), \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)} \cdot xi, \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right)} \]
  3. Simplified99.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, \mathsf{fma}\left(\sin \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot yi, \left(1 - ux\right) \cdot \left(zi \cdot \left(ux \cdot maxCos\right)\right)\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in maxCos around 0 99.3%

    \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, \color{blue}{maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + yi \cdot \sin \left(2 \cdot \left(uy \cdot \pi\right)\right)}\right) \]
  6. Step-by-step derivation
    1. add-cube-cbrt99.1%

      \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + yi \cdot \sin \left(2 \cdot \left(uy \cdot \color{blue}{\left(\left(\sqrt[3]{\pi} \cdot \sqrt[3]{\pi}\right) \cdot \sqrt[3]{\pi}\right)}\right)\right)\right) \]
    2. pow399.1%

      \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + yi \cdot \sin \left(2 \cdot \left(uy \cdot \color{blue}{{\left(\sqrt[3]{\pi}\right)}^{3}}\right)\right)\right) \]
  7. Applied egg-rr99.1%

    \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + yi \cdot \sin \left(2 \cdot \left(uy \cdot \color{blue}{{\left(\sqrt[3]{\pi}\right)}^{3}}\right)\right)\right) \]
  8. Taylor expanded in ux around 0 99.1%

    \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \color{blue}{\left(maxCos \cdot ux\right)}} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + yi \cdot \sin \left(2 \cdot \left(uy \cdot {\left(\sqrt[3]{\pi}\right)}^{3}\right)\right)\right) \]
  9. Taylor expanded in uy around 0 93.5%

    \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(maxCos \cdot ux\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + \color{blue}{2 \cdot \left(uy \cdot \left(yi \cdot \pi\right)\right)}\right) \]
  10. Step-by-step derivation
    1. associate-*r*93.6%

      \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(maxCos \cdot ux\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + 2 \cdot \color{blue}{\left(\left(uy \cdot yi\right) \cdot \pi\right)}\right) \]
    2. *-commutative93.6%

      \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(maxCos \cdot ux\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + 2 \cdot \color{blue}{\left(\pi \cdot \left(uy \cdot yi\right)\right)}\right) \]
  11. Simplified93.6%

    \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(maxCos \cdot ux\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + \color{blue}{2 \cdot \left(\pi \cdot \left(uy \cdot yi\right)\right)}\right) \]
  12. Final simplification93.6%

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

Alternative 5: 89.4% accurate, 1.4× speedup?

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

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

    \[\left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
  2. Step-by-step derivation
    1. associate-+l+99.3%

      \[\leadsto \color{blue}{\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right)} \]
    2. associate-*l*99.3%

      \[\leadsto \color{blue}{\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \left(\sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)} \cdot xi\right)} + \left(\left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right) \]
    3. fma-define99.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right), \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)} \cdot xi, \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right)} \]
  3. Simplified99.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, \mathsf{fma}\left(\sin \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot yi, \left(1 - ux\right) \cdot \left(zi \cdot \left(ux \cdot maxCos\right)\right)\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in maxCos around 0 99.3%

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

    \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + \color{blue}{2 \cdot \left(uy \cdot \left(yi \cdot \pi\right)\right)}\right) \]
  7. Step-by-step derivation
    1. *-commutative93.5%

      \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + 2 \cdot \color{blue}{\left(\left(yi \cdot \pi\right) \cdot uy\right)}\right) \]
    2. associate-*r*93.5%

      \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + \color{blue}{\left(2 \cdot \left(yi \cdot \pi\right)\right) \cdot uy}\right) \]
    3. *-commutative93.5%

      \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + \color{blue}{\left(\left(yi \cdot \pi\right) \cdot 2\right)} \cdot uy\right) \]
    4. associate-*l*93.5%

      \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + \color{blue}{\left(yi \cdot \pi\right) \cdot \left(2 \cdot uy\right)}\right) \]
    5. *-commutative93.5%

      \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + \color{blue}{\left(\pi \cdot yi\right)} \cdot \left(2 \cdot uy\right)\right) \]
  8. Simplified93.5%

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

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

    \[\leadsto \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \color{blue}{\left(maxCos \cdot ux\right)} \cdot \left(maxCos \cdot ux\right)} \cdot xi, maxCos \cdot \left(ux \cdot \left(zi \cdot \left(1 - ux\right)\right)\right) + \left(\pi \cdot yi\right) \cdot \left(2 \cdot uy\right)\right) \]
  11. Final simplification93.5%

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

Alternative 6: 58.3% accurate, 1.4× speedup?

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

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

    \[\left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
  2. Step-by-step derivation
    1. associate-+l+99.3%

      \[\leadsto \color{blue}{\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right)} \]
    2. associate-*l*99.3%

      \[\leadsto \color{blue}{\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \left(\sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)} \cdot xi\right)} + \left(\left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right) \]
    3. fma-define99.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right), \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)} \cdot xi, \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right)} \]
  3. Simplified99.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, \mathsf{fma}\left(\sin \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot yi, \left(1 - ux\right) \cdot \left(zi \cdot \left(ux \cdot maxCos\right)\right)\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in uy around 0 62.9%

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

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

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

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

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

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

Alternative 7: 56.2% accurate, 1.4× speedup?

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

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

    \[\left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
  2. Step-by-step derivation
    1. associate-+l+99.3%

      \[\leadsto \color{blue}{\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right)} \]
    2. associate-*l*99.3%

      \[\leadsto \color{blue}{\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \left(\sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)} \cdot xi\right)} + \left(\left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right) \]
    3. fma-define99.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right), \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)} \cdot xi, \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi\right)} \]
  3. Simplified99.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot xi, \mathsf{fma}\left(\sin \left(uy \cdot \left(2 \cdot \pi\right)\right), \sqrt{1 - \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right) \cdot \left(\left(1 - ux\right) \cdot \left(ux \cdot maxCos\right)\right)} \cdot yi, \left(1 - ux\right) \cdot \left(zi \cdot \left(ux \cdot maxCos\right)\right)\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in uy around 0 62.9%

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

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

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

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

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

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

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

Reproduce

?
herbie shell --seed 2024086 
(FPCore (xi yi zi ux uy maxCos)
  :name "UniformSampleCone 2"
  :precision binary32
  :pre (and (and (and (and (and (and (<= -10000.0 xi) (<= xi 10000.0)) (and (<= -10000.0 yi) (<= yi 10000.0))) (and (<= -10000.0 zi) (<= zi 10000.0))) (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) maxCos) ux) (* (* (- 1.0 ux) maxCos) ux))))) xi) (* (* (sin (* (* uy 2.0) PI)) (sqrt (- 1.0 (* (* (* (- 1.0 ux) maxCos) ux) (* (* (- 1.0 ux) maxCos) ux))))) yi)) (* (* (* (- 1.0 ux) maxCos) ux) zi)))