
(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
(*
(- s)
(log
(-
(/
1.0
(+
(*
u
(-
(/ 1.0 (+ 1.0 (exp (/ (- (PI)) s))))
(/ 1.0 (+ 1.0 (exp (* (/ 1.0 s) (PI)))))))
(/ 1.0 (+ 1.0 (exp (/ (* (PI) (log (E))) s))))))
1.0))))\begin{array}{l}
\\
\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\mathsf{PI}\left(\right)}{s}}} - \frac{1}{1 + e^{\frac{1}{s} \cdot \mathsf{PI}\left(\right)}}\right) + \frac{1}{1 + e^{\frac{\mathsf{PI}\left(\right) \cdot \log \mathsf{E}\left(\right)}{s}}}} - 1\right)
\end{array}
Initial program 99.1%
lift-/.f32N/A
clear-numN/A
associate-/r/N/A
lower-*.f32N/A
lower-/.f3299.1
Applied rewrites99.1%
lift-PI.f32N/A
add-log-expN/A
*-un-lft-identityN/A
lift-PI.f32N/A
exp-prodN/A
log-powN/A
lower-*.f32N/A
lower-log.f32N/A
exp-1-eN/A
lower-E.f3299.1
Applied rewrites99.1%
(FPCore (u s)
:precision binary32
(*
(- s)
(log
(-
(/
1.0
(+
(*
u
(-
(/ 1.0 (+ 1.0 (exp (/ (- (PI)) s))))
(/ 1.0 (+ 1.0 (exp (* (/ 1.0 s) (PI)))))))
(/ 1.0 (+ 1.0 (exp (/ (PI) s))))))
1.0))))\begin{array}{l}
\\
\left(-s\right) \cdot \log \left(\frac{1}{u \cdot \left(\frac{1}{1 + e^{\frac{-\mathsf{PI}\left(\right)}{s}}} - \frac{1}{1 + e^{\frac{1}{s} \cdot \mathsf{PI}\left(\right)}}\right) + \frac{1}{1 + e^{\frac{\mathsf{PI}\left(\right)}{s}}}} - 1\right)
\end{array}
Initial program 99.1%
lift-/.f32N/A
clear-numN/A
associate-/r/N/A
lower-*.f32N/A
lower-/.f3299.1
Applied rewrites99.1%
(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
(*
(- s)
(log
(-
(/
(/ 1.0 u)
(- (/ 1.0 (+ (exp (/ (- (PI)) s)) 1.0)) (/ 1.0 (+ (exp (/ (PI) s)) 1.0))))
1.0))))\begin{array}{l}
\\
\left(-s\right) \cdot \log \left(\frac{\frac{1}{u}}{\frac{1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1} - \frac{1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} + 1}} - 1\right)
\end{array}
Initial program 99.1%
Taylor expanded in u around inf
lower--.f32N/A
Applied rewrites97.6%
(FPCore (u s)
:precision binary32
(let* ((t_0 (* 0.25 (PI)))
(t_1 (fma (* u (PI)) -0.5 t_0))
(t_2 (pow t_1 2.0)))
(if (<= s 1.000000031374395e-22)
(fma
(/
(fma t_2 -16.0 (/ 1.0 (pow (* (* (PI) (fma -0.5 u 0.25)) -4.0) -2.0)))
s)
-0.5
(* t_1 -4.0))
(fma
(/ (fma t_2 -16.0 (fma 16.0 (pow (fma (PI) (* u -0.5) t_0) 2.0) 0.0)) s)
-0.5
(fma t_0 -4.0 (* (* (* -0.5 u) (PI)) -4.0))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.25 \cdot \mathsf{PI}\left(\right)\\
t_1 := \mathsf{fma}\left(u \cdot \mathsf{PI}\left(\right), -0.5, t\_0\right)\\
t_2 := {t\_1}^{2}\\
\mathbf{if}\;s \leq 1.000000031374395 \cdot 10^{-22}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\mathsf{fma}\left(t\_2, -16, \frac{1}{{\left(\left(\mathsf{PI}\left(\right) \cdot \mathsf{fma}\left(-0.5, u, 0.25\right)\right) \cdot -4\right)}^{-2}}\right)}{s}, -0.5, t\_1 \cdot -4\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\mathsf{fma}\left(t\_2, -16, \mathsf{fma}\left(16, {\left(\mathsf{fma}\left(\mathsf{PI}\left(\right), u \cdot -0.5, t\_0\right)\right)}^{2}, 0\right)\right)}{s}, -0.5, \mathsf{fma}\left(t\_0, -4, \left(\left(-0.5 \cdot u\right) \cdot \mathsf{PI}\left(\right)\right) \cdot -4\right)\right)\\
\end{array}
\end{array}
if s < 1.00000003e-22Initial program 99.0%
Taylor expanded in s around -inf
Applied rewrites7.3%
Applied rewrites8.4%
Applied rewrites10.9%
if 1.00000003e-22 < s Initial program 99.1%
Taylor expanded in s around -inf
Applied rewrites8.0%
Applied rewrites7.7%
Applied rewrites3.9%
(FPCore (u s)
:precision binary32
(let* ((t_0 (fma (* u (PI)) -0.5 (* 0.25 (PI)))))
(if (<= s 9.99999983775159e-18)
(fma
(/
(fma
(pow t_0 2.0)
-16.0
(/ 1.0 (pow (* (* (PI) (fma -0.5 u 0.25)) -4.0) -2.0)))
s)
-0.5
(* t_0 -4.0))
(* (- (* 2.0 (PI)) (/ (PI) u)) u))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(u \cdot \mathsf{PI}\left(\right), -0.5, 0.25 \cdot \mathsf{PI}\left(\right)\right)\\
\mathbf{if}\;s \leq 9.99999983775159 \cdot 10^{-18}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\mathsf{fma}\left({t\_0}^{2}, -16, \frac{1}{{\left(\left(\mathsf{PI}\left(\right) \cdot \mathsf{fma}\left(-0.5, u, 0.25\right)\right) \cdot -4\right)}^{-2}}\right)}{s}, -0.5, t\_0 \cdot -4\right)\\
\mathbf{else}:\\
\;\;\;\;\left(2 \cdot \mathsf{PI}\left(\right) - \frac{\mathsf{PI}\left(\right)}{u}\right) \cdot u\\
\end{array}
\end{array}
if s < 9.99999984e-18Initial program 99.0%
Taylor expanded in s around -inf
Applied rewrites7.1%
Applied rewrites8.6%
Applied rewrites9.5%
if 9.99999984e-18 < s Initial program 99.1%
Taylor expanded in s around -inf
Applied rewrites7.8%
Taylor expanded in s around inf
Applied rewrites15.7%
Taylor expanded in u around inf
Applied rewrites15.9%
(FPCore (u s)
:precision binary32
(let* ((t_0 (* 0.25 (PI))))
(fma
(/
(fma
(pow (fma (* u (PI)) -0.5 t_0) 2.0)
-16.0
(fma 16.0 (pow t_0 2.0) 0.0))
s)
-0.5
(* (* 2.0 (PI)) u))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.25 \cdot \mathsf{PI}\left(\right)\\
\mathsf{fma}\left(\frac{\mathsf{fma}\left({\left(\mathsf{fma}\left(u \cdot \mathsf{PI}\left(\right), -0.5, t\_0\right)\right)}^{2}, -16, \mathsf{fma}\left(16, {t\_0}^{2}, 0\right)\right)}{s}, -0.5, \left(2 \cdot \mathsf{PI}\left(\right)\right) \cdot u\right)
\end{array}
\end{array}
Initial program 99.1%
Taylor expanded in s around -inf
Applied rewrites8.2%
Taylor expanded in u around 0
Applied rewrites11.8%
Taylor expanded in u around inf
Applied rewrites12.7%
(FPCore (u s)
:precision binary32
(let* ((t_0 (* (PI) (PI))))
(if (<= s 4.0999998698886424e-21)
(fma
(/
(fma
(pow (* (PI) (fma -0.5 u 0.25)) 2.0)
-16.0
(fma (* u t_0) (fma 4.0 u -4.0) t_0))
s)
-0.5
(* (fma (* u (PI)) -0.5 (* 0.25 (PI))) -4.0))
(* (- (* 2.0 (PI)) (/ (PI) u)) u))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)\\
\mathbf{if}\;s \leq 4.0999998698886424 \cdot 10^{-21}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\mathsf{fma}\left({\left(\mathsf{PI}\left(\right) \cdot \mathsf{fma}\left(-0.5, u, 0.25\right)\right)}^{2}, -16, \mathsf{fma}\left(u \cdot t\_0, \mathsf{fma}\left(4, u, -4\right), t\_0\right)\right)}{s}, -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(2 \cdot \mathsf{PI}\left(\right) - \frac{\mathsf{PI}\left(\right)}{u}\right) \cdot u\\
\end{array}
\end{array}
if s < 4.09999987e-21Initial program 99.0%
Taylor expanded in s around -inf
Applied rewrites7.4%
Taylor expanded in u around 0
Applied rewrites9.0%
Taylor expanded in u around 0
Applied rewrites8.6%
Applied rewrites8.0%
if 4.09999987e-21 < s Initial program 99.1%
Taylor expanded in s around -inf
Applied rewrites8.8%
Taylor expanded in s around inf
Applied rewrites14.7%
Taylor expanded in u around inf
Applied rewrites14.9%
(FPCore (u s) :precision binary32 (* (- (* 2.0 (PI)) (/ (PI) u)) u))
\begin{array}{l}
\\
\left(2 \cdot \mathsf{PI}\left(\right) - \frac{\mathsf{PI}\left(\right)}{u}\right) \cdot u
\end{array}
Initial program 99.1%
Taylor expanded in s around -inf
Applied rewrites7.8%
Taylor expanded in s around inf
Applied rewrites11.3%
Taylor expanded in u around inf
Applied rewrites11.4%
(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 99.1%
Taylor expanded in s around -inf
Applied rewrites8.1%
Taylor expanded in s around inf
Applied rewrites11.3%
Applied rewrites11.4%
(FPCore (u s) :precision binary32 (- (PI)))
\begin{array}{l}
\\
-\mathsf{PI}\left(\right)
\end{array}
Initial program 99.1%
Taylor expanded in u around 0
mul-1-negN/A
lower-neg.f32N/A
lower-PI.f3211.3
Applied rewrites11.3%
(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.3%
Taylor expanded in s around 0
Applied rewrites10.6%
herbie shell --seed 2024318
(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))))