
(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 (cbrt (PI))))
(*
(log
(-
(/
1.0
(+
(/ 1.0 (+ (exp (* (/ t_0 s) (pow t_0 2.0))) 1.0))
(*
(-
(/ 1.0 (+ (exp (/ (- (PI)) s)) 1.0))
(/ 1.0 (+ (exp (/ (PI) s)) 1.0)))
u)))
1.0))
(- s))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt[3]{\mathsf{PI}\left(\right)}\\
\log \left(\frac{1}{\frac{1}{e^{\frac{t\_0}{s} \cdot {t\_0}^{2}} + 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}
\end{array}
Initial program 98.9%
lift-/.f32N/A
lift-PI.f32N/A
add-cube-cbrtN/A
associate-/l*N/A
lower-*.f32N/A
pow2N/A
lower-pow.f32N/A
lift-PI.f32N/A
lower-cbrt.f32N/A
lower-/.f32N/A
lift-PI.f32N/A
lower-cbrt.f3298.9
Applied rewrites98.9%
Final simplification98.9%
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ 1.0 (+ (exp (/ (PI) s)) 1.0)))
(t_1 (* (PI) u))
(t_2 (* -0.25 (PI)))
(t_3 (fma t_1 0.5 t_2)))
(if (<=
(*
(log
(-
(/ 1.0 (+ (* (- (/ 1.0 (+ (exp (/ (- (PI)) s)) 1.0)) t_0) u) t_0))
1.0))
(- s))
-1.999999936531045e-19)
(/ (- (pow (PI) 3.0)) (fma (PI) (fma 1.0 (PI) 0.0) (* 0.0 (PI))))
(fma
(/
(fma (pow t_3 2.0) -16.0 (/ 1.0 (pow (* (fma 0.5 t_1 t_2) 4.0) -2.0)))
s)
-0.5
(* 4.0 t_3)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} + 1}\\
t_1 := \mathsf{PI}\left(\right) \cdot u\\
t_2 := -0.25 \cdot \mathsf{PI}\left(\right)\\
t_3 := \mathsf{fma}\left(t\_1, 0.5, t\_2\right)\\
\mathbf{if}\;\log \left(\frac{1}{\left(\frac{1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1} - t\_0\right) \cdot u + t\_0} - 1\right) \cdot \left(-s\right) \leq -1.999999936531045 \cdot 10^{-19}:\\
\;\;\;\;\frac{-{\mathsf{PI}\left(\right)}^{3}}{\mathsf{fma}\left(\mathsf{PI}\left(\right), \mathsf{fma}\left(1, \mathsf{PI}\left(\right), 0\right), 0 \cdot \mathsf{PI}\left(\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\mathsf{fma}\left({t\_3}^{2}, -16, \frac{1}{{\left(\mathsf{fma}\left(0.5, t\_1, t\_2\right) \cdot 4\right)}^{-2}}\right)}{s}, -0.5, 4 \cdot t\_3\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)))) < -1.99999994e-19Initial program 99.0%
Taylor expanded in u around 0
mul-1-negN/A
lower-neg.f32N/A
lower-PI.f3214.4
Applied rewrites14.4%
Applied rewrites8.1%
Applied rewrites0.0%
if -1.99999994e-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.1%
Applied rewrites7.6%
Final simplification4.6%
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ 1.0 (+ (exp (/ (PI) s)) 1.0)))
(t_1 (* (PI) u))
(t_2 (* -0.25 (PI)))
(t_3 (fma 0.5 t_1 t_2))
(t_4 (fma t_1 0.5 t_2)))
(if (<=
(*
(log
(-
(/ 1.0 (+ (* (- (/ 1.0 (+ (exp (/ (- (PI)) s)) 1.0)) t_0) u) t_0))
1.0))
(- s))
-6.000000068087077e-18)
(/ (- (pow (PI) 3.0)) (fma (PI) (fma 1.0 (PI) 0.0) (* 0.0 (PI))))
(fma
(/ (fma (pow t_4 2.0) -16.0 (* (* (* t_3 4.0) 4.0) t_3)) s)
-0.5
(* 4.0 t_4)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} + 1}\\
t_1 := \mathsf{PI}\left(\right) \cdot u\\
t_2 := -0.25 \cdot \mathsf{PI}\left(\right)\\
t_3 := \mathsf{fma}\left(0.5, t\_1, t\_2\right)\\
t_4 := \mathsf{fma}\left(t\_1, 0.5, t\_2\right)\\
\mathbf{if}\;\log \left(\frac{1}{\left(\frac{1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1} - t\_0\right) \cdot u + t\_0} - 1\right) \cdot \left(-s\right) \leq -6.000000068087077 \cdot 10^{-18}:\\
\;\;\;\;\frac{-{\mathsf{PI}\left(\right)}^{3}}{\mathsf{fma}\left(\mathsf{PI}\left(\right), \mathsf{fma}\left(1, \mathsf{PI}\left(\right), 0\right), 0 \cdot \mathsf{PI}\left(\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\mathsf{fma}\left({t\_4}^{2}, -16, \left(\left(t\_3 \cdot 4\right) \cdot 4\right) \cdot t\_3\right)}{s}, -0.5, 4 \cdot t\_4\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)))) < -6.00000007e-18Initial program 99.0%
Taylor expanded in u around 0
mul-1-negN/A
lower-neg.f32N/A
lower-PI.f3214.6
Applied rewrites14.6%
Applied rewrites8.2%
Applied rewrites0.1%
if -6.00000007e-18 < (*.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.8%
Taylor expanded in s around inf
Applied rewrites6.2%
Applied rewrites6.4%
Final simplification4.1%
(FPCore (u s)
:precision binary32
(let* ((t_0 (- (PI))) (t_1 (/ 1.0 (+ (exp (/ (PI) s)) 1.0))))
(if (<=
(*
(log
(-
(/ 1.0 (+ (* (- (/ 1.0 (+ (exp (/ t_0 s)) 1.0)) t_1) u) t_1))
1.0))
(- s))
-9.999999682655225e-20)
t_0
0.0)))\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\mathsf{PI}\left(\right)\\
t_1 := \frac{1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} + 1}\\
\mathbf{if}\;\log \left(\frac{1}{\left(\frac{1}{e^{\frac{t\_0}{s}} + 1} - t\_1\right) \cdot u + t\_1} - 1\right) \cdot \left(-s\right) \leq -9.999999682655225 \cdot 10^{-20}:\\
\;\;\;\;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)))) < -9.99999968e-20Initial program 99.0%
Taylor expanded in u around 0
mul-1-negN/A
lower-neg.f32N/A
lower-PI.f3214.3
Applied rewrites14.3%
if -9.99999968e-20 < (*.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.1%
Taylor expanded in s around 0
Applied rewrites13.6%
Final simplification13.9%
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ 1.0 (+ (exp (/ (PI) s)) 1.0))))
(*
(log
(-
(/ 1.0 (+ (* (- (/ 1.0 (+ (exp (/ (- (PI)) s)) 1.0)) t_0) u) t_0))
1.0))
(- s))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} + 1}\\
\log \left(\frac{1}{\left(\frac{1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1} - t\_0\right) \cdot u + t\_0} - 1\right) \cdot \left(-s\right)
\end{array}
\end{array}
Initial program 98.9%
Final simplification98.9%
(FPCore (u s)
:precision binary32
(*
(log
(-
(/
(/ 1.0 u)
(- (/ 1.0 (+ (exp (/ (- (PI)) s)) 1.0)) (/ 1.0 (+ (exp (/ (PI) s)) 1.0))))
1.0))
(- s)))\begin{array}{l}
\\
\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) \cdot \left(-s\right)
\end{array}
Initial program 98.9%
Taylor expanded in u around inf
lower--.f32N/A
Applied rewrites98.1%
Final simplification98.1%
(FPCore (u s) :precision binary32 (/ (- (pow (PI) 3.0)) (fma (PI) (fma 1.0 (PI) 0.0) (* 0.0 (PI)))))
\begin{array}{l}
\\
\frac{-{\mathsf{PI}\left(\right)}^{3}}{\mathsf{fma}\left(\mathsf{PI}\left(\right), \mathsf{fma}\left(1, \mathsf{PI}\left(\right), 0\right), 0 \cdot \mathsf{PI}\left(\right)\right)}
\end{array}
Initial program 98.9%
Taylor expanded in u around 0
mul-1-negN/A
lower-neg.f32N/A
lower-PI.f3210.8
Applied rewrites10.8%
Applied rewrites6.7%
Applied rewrites0.0%
Final simplification0.1%
(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 98.9%
Taylor expanded in s around inf
Applied rewrites7.6%
Taylor expanded in s around inf
Applied rewrites10.8%
Taylor expanded in u around inf
Applied rewrites11.0%
(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 98.9%
Taylor expanded in s around inf
Applied rewrites7.8%
Taylor expanded in s around inf
Applied rewrites10.8%
Applied rewrites11.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 98.9%
Taylor expanded in s around inf
Applied rewrites7.7%
Taylor expanded in s around 0
Applied rewrites10.7%
herbie shell --seed 2024308
(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))))