
(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 9 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) (* (- 1.0 maxCos) (- 1.0 maxCos)))))
(cos (* (PI) (* 2.0 uy)))))\begin{array}{l}
\\
\sqrt{\left(ux \cdot ux\right) \cdot \left(\frac{-2 \cdot maxCos + 2}{ux} - \left(1 - maxCos\right) \cdot \left(1 - maxCos\right)\right)} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 58.7%
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
lower-fma.f32N/A
lower-pow.f32N/A
lower--.f32N/A
unpow2N/A
lower-*.f3268.4
Applied rewrites68.1%
Applied rewrites98.7%
Applied rewrites98.7%
Final simplification98.7%
(FPCore (ux uy maxCos) :precision binary32 (* (cos (* (* (PI) uy) 2.0)) (sqrt (- ux (- (* (* (- (- 2.0 ux) ux) ux) maxCos) (* (- 1.0 ux) ux))))))
\begin{array}{l}
\\
\cos \left(\left(\mathsf{PI}\left(\right) \cdot uy\right) \cdot 2\right) \cdot \sqrt{ux - \left(\left(\left(\left(2 - ux\right) - ux\right) \cdot ux\right) \cdot maxCos - \left(1 - ux\right) \cdot ux\right)}
\end{array}
Initial program 58.7%
lift-*.f32N/A
lift-+.f32N/A
lift--.f32N/A
sub-negN/A
associate-+l+N/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-+.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
neg-mul-1N/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites42.5%
Taylor expanded in uy around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites29.3%
Taylor expanded in maxCos around 0
Applied rewrites98.3%
Final simplification98.3%
(FPCore (ux uy maxCos) :precision binary32 (* (sqrt (+ (* (- 1.0 ux) ux) ux)) (cos (* (PI) (* 2.0 uy)))))
\begin{array}{l}
\\
\sqrt{\left(1 - ux\right) \cdot ux + ux} \cdot \cos \left(\mathsf{PI}\left(\right) \cdot \left(2 \cdot uy\right)\right)
\end{array}
Initial program 58.7%
lift-*.f32N/A
lift-+.f32N/A
lift--.f32N/A
sub-negN/A
associate-+l+N/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-+.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
neg-mul-1N/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites42.5%
Taylor expanded in maxCos around 0
associate-*r*N/A
mul-1-negN/A
cancel-sign-subN/A
lower-+.f32N/A
*-commutativeN/A
lower-*.f32N/A
lower--.f3290.3
Applied rewrites90.3%
Final simplification90.3%
(FPCore (ux uy maxCos)
:precision binary32
(let* ((t_0 (- (* maxCos ux) (+ -1.0 ux))))
(if (<= t_0 0.9999999403953552)
(*
(sqrt (- 1.0 (* (- 1.0 (- ux (* maxCos ux))) t_0)))
(+ (* (* -2.0 uy) (* (* (PI) (PI)) uy)) 1.0))
(sqrt
(- ux (* (fma (- (fma ux maxCos 1.0) ux) (- maxCos 1.0) maxCos) ux))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := maxCos \cdot ux - \left(-1 + ux\right)\\
\mathbf{if}\;t\_0 \leq 0.9999999403953552:\\
\;\;\;\;\sqrt{1 - \left(1 - \left(ux - maxCos \cdot ux\right)\right) \cdot t\_0} \cdot \left(\left(-2 \cdot uy\right) \cdot \left(\left(\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\right) \cdot uy\right) + 1\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{ux - \mathsf{fma}\left(\mathsf{fma}\left(ux, maxCos, 1\right) - ux, maxCos - 1, maxCos\right) \cdot ux}\\
\end{array}
\end{array}
if (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) < 0.99999994Initial program 73.2%
Taylor expanded in uy around 0
+-commutativeN/A
associate-*r*N/A
lower-fma.f32N/A
*-commutativeN/A
lower-*.f32N/A
unpow2N/A
lower-*.f32N/A
unpow2N/A
lower-*.f32N/A
lower-PI.f32N/A
lower-PI.f3262.1
Applied rewrites61.7%
lift-+.f32N/A
lift--.f32N/A
associate-+l-N/A
lift-*.f32N/A
*-commutativeN/A
lift-*.f32N/A
lift--.f32N/A
lift--.f3262.1
Applied rewrites61.8%
Applied rewrites67.1%
if 0.99999994 < (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) Initial program 6.8%
lift-*.f32N/A
lift-+.f32N/A
lift--.f32N/A
sub-negN/A
associate-+l+N/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-+.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
neg-mul-1N/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites6.7%
Taylor expanded in uy around 0
lower-sqrt.f32N/A
lower--.f32N/A
+-commutativeN/A
*-commutativeN/A
distribute-lft-outN/A
lower-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower--.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower--.f3228.8
Applied rewrites28.8%
Final simplification58.7%
(FPCore (ux uy maxCos) :precision binary32 (sqrt (- ux (* (fma (- (fma ux maxCos 1.0) ux) (- maxCos 1.0) maxCos) ux))))
float code(float ux, float uy, float maxCos) {
return sqrtf((ux - (fmaf((fmaf(ux, maxCos, 1.0f) - ux), (maxCos - 1.0f), maxCos) * ux)));
}
function code(ux, uy, maxCos) return sqrt(Float32(ux - Float32(fma(Float32(fma(ux, maxCos, Float32(1.0)) - ux), Float32(maxCos - Float32(1.0)), maxCos) * ux))) end
\begin{array}{l}
\\
\sqrt{ux - \mathsf{fma}\left(\mathsf{fma}\left(ux, maxCos, 1\right) - ux, maxCos - 1, maxCos\right) \cdot ux}
\end{array}
Initial program 58.7%
lift-*.f32N/A
lift-+.f32N/A
lift--.f32N/A
sub-negN/A
associate-+l+N/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-+.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower-*.f32N/A
lift-+.f32N/A
+-commutativeN/A
lift-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
neg-mul-1N/A
lift-*.f32N/A
*-commutativeN/A
Applied rewrites42.8%
Taylor expanded in uy around 0
lower-sqrt.f32N/A
lower--.f32N/A
+-commutativeN/A
*-commutativeN/A
distribute-lft-outN/A
lower-*.f32N/A
*-commutativeN/A
lower-fma.f32N/A
lower--.f32N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f32N/A
lower--.f3229.5
Applied rewrites29.5%
Final simplification29.5%
(FPCore (ux uy maxCos) :precision binary32 (let* ((t_0 (- (fma ux maxCos 1.0) ux))) (sqrt (- 1.0 (* t_0 t_0)))))
float code(float ux, float uy, float maxCos) {
float t_0 = fmaf(ux, maxCos, 1.0f) - ux;
return sqrtf((1.0f - (t_0 * t_0)));
}
function code(ux, uy, maxCos) t_0 = Float32(fma(ux, maxCos, Float32(1.0)) - ux) return sqrt(Float32(Float32(1.0) - Float32(t_0 * t_0))) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(ux, maxCos, 1\right) - ux\\
\sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}
Initial program 58.7%
Taylor expanded in uy around 0
lower-sqrt.f32N/A
lower--.f32N/A
lower-pow.f32N/A
lower--.f32N/A
+-commutativeN/A
lower-fma.f3248.3
Applied rewrites48.3%
Applied rewrites48.3%
(FPCore (ux uy maxCos) :precision binary32 (sqrt (- 1.0 (* (* maxCos maxCos) (* ux ux)))))
float code(float ux, float uy, float maxCos) {
return sqrtf((1.0f - ((maxCos * maxCos) * (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 = sqrt((1.0e0 - ((maxcos * maxcos) * (ux * ux))))
end function
function code(ux, uy, maxCos) return sqrt(Float32(Float32(1.0) - Float32(Float32(maxCos * maxCos) * Float32(ux * ux)))) end
function tmp = code(ux, uy, maxCos) tmp = sqrt((single(1.0) - ((maxCos * maxCos) * (ux * ux)))); end
\begin{array}{l}
\\
\sqrt{1 - \left(maxCos \cdot maxCos\right) \cdot \left(ux \cdot ux\right)}
\end{array}
Initial program 58.7%
Taylor expanded in uy around 0
lower-sqrt.f32N/A
lower--.f32N/A
lower-pow.f32N/A
lower--.f32N/A
+-commutativeN/A
lower-fma.f3248.3
Applied rewrites48.3%
Taylor expanded in maxCos around inf
Applied rewrites19.8%
(FPCore (ux uy maxCos) :precision binary32 (sqrt (* (fma -2.0 maxCos 2.0) ux)))
float code(float ux, float uy, float maxCos) {
return sqrtf((fmaf(-2.0f, maxCos, 2.0f) * ux));
}
function code(ux, uy, maxCos) return sqrt(Float32(fma(Float32(-2.0), maxCos, Float32(2.0)) * ux)) end
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux}
\end{array}
Initial program 58.7%
Taylor expanded in uy around 0
lower-sqrt.f32N/A
lower--.f32N/A
lower-pow.f32N/A
lower--.f32N/A
+-commutativeN/A
lower-fma.f3248.3
Applied rewrites48.3%
Taylor expanded in maxCos around inf
Applied rewrites-0.0%
Taylor expanded in ux around 0
Applied rewrites59.0%
(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 58.7%
Taylor expanded in uy around 0
lower-sqrt.f32N/A
lower--.f32N/A
lower-pow.f32N/A
lower--.f32N/A
+-commutativeN/A
lower-fma.f3248.3
Applied rewrites48.3%
Taylor expanded in maxCos around inf
Applied rewrites-0.0%
herbie shell --seed 2024296
(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)))))))