
(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))))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 6 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))))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (+ (* (- 2.0 (* (pow (- maxCos 1.0) 2.0) ux)) ux) (* (* -2.0 maxCos) ux))) (cos (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(2 - {\left(maxCos - 1\right)}^{2} \cdot ux\right) \cdot ux + \left(-2 \cdot maxCos\right) \cdot ux} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 56.8%
lift-+.f32N/A
lift--.f32N/A
flip--N/A
metadata-evalN/A
div-subN/A
associate-+l-N/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower--.f32N/A
lower-/.f32N/A
lower-*.f32N/A
+-commutativeN/A
lower-+.f3256.8
lift-*.f32N/A
*-commutativeN/A
lower-*.f3256.8
Applied rewrites56.8%
Taylor expanded in ux around 0
*-commutativeN/A
associate--l+N/A
cancel-sign-sub-invN/A
metadata-evalN/A
+-commutativeN/A
lower-*.f32N/A
associate-+r+N/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
+-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-pow.f32N/A
lower--.f3269.4
Applied rewrites69.4%
Applied rewrites99.0%
Final simplification99.0%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (- (* (- 2.0 ux) ux) (* (fma -2.0 ux 2.0) (* maxCos ux)))) (cos (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(2 - ux\right) \cdot ux - \mathsf{fma}\left(-2, ux, 2\right) \cdot \left(maxCos \cdot ux\right)} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 56.8%
lift-+.f32N/A
lift--.f32N/A
flip--N/A
metadata-evalN/A
div-subN/A
associate-+l-N/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower--.f32N/A
lower-/.f32N/A
lower-*.f32N/A
+-commutativeN/A
lower-+.f3256.8
lift-*.f32N/A
*-commutativeN/A
lower-*.f3256.8
Applied rewrites56.8%
Taylor expanded in ux around 0
*-commutativeN/A
associate--l+N/A
cancel-sign-sub-invN/A
metadata-evalN/A
+-commutativeN/A
lower-*.f32N/A
associate-+r+N/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
+-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-pow.f32N/A
lower--.f3269.4
Applied rewrites69.4%
Taylor expanded in maxCos around 0
Applied rewrites91.7%
Final simplification92.7%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (- 2.0 ux) ux)) (cos (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(2 - ux\right) \cdot ux} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 56.8%
lift-+.f32N/A
lift--.f32N/A
flip--N/A
metadata-evalN/A
div-subN/A
associate-+l-N/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
lower-+.f32N/A
lower--.f32N/A
lower-/.f32N/A
lower-*.f32N/A
+-commutativeN/A
lower-+.f3256.8
lift-*.f32N/A
*-commutativeN/A
lower-*.f3256.8
Applied rewrites56.8%
Taylor expanded in ux around 0
*-commutativeN/A
associate--l+N/A
cancel-sign-sub-invN/A
metadata-evalN/A
+-commutativeN/A
lower-*.f32N/A
associate-+r+N/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
+-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-pow.f32N/A
lower--.f3269.4
Applied rewrites69.4%
Taylor expanded in maxCos around 0
Applied rewrites92.7%
Final simplification92.7%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* 4.0 ux)) (sqrt 0.5)))
float code(float ux, float uy, float maxCos) {
return sqrtf((4.0f * ux)) * sqrtf(0.5f);
}
real(4) function code(ux, uy, maxcos)
real(4), intent (in) :: ux
real(4), intent (in) :: uy
real(4), intent (in) :: maxcos
code = sqrt((4.0e0 * ux)) * sqrt(0.5e0)
end function
function code(ux, uy, maxCos) return Float32(sqrt(Float32(Float32(4.0) * ux)) * sqrt(Float32(0.5))) end
function tmp = code(ux, uy, maxCos) tmp = sqrt((single(4.0) * ux)) * sqrt(single(0.5)); end
\begin{array}{l}
\\
\sqrt{4 \cdot ux} \cdot \sqrt{0.5}
\end{array}
Initial program 56.8%
lift-*.f32N/A
lift-sqrt.f32N/A
lift--.f32N/A
flip--N/A
sqrt-divN/A
associate-*r/N/A
Applied rewrites41.9%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-sqrt.f32N/A
lower-cos.f32N/A
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-sqrt.f32N/A
*-commutativeN/A
lower-*.f32N/A
cancel-sign-sub-invN/A
metadata-evalN/A
+-commutativeN/A
lower-fma.f3256.7
Applied rewrites57.2%
Taylor expanded in maxCos around 0
Applied rewrites74.1%
Taylor expanded in uy around 0
Applied rewrites63.8%
Final simplification63.8%
(FPCore (ux uy maxCos) :precision binary32 (sqrt (- 1.0 (* (* ux ux) (* maxCos maxCos)))))
float code(float ux, float uy, float maxCos) {
return sqrtf((1.0f - ((ux * ux) * (maxCos * 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((1.0e0 - ((ux * ux) * (maxcos * maxcos))))
end function
function code(ux, uy, maxCos) return sqrt(Float32(Float32(1.0) - Float32(Float32(ux * ux) * Float32(maxCos * maxCos)))) end
function tmp = code(ux, uy, maxCos) tmp = sqrt((single(1.0) - ((ux * ux) * (maxCos * maxCos)))); end
\begin{array}{l}
\\
\sqrt{1 - \left(ux \cdot ux\right) \cdot \left(maxCos \cdot maxCos\right)}
\end{array}
Initial program 56.8%
Taylor expanded in uy around 0
lower-sqrt.f32N/A
lower--.f32N/A
lower-pow.f32N/A
lower--.f32N/A
+-commutativeN/A
lower-fma.f3249.3
Applied rewrites49.2%
Taylor expanded in maxCos around inf
Applied rewrites19.9%
Final simplification19.9%
(FPCore (ux uy maxCos) :precision binary32 (* (* (sqrt -1.0) ux) maxCos))
float code(float ux, float uy, float maxCos) {
return (sqrtf(-1.0f) * ux) * 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((-1.0e0)) * ux) * maxcos
end function
function code(ux, uy, maxCos) return Float32(Float32(sqrt(Float32(-1.0)) * ux) * maxCos) end
function tmp = code(ux, uy, maxCos) tmp = (sqrt(single(-1.0)) * ux) * maxCos; end
\begin{array}{l}
\\
\left(\sqrt{-1} \cdot ux\right) \cdot maxCos
\end{array}
Initial program 56.8%
Taylor expanded in uy around 0
lower-sqrt.f32N/A
lower--.f32N/A
lower-pow.f32N/A
lower--.f32N/A
+-commutativeN/A
lower-fma.f3249.3
Applied rewrites49.2%
Taylor expanded in maxCos around inf
Applied rewrites-0.0%
herbie shell --seed 2024284
(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)))))))