
(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 (* (* ux ux) (- (/ (+ (* maxCos -2.0) 2.0) ux) (pow (- maxCos 1.0) 2.0)))) (cos (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(ux \cdot ux\right) \cdot \left(\frac{maxCos \cdot -2 + 2}{ux} - {\left(maxCos - 1\right)}^{2}\right)} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 56.3%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
associate--r+N/A
associate-*r/N/A
metadata-evalN/A
associate-*r/N/A
div-subN/A
cancel-sign-sub-invN/A
metadata-evalN/A
lower--.f32N/A
lower-/.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower-pow.f32N/A
lower--.f32N/A
unpow2N/A
lower-*.f3270.1
Applied rewrites70.1%
Applied rewrites98.8%
Final simplification98.8%
(FPCore (ux uy maxCos)
:precision binary32
(*
(sqrt
(*
(- (* (- (/ 2.0 maxCos) 2.0) maxCos) (* (pow (- maxCos 1.0) 2.0) ux))
ux))
(cos (* (PI) (* 2.0 uy)))))\begin{array}{l}
\\
\sqrt{\left(\left(\frac{2}{maxCos} - 2\right) \cdot maxCos - {\left(maxCos - 1\right)}^{2} \cdot ux\right) \cdot ux} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 56.3%
Taylor expanded in maxCos around inf
*-commutativeN/A
lower-*.f32N/A
associate--l+N/A
div-subN/A
+-commutativeN/A
lower-+.f32N/A
lower-/.f32N/A
lower--.f3256.4
Applied rewrites56.4%
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--.f3270.3
Applied rewrites70.6%
Taylor expanded in maxCos around inf
Applied rewrites98.5%
Final simplification98.5%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (- (* (- 2.0 ux) ux) (* (* (fma ux -2.0 2.0) maxCos) ux))) (cos (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(2 - ux\right) \cdot ux - \left(\mathsf{fma}\left(ux, -2, 2\right) \cdot maxCos\right) \cdot ux} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 56.3%
Taylor expanded in maxCos around inf
*-commutativeN/A
lower-*.f32N/A
associate--l+N/A
div-subN/A
+-commutativeN/A
lower-+.f32N/A
lower-/.f32N/A
lower--.f3256.4
Applied rewrites56.4%
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--.f3270.3
Applied rewrites70.3%
Taylor expanded in maxCos around 0
Applied rewrites91.8%
Final simplification93.5%
(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.3%
Taylor expanded in maxCos around inf
*-commutativeN/A
lower-*.f32N/A
associate--l+N/A
div-subN/A
+-commutativeN/A
lower-+.f32N/A
lower-/.f32N/A
lower--.f3256.4
Applied rewrites56.4%
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--.f3270.3
Applied rewrites70.3%
Taylor expanded in maxCos around 0
Applied rewrites94.9%
Final simplification94.9%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (- (* (- 2.0 ux) ux) (* (fma -2.0 ux 2.0) (* ux maxCos)))) 1.0))
float code(float ux, float uy, float maxCos) {
return sqrtf((((2.0f - ux) * ux) - (fmaf(-2.0f, ux, 2.0f) * (ux * maxCos)))) * 1.0f;
}
function code(ux, uy, maxCos) return Float32(sqrt(Float32(Float32(Float32(Float32(2.0) - ux) * ux) - Float32(fma(Float32(-2.0), ux, Float32(2.0)) * Float32(ux * maxCos)))) * Float32(1.0)) end
\begin{array}{l}
\\
\sqrt{\left(2 - ux\right) \cdot ux - \mathsf{fma}\left(-2, ux, 2\right) \cdot \left(ux \cdot maxCos\right)} \cdot 1
\end{array}
Initial program 56.3%
Taylor expanded in uy around 0
Applied rewrites49.2%
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--.f3277.8
Applied rewrites77.5%
Taylor expanded in maxCos around 0
Applied rewrites80.1%
Final simplification79.7%
(FPCore (ux uy maxCos) :precision binary32 (* 1.0 (sqrt (* (- 2.0 ux) ux))))
float code(float ux, float uy, float maxCos) {
return 1.0f * sqrtf(((2.0f - ux) * ux));
}
real(4) function code(ux, uy, maxcos)
real(4), intent (in) :: ux
real(4), intent (in) :: uy
real(4), intent (in) :: maxcos
code = 1.0e0 * sqrt(((2.0e0 - ux) * ux))
end function
function code(ux, uy, maxCos) return Float32(Float32(1.0) * sqrt(Float32(Float32(Float32(2.0) - ux) * ux))) end
function tmp = code(ux, uy, maxCos) tmp = single(1.0) * sqrt(((single(2.0) - ux) * ux)); end
\begin{array}{l}
\\
1 \cdot \sqrt{\left(2 - ux\right) \cdot ux}
\end{array}
Initial program 56.3%
Taylor expanded in uy around 0
Applied rewrites49.2%
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--.f3257.5
Applied rewrites77.5%
Taylor expanded in maxCos around 0
Applied rewrites77.8%
(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 56.3%
Taylor expanded in uy around 0
Applied rewrites49.2%
Taylor expanded in ux around 0
Applied rewrites6.6%
lift--.f32N/A
sub-negN/A
+-commutativeN/A
neg-mul-1N/A
metadata-evalN/A
lower-fma.f32N/A
metadata-eval19.4
Applied rewrites20.0%
Taylor expanded in uy around 0
Applied rewrites20.0%
Final simplification19.9%
(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 56.3%
Taylor expanded in uy around 0
Applied rewrites49.2%
Taylor expanded in ux around 0
Applied rewrites6.6%
Final simplification6.6%
herbie shell --seed 2024276
(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)))))))