UniformSampleCone, x

Percentage Accurate: 57.1% → 99.0%
Time: 17.0s
Alternatives: 7
Speedup: 2.2×

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 7 alternatives:

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

Initial Program: 57.1% 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, 0.7× speedup?

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

\\
\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(\left(2 - ux \cdot {\left(-1 + maxCos\right)}^{2}\right) - 2 \cdot maxCos\right)}
\end{array}
Derivation
  1. Initial program 59.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 98.7%

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\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. Final simplification98.7%

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

Alternative 2: 98.2% accurate, 1.0× speedup?

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

\\
\cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{ux \cdot \left(2 + \left(maxCos \cdot -2 + ux \cdot \left(-1 + 2 \cdot maxCos\right)\right)\right)}
\end{array}
Derivation
  1. Initial program 59.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. Step-by-step derivation
    1. associate-*l*59.5%

      \[\leadsto \cos \color{blue}{\left(uy \cdot \left(2 \cdot \pi\right)\right)} \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
    2. sub-neg59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{1 + \left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)\right)}} \]
    3. +-commutative59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{\left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)\right) + 1}} \]
    4. distribute-rgt-neg-in59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right)\right)} + 1} \]
    5. fma-define59.7%

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

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

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

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

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

Alternative 3: 92.4% accurate, 1.1× speedup?

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

\\
\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 - ux\right)}
\end{array}
Derivation
  1. Initial program 59.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 98.7%

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\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. Taylor expanded in maxCos around 0 92.4%

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\color{blue}{ux \cdot \left(2 + -1 \cdot ux\right)}} \]
  5. Step-by-step derivation
    1. neg-mul-192.4%

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 + \color{blue}{\left(-ux\right)}\right)} \]
    2. unsub-neg92.4%

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

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\color{blue}{ux \cdot \left(2 - ux\right)}} \]
  7. Final simplification92.4%

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 - ux\right)} \]
  8. Add Preprocessing

Alternative 4: 79.5% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \sqrt{\left(-maxCos \cdot \left(ux \cdot \left(2 + ux \cdot -2\right)\right)\right) - ux \cdot \left(ux + -2\right)} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (sqrt (- (- (* maxCos (* ux (+ 2.0 (* ux -2.0))))) (* ux (+ ux -2.0)))))
float code(float ux, float uy, float maxCos) {
	return sqrtf((-(maxCos * (ux * (2.0f + (ux * -2.0f)))) - (ux * (ux + -2.0f))));
}
real(4) function code(ux, uy, maxcos)
    real(4), intent (in) :: ux
    real(4), intent (in) :: uy
    real(4), intent (in) :: maxcos
    code = sqrt((-(maxcos * (ux * (2.0e0 + (ux * (-2.0e0))))) - (ux * (ux + (-2.0e0)))))
end function
function code(ux, uy, maxCos)
	return sqrt(Float32(Float32(-Float32(maxCos * Float32(ux * Float32(Float32(2.0) + Float32(ux * Float32(-2.0)))))) - Float32(ux * Float32(ux + Float32(-2.0)))))
end
function tmp = code(ux, uy, maxCos)
	tmp = sqrt((-(maxCos * (ux * (single(2.0) + (ux * single(-2.0))))) - (ux * (ux + single(-2.0)))));
end
\begin{array}{l}

\\
\sqrt{\left(-maxCos \cdot \left(ux \cdot \left(2 + ux \cdot -2\right)\right)\right) - ux \cdot \left(ux + -2\right)}
\end{array}
Derivation
  1. Initial program 59.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. Step-by-step derivation
    1. associate-*l*59.5%

      \[\leadsto \cos \color{blue}{\left(uy \cdot \left(2 \cdot \pi\right)\right)} \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
    2. sub-neg59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{1 + \left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)\right)}} \]
    3. +-commutative59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{\left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)\right) + 1}} \]
    4. distribute-rgt-neg-in59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right)\right)} + 1} \]
    5. fma-define59.7%

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

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

    \[\leadsto \color{blue}{\sqrt{1 + -1 \cdot \left(\left(1 + ux \cdot \left(maxCos - 1\right)\right) \cdot \left(\left(1 + maxCos \cdot ux\right) - ux\right)\right)}} \]
  6. Simplified47.9%

    \[\leadsto \color{blue}{\sqrt{1 + \left(-\left(1 + ux \cdot \left(-1 + maxCos\right)\right) \cdot \left(1 + \left(ux \cdot maxCos - ux\right)\right)\right)}} \]
  7. Taylor expanded in ux around 0 50.0%

    \[\leadsto \sqrt{1 + \left(-\color{blue}{\left(1 + ux \cdot \left(\left(2 \cdot maxCos + ux \cdot {\left(maxCos - 1\right)}^{2}\right) - 2\right)\right)}\right)} \]
  8. Taylor expanded in maxCos around 0 74.3%

    \[\leadsto \sqrt{\color{blue}{-1 \cdot \left(maxCos \cdot \left(ux \cdot \left(2 + -2 \cdot ux\right)\right)\right) - ux \cdot \left(ux - 2\right)}} \]
  9. Step-by-step derivation
    1. mul-1-neg74.3%

      \[\leadsto \sqrt{\color{blue}{\left(-maxCos \cdot \left(ux \cdot \left(2 + -2 \cdot ux\right)\right)\right)} - ux \cdot \left(ux - 2\right)} \]
    2. *-commutative74.3%

      \[\leadsto \sqrt{\left(-maxCos \cdot \left(ux \cdot \left(2 + \color{blue}{ux \cdot -2}\right)\right)\right) - ux \cdot \left(ux - 2\right)} \]
    3. sub-neg74.3%

      \[\leadsto \sqrt{\left(-maxCos \cdot \left(ux \cdot \left(2 + ux \cdot -2\right)\right)\right) - ux \cdot \color{blue}{\left(ux + \left(-2\right)\right)}} \]
    4. metadata-eval74.3%

      \[\leadsto \sqrt{\left(-maxCos \cdot \left(ux \cdot \left(2 + ux \cdot -2\right)\right)\right) - ux \cdot \left(ux + \color{blue}{-2}\right)} \]
  10. Simplified74.3%

    \[\leadsto \sqrt{\color{blue}{\left(-maxCos \cdot \left(ux \cdot \left(2 + ux \cdot -2\right)\right)\right) - ux \cdot \left(ux + -2\right)}} \]
  11. Final simplification74.3%

    \[\leadsto \sqrt{\left(-maxCos \cdot \left(ux \cdot \left(2 + ux \cdot -2\right)\right)\right) - ux \cdot \left(ux + -2\right)} \]
  12. Add Preprocessing

Alternative 5: 79.5% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \sqrt{ux \cdot \left(2 + \left(maxCos \cdot -2 + ux \cdot \left(-1 + 2 \cdot maxCos\right)\right)\right)} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (sqrt (* ux (+ 2.0 (+ (* maxCos -2.0) (* ux (+ -1.0 (* 2.0 maxCos))))))))
float code(float ux, float uy, float maxCos) {
	return sqrtf((ux * (2.0f + ((maxCos * -2.0f) + (ux * (-1.0f + (2.0f * maxCos)))))));
}
real(4) function code(ux, uy, maxcos)
    real(4), intent (in) :: ux
    real(4), intent (in) :: uy
    real(4), intent (in) :: maxcos
    code = sqrt((ux * (2.0e0 + ((maxcos * (-2.0e0)) + (ux * ((-1.0e0) + (2.0e0 * maxcos)))))))
end function
function code(ux, uy, maxCos)
	return sqrt(Float32(ux * Float32(Float32(2.0) + Float32(Float32(maxCos * Float32(-2.0)) + Float32(ux * Float32(Float32(-1.0) + Float32(Float32(2.0) * maxCos)))))))
end
function tmp = code(ux, uy, maxCos)
	tmp = sqrt((ux * (single(2.0) + ((maxCos * single(-2.0)) + (ux * (single(-1.0) + (single(2.0) * maxCos)))))));
end
\begin{array}{l}

\\
\sqrt{ux \cdot \left(2 + \left(maxCos \cdot -2 + ux \cdot \left(-1 + 2 \cdot maxCos\right)\right)\right)}
\end{array}
Derivation
  1. Initial program 59.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. Step-by-step derivation
    1. associate-*l*59.5%

      \[\leadsto \cos \color{blue}{\left(uy \cdot \left(2 \cdot \pi\right)\right)} \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
    2. sub-neg59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{1 + \left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)\right)}} \]
    3. +-commutative59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{\left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)\right) + 1}} \]
    4. distribute-rgt-neg-in59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right)\right)} + 1} \]
    5. fma-define59.7%

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

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

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

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

    \[\leadsto \color{blue}{\sqrt{ux \cdot \left(2 + \left(-2 \cdot maxCos + ux \cdot \left(2 \cdot maxCos - 1\right)\right)\right)}} \]
  8. Final simplification74.3%

    \[\leadsto \sqrt{ux \cdot \left(2 + \left(maxCos \cdot -2 + ux \cdot \left(-1 + 2 \cdot maxCos\right)\right)\right)} \]
  9. Add Preprocessing

Alternative 6: 75.5% accurate, 2.1× speedup?

\[\begin{array}{l} \\ \sqrt{ux \cdot \left(\left(-ux\right) - -2\right)} \end{array} \]
(FPCore (ux uy maxCos) :precision binary32 (sqrt (* ux (- (- ux) -2.0))))
float code(float ux, float uy, float maxCos) {
	return sqrtf((ux * (-ux - -2.0f)));
}
real(4) function code(ux, uy, maxcos)
    real(4), intent (in) :: ux
    real(4), intent (in) :: uy
    real(4), intent (in) :: maxcos
    code = sqrt((ux * (-ux - (-2.0e0))))
end function
function code(ux, uy, maxCos)
	return sqrt(Float32(ux * Float32(Float32(-ux) - Float32(-2.0))))
end
function tmp = code(ux, uy, maxCos)
	tmp = sqrt((ux * (-ux - single(-2.0))));
end
\begin{array}{l}

\\
\sqrt{ux \cdot \left(\left(-ux\right) - -2\right)}
\end{array}
Derivation
  1. Initial program 59.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. Step-by-step derivation
    1. associate-*l*59.5%

      \[\leadsto \cos \color{blue}{\left(uy \cdot \left(2 \cdot \pi\right)\right)} \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
    2. sub-neg59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{1 + \left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)\right)}} \]
    3. +-commutative59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{\left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)\right) + 1}} \]
    4. distribute-rgt-neg-in59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right)\right)} + 1} \]
    5. fma-define59.7%

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

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

    \[\leadsto \color{blue}{\sqrt{1 + -1 \cdot \left(\left(1 + ux \cdot \left(maxCos - 1\right)\right) \cdot \left(\left(1 + maxCos \cdot ux\right) - ux\right)\right)}} \]
  6. Simplified47.9%

    \[\leadsto \color{blue}{\sqrt{1 + \left(-\left(1 + ux \cdot \left(-1 + maxCos\right)\right) \cdot \left(1 + \left(ux \cdot maxCos - ux\right)\right)\right)}} \]
  7. Taylor expanded in ux around 0 50.0%

    \[\leadsto \sqrt{1 + \left(-\color{blue}{\left(1 + ux \cdot \left(\left(2 \cdot maxCos + ux \cdot {\left(maxCos - 1\right)}^{2}\right) - 2\right)\right)}\right)} \]
  8. Taylor expanded in maxCos around 0 70.9%

    \[\leadsto \sqrt{\color{blue}{-1 \cdot \left(ux \cdot \left(ux - 2\right)\right)}} \]
  9. Step-by-step derivation
    1. associate-*r*70.9%

      \[\leadsto \sqrt{\color{blue}{\left(-1 \cdot ux\right) \cdot \left(ux - 2\right)}} \]
    2. mul-1-neg70.9%

      \[\leadsto \sqrt{\color{blue}{\left(-ux\right)} \cdot \left(ux - 2\right)} \]
    3. sub-neg70.9%

      \[\leadsto \sqrt{\left(-ux\right) \cdot \color{blue}{\left(ux + \left(-2\right)\right)}} \]
    4. metadata-eval70.9%

      \[\leadsto \sqrt{\left(-ux\right) \cdot \left(ux + \color{blue}{-2}\right)} \]
  10. Simplified70.9%

    \[\leadsto \sqrt{\color{blue}{\left(-ux\right) \cdot \left(ux + -2\right)}} \]
  11. Final simplification70.9%

    \[\leadsto \sqrt{ux \cdot \left(\left(-ux\right) - -2\right)} \]
  12. Add Preprocessing

Alternative 7: 62.0% accurate, 2.2× speedup?

\[\begin{array}{l} \\ \sqrt{2 \cdot ux} \end{array} \]
(FPCore (ux uy maxCos) :precision binary32 (sqrt (* 2.0 ux)))
float code(float ux, float uy, float maxCos) {
	return sqrtf((2.0f * ux));
}
real(4) function code(ux, uy, maxcos)
    real(4), intent (in) :: ux
    real(4), intent (in) :: uy
    real(4), intent (in) :: maxcos
    code = sqrt((2.0e0 * ux))
end function
function code(ux, uy, maxCos)
	return sqrt(Float32(Float32(2.0) * ux))
end
function tmp = code(ux, uy, maxCos)
	tmp = sqrt((single(2.0) * ux));
end
\begin{array}{l}

\\
\sqrt{2 \cdot ux}
\end{array}
Derivation
  1. Initial program 59.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. Step-by-step derivation
    1. associate-*l*59.5%

      \[\leadsto \cos \color{blue}{\left(uy \cdot \left(2 \cdot \pi\right)\right)} \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
    2. sub-neg59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{1 + \left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)\right)}} \]
    3. +-commutative59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{\left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)\right) + 1}} \]
    4. distribute-rgt-neg-in59.5%

      \[\leadsto \cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\color{blue}{\left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(-\left(\left(1 - ux\right) + ux \cdot maxCos\right)\right)} + 1} \]
    5. fma-define59.7%

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

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

    \[\leadsto \color{blue}{\sqrt{1 + -1 \cdot \left(\left(1 + ux \cdot \left(maxCos - 1\right)\right) \cdot \left(\left(1 + maxCos \cdot ux\right) - ux\right)\right)}} \]
  6. Simplified47.9%

    \[\leadsto \color{blue}{\sqrt{1 + \left(-\left(1 + ux \cdot \left(-1 + maxCos\right)\right) \cdot \left(1 + \left(ux \cdot maxCos - ux\right)\right)\right)}} \]
  7. Taylor expanded in ux around 0 59.4%

    \[\leadsto \color{blue}{\sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}} \]
  8. Taylor expanded in maxCos around 0 57.4%

    \[\leadsto \sqrt{\color{blue}{2 \cdot ux}} \]
  9. Step-by-step derivation
    1. *-commutative57.4%

      \[\leadsto \sqrt{\color{blue}{ux \cdot 2}} \]
  10. Simplified57.4%

    \[\leadsto \sqrt{\color{blue}{ux \cdot 2}} \]
  11. Final simplification57.4%

    \[\leadsto \sqrt{2 \cdot ux} \]
  12. Add Preprocessing

Reproduce

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