
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ 1.0 (+ 1.0 (exp (/ (PI) s))))))
(*
(- s)
(log
(-
(/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ (- (PI)) s)))) t_0)) t_0))
1.0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{1 + e^{\frac{\mathsf{PI}\left(\right)}{s}}}\\
\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\mathsf{PI}\left(\right)}{s}}} - t\_0\right) + t\_0} - 1\right)
\end{array}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ 1.0 (+ 1.0 (exp (/ (PI) s))))))
(*
(- s)
(log
(-
(/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ (- (PI)) s)))) t_0)) t_0))
1.0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{1 + e^{\frac{\mathsf{PI}\left(\right)}{s}}}\\
\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\mathsf{PI}\left(\right)}{s}}} - t\_0\right) + t\_0} - 1\right)
\end{array}
\end{array}
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ 1.0 (+ 1.0 (exp (/ (PI) s))))))
(*
(- s)
(log
(-
(/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ (- (PI)) s)))) t_0)) t_0))
1.0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{1 + e^{\frac{\mathsf{PI}\left(\right)}{s}}}\\
\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\mathsf{PI}\left(\right)}{s}}} - t\_0\right) + t\_0} - 1\right)
\end{array}
\end{array}
Initial program 98.8%
(FPCore (u s)
:precision binary32
(*
(- s)
(log
(-
(/
1.0
(*
(- (/ 1.0 (+ (exp (/ (- (PI)) s)) 1.0)) (/ 1.0 (+ (exp (/ (PI) s)) 1.0)))
u))
1.0))))\begin{array}{l}
\\
\left(-s\right) \cdot \log \left(\frac{1}{\left(\frac{1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1} - \frac{1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} + 1}\right) \cdot u} - 1\right)
\end{array}
Initial program 98.8%
Taylor expanded in u around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites1.5%
Taylor expanded in u around inf
Applied rewrites96.9%
(FPCore (u s)
:precision binary32
(*
(- s)
(log
(-
(-
(fma (/ (fma (* 0.5 (PI)) u (* 0.25 (PI))) s) -4.0 2.0)
(* -2.0 (/ (PI) s)))
1.0))))\begin{array}{l}
\\
\left(-s\right) \cdot \log \left(\left(\mathsf{fma}\left(\frac{\mathsf{fma}\left(0.5 \cdot \mathsf{PI}\left(\right), u, 0.25 \cdot \mathsf{PI}\left(\right)\right)}{s}, -4, 2\right) - -2 \cdot \frac{\mathsf{PI}\left(\right)}{s}\right) - 1\right)
\end{array}
Initial program 98.8%
lift-/.f32N/A
remove-double-negN/A
lift-neg.f32N/A
neg-sub0N/A
div-subN/A
frac-2negN/A
metadata-evalN/A
lift-neg.f32N/A
frac-subN/A
lower-/.f32N/A
lower--.f32N/A
lower-*.f32N/A
lower-*.f32N/A
lower-*.f3298.8
Applied rewrites98.8%
Applied rewrites98.8%
Taylor expanded in s around inf
lower--.f32N/A
Applied rewrites20.3%
(FPCore (u s) :precision binary32 (* (- s) (log (fma (fma (* u (PI)) -0.5 (* 0.25 (PI))) (/ 4.0 s) 1.0))))
\begin{array}{l}
\\
\left(-s\right) \cdot \log \left(\mathsf{fma}\left(\mathsf{fma}\left(u \cdot \mathsf{PI}\left(\right), -0.5, 0.25 \cdot \mathsf{PI}\left(\right)\right), \frac{4}{s}, 1\right)\right)
\end{array}
Initial program 98.8%
Taylor expanded in s around -inf
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f32N/A
Applied rewrites10.4%
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ (PI) u)) (t_1 (/ (PI) s)))
(if (<= s 9.999999682655225e-20)
(fma
(*
(+
(* (* t_1 (PI)) -4.0)
(fma (* -4.0 t_0) t_1 (* 4.0 (* t_1 (+ t_0 (PI))))))
(* u u))
-0.5
(* (fma (* u (PI)) -0.5 (* 0.25 (PI))) -4.0))
(- (* (* (PI) u) 2.0) (PI)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{PI}\left(\right)}{u}\\
t_1 := \frac{\mathsf{PI}\left(\right)}{s}\\
\mathbf{if}\;s \leq 9.999999682655225 \cdot 10^{-20}:\\
\;\;\;\;\mathsf{fma}\left(\left(\left(t\_1 \cdot \mathsf{PI}\left(\right)\right) \cdot -4 + \mathsf{fma}\left(-4 \cdot t\_0, t\_1, 4 \cdot \left(t\_1 \cdot \left(t\_0 + \mathsf{PI}\left(\right)\right)\right)\right)\right) \cdot \left(u \cdot u\right), -0.5, \mathsf{fma}\left(u \cdot \mathsf{PI}\left(\right), -0.5, 0.25 \cdot \mathsf{PI}\left(\right)\right) \cdot -4\right)\\
\mathbf{else}:\\
\;\;\;\;\left(\mathsf{PI}\left(\right) \cdot u\right) \cdot 2 - \mathsf{PI}\left(\right)\\
\end{array}
\end{array}
if s < 9.99999968e-20Initial program 98.7%
Taylor expanded in s around -inf
Applied rewrites7.9%
Taylor expanded in u around inf
Applied rewrites7.5%
Applied rewrites7.5%
if 9.99999968e-20 < s Initial program 98.9%
Taylor expanded in s around -inf
Applied rewrites9.0%
Applied rewrites10.1%
Applied rewrites16.6%
Final simplification11.9%
(FPCore (u s) :precision binary32 (- (* (* (PI) u) 2.0) (PI)))
\begin{array}{l}
\\
\left(\mathsf{PI}\left(\right) \cdot u\right) \cdot 2 - \mathsf{PI}\left(\right)
\end{array}
Initial program 98.8%
Taylor expanded in s around -inf
Applied rewrites7.8%
Applied rewrites9.3%
Applied rewrites12.0%
Final simplification12.0%
(FPCore (u s) :precision binary32 (- (PI)))
\begin{array}{l}
\\
-\mathsf{PI}\left(\right)
\end{array}
Initial program 98.8%
Taylor expanded in u around 0
mul-1-negN/A
lower-neg.f32N/A
lower-PI.f3211.6
Applied rewrites11.6%
(FPCore (u s) :precision binary32 0.0)
float code(float u, float s) {
return 0.0f;
}
real(4) function code(u, s)
real(4), intent (in) :: u
real(4), intent (in) :: s
code = 0.0e0
end function
function code(u, s) return Float32(0.0) end
function tmp = code(u, s) tmp = single(0.0); end
\begin{array}{l}
\\
0
\end{array}
Initial program 98.8%
Taylor expanded in s around -inf
Applied rewrites7.6%
Taylor expanded in s around 0
Applied rewrites10.4%
Taylor expanded in s around 0
Applied rewrites10.4%
herbie shell --seed 2024314
(FPCore (u s)
:name "Sample trimmed logistic on [-pi, pi]"
:precision binary32
:pre (and (and (<= 2.328306437e-10 u) (<= u 1.0)) (and (<= 0.0 s) (<= s 1.0651631)))
(* (- s) (log (- (/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ (- (PI)) s)))) (/ 1.0 (+ 1.0 (exp (/ (PI) s)))))) (/ 1.0 (+ 1.0 (exp (/ (PI) s)))))) 1.0))))