
(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 (- (exp (/ (PI) s)) -1.0))))
(*
(log
(-
-1.0
(/ -1.0 (- (* (- t_0 (/ -1.0 (- (exp (/ (- (PI)) s)) -1.0))) u) t_0))))
(- s))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{-1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} - -1}\\
\log \left(-1 - \frac{-1}{\left(t\_0 - \frac{-1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} - -1}\right) \cdot u - t\_0}\right) \cdot \left(-s\right)
\end{array}
\end{array}
Initial program 99.0%
Final simplification99.0%
(FPCore (u s)
:precision binary32
(*
(log
(-
(/
1.0
(*
(-
(/ -1.0 (- (exp (/ (PI) s)) -1.0))
(/ -1.0 (- (exp (/ (- (PI)) s)) -1.0)))
u))
1.0))
(- s)))\begin{array}{l}
\\
\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) \cdot \left(-s\right)
\end{array}
Initial program 99.0%
Taylor expanded in u around inf
*-commutativeN/A
lower-*.f32N/A
Applied rewrites97.6%
Final simplification97.6%
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ (PI) s)))
(if (<= s 4.399999888767292e-24)
(* (log (- (/ 1.0 (fma -0.25 t_0 0.5)) 1.0)) (- s))
(*
(log
(-
(/ 1.0 (* (fma (/ (- 0.5 (* 0.25 t_0)) u) -1.0 (* -0.5 t_0)) (- u)))
1.0))
(- s)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{PI}\left(\right)}{s}\\
\mathbf{if}\;s \leq 4.399999888767292 \cdot 10^{-24}:\\
\;\;\;\;\log \left(\frac{1}{\mathsf{fma}\left(-0.25, t\_0, 0.5\right)} - 1\right) \cdot \left(-s\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(\frac{1}{\mathsf{fma}\left(\frac{0.5 - 0.25 \cdot t\_0}{u}, -1, -0.5 \cdot t\_0\right) \cdot \left(-u\right)} - 1\right) \cdot \left(-s\right)\\
\end{array}
\end{array}
if s < 4.39999989e-24Initial 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 0
Applied rewrites13.8%
if 4.39999989e-24 < s Initial program 99.1%
Taylor expanded in s around -inf
mul-1-negN/A
unsub-negN/A
lower--.f32N/A
lower-/.f32N/A
Applied rewrites7.7%
Taylor expanded in u around -inf
Applied rewrites8.1%
Final simplification7.8%
(FPCore (u s) :precision binary32 (if (<= s 1.000000013351432e-10) (* (log (- (/ 1.0 (fma -0.25 (/ (PI) s) 0.5)) 1.0)) (- s)) (* (* u u) (/ (+ (/ (PI) u) (* -2.0 (PI))) (- u)))))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;s \leq 1.000000013351432 \cdot 10^{-10}:\\
\;\;\;\;\log \left(\frac{1}{\mathsf{fma}\left(-0.25, \frac{\mathsf{PI}\left(\right)}{s}, 0.5\right)} - 1\right) \cdot \left(-s\right)\\
\mathbf{else}:\\
\;\;\;\;\left(u \cdot u\right) \cdot \frac{\frac{\mathsf{PI}\left(\right)}{u} + -2 \cdot \mathsf{PI}\left(\right)}{-u}\\
\end{array}
\end{array}
if s < 1.00000001e-10Initial 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 0
Applied rewrites8.7%
if 1.00000001e-10 < s Initial program 99.2%
Taylor expanded in s around inf
Applied rewrites5.1%
Applied rewrites14.3%
Taylor expanded in u around -inf
Applied rewrites20.8%
Final simplification14.1%
(FPCore (u s) :precision binary32 (* (* u u) (/ (+ (/ (PI) u) (* -2.0 (PI))) (- u))))
\begin{array}{l}
\\
\left(u \cdot u\right) \cdot \frac{\frac{\mathsf{PI}\left(\right)}{u} + -2 \cdot \mathsf{PI}\left(\right)}{-u}
\end{array}
Initial program 99.0%
Taylor expanded in s around inf
Applied rewrites5.8%
Applied rewrites9.9%
Taylor expanded in u around -inf
Applied rewrites12.0%
Final simplification12.0%
(FPCore (u s) :precision binary32 (* -4.0 (* (fma -0.5 u 0.25) (PI))))
\begin{array}{l}
\\
-4 \cdot \left(\mathsf{fma}\left(-0.5, u, 0.25\right) \cdot \mathsf{PI}\left(\right)\right)
\end{array}
Initial program 99.0%
lift-neg.f32N/A
neg-sub0N/A
flip--N/A
lower-/.f32N/A
metadata-evalN/A
lower--.f32N/A
lower-*.f32N/A
lower-+.f3261.0
Applied rewrites61.0%
lift-/.f32N/A
clear-numN/A
lower-/.f32N/A
lift-+.f32N/A
+-lft-identityN/A
clear-numN/A
lift--.f32N/A
metadata-evalN/A
lift-*.f32N/A
+-lft-identityN/A
flip--N/A
neg-sub0N/A
lift-neg.f32N/A
lower-/.f3298.7
Applied rewrites98.7%
Taylor expanded in s around -inf
*-commutativeN/A
lower-*.f32N/A
cancel-sign-sub-invN/A
distribute-rgt-out--N/A
metadata-evalN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
metadata-evalN/A
distribute-rgt-outN/A
lower-*.f32N/A
lower-PI.f32N/A
lower-fma.f3211.8
Applied rewrites11.8%
Final simplification12.3%
(FPCore (u s) :precision binary32 (- (PI)))
\begin{array}{l}
\\
-\mathsf{PI}\left(\right)
\end{array}
Initial program 99.0%
Taylor expanded in u around 0
mul-1-negN/A
lower-neg.f32N/A
lower-PI.f3211.8
Applied rewrites11.8%
(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.0%
Taylor expanded in s around inf
Applied rewrites6.1%
Applied rewrites9.4%
Taylor expanded in s around 0
Applied rewrites10.3%
herbie shell --seed 2024270
(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))))