
(FPCore (ux uy maxCos) :precision binary32 (+ (- 1.0 ux) (* ux maxCos)))
float code(float ux, float uy, float maxCos) {
return (1.0f - ux) + (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 = (1.0e0 - ux) + (ux * maxcos)
end function
function code(ux, uy, maxCos) return Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos)) end
function tmp = code(ux, uy, maxCos) tmp = (single(1.0) - ux) + (ux * maxCos); end
\begin{array}{l}
\\
\left(1 - ux\right) + ux \cdot maxCos
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (ux uy maxCos) :precision binary32 (+ (- 1.0 ux) (* ux maxCos)))
float code(float ux, float uy, float maxCos) {
return (1.0f - ux) + (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 = (1.0e0 - ux) + (ux * maxcos)
end function
function code(ux, uy, maxCos) return Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos)) end
function tmp = code(ux, uy, maxCos) tmp = (single(1.0) - ux) + (ux * maxCos); end
\begin{array}{l}
\\
\left(1 - ux\right) + ux \cdot maxCos
\end{array}
(FPCore (ux uy maxCos) :precision binary32 (fma ux (+ maxCos -1.0) 1.0))
float code(float ux, float uy, float maxCos) {
return fmaf(ux, (maxCos + -1.0f), 1.0f);
}
function code(ux, uy, maxCos) return fma(ux, Float32(maxCos + Float32(-1.0)), Float32(1.0)) end
\begin{array}{l}
\\
\mathsf{fma}\left(ux, maxCos + -1, 1\right)
\end{array}
Initial program 99.8%
cancel-sign-sub99.8%
distribute-lft-neg-out99.8%
distribute-rgt-neg-out99.8%
sub-neg99.8%
associate-+r-99.8%
+-commutative99.8%
neg-mul-199.8%
*-commutative99.8%
distribute-rgt-out--99.8%
fma-define99.9%
sub-neg99.9%
remove-double-neg99.9%
+-commutative99.9%
Simplified99.9%
(FPCore (ux uy maxCos) :precision binary32 (+ 1.0 (* maxCos (- ux (/ ux maxCos)))))
float code(float ux, float uy, float maxCos) {
return 1.0f + (maxCos * (ux - (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 = 1.0e0 + (maxcos * (ux - (ux / maxcos)))
end function
function code(ux, uy, maxCos) return Float32(Float32(1.0) + Float32(maxCos * Float32(ux - Float32(ux / maxCos)))) end
function tmp = code(ux, uy, maxCos) tmp = single(1.0) + (maxCos * (ux - (ux / maxCos))); end
\begin{array}{l}
\\
1 + maxCos \cdot \left(ux - \frac{ux}{maxCos}\right)
\end{array}
Initial program 99.8%
Taylor expanded in maxCos around inf 97.7%
sub-neg97.7%
+-commutative97.7%
mul-1-neg97.7%
associate-+l+97.8%
+-commutative97.8%
distribute-lft-out98.6%
mul-1-neg98.6%
unsub-neg98.6%
rgt-mult-inverse99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (ux uy maxCos) :precision binary32 (+ (- 1.0 ux) (* ux maxCos)))
float code(float ux, float uy, float maxCos) {
return (1.0f - ux) + (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 = (1.0e0 - ux) + (ux * maxcos)
end function
function code(ux, uy, maxCos) return Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos)) end
function tmp = code(ux, uy, maxCos) tmp = (single(1.0) - ux) + (ux * maxCos); end
\begin{array}{l}
\\
\left(1 - ux\right) + ux \cdot maxCos
\end{array}
Initial program 99.8%
(FPCore (ux uy maxCos) :precision binary32 (- 1.0 ux))
float code(float ux, float uy, float maxCos) {
return 1.0f - 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 - ux
end function
function code(ux, uy, maxCos) return Float32(Float32(1.0) - ux) end
function tmp = code(ux, uy, maxCos) tmp = single(1.0) - ux; end
\begin{array}{l}
\\
1 - ux
\end{array}
Initial program 99.8%
Taylor expanded in maxCos around inf 97.7%
sub-neg97.7%
+-commutative97.7%
mul-1-neg97.7%
associate-+l+97.8%
+-commutative97.8%
distribute-lft-out98.6%
mul-1-neg98.6%
unsub-neg98.6%
rgt-mult-inverse99.8%
Simplified99.8%
Taylor expanded in maxCos around 0 98.6%
mul-1-neg98.6%
sub-neg98.6%
Simplified98.6%
(FPCore (ux uy maxCos) :precision binary32 1.0)
float code(float ux, float uy, float maxCos) {
return 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 = 1.0e0
end function
function code(ux, uy, maxCos) return Float32(1.0) end
function tmp = code(ux, uy, maxCos) tmp = single(1.0); end
\begin{array}{l}
\\
1
\end{array}
Initial program 99.8%
Taylor expanded in maxCos around inf 97.7%
sub-neg97.7%
+-commutative97.7%
mul-1-neg97.7%
associate-+l+97.8%
+-commutative97.8%
distribute-lft-out98.6%
mul-1-neg98.6%
unsub-neg98.6%
rgt-mult-inverse99.8%
Simplified99.8%
Taylor expanded in ux around 0 71.4%
herbie shell --seed 2024152
(FPCore (ux uy maxCos)
:name "UniformSampleCone, z"
: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)))
(+ (- 1.0 ux) (* ux maxCos)))