
(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 9 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 (fma (* (PI) u) 0.5 (* -0.25 (PI))))
(t_1 (pow t_0 2.0))
(t_2 (/ -1.0 (+ (exp (/ (PI) s)) 1.0))))
(if (<=
(*
(log
(-
-1.0
(/
-1.0
(- (* (- t_2 (/ -1.0 (+ (exp (/ (- (PI)) s)) 1.0))) u) t_2))))
(- s))
-1.0000000036274937e-15)
(* (* u u) (- (/ 0.0 s) (/ (+ (/ (PI) u) (* -2.0 (PI))) u)))
(fma (/ (fma 16.0 t_1 (* t_1 -16.0)) s) -0.5 (* t_0 4.0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\mathsf{PI}\left(\right) \cdot u, 0.5, -0.25 \cdot \mathsf{PI}\left(\right)\right)\\
t_1 := {t\_0}^{2}\\
t_2 := \frac{-1}{e^{\frac{\mathsf{PI}\left(\right)}{s}} + 1}\\
\mathbf{if}\;\log \left(-1 - \frac{-1}{\left(t\_2 - \frac{-1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1}\right) \cdot u - t\_2}\right) \cdot \left(-s\right) \leq -1.0000000036274937 \cdot 10^{-15}:\\
\;\;\;\;\left(u \cdot u\right) \cdot \left(\frac{0}{s} - \frac{\frac{\mathsf{PI}\left(\right)}{u} + -2 \cdot \mathsf{PI}\left(\right)}{u}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\mathsf{fma}\left(16, t\_1, t\_1 \cdot -16\right)}{s}, -0.5, t\_0 \cdot 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)))) < -1e-15Initial program 99.2%
Taylor expanded in s around inf
Applied rewrites5.9%
Taylor expanded in u around -inf
Applied rewrites17.6%
if -1e-15 < (*.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 rewrites4.9%
Applied rewrites7.8%
Applied rewrites7.8%
Final simplification12.9%
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ (PI) u))
(t_1 (/ (PI) s))
(t_2 (* (+ t_0 (PI)) t_1))
(t_3 (/ -1.0 (+ (exp t_1) 1.0))))
(if (<=
(*
(log
(-
-1.0
(/
-1.0
(- (* (- t_3 (/ -1.0 (+ (exp (/ (- (PI)) s)) 1.0))) u) t_3))))
(- s))
-1.99999996490334e-13)
(* (* u u) (- (/ 0.0 s) (/ (+ t_0 (* -2.0 (PI))) u)))
(fma
(+ (* (* t_2 4.0) (* u u)) (* (* -4.0 t_2) (* u u)))
-0.5
(* (fma (* (PI) u) 0.5 (* -0.25 (PI))) 4.0)))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{PI}\left(\right)}{u}\\
t_1 := \frac{\mathsf{PI}\left(\right)}{s}\\
t_2 := \left(t\_0 + \mathsf{PI}\left(\right)\right) \cdot t\_1\\
t_3 := \frac{-1}{e^{t\_1} + 1}\\
\mathbf{if}\;\log \left(-1 - \frac{-1}{\left(t\_3 - \frac{-1}{e^{\frac{-\mathsf{PI}\left(\right)}{s}} + 1}\right) \cdot u - t\_3}\right) \cdot \left(-s\right) \leq -1.99999996490334 \cdot 10^{-13}:\\
\;\;\;\;\left(u \cdot u\right) \cdot \left(\frac{0}{s} - \frac{t\_0 + -2 \cdot \mathsf{PI}\left(\right)}{u}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\left(t\_2 \cdot 4\right) \cdot \left(u \cdot u\right) + \left(-4 \cdot t\_2\right) \cdot \left(u \cdot u\right), -0.5, \mathsf{fma}\left(\mathsf{PI}\left(\right) \cdot u, 0.5, -0.25 \cdot \mathsf{PI}\left(\right)\right) \cdot 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)))) < -1.99999996e-13Initial program 99.2%
Taylor expanded in s around inf
Applied rewrites6.0%
Taylor expanded in u around -inf
Applied rewrites18.6%
if -1.99999996e-13 < (*.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 rewrites5.1%
Taylor expanded in u around inf
Applied rewrites8.2%
Applied rewrites8.1%
Final simplification12.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 rewrites96.8%
Final simplification96.8%
(FPCore (u s) :precision binary32 (* (log (expm1 (- (log 0.5)))) (- s)))
float code(float u, float s) {
return logf(expm1f(-logf(0.5f))) * -s;
}
function code(u, s) return Float32(log(expm1(Float32(-log(Float32(0.5))))) * Float32(-s)) end
\begin{array}{l}
\\
\log \left(\mathsf{expm1}\left(-\log 0.5\right)\right) \cdot \left(-s\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around inf
Applied rewrites10.1%
lift--.f32N/A
Applied rewrites23.5%
Final simplification23.5%
(FPCore (u s)
:precision binary32
(let* ((t_0 (/ (PI) s)) (t_1 (/ (PI) u)) (t_2 (+ t_1 (PI))))
(if (<= s 9.9999998245167e-14)
(fma
(* (+ (* (* -4.0 t_0) t_2) (* (* t_2 t_0) 4.0)) (* u u))
-0.5
(* (fma (* (PI) u) 0.5 (* -0.25 (PI))) 4.0))
(* (* u u) (- (/ 0.0 s) (/ (+ t_1 (* -2.0 (PI))) u))))))\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{PI}\left(\right)}{s}\\
t_1 := \frac{\mathsf{PI}\left(\right)}{u}\\
t_2 := t\_1 + \mathsf{PI}\left(\right)\\
\mathbf{if}\;s \leq 9.9999998245167 \cdot 10^{-14}:\\
\;\;\;\;\mathsf{fma}\left(\left(\left(-4 \cdot t\_0\right) \cdot t\_2 + \left(t\_2 \cdot t\_0\right) \cdot 4\right) \cdot \left(u \cdot u\right), -0.5, \mathsf{fma}\left(\mathsf{PI}\left(\right) \cdot u, 0.5, -0.25 \cdot \mathsf{PI}\left(\right)\right) \cdot 4\right)\\
\mathbf{else}:\\
\;\;\;\;\left(u \cdot u\right) \cdot \left(\frac{0}{s} - \frac{t\_1 + -2 \cdot \mathsf{PI}\left(\right)}{u}\right)\\
\end{array}
\end{array}
if s < 9.99999982e-14Initial program 98.9%
Taylor expanded in s around inf
Applied rewrites5.2%
Taylor expanded in u around inf
Applied rewrites8.3%
Applied rewrites8.3%
Applied rewrites8.2%
if 9.99999982e-14 < s Initial program 99.1%
Taylor expanded in s around inf
Applied rewrites6.0%
Taylor expanded in u around -inf
Applied rewrites19.1%
Final simplification12.7%
(FPCore (u s) :precision binary32 (* (* u u) (- (/ 0.0 s) (/ (+ (/ (PI) u) (* -2.0 (PI))) u))))
\begin{array}{l}
\\
\left(u \cdot u\right) \cdot \left(\frac{0}{s} - \frac{\frac{\mathsf{PI}\left(\right)}{u} + -2 \cdot \mathsf{PI}\left(\right)}{u}\right)
\end{array}
Initial program 99.0%
Taylor expanded in s around inf
Applied rewrites5.2%
Taylor expanded in u around -inf
Applied rewrites12.8%
Final simplification12.8%
(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.f3212.7
Applied rewrites12.7%
(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 rewrites5.2%
Taylor expanded in s around 0
Applied rewrites10.1%
Taylor expanded in s around 0
Applied rewrites10.1%
herbie shell --seed 2024248
(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))))