
(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 8 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 (* (exp (log (* uy 2.0))) PI)) (sqrt (* ux (- 2.0 (+ (* 2.0 maxCos) (* ux (pow (+ -1.0 maxCos) 2.0))))))))
float code(float ux, float uy, float maxCos) {
return cosf((expf(logf((uy * 2.0f))) * ((float) M_PI))) * sqrtf((ux * (2.0f - ((2.0f * maxCos) + (ux * powf((-1.0f + maxCos), 2.0f))))));
}
function code(ux, uy, maxCos) return Float32(cos(Float32(exp(log(Float32(uy * Float32(2.0)))) * Float32(pi))) * sqrt(Float32(ux * Float32(Float32(2.0) - Float32(Float32(Float32(2.0) * maxCos) + Float32(ux * (Float32(Float32(-1.0) + maxCos) ^ Float32(2.0)))))))) end
function tmp = code(ux, uy, maxCos) tmp = cos((exp(log((uy * single(2.0)))) * single(pi))) * sqrt((ux * (single(2.0) - ((single(2.0) * maxCos) + (ux * ((single(-1.0) + maxCos) ^ single(2.0))))))); end
\begin{array}{l}
\\
\cos \left(e^{\log \left(uy \cdot 2\right)} \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 - \left(2 \cdot maxCos + ux \cdot {\left(-1 + maxCos\right)}^{2}\right)\right)}
\end{array}
Initial program 59.3%
Taylor expanded in ux around 0 99.0%
associate--l+99.0%
associate-*r*99.0%
mul-1-neg99.0%
sub-neg99.0%
metadata-eval99.0%
+-commutative99.0%
Simplified99.0%
add-exp-log99.0%
Applied egg-rr99.0%
Final simplification99.0%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (cos (* (* uy 2.0) PI))))
(if (<= t_0 0.9999992251396179)
(* t_0 (sqrt (* ux (- 2.0 ux))))
(sqrt
(* ux (+ 2.0 (- (* maxCos (- (- (* 2.0 ux) (* ux maxCos)) 2.0)) ux)))))))
float code(float ux, float uy, float maxCos) {
float t_0 = cosf(((uy * 2.0f) * ((float) M_PI)));
float tmp;
if (t_0 <= 0.9999992251396179f) {
tmp = t_0 * sqrtf((ux * (2.0f - ux)));
} else {
tmp = sqrtf((ux * (2.0f + ((maxCos * (((2.0f * ux) - (ux * maxCos)) - 2.0f)) - ux))));
}
return tmp;
}
function code(ux, uy, maxCos) t_0 = cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) tmp = Float32(0.0) if (t_0 <= Float32(0.9999992251396179)) tmp = Float32(t_0 * sqrt(Float32(ux * Float32(Float32(2.0) - ux)))); else tmp = sqrt(Float32(ux * Float32(Float32(2.0) + Float32(Float32(maxCos * Float32(Float32(Float32(Float32(2.0) * ux) - Float32(ux * maxCos)) - Float32(2.0))) - ux)))); 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 (t_0 <= single(0.9999992251396179)) tmp = t_0 * sqrt((ux * (single(2.0) - ux))); else tmp = sqrt((ux * (single(2.0) + ((maxCos * (((single(2.0) * ux) - (ux * maxCos)) - single(2.0))) - ux)))); 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}\;t\_0 \leq 0.9999992251396179:\\
\;\;\;\;t\_0 \cdot \sqrt{ux \cdot \left(2 - ux\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{ux \cdot \left(2 + \left(maxCos \cdot \left(\left(2 \cdot ux - ux \cdot maxCos\right) - 2\right) - ux\right)\right)}\\
\end{array}
\end{array}
if (cos.f32 (*.f32 (*.f32 uy #s(literal 2 binary32)) (PI.f32))) < 0.999999225Initial program 60.6%
Taylor expanded in ux around 0 98.1%
associate--l+98.2%
associate-*r*98.2%
mul-1-neg98.2%
sub-neg98.2%
metadata-eval98.2%
+-commutative98.2%
Simplified98.2%
Taylor expanded in maxCos around -inf 50.0%
Taylor expanded in maxCos around 0 92.8%
neg-mul-192.8%
unsub-neg92.8%
Simplified92.8%
if 0.999999225 < (cos.f32 (*.f32 (*.f32 uy #s(literal 2 binary32)) (PI.f32))) Initial program 58.7%
Taylor expanded in ux around 0 99.4%
associate--l+99.5%
associate-*r*99.5%
mul-1-neg99.5%
sub-neg99.5%
metadata-eval99.5%
+-commutative99.5%
Simplified99.5%
Taylor expanded in maxCos around 0 99.5%
Taylor expanded in uy around 0 98.9%
Final simplification96.9%
(FPCore (ux uy maxCos) :precision binary32 (* (cos (* (* uy 2.0) PI)) (sqrt (* ux (+ 2.0 (- (* maxCos (+ -2.0 (* ux (- 2.0 maxCos)))) 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 - maxCos)))) - 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(-2.0) + Float32(ux * Float32(Float32(2.0) - maxCos)))) - 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) - maxCos)))) - 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 + ux \cdot \left(2 - maxCos\right)\right) - ux\right)\right)}
\end{array}
Initial program 59.3%
Taylor expanded in ux around 0 99.0%
associate--l+99.0%
associate-*r*99.0%
mul-1-neg99.0%
sub-neg99.0%
metadata-eval99.0%
+-commutative99.0%
Simplified99.0%
Taylor expanded in maxCos around 0 99.0%
distribute-lft-in99.0%
+-commutative99.0%
fma-define99.0%
associate--l+99.0%
associate-*r*99.0%
fma-define99.0%
*-commutative99.0%
fma-neg99.0%
metadata-eval99.0%
mul-1-neg99.0%
Applied egg-rr99.0%
Simplified99.0%
(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 59.3%
Taylor expanded in ux around 0 99.0%
associate--l+99.0%
associate-*r*99.0%
mul-1-neg99.0%
sub-neg99.0%
metadata-eval99.0%
+-commutative99.0%
Simplified99.0%
Taylor expanded in maxCos around 0 98.0%
Final simplification98.0%
(FPCore (ux uy maxCos) :precision binary32 (* (cos (* (* uy 2.0) PI)) (sqrt (* ux (+ 2.0 (- (* maxCos -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))));
}
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(-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)))); end
\begin{array}{l}
\\
\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 + \left(maxCos \cdot -2 - ux\right)\right)}
\end{array}
Initial program 59.3%
Taylor expanded in ux around 0 99.0%
associate--l+99.0%
associate-*r*99.0%
mul-1-neg99.0%
sub-neg99.0%
metadata-eval99.0%
+-commutative99.0%
Simplified99.0%
Taylor expanded in maxCos around 0 99.0%
Taylor expanded in ux around 0 97.1%
*-commutative97.1%
Simplified97.1%
Final simplification97.1%
(FPCore (ux uy maxCos) :precision binary32 (sqrt (* ux (+ 2.0 (- (* maxCos (- (- (* 2.0 ux) (* ux maxCos)) 2.0)) ux)))))
float code(float ux, float uy, float maxCos) {
return sqrtf((ux * (2.0f + ((maxCos * (((2.0f * ux) - (ux * maxCos)) - 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((ux * (2.0e0 + ((maxcos * (((2.0e0 * ux) - (ux * maxcos)) - 2.0e0)) - ux))))
end function
function code(ux, uy, maxCos) return sqrt(Float32(ux * Float32(Float32(2.0) + Float32(Float32(maxCos * Float32(Float32(Float32(Float32(2.0) * ux) - Float32(ux * maxCos)) - Float32(2.0))) - ux)))) end
function tmp = code(ux, uy, maxCos) tmp = sqrt((ux * (single(2.0) + ((maxCos * (((single(2.0) * ux) - (ux * maxCos)) - single(2.0))) - ux)))); end
\begin{array}{l}
\\
\sqrt{ux \cdot \left(2 + \left(maxCos \cdot \left(\left(2 \cdot ux - ux \cdot maxCos\right) - 2\right) - ux\right)\right)}
\end{array}
Initial program 59.3%
Taylor expanded in ux around 0 99.0%
associate--l+99.0%
associate-*r*99.0%
mul-1-neg99.0%
sub-neg99.0%
metadata-eval99.0%
+-commutative99.0%
Simplified99.0%
Taylor expanded in maxCos around 0 99.0%
Taylor expanded in uy around 0 82.1%
Final simplification82.1%
(FPCore (ux uy maxCos) :precision binary32 (sqrt (* ux (- 2.0 ux))))
float code(float ux, float uy, float maxCos) {
return sqrtf((ux * (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((ux * (2.0e0 - ux)))
end function
function code(ux, uy, maxCos) return sqrt(Float32(ux * Float32(Float32(2.0) - ux))) end
function tmp = code(ux, uy, maxCos) tmp = sqrt((ux * (single(2.0) - ux))); end
\begin{array}{l}
\\
\sqrt{ux \cdot \left(2 - ux\right)}
\end{array}
Initial program 59.3%
associate-*l*59.3%
sub-neg59.3%
+-commutative59.3%
distribute-rgt-neg-in59.3%
fma-define59.2%
Simplified59.3%
Taylor expanded in uy around 0 51.4%
mul-1-neg51.4%
sub-neg51.4%
metadata-eval51.4%
+-commutative51.4%
associate--l+51.6%
*-commutative51.6%
Simplified51.6%
Taylor expanded in maxCos around 0 49.6%
Taylor expanded in ux around 0 77.2%
neg-mul-177.2%
unsub-neg77.2%
Simplified77.2%
(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}
Initial program 59.3%
associate-*l*59.3%
sub-neg59.3%
+-commutative59.3%
distribute-rgt-neg-in59.3%
fma-define59.2%
Simplified59.3%
Taylor expanded in uy around 0 51.4%
mul-1-neg51.4%
sub-neg51.4%
metadata-eval51.4%
+-commutative51.4%
associate--l+51.6%
*-commutative51.6%
Simplified51.6%
Taylor expanded in maxCos around 0 49.6%
Taylor expanded in ux around 0 62.6%
*-commutative62.6%
Simplified62.6%
Final simplification62.6%
herbie shell --seed 2024137
(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)))))))