
(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 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))))))
\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 (* ux (pow (- maxCos 1.0) 2.0))) (* maxCos 2.0)) ux)) (cos (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(\left(2 - ux \cdot {\left(maxCos - 1\right)}^{2}\right) - maxCos \cdot 2\right) \cdot ux} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 55.3%
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-+.f3255.2
lift-*.f32N/A
*-commutativeN/A
lower-*.f3255.2
Applied rewrites55.2%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
mul-1-negN/A
sub-negN/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3248.2
Applied rewrites48.2%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-pow.f32N/A
lower--.f32N/A
lower-*.f3298.6
Applied rewrites98.6%
Final simplification98.6%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (- (- 2.0 (* (fma -2.0 ux 2.0) maxCos)) ux) ux)) (cos (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(\left(2 - \mathsf{fma}\left(-2, ux, 2\right) \cdot maxCos\right) - ux\right) \cdot ux} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 55.3%
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-+.f3255.2
lift-*.f32N/A
*-commutativeN/A
lower-*.f3255.2
Applied rewrites55.2%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
mul-1-negN/A
sub-negN/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3248.2
Applied rewrites48.2%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-pow.f32N/A
lower--.f32N/A
lower-*.f3298.6
Applied rewrites98.6%
Taylor expanded in maxCos around 0
Applied rewrites90.3%
Final simplification90.5%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= maxCos 1.9999999949504854e-6)
(* (sqrt (* (- 2.0 ux) ux)) (cos (* (PI) (* 2.0 uy))))
(*
(sqrt (* (- 2.0 (- (* ux (pow (- maxCos 1.0) 2.0)) (* -2.0 maxCos))) ux))
1.0)))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;maxCos \leq 1.9999999949504854 \cdot 10^{-6}:\\
\;\;\;\;\sqrt{\left(2 - ux\right) \cdot ux} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(2 - \left(ux \cdot {\left(maxCos - 1\right)}^{2} - -2 \cdot maxCos\right)\right) \cdot ux} \cdot 1\\
\end{array}
\end{array}
if maxCos < 1.99999999e-6Initial program 55.5%
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-+.f3255.5
lift-*.f32N/A
*-commutativeN/A
lower-*.f3255.5
Applied rewrites55.5%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
mul-1-negN/A
sub-negN/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3248.9
Applied rewrites48.9%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-pow.f32N/A
lower--.f32N/A
lower-*.f3298.6
Applied rewrites98.6%
Taylor expanded in maxCos around 0
Applied rewrites98.3%
if 1.99999999e-6 < maxCos Initial program 54.4%
Taylor expanded in uy around 0
Applied rewrites47.1%
Taylor expanded in ux around 0
*-commutativeN/A
cancel-sign-sub-invN/A
metadata-evalN/A
associate-+r+N/A
+-commutativeN/A
lower-*.f32N/A
Applied rewrites6.5%
Applied rewrites83.4%
Final simplification95.4%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= maxCos 1.9999999949504854e-6)
(* (sqrt (* (- 2.0 ux) ux)) (cos (* (PI) (* 2.0 uy))))
(*
(sqrt
(*
(*
(* maxCos maxCos)
(- (/ (- (* ux 2.0) (- 2.0 (/ (- 2.0 ux) maxCos))) maxCos) ux))
ux))
1.0)))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;maxCos \leq 1.9999999949504854 \cdot 10^{-6}:\\
\;\;\;\;\sqrt{\left(2 - ux\right) \cdot ux} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(\left(maxCos \cdot maxCos\right) \cdot \left(\frac{ux \cdot 2 - \left(2 - \frac{2 - ux}{maxCos}\right)}{maxCos} - ux\right)\right) \cdot ux} \cdot 1\\
\end{array}
\end{array}
if maxCos < 1.99999999e-6Initial program 55.5%
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-+.f3255.5
lift-*.f32N/A
*-commutativeN/A
lower-*.f3255.5
Applied rewrites55.5%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
mul-1-negN/A
sub-negN/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3248.9
Applied rewrites48.9%
Taylor expanded in ux around 0
*-commutativeN/A
lower-*.f32N/A
lower--.f32N/A
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower-pow.f32N/A
lower--.f32N/A
lower-*.f3298.6
Applied rewrites98.6%
Taylor expanded in maxCos around 0
Applied rewrites98.3%
if 1.99999999e-6 < maxCos Initial program 54.4%
Taylor expanded in uy around 0
Applied rewrites47.1%
Taylor expanded in ux around 0
*-commutativeN/A
cancel-sign-sub-invN/A
metadata-evalN/A
associate-+r+N/A
+-commutativeN/A
lower-*.f32N/A
Applied rewrites6.5%
Taylor expanded in maxCos around -inf
Applied rewrites83.1%
Final simplification95.3%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (- (- 2.0 (* (fma -2.0 ux 2.0) maxCos)) ux) ux)) 1.0))
float code(float ux, float uy, float maxCos) {
return sqrtf((((2.0f - (fmaf(-2.0f, ux, 2.0f) * maxCos)) - ux) * ux)) * 1.0f;
}
function code(ux, uy, maxCos) return Float32(sqrt(Float32(Float32(Float32(Float32(2.0) - Float32(fma(Float32(-2.0), ux, Float32(2.0)) * maxCos)) - ux) * ux)) * Float32(1.0)) end
\begin{array}{l}
\\
\sqrt{\left(\left(2 - \mathsf{fma}\left(-2, ux, 2\right) \cdot maxCos\right) - ux\right) \cdot ux} \cdot 1
\end{array}
Initial program 55.3%
Taylor expanded in uy around 0
Applied rewrites45.9%
Taylor expanded in ux around 0
*-commutativeN/A
cancel-sign-sub-invN/A
metadata-evalN/A
associate-+r+N/A
+-commutativeN/A
lower-*.f32N/A
Applied rewrites2.4%
Taylor expanded in maxCos around 0
Applied rewrites77.0%
Final simplification77.1%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (- 2.0 ux) ux)) 1.0))
float code(float ux, float uy, float maxCos) {
return sqrtf(((2.0f - ux) * ux)) * 1.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(((2.0e0 - ux) * ux)) * 1.0e0
end function
function code(ux, uy, maxCos) return Float32(sqrt(Float32(Float32(Float32(2.0) - ux) * ux)) * Float32(1.0)) end
function tmp = code(ux, uy, maxCos) tmp = sqrt(((single(2.0) - ux) * ux)) * single(1.0); end
\begin{array}{l}
\\
\sqrt{\left(2 - ux\right) \cdot ux} \cdot 1
\end{array}
Initial program 55.3%
Taylor expanded in uy around 0
Applied rewrites45.9%
Taylor expanded in ux around 0
*-commutativeN/A
cancel-sign-sub-invN/A
metadata-evalN/A
associate-+r+N/A
+-commutativeN/A
lower-*.f32N/A
Applied rewrites1.8%
Taylor expanded in maxCos around 0
Applied rewrites72.9%
Final simplification72.9%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (fma -1.0 1.0 1.0)) 1.0))
float code(float ux, float uy, float maxCos) {
return sqrtf(fmaf(-1.0f, 1.0f, 1.0f)) * 1.0f;
}
function code(ux, uy, maxCos) return Float32(sqrt(fma(Float32(-1.0), Float32(1.0), Float32(1.0))) * Float32(1.0)) end
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(-1, 1, 1\right)} \cdot 1
\end{array}
Initial program 55.3%
Taylor expanded in uy around 0
Applied rewrites45.9%
Taylor expanded in ux around 0
Applied rewrites6.6%
lift--.f32N/A
sub-negN/A
+-commutativeN/A
neg-mul-1N/A
lower-fma.f3219.4
Applied rewrites19.7%
Final simplification19.8%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (- 1.0 1.0)) 1.0))
float code(float ux, float uy, float maxCos) {
return sqrtf((1.0f - 1.0f)) * 1.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((1.0e0 - 1.0e0)) * 1.0e0
end function
function code(ux, uy, maxCos) return Float32(sqrt(Float32(Float32(1.0) - Float32(1.0))) * Float32(1.0)) end
function tmp = code(ux, uy, maxCos) tmp = sqrt((single(1.0) - single(1.0))) * single(1.0); end
\begin{array}{l}
\\
\sqrt{1 - 1} \cdot 1
\end{array}
Initial program 55.3%
Taylor expanded in uy around 0
Applied rewrites45.9%
Taylor expanded in ux around 0
Applied rewrites6.6%
Final simplification6.6%
herbie shell --seed 2024271
(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)))))))