
(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:
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(FPCore (ux uy maxCos) :precision binary32 (* (cos (* (* uy 2.0) (pow (cbrt PI) 3.0))) (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) * powf(cbrtf(((float) M_PI)), 3.0f))) * 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)) * (cbrt(Float32(pi)) ^ Float32(3.0)))) * sqrt(Float32(ux * Float32(Float32(Float32(2.0) - Float32(ux * (Float32(Float32(-1.0) + maxCos) ^ Float32(2.0)))) - Float32(Float32(2.0) * maxCos))))) end
\begin{array}{l}
\\
\cos \left(\left(uy \cdot 2\right) \cdot {\left(\sqrt[3]{\pi}\right)}^{3}\right) \cdot \sqrt{ux \cdot \left(\left(2 - ux \cdot {\left(-1 + maxCos\right)}^{2}\right) - 2 \cdot maxCos\right)}
\end{array}
Initial program 60.0%
Taylor expanded in ux around 0 99.1%
add-cube-cbrt99.1%
pow399.1%
Applied egg-rr99.1%
Final simplification99.1%
(FPCore (ux uy maxCos)
:precision binary32
(*
(cos (* uy (* 2.0 PI)))
(sqrt
(*
ux
(+ (* ux (* (+ -1.0 maxCos) (- 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 * ((ux * ((-1.0f + maxCos) * (1.0f - maxCos))) + (2.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(ux * Float32(Float32(Float32(-1.0) + maxCos) * Float32(Float32(1.0) - maxCos))) + Float32(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 * ((ux * ((single(-1.0) + maxCos) * (single(1.0) - maxCos))) + (single(2.0) - (single(2.0) * maxCos))))); end
\begin{array}{l}
\\
\cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{ux \cdot \left(ux \cdot \left(\left(-1 + maxCos\right) \cdot \left(1 - maxCos\right)\right) + \left(2 - 2 \cdot maxCos\right)\right)}
\end{array}
Initial program 60.0%
associate-*l*60.0%
sub-neg60.0%
+-commutative60.0%
distribute-rgt-neg-in60.0%
fma-define60.1%
Simplified60.2%
Taylor expanded in uy around inf 60.0%
associate-*r*60.0%
*-commutative60.0%
associate-*r*60.0%
mul-1-neg60.0%
sub-neg60.0%
metadata-eval60.0%
+-commutative60.0%
associate--l+60.0%
*-commutative60.0%
Simplified60.0%
Taylor expanded in ux around -inf 60.3%
distribute-lft-out60.3%
*-commutative60.3%
sub-neg60.3%
metadata-eval60.3%
+-commutative60.3%
neg-mul-160.3%
sub-neg60.3%
Simplified60.3%
Taylor expanded in ux around 0 99.1%
Final simplification99.1%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* ux (- (+ 2.0 (* ux (* (+ -1.0 maxCos) (- 1.0 maxCos)))) (* 2.0 maxCos)))) (cos (* 2.0 (* uy PI)))))
float code(float ux, float uy, float maxCos) {
return sqrtf((ux * ((2.0f + (ux * ((-1.0f + maxCos) * (1.0f - maxCos)))) - (2.0f * maxCos)))) * cosf((2.0f * (uy * ((float) M_PI))));
}
function code(ux, uy, maxCos) return Float32(sqrt(Float32(ux * Float32(Float32(Float32(2.0) + Float32(ux * Float32(Float32(Float32(-1.0) + maxCos) * Float32(Float32(1.0) - maxCos)))) - Float32(Float32(2.0) * maxCos)))) * cos(Float32(Float32(2.0) * Float32(uy * Float32(pi))))) end
function tmp = code(ux, uy, maxCos) tmp = sqrt((ux * ((single(2.0) + (ux * ((single(-1.0) + maxCos) * (single(1.0) - maxCos)))) - (single(2.0) * maxCos)))) * cos((single(2.0) * (uy * single(pi)))); end
\begin{array}{l}
\\
\sqrt{ux \cdot \left(\left(2 + ux \cdot \left(\left(-1 + maxCos\right) \cdot \left(1 - maxCos\right)\right)\right) - 2 \cdot maxCos\right)} \cdot \cos \left(2 \cdot \left(uy \cdot \pi\right)\right)
\end{array}
Initial program 60.0%
associate-*l*60.0%
sub-neg60.0%
+-commutative60.0%
distribute-rgt-neg-in60.0%
fma-define60.1%
Simplified60.2%
Taylor expanded in ux around inf 98.9%
Taylor expanded in ux around 0 99.0%
associate--l+99.0%
mul-1-neg99.0%
sub-neg99.0%
metadata-eval99.0%
distribute-neg-in99.0%
metadata-eval99.0%
+-commutative99.0%
sub-neg99.0%
*-commutative99.0%
sub-neg99.0%
metadata-eval99.0%
+-commutative99.0%
Simplified99.0%
Taylor expanded in uy around inf 99.1%
Final simplification99.1%
(FPCore (ux uy maxCos) :precision binary32 (* (cos (* (* uy 2.0) PI)) (sqrt (+ (* maxCos (* ux (- (* 2.0 ux) 2.0))) (* ux (- 2.0 ux))))))
float code(float ux, float uy, float maxCos) {
return cosf(((uy * 2.0f) * ((float) M_PI))) * sqrtf(((maxCos * (ux * ((2.0f * ux) - 2.0f))) + (ux * (2.0f - ux))));
}
function code(ux, uy, maxCos) return Float32(cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(Float32(maxCos * Float32(ux * Float32(Float32(Float32(2.0) * ux) - Float32(2.0)))) + Float32(ux * Float32(Float32(2.0) - ux))))) end
function tmp = code(ux, uy, maxCos) tmp = cos(((uy * single(2.0)) * single(pi))) * sqrt(((maxCos * (ux * ((single(2.0) * ux) - single(2.0)))) + (ux * (single(2.0) - ux)))); end
\begin{array}{l}
\\
\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{maxCos \cdot \left(ux \cdot \left(2 \cdot ux - 2\right)\right) + ux \cdot \left(2 - ux\right)}
\end{array}
Initial program 60.0%
Taylor expanded in ux around 0 99.1%
Taylor expanded in maxCos around 0 98.7%
Final simplification98.7%
(FPCore (ux uy maxCos) :precision binary32 (* (cos (* (* uy 2.0) PI)) (sqrt (* ux (+ 2.0 (- (* maxCos (- (* 2.0 ux) 2.0)) ux))))))
float code(float ux, float uy, float maxCos) {
return cosf(((uy * 2.0f) * ((float) M_PI))) * sqrtf((ux * (2.0f + ((maxCos * ((2.0f * 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) + Float32(Float32(maxCos * Float32(Float32(Float32(2.0) * ux) - Float32(2.0))) - ux))))) 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(2.0))) - ux)))); end
\begin{array}{l}
\\
\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 + \left(maxCos \cdot \left(2 \cdot ux - 2\right) - ux\right)\right)}
\end{array}
Initial program 60.0%
Taylor expanded in ux around 0 99.1%
Taylor expanded in maxCos around 0 98.7%
Final simplification98.7%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (cos (* (* uy 2.0) PI))))
(if (<= maxCos 0.00019999999494757503)
(* t_0 (sqrt (* ux (- 2.0 ux))))
(* t_0 (sqrt (* ux (- 2.0 (* 2.0 maxCos))))))))
float code(float ux, float uy, float maxCos) {
float t_0 = cosf(((uy * 2.0f) * ((float) M_PI)));
float tmp;
if (maxCos <= 0.00019999999494757503f) {
tmp = t_0 * sqrtf((ux * (2.0f - ux)));
} else {
tmp = t_0 * sqrtf((ux * (2.0f - (2.0f * maxCos))));
}
return tmp;
}
function code(ux, uy, maxCos) t_0 = cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) tmp = Float32(0.0) if (maxCos <= Float32(0.00019999999494757503)) tmp = Float32(t_0 * sqrt(Float32(ux * Float32(Float32(2.0) - ux)))); else tmp = Float32(t_0 * sqrt(Float32(ux * Float32(Float32(2.0) - Float32(Float32(2.0) * maxCos))))); end return tmp end
function tmp_2 = code(ux, uy, maxCos) t_0 = cos(((uy * single(2.0)) * single(pi))); tmp = single(0.0); if (maxCos <= single(0.00019999999494757503)) tmp = t_0 * sqrt((ux * (single(2.0) - ux))); else tmp = t_0 * sqrt((ux * (single(2.0) - (single(2.0) * maxCos)))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \cos \left(\left(uy \cdot 2\right) \cdot \pi\right)\\
\mathbf{if}\;maxCos \leq 0.00019999999494757503:\\
\;\;\;\;t\_0 \cdot \sqrt{ux \cdot \left(2 - ux\right)}\\
\mathbf{else}:\\
\;\;\;\;t\_0 \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\\
\end{array}
\end{array}
if maxCos < 1.99999995e-4Initial program 61.2%
Taylor expanded in ux around 0 99.1%
Taylor expanded in maxCos around 0 97.9%
neg-mul-197.9%
unsub-neg97.9%
Simplified97.9%
if 1.99999995e-4 < maxCos Initial program 45.5%
Taylor expanded in ux around 0 86.7%
(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}
Initial program 60.0%
Taylor expanded in ux around 0 99.1%
Taylor expanded in maxCos around 0 93.8%
neg-mul-193.8%
unsub-neg93.8%
Simplified93.8%
(FPCore (ux uy maxCos) :precision binary32 (sqrt (* ux (- (+ 2.0 (* ux (* (+ -1.0 maxCos) (- 1.0 maxCos)))) (* 2.0 maxCos)))))
float code(float ux, float uy, float maxCos) {
return sqrtf((ux * ((2.0f + (ux * ((-1.0f + maxCos) * (1.0f - maxCos)))) - (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 + (ux * (((-1.0e0) + maxcos) * (1.0e0 - maxcos)))) - (2.0e0 * maxcos))))
end function
function code(ux, uy, maxCos) return sqrt(Float32(ux * Float32(Float32(Float32(2.0) + Float32(ux * Float32(Float32(Float32(-1.0) + maxCos) * Float32(Float32(1.0) - maxCos)))) - Float32(Float32(2.0) * maxCos)))) end
function tmp = code(ux, uy, maxCos) tmp = sqrt((ux * ((single(2.0) + (ux * ((single(-1.0) + maxCos) * (single(1.0) - maxCos)))) - (single(2.0) * maxCos)))); end
\begin{array}{l}
\\
\sqrt{ux \cdot \left(\left(2 + ux \cdot \left(\left(-1 + maxCos\right) \cdot \left(1 - maxCos\right)\right)\right) - 2 \cdot maxCos\right)}
\end{array}
Initial program 60.0%
associate-*l*60.0%
sub-neg60.0%
+-commutative60.0%
distribute-rgt-neg-in60.0%
fma-define60.1%
Simplified60.2%
Taylor expanded in ux around inf 98.9%
Taylor expanded in ux around 0 99.0%
associate--l+99.0%
mul-1-neg99.0%
sub-neg99.0%
metadata-eval99.0%
distribute-neg-in99.0%
metadata-eval99.0%
+-commutative99.0%
sub-neg99.0%
*-commutative99.0%
sub-neg99.0%
metadata-eval99.0%
+-commutative99.0%
Simplified99.0%
Taylor expanded in uy around 0 83.0%
Final simplification83.0%
(FPCore (ux uy maxCos) :precision binary32 (* ux (sqrt (+ -1.0 (/ 2.0 ux)))))
float code(float ux, float uy, float maxCos) {
return ux * sqrtf((-1.0f + (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 = ux * sqrt(((-1.0e0) + (2.0e0 / ux)))
end function
function code(ux, uy, maxCos) return Float32(ux * sqrt(Float32(Float32(-1.0) + Float32(Float32(2.0) / ux)))) end
function tmp = code(ux, uy, maxCos) tmp = ux * sqrt((single(-1.0) + (single(2.0) / ux))); end
\begin{array}{l}
\\
ux \cdot \sqrt{-1 + \frac{2}{ux}}
\end{array}
Initial program 60.0%
associate-*l*60.0%
sub-neg60.0%
+-commutative60.0%
distribute-rgt-neg-in60.0%
fma-define60.1%
Simplified60.2%
Taylor expanded in ux around inf 98.9%
Taylor expanded in maxCos around 0 93.4%
sub-neg93.4%
associate-*r/93.4%
metadata-eval93.4%
metadata-eval93.4%
Simplified93.4%
Taylor expanded in uy around 0 79.3%
sub-neg79.3%
metadata-eval79.3%
+-commutative79.3%
associate-*r/79.3%
metadata-eval79.3%
Simplified79.3%
(FPCore (ux uy maxCos) :precision binary32 0.0)
float code(float ux, float uy, float maxCos) {
return 0.0f;
}
real(4) function code(ux, uy, maxcos)
real(4), intent (in) :: ux
real(4), intent (in) :: uy
real(4), intent (in) :: maxcos
code = 0.0e0
end function
function code(ux, uy, maxCos) return Float32(0.0) end
function tmp = code(ux, uy, maxCos) tmp = single(0.0); end
\begin{array}{l}
\\
0
\end{array}
Initial program 60.0%
associate-*l*60.0%
sub-neg60.0%
+-commutative60.0%
distribute-rgt-neg-in60.0%
fma-define60.1%
Simplified60.2%
Taylor expanded in uy around 0 53.2%
mul-1-neg53.2%
sub-neg53.2%
metadata-eval53.2%
+-commutative53.2%
associate--l+53.1%
*-commutative53.1%
Simplified53.1%
Taylor expanded in ux around 0 6.6%
pow1/26.6%
metadata-eval6.6%
metadata-eval6.6%
metadata-eval6.6%
Applied egg-rr6.6%
herbie shell --seed 2024166
(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)))))))