
(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 11 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) (- (/ (+ (* -2.0 maxCos) 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{-2 \cdot maxCos + 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 55.8%
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-*.f3268.6
Applied rewrites68.6%
Applied rewrites98.6%
Final simplification98.6%
(FPCore (ux uy maxCos)
:precision binary32
(*
(sqrt
(-
(* (- (/ (- 2.0 maxCos) ux) (- 1.0 maxCos)) (* ux ux))
(* (* (fma maxCos ux (- 1.0 ux)) maxCos) ux)))
(cos (* (PI) (* 2.0 uy)))))\begin{array}{l}
\\
\sqrt{\left(\frac{2 - maxCos}{ux} - \left(1 - maxCos\right)\right) \cdot \left(ux \cdot ux\right) - \left(\mathsf{fma}\left(maxCos, ux, 1 - ux\right) \cdot maxCos\right) \cdot ux} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 55.8%
lift--.f32N/A
lift-*.f32N/A
lift-+.f32N/A
distribute-rgt-inN/A
associate--r+N/A
lower--.f32N/A
lower--.f32N/A
*-commutativeN/A
lower-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
*-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites40.1%
Taylor expanded in ux around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites92.0%
Final simplification93.8%
(FPCore (ux uy maxCos) :precision binary32 (if (<= (* 2.0 uy) 0.0012000000569969416) (* (sqrt (* (* (- (/ 2.0 ux) 1.0) ux) ux)) 1.0) (* (sqrt (* 2.0 ux)) (cos (* (PI) (* 2.0 uy))))))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;2 \cdot uy \leq 0.0012000000569969416:\\
\;\;\;\;\sqrt{\left(\left(\frac{2}{ux} - 1\right) \cdot ux\right) \cdot ux} \cdot 1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{2 \cdot ux} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)\\
\end{array}
\end{array}
if (*.f32 uy #s(literal 2 binary32)) < 0.00120000006Initial program 57.0%
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-*.f3293.7
Applied rewrites93.7%
Taylor expanded in maxCos around 0
Applied rewrites94.1%
Taylor expanded in uy around 0
Applied rewrites93.2%
if 0.00120000006 < (*.f32 uy #s(literal 2 binary32)) Initial program 53.2%
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-*.f3218.1
Applied rewrites18.1%
Taylor expanded in maxCos around 0
Applied rewrites92.4%
Taylor expanded in ux around 0
Applied rewrites75.9%
Final simplification87.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 55.8%
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-*.f3268.6
Applied rewrites68.6%
Taylor expanded in maxCos around 0
Applied rewrites93.5%
Taylor expanded in ux around 0
Applied rewrites93.6%
Final simplification93.6%
(FPCore (ux uy maxCos)
:precision binary32
(if (<= maxCos 9.9999998245167e-15)
(* (sqrt (* (* (- (/ 2.0 ux) 1.0) ux) ux)) 1.0)
(*
(sqrt
(*
(* maxCos maxCos)
(-
(/ (- (/ (* (- 2.0 ux) ux) maxCos) (* (* (- 1.0 ux) ux) 2.0)) maxCos)
(* ux ux))))
1.0)))
float code(float ux, float uy, float maxCos) {
float tmp;
if (maxCos <= 9.9999998245167e-15f) {
tmp = sqrtf(((((2.0f / ux) - 1.0f) * ux) * ux)) * 1.0f;
} else {
tmp = sqrtf(((maxCos * maxCos) * ((((((2.0f - ux) * ux) / maxCos) - (((1.0f - ux) * ux) * 2.0f)) / maxCos) - (ux * ux)))) * 1.0f;
}
return tmp;
}
real(4) function code(ux, uy, maxcos)
real(4), intent (in) :: ux
real(4), intent (in) :: uy
real(4), intent (in) :: maxcos
real(4) :: tmp
if (maxcos <= 9.9999998245167e-15) then
tmp = sqrt(((((2.0e0 / ux) - 1.0e0) * ux) * ux)) * 1.0e0
else
tmp = sqrt(((maxcos * maxcos) * ((((((2.0e0 - ux) * ux) / maxcos) - (((1.0e0 - ux) * ux) * 2.0e0)) / maxcos) - (ux * ux)))) * 1.0e0
end if
code = tmp
end function
function code(ux, uy, maxCos) tmp = Float32(0.0) if (maxCos <= Float32(9.9999998245167e-15)) tmp = Float32(sqrt(Float32(Float32(Float32(Float32(Float32(2.0) / ux) - Float32(1.0)) * ux) * ux)) * Float32(1.0)); else tmp = Float32(sqrt(Float32(Float32(maxCos * maxCos) * Float32(Float32(Float32(Float32(Float32(Float32(Float32(2.0) - ux) * ux) / maxCos) - Float32(Float32(Float32(Float32(1.0) - ux) * ux) * Float32(2.0))) / maxCos) - Float32(ux * ux)))) * Float32(1.0)); end return tmp end
function tmp_2 = code(ux, uy, maxCos) tmp = single(0.0); if (maxCos <= single(9.9999998245167e-15)) tmp = sqrt(((((single(2.0) / ux) - single(1.0)) * ux) * ux)) * single(1.0); else tmp = sqrt(((maxCos * maxCos) * ((((((single(2.0) - ux) * ux) / maxCos) - (((single(1.0) - ux) * ux) * single(2.0))) / maxCos) - (ux * ux)))) * single(1.0); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;maxCos \leq 9.9999998245167 \cdot 10^{-15}:\\
\;\;\;\;\sqrt{\left(\left(\frac{2}{ux} - 1\right) \cdot ux\right) \cdot ux} \cdot 1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(maxCos \cdot maxCos\right) \cdot \left(\frac{\frac{\left(2 - ux\right) \cdot ux}{maxCos} - \left(\left(1 - ux\right) \cdot ux\right) \cdot 2}{maxCos} - ux \cdot ux\right)} \cdot 1\\
\end{array}
\end{array}
if maxCos < 9.99999982e-15Initial program 54.8%
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-*.f3269.7
Applied rewrites69.7%
Taylor expanded in maxCos around 0
Applied rewrites98.9%
Taylor expanded in uy around 0
Applied rewrites77.9%
if 9.99999982e-15 < maxCos Initial program 57.5%
Taylor expanded in maxCos around -inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites56.1%
Taylor expanded in ux around 0
Applied rewrites98.7%
Taylor expanded in uy around 0
Applied rewrites82.2%
Final simplification79.5%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (- (+ -1.0 ux) (* maxCos ux))))
(if (<= (- 1.0 (* t_0 t_0)) 0.00022000000171829015)
(* (sqrt (* (fma -2.0 maxCos 2.0) ux)) 1.0)
(* (sqrt (- 1.0 (* (- 1.0 ux) (- (* maxCos ux) (+ -1.0 ux))))) 1.0))))
float code(float ux, float uy, float maxCos) {
float t_0 = (-1.0f + ux) - (maxCos * ux);
float tmp;
if ((1.0f - (t_0 * t_0)) <= 0.00022000000171829015f) {
tmp = sqrtf((fmaf(-2.0f, maxCos, 2.0f) * ux)) * 1.0f;
} else {
tmp = sqrtf((1.0f - ((1.0f - ux) * ((maxCos * ux) - (-1.0f + ux))))) * 1.0f;
}
return tmp;
}
function code(ux, uy, maxCos) t_0 = Float32(Float32(Float32(-1.0) + ux) - Float32(maxCos * ux)) tmp = Float32(0.0) if (Float32(Float32(1.0) - Float32(t_0 * t_0)) <= Float32(0.00022000000171829015)) tmp = Float32(sqrt(Float32(fma(Float32(-2.0), maxCos, Float32(2.0)) * ux)) * Float32(1.0)); else tmp = Float32(sqrt(Float32(Float32(1.0) - Float32(Float32(Float32(1.0) - ux) * Float32(Float32(maxCos * ux) - Float32(Float32(-1.0) + ux))))) * Float32(1.0)); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(-1 + ux\right) - maxCos \cdot ux\\
\mathbf{if}\;1 - t\_0 \cdot t\_0 \leq 0.00022000000171829015:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux} \cdot 1\\
\mathbf{else}:\\
\;\;\;\;\sqrt{1 - \left(1 - ux\right) \cdot \left(maxCos \cdot ux - \left(-1 + ux\right)\right)} \cdot 1\\
\end{array}
\end{array}
if (-.f32 #s(literal 1 binary32) (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)))) < 2.20000002e-4Initial program 35.0%
Taylor expanded in uy around 0
Applied rewrites31.1%
Taylor expanded in ux around 0
cancel-sign-sub-invN/A
metadata-evalN/A
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3271.8
Applied rewrites-0.0%
if 2.20000002e-4 < (-.f32 #s(literal 1 binary32) (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)))) Initial program 89.7%
Taylor expanded in uy around 0
Applied rewrites75.3%
Taylor expanded in maxCos around 0
lower--.f3272.4
Applied rewrites72.4%
Final simplification72.0%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (* (- (/ 2.0 ux) 1.0) ux) ux)) 1.0))
float code(float ux, float uy, float maxCos) {
return sqrtf(((((2.0f / ux) - 1.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) - 1.0e0) * ux) * ux)) * 1.0e0
end function
function code(ux, uy, maxCos) return Float32(sqrt(Float32(Float32(Float32(Float32(Float32(2.0) / ux) - Float32(1.0)) * ux) * ux)) * Float32(1.0)) end
function tmp = code(ux, uy, maxCos) tmp = sqrt(((((single(2.0) / ux) - single(1.0)) * ux) * ux)) * single(1.0); end
\begin{array}{l}
\\
\sqrt{\left(\left(\frac{2}{ux} - 1\right) \cdot ux\right) \cdot ux} \cdot 1
\end{array}
Initial program 55.8%
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-*.f3268.6
Applied rewrites68.6%
Taylor expanded in maxCos around 0
Applied rewrites93.5%
Taylor expanded in uy around 0
Applied rewrites76.1%
Final simplification76.1%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (fma maxCos -2.0 2.0) ux)) 1.0))
float code(float ux, float uy, float maxCos) {
return sqrtf((fmaf(maxCos, -2.0f, 2.0f) * ux)) * 1.0f;
}
function code(ux, uy, maxCos) return Float32(sqrt(Float32(fma(maxCos, Float32(-2.0), Float32(2.0)) * ux)) * Float32(1.0)) end
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(maxCos, -2, 2\right) \cdot ux} \cdot 1
\end{array}
Initial program 55.8%
Taylor expanded in uy around 0
Applied rewrites47.9%
lift-*.f32N/A
lift-+.f32N/A
distribute-lft-inN/A
lift-+.f32N/A
lift-*.f32N/A
*-commutativeN/A
+-commutativeN/A
lift-fma.f32N/A
lift-*.f32N/A
*-commutativeN/A
associate-*l*N/A
lift-*.f32N/A
lift-*.f32N/A
lower-fma.f3219.6
lift-+.f32N/A
lift-*.f32N/A
*-commutativeN/A
+-commutativeN/A
lift-fma.f3219.6
Applied rewrites19.6%
Taylor expanded in ux around 0
cancel-sign-sub-invN/A
metadata-evalN/A
lower-*.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f3262.2
Applied rewrites62.2%
Final simplification62.2%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (* (fma -2.0 maxCos 2.0) ux)) 1.0))
float code(float ux, float uy, float maxCos) {
return sqrtf((fmaf(-2.0f, maxCos, 2.0f) * ux)) * 1.0f;
}
function code(ux, uy, maxCos) return Float32(sqrt(Float32(fma(Float32(-2.0), maxCos, Float32(2.0)) * ux)) * Float32(1.0)) end
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux} \cdot 1
\end{array}
Initial program 55.8%
Taylor expanded in uy around 0
Applied rewrites47.9%
Taylor expanded in ux around 0
cancel-sign-sub-invN/A
metadata-evalN/A
*-commutativeN/A
lower-*.f32N/A
+-commutativeN/A
lower-fma.f3262.2
Applied rewrites62.2%
Final simplification62.1%
(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.8%
Taylor expanded in uy around 0
Applied rewrites47.9%
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.6
Applied rewrites19.6%
Final simplification-0.0%
(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.8%
Taylor expanded in uy around 0
Applied rewrites47.9%
Taylor expanded in ux around 0
Applied rewrites6.6%
Final simplification6.6%
herbie shell --seed 2024255
(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)))))))