
(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 (+ (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
(let* ((t_0 (/ (PI) s)) (t_1 (/ -1.0 (+ (exp t_0) 1.0))))
(if (<=
(*
(log
(-
-1.0
(/
-1.0
(- (* (- t_1 (/ -1.0 (+ (exp (/ (- (PI)) s)) 1.0))) u) t_1))))
(- s))
-2.999999970665357e-10)
(* (log (* (- (/ (+ t_0 1.0) u) (* 2.0 t_0)) u)) (- s))
(*
(log
(fma
(fma (* (PI) u) -0.5 (* (pow (pow (PI) 3.0) 0.3333333333333333) 0.25))
(/ 4.0 s)
1.0))
(- s)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{PI}\left(\right)}{s}\\
t_1 := \frac{-1}{e^{t\_0} + 1}\\
\mathbf{if}\;\log \left(-1 - \frac{-1}{\left(t\_1 - \frac{-1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1}\right) \cdot u - t\_1}\right) \cdot \left(-s\right) \leq -2.999999970665357 \cdot 10^{-10}:\\
\;\;\;\;\log \left(\left(\frac{t\_0 + 1}{u} - 2 \cdot t\_0\right) \cdot u\right) \cdot \left(-s\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{PI}\left(\right) \cdot u, -0.5, {\left({\mathsf{PI}\left(\right)}^{3}\right)}^{0.3333333333333333} \cdot 0.25\right), \frac{4}{s}, 1\right)\right) \cdot \left(-s\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)))) < -2.99999997e-10Initial program 99.3%
Taylor expanded in s around -inf
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f32N/A
Applied rewrites6.9%
Taylor expanded in u around -inf
Applied rewrites30.5%
if -2.99999997e-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.2%
Applied rewrites11.5%
Applied rewrites27.8%
Final simplification28.7%
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ (PI) s)) (t_1 (/ -1.0 (+ (exp t_0) 1.0))))
(if (<=
(*
(log
(-
-1.0
(/
-1.0
(- (* (- t_1 (/ -1.0 (+ (exp (/ (- (PI)) s)) 1.0))) u) t_1))))
(- s))
-2.999999970665357e-10)
(* (log (* (- (/ (+ t_0 1.0) u) (* 2.0 t_0)) u)) (- s))
(*
(log
(fma
(fma (* (PI) u) -0.5 (* (pow (sqrt (PI)) 2.0) 0.25))
(/ 4.0 s)
1.0))
(- s)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{PI}\left(\right)}{s}\\
t_1 := \frac{-1}{e^{t\_0} + 1}\\
\mathbf{if}\;\log \left(-1 - \frac{-1}{\left(t\_1 - \frac{-1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1}\right) \cdot u - t\_1}\right) \cdot \left(-s\right) \leq -2.999999970665357 \cdot 10^{-10}:\\
\;\;\;\;\log \left(\left(\frac{t\_0 + 1}{u} - 2 \cdot t\_0\right) \cdot u\right) \cdot \left(-s\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{PI}\left(\right) \cdot u, -0.5, {\left(\sqrt{\mathsf{PI}\left(\right)}\right)}^{2} \cdot 0.25\right), \frac{4}{s}, 1\right)\right) \cdot \left(-s\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)))) < -2.99999997e-10Initial program 99.3%
Taylor expanded in s around -inf
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f32N/A
Applied rewrites6.9%
Taylor expanded in u around -inf
Applied rewrites30.5%
if -2.99999997e-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.5%
Applied rewrites28.1%
Final simplification28.7%
(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 rewrites98.7%
Final simplification98.7%
(FPCore (u s) :precision binary32 (* (log (+ (* (* (fma -0.5 u 0.25) (PI)) (/ 4.0 s)) 1.0)) (- s)))
\begin{array}{l}
\\
\log \left(\left(\mathsf{fma}\left(-0.5, u, 0.25\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \frac{4}{s} + 1\right) \cdot \left(-s\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f32N/A
Applied rewrites10.4%
Applied rewrites10.4%
Applied rewrites11.3%
Final simplification11.5%
(FPCore (u s) :precision binary32 (* (log (+ (/ (PI) s) 1.0)) (- s)))
\begin{array}{l}
\\
\log \left(\frac{\mathsf{PI}\left(\right)}{s} + 1\right) \cdot \left(-s\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
associate-*r/N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f32N/A
Applied rewrites10.4%
Taylor expanded in u around 0
Applied rewrites25.0%
Final simplification25.0%
(FPCore (u s) :precision binary32 (fma (/ -1.0 (/ s 0.0)) -0.5 (* -4.0 (* (* (- (/ 0.25 u) 0.5) u) (PI)))))
\begin{array}{l}
\\
\mathsf{fma}\left(\frac{-1}{\frac{s}{0}}, -0.5, -4 \cdot \left(\left(\left(\frac{0.25}{u} - 0.5\right) \cdot u\right) \cdot \mathsf{PI}\left(\right)\right)\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
Applied rewrites7.7%
Applied rewrites10.5%
Applied rewrites10.5%
Taylor expanded in u around inf
Applied rewrites10.7%
Final simplification10.7%
(FPCore (u s) :precision binary32 (+ (* (* 0.25 (PI)) -4.0) (* (* (* (PI) u) -0.5) -4.0)))
\begin{array}{l}
\\
\left(0.25 \cdot \mathsf{PI}\left(\right)\right) \cdot -4 + \left(\left(\mathsf{PI}\left(\right) \cdot u\right) \cdot -0.5\right) \cdot -4
\end{array}
Initial program 99.0%
Taylor expanded in s around -inf
Applied rewrites7.6%
Applied rewrites10.5%
Applied rewrites10.7%
Final simplification10.7%
(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.f3210.5
Applied rewrites10.5%
(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 rewrites7.9%
Taylor expanded in s around 0
Applied rewrites10.4%
Taylor expanded in s around 0
Applied rewrites10.4%
herbie shell --seed 2024276
(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))))