
(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 11 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
(*
(- s)
(log
(/
(-
(/
1.0
(-
(/ 1.0 (+ (exp (/ (- (PI)) s)) 1.0))
(/ 1.0 (+ (exp (/ (PI) s)) 1.0))))
u)
u))))\begin{array}{l}
\\
\left(-s\right) \cdot \log \left(\frac{\frac{1}{\frac{1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1} - \frac{1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} + 1}} - u}{u}\right)
\end{array}
Initial program 98.9%
Taylor expanded in u around inf
lower--.f32N/A
Applied rewrites98.6%
Taylor expanded in u around 0
Applied rewrites98.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.9%
Taylor expanded in u around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites1.7%
Taylor expanded in u around inf
Applied rewrites98.6%
Final simplification98.6%
(FPCore (u s)
:precision binary32
(if (<= s 4.999999841327613e-21)
(*
(- s)
(log
(- (/ 1.0 (fma u (/ (fma 0.5 (PI) (* (/ (PI) u) -0.25)) s) 0.5)) 1.0)))
(* (- s) (log (- (+ (/ (PI) s) 1.0) (* (/ -0.5 s) (/ (* (PI) (PI)) s)))))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;s \leq 4.999999841327613 \cdot 10^{-21}:\\
\;\;\;\;\left(-s\right) \cdot \log \left(\frac{1}{\mathsf{fma}\left(u, \frac{\mathsf{fma}\left(0.5, \mathsf{PI}\left(\right), \frac{\mathsf{PI}\left(\right)}{u} \cdot -0.25\right)}{s}, 0.5\right)} - 1\right)\\
\mathbf{else}:\\
\;\;\;\;\left(-s\right) \cdot \log \left(\left(\frac{\mathsf{PI}\left(\right)}{s} + 1\right) - \frac{-0.5}{s} \cdot \frac{\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)}{s}\right)\\
\end{array}
\end{array}
if s < 4.99999984e-21Initial program 98.9%
Taylor expanded in s around -inf
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
lower-/.f32N/A
Applied rewrites-0.0%
Taylor expanded in u around -inf
Applied rewrites-0.0%
Taylor expanded in s around -inf
Applied rewrites13.1%
if 4.99999984e-21 < s Initial program 98.9%
Taylor expanded in s around inf
cancel-sign-sub-invN/A
metadata-evalN/A
+-commutativeN/A
Applied rewrites5.6%
Taylor expanded in u around 0
Applied rewrites23.5%
Final simplification15.8%
(FPCore (u s) :precision binary32 (* (- s) (log (- (/ 1.0 (fma -0.25 (/ (PI) s) 0.5)) 1.0))))
\begin{array}{l}
\\
\left(-s\right) \cdot \log \left(\frac{1}{\mathsf{fma}\left(-0.25, \frac{\mathsf{PI}\left(\right)}{s}, 0.5\right)} - 1\right)
\end{array}
Initial program 98.9%
Taylor expanded in s around -inf
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
lower-/.f32N/A
Applied rewrites-0.0%
Taylor expanded in u around -inf
Applied rewrites7.3%
Taylor expanded in u around 0
Applied rewrites10.4%
Final simplification10.4%
(FPCore (u s) :precision binary32 (* (/ (+ (* -2.0 (PI)) (/ (PI) u)) (- u)) (* u u)))
\begin{array}{l}
\\
\frac{-2 \cdot \mathsf{PI}\left(\right) + \frac{\mathsf{PI}\left(\right)}{u}}{-u} \cdot \left(u \cdot u\right)
\end{array}
Initial program 98.9%
Taylor expanded in u around inf
lower--.f32N/A
Applied rewrites98.6%
Taylor expanded in s around -inf
Applied rewrites7.4%
Taylor expanded in u around -inf
Applied rewrites10.6%
Final simplification10.6%
(FPCore (u s) :precision binary32 (* (- s) (fma (/ 1.0 s) (PI) 0.0)))
\begin{array}{l}
\\
\left(-s\right) \cdot \mathsf{fma}\left(\frac{1}{s}, \mathsf{PI}\left(\right), 0\right)
\end{array}
Initial program 98.9%
Taylor expanded in u around inf
lower--.f32N/A
Applied rewrites98.6%
Taylor expanded in u around 0
lower-/.f32N/A
lower-PI.f3210.4
Applied rewrites10.4%
Applied rewrites10.4%
(FPCore (u s) :precision binary32 (* (fma (* -0.5 (PI)) u (* 0.25 (PI))) -4.0))
\begin{array}{l}
\\
\mathsf{fma}\left(-0.5 \cdot \mathsf{PI}\left(\right), u, 0.25 \cdot \mathsf{PI}\left(\right)\right) \cdot -4
\end{array}
Initial program 98.9%
Taylor expanded in u around inf
lower--.f32N/A
Applied rewrites98.6%
Taylor expanded in u around 0
Applied rewrites98.8%
Taylor expanded in s around -inf
*-commutativeN/A
lower-*.f32N/A
cancel-sign-sub-invN/A
distribute-rgt-out--N/A
metadata-evalN/A
*-commutativeN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f32N/A
lower-*.f32N/A
lower-PI.f32N/A
lower-*.f32N/A
lower-PI.f3210.4
Applied rewrites10.4%
(FPCore (u s) :precision binary32 (* (* (PI) (fma -0.5 u 0.25)) -4.0))
\begin{array}{l}
\\
\left(\mathsf{PI}\left(\right) \cdot \mathsf{fma}\left(-0.5, u, 0.25\right)\right) \cdot -4
\end{array}
Initial program 98.9%
Taylor expanded in u around inf
lower--.f32N/A
Applied rewrites98.6%
Taylor expanded in s around -inf
Applied rewrites7.6%
Taylor expanded in s around inf
Applied rewrites10.4%
(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.4
Applied rewrites10.4%
(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 u around inf
lower--.f32N/A
Applied rewrites98.6%
Taylor expanded in s around -inf
Applied rewrites7.4%
Taylor expanded in s around 0
Applied rewrites10.4%
Taylor expanded in s around 0
Applied rewrites10.4%
herbie shell --seed 2024307
(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))))