
(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 12 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 99.1%
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ 1.0 (+ 1.0 (exp (/ (PI) s))))))
(if (<=
(*
(- s)
(log
(-
(/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ (- (PI)) s)))) t_0)) t_0))
1.0)))
-1.5000000170217692e-19)
(* (- s) (* (* -0.25 (PI)) (/ -4.0 s)))
0.0)))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \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\_0\right) + t\_0} - 1\right) \leq -1.5000000170217692 \cdot 10^{-19}:\\
\;\;\;\;\left(-s\right) \cdot \left(\left(-0.25 \cdot \mathsf{PI}\left(\right)\right) \cdot \frac{-4}{s}\right)\\
\mathbf{else}:\\
\;\;\;\;0\\
\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)))) < -1.50000002e-19Initial program 99.3%
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-/.f3214.0
Applied rewrites13.9%
Taylor expanded in u around 0
Applied rewrites14.0%
if -1.50000002e-19 < (*.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
Applied rewrites7.1%
Applied rewrites7.9%
Taylor expanded in s around 0
Applied rewrites13.8%
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ (PI) s)) (t_1 (/ 1.0 (+ 1.0 (exp t_0)))))
(if (<=
(*
(- s)
(log
(-
(/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ (- (PI)) s)))) t_1)) t_1))
1.0)))
-1.5000000170217692e-19)
(* (- s) t_0)
0.0)))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{PI}\left(\right)}{s}\\
t_1 := \frac{1}{1 + e^{t\_0}}\\
\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 -1.5000000170217692 \cdot 10^{-19}:\\
\;\;\;\;\left(-s\right) \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;0\\
\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)))) < -1.50000002e-19Initial program 99.3%
Taylor expanded in u around 0
lower-/.f32N/A
lower-PI.f3214.0
Applied rewrites14.0%
if -1.50000002e-19 < (*.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
Applied rewrites6.7%
Applied rewrites6.9%
Taylor expanded in s around 0
Applied rewrites13.8%
(FPCore (u s)
:precision binary32
(let* ((t_0 (- (PI))) (t_1 (/ 1.0 (+ 1.0 (exp (/ (PI) s))))))
(if (<=
(*
(- s)
(log
(-
(/ 1.0 (+ (* u (- (/ 1.0 (+ 1.0 (exp (/ t_0 s)))) t_1)) t_1))
1.0)))
-1.5000000170217692e-19)
t_0
0.0)))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\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{t\_0}{s}}} - t\_1\right) + t\_1} - 1\right) \leq -1.5000000170217692 \cdot 10^{-19}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;0\\
\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)))) < -1.50000002e-19Initial program 99.3%
Taylor expanded in u around 0
mul-1-negN/A
lower-neg.f32N/A
lower-PI.f3214.0
Applied rewrites14.0%
if -1.50000002e-19 < (*.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
Applied rewrites6.6%
Applied rewrites7.5%
Taylor expanded in s around 0
Applied rewrites13.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 99.1%
Taylor expanded in u around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.5%
(FPCore (u s)
:precision binary32
(*
(- s)
(log
(-
(/
1.0
(*
(-
(/ 1.0 (+ (exp (/ (- (PI)) s)) 1.0))
(/ 1.0 (+ (+ (fma (/ 0.5 s) (/ (* (PI) (PI)) s) 1.0) (/ (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}{\left(\mathsf{fma}\left(\frac{0.5}{s}, \frac{\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)}{s}, 1\right) + \frac{\mathsf{PI}\left(\right)}{s}\right) + 1}\right) \cdot u} - 1\right)
\end{array}
Initial program 99.1%
Taylor expanded in u around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.5%
Taylor expanded in s around inf
Applied rewrites95.3%
(FPCore (u s)
:precision binary32
(*
(- s)
(log
(-
(/
1.0
(*
(-
(/ 1.0 (+ (exp (/ (- (PI)) s)) 1.0))
(/ 1.0 (+ (+ (/ (PI) s) 1.0) 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}{\left(\frac{\mathsf{PI}\left(\right)}{s} + 1\right) + 1}\right) \cdot u} - 1\right)
\end{array}
Initial program 99.1%
Taylor expanded in u around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites98.5%
Taylor expanded in s around inf
Applied rewrites95.2%
(FPCore (u s)
:precision binary32
(*
(- s)
(log
(-
(-
(fma (/ (fma (* (* 0.5 (PI)) 0.5) u (* 0.25 (PI))) s) -8.0 2.0)
(* (/ (* (PI) 12.0) s) -0.25))
1.0))))\begin{array}{l}
\\
\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, 0.25 \cdot \mathsf{PI}\left(\right)\right)}{s}, -8, 2\right) - \frac{\mathsf{PI}\left(\right) \cdot 12}{s} \cdot -0.25\right) - 1\right)
\end{array}
Initial program 99.1%
Applied rewrites97.6%
Taylor expanded in s around inf
lower--.f32N/A
Applied rewrites20.9%
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ (* (PI) (PI)) s)))
(fma
(fma t_0 -1.0 (fma 0.0 u t_0))
-0.5
(* (- (* 0.25 (PI)) (* (* u (PI)) 0.5)) -4.0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)}{s}\\
\mathsf{fma}\left(\mathsf{fma}\left(t\_0, -1, \mathsf{fma}\left(0, u, t\_0\right)\right), -0.5, \left(0.25 \cdot \mathsf{PI}\left(\right) - \left(u \cdot \mathsf{PI}\left(\right)\right) \cdot 0.5\right) \cdot -4\right)
\end{array}
\end{array}
Initial program 99.1%
Taylor expanded in s around -inf
Applied rewrites7.7%
Taylor expanded in u around 0
Applied rewrites10.7%
Applied rewrites5.0%
Applied rewrites10.9%
Final simplification10.9%
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ (* (PI) (PI)) s)))
(fma
(fma t_0 -1.0 (fma 0.0 u t_0))
-0.5
(* (* (PI) (+ 0.25 (* -0.5 u))) -4.0))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)}{s}\\
\mathsf{fma}\left(\mathsf{fma}\left(t\_0, -1, \mathsf{fma}\left(0, u, t\_0\right)\right), -0.5, \left(\mathsf{PI}\left(\right) \cdot \left(0.25 + -0.5 \cdot u\right)\right) \cdot -4\right)
\end{array}
\end{array}
Initial program 99.1%
Taylor expanded in s around -inf
Applied rewrites7.8%
Taylor expanded in u around 0
Applied rewrites10.7%
Applied rewrites5.0%
Applied rewrites10.9%
Final simplification10.9%
(FPCore (u s) :precision binary32 (if (<= s 9.999999682655225e-21) 0.0 (* (- s) (* (- (* 0.5 (* (PI) u)) (* 0.25 (PI))) (/ -4.0 s)))))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;s \leq 9.999999682655225 \cdot 10^{-21}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;\left(-s\right) \cdot \left(\left(0.5 \cdot \left(\mathsf{PI}\left(\right) \cdot u\right) - 0.25 \cdot \mathsf{PI}\left(\right)\right) \cdot \frac{-4}{s}\right)\\
\end{array}
\end{array}
if s < 9.99999968e-21Initial program 99.0%
Taylor expanded in s around -inf
Applied rewrites6.9%
Applied rewrites7.5%
Taylor expanded in s around 0
Applied rewrites14.0%
if 9.99999968e-21 < s Initial program 99.2%
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-/.f3213.7
Applied rewrites13.6%
Applied rewrites14.0%
(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 99.1%
Taylor expanded in s around -inf
Applied rewrites8.2%
Applied rewrites8.2%
Taylor expanded in s around 0
Applied rewrites10.7%
herbie shell --seed 2024333
(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))))