
(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 10 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.9%
(FPCore (u s)
:precision binary32
(let* ((t_0 (* 0.25 (PI))) (t_1 (/ 1.0 (+ 1.0 (exp (/ (PI) s))))))
(if (<=
(*
(- s)
(log
(-
(/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ (- (PI)) s)))) t_1)) t_1))
1.0)))
-9.999999717180685e-10)
(*
(- s)
(log
(-
(-
(fma (/ (fma (* (* 0.5 (PI)) 0.5) u t_0) s) -8.0 2.0)
(* (/ (* (PI) 12.0) s) -0.25))
1.0)))
(*
(- s)
(log (fma (fma (* u (pow (sqrt (PI)) 2.0)) -0.5 t_0) (/ 4.0 s) 1.0))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.25 \cdot \mathsf{PI}\left(\right)\\
t_1 := \frac{1}{1 + e^{\frac{\mathsf{PI}\left(\right)}{s}}}\\
\mathbf{if}\;\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\mathsf{PI}\left(\right)}{s}}} - t\_1\right) + t\_1} - 1\right) \leq -9.999999717180685 \cdot 10^{-10}:\\
\;\;\;\;\left(-s\right) \cdot \log \left(\left(\mathsf{fma}\left(\frac{\mathsf{fma}\left(\left(0.5 \cdot \mathsf{PI}\left(\right)\right) \cdot 0.5, u, t\_0\right)}{s}, -8, 2\right) - \frac{\mathsf{PI}\left(\right) \cdot 12}{s} \cdot -0.25\right) - 1\right)\\
\mathbf{else}:\\
\;\;\;\;\left(-s\right) \cdot \log \left(\mathsf{fma}\left(\mathsf{fma}\left(u \cdot {\left(\sqrt{\mathsf{PI}\left(\right)}\right)}^{2}, -0.5, t\_0\right), \frac{4}{s}, 1\right)\right)\\
\end{array}
\end{array}
if (*.f32 (neg.f32 s) (log.f32 (-.f32 (/.f32 #s(literal 1 binary32) (+.f32 (*.f32 u (-.f32 (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (PI.f32)) s)))) (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (PI.f32) s)))))) (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (PI.f32) s)))))) #s(literal 1 binary32)))) < -9.99999972e-10Initial program 98.9%
Applied rewrites97.7%
Taylor expanded in s around inf
lower--.f32N/A
Applied rewrites19.6%
if -9.99999972e-10 < (*.f32 (neg.f32 s) (log.f32 (-.f32 (/.f32 #s(literal 1 binary32) (+.f32 (*.f32 u (-.f32 (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (PI.f32)) s)))) (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (PI.f32) s)))))) (/.f32 #s(literal 1 binary32) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (PI.f32) s)))))) #s(literal 1 binary32)))) Initial program 98.9%
Taylor expanded in s around -inf
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f32N/A
Applied rewrites11.4%
Applied rewrites28.3%
(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.9%
Taylor expanded in u around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.8%
(FPCore (u s) :precision binary32 (* (- s) (log (+ (/ (PI) s) 1.0))))
\begin{array}{l}
\\
\left(-s\right) \cdot \log \left(\frac{\mathsf{PI}\left(\right)}{s} + 1\right)
\end{array}
Initial program 98.9%
Taylor expanded in s around -inf
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f32N/A
Applied rewrites10.1%
Taylor expanded in u around 0
Applied rewrites25.3%
(FPCore (u s) :precision binary32 (let* ((t_0 (sqrt (- (PI))))) (* (- s) (* (fma (* u (PI)) -0.5 (* 0.25 (* t_0 t_0))) (/ 4.0 s)))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{-\mathsf{PI}\left(\right)}\\
\left(-s\right) \cdot \left(\mathsf{fma}\left(u \cdot \mathsf{PI}\left(\right), -0.5, 0.25 \cdot \left(t\_0 \cdot t\_0\right)\right) \cdot \frac{4}{s}\right)
\end{array}
\end{array}
Initial program 98.9%
Taylor expanded in s around -inf
associate-*r/N/A
*-commutativeN/A
associate-/l*N/A
lower-*.f32N/A
fp-cancel-sub-sign-invN/A
distribute-rgt-out--N/A
metadata-evalN/A
associate-*r*N/A
metadata-evalN/A
lower-fma.f32N/A
lower-*.f32N/A
lower-PI.f32N/A
lower-*.f32N/A
lower-PI.f32N/A
lower-/.f3210.9
Applied rewrites10.8%
Applied rewrites12.1%
(FPCore (u s) :precision binary32 (let* ((t_0 (/ (PI) u))) (* (fma (/ -1.0 u) t_0 (fma t_0 2.0 0.0)) (* u u))))
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{PI}\left(\right)}{u}\\
\mathsf{fma}\left(\frac{-1}{u}, t\_0, \mathsf{fma}\left(t\_0, 2, 0\right)\right) \cdot \left(u \cdot u\right)
\end{array}
\end{array}
Initial program 98.9%
Taylor expanded in s around -inf
Applied rewrites7.8%
Taylor expanded in s around 0
Applied rewrites10.1%
Taylor expanded in u around inf
Applied rewrites10.1%
Final simplification10.1%
(FPCore (u s) :precision binary32 (fma 0.0 -0.5 (* (/ (* (PI) (- (* (* u u) 0.25) 0.0625)) (- (* -0.5 u) 0.25)) -4.0)))
\begin{array}{l}
\\
\mathsf{fma}\left(0, -0.5, \frac{\mathsf{PI}\left(\right) \cdot \left(\left(u \cdot u\right) \cdot 0.25 - 0.0625\right)}{-0.5 \cdot u - 0.25} \cdot -4\right)
\end{array}
Initial program 98.9%
Taylor expanded in s around -inf
Applied rewrites7.7%
Applied rewrites10.8%
Applied rewrites10.9%
Final simplification11.0%
(FPCore (u s) :precision binary32 (+ (* (* (* -0.5 u) (PI)) -4.0) (* (* 0.25 (PI)) -4.0)))
\begin{array}{l}
\\
\left(\left(-0.5 \cdot u\right) \cdot \mathsf{PI}\left(\right)\right) \cdot -4 + \left(0.25 \cdot \mathsf{PI}\left(\right)\right) \cdot -4
\end{array}
Initial program 98.9%
Taylor expanded in s around -inf
Applied rewrites8.1%
Applied rewrites10.8%
Applied rewrites11.0%
(FPCore (u s) :precision binary32 (- (PI)))
\begin{array}{l}
\\
-\mathsf{PI}\left(\right)
\end{array}
Initial program 98.9%
Taylor expanded in u around 0
mul-1-negN/A
lower-neg.f32N/A
lower-PI.f3210.9
Applied rewrites10.9%
(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.9%
Taylor expanded in s around -inf
Applied rewrites8.1%
Taylor expanded in s around 0
Applied rewrites10.1%
herbie shell --seed 2024339
(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))))