
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ 1.0 t_0))) (/ t_0 (* (* s t_1) t_1))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
float t_1 = 1.0f + t_0;
return t_0 / ((s * t_1) * t_1);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: t_1
t_0 = exp((-abs(x) / s))
t_1 = 1.0e0 + t_0
code = t_0 / ((s * t_1) * t_1)
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) t_1 = Float32(Float32(1.0) + t_0) return Float32(t_0 / Float32(Float32(s * t_1) * t_1)) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); t_1 = single(1.0) + t_0; tmp = t_0 / ((s * t_1) * t_1); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := 1 + t\_0\\
\frac{t\_0}{\left(s \cdot t\_1\right) \cdot t\_1}
\end{array}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ 1.0 t_0))) (/ t_0 (* (* s t_1) t_1))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
float t_1 = 1.0f + t_0;
return t_0 / ((s * t_1) * t_1);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: t_1
t_0 = exp((-abs(x) / s))
t_1 = 1.0e0 + t_0
code = t_0 / ((s * t_1) * t_1)
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) t_1 = Float32(Float32(1.0) + t_0) return Float32(t_0 / Float32(Float32(s * t_1) * t_1)) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); t_1 = single(1.0) + t_0; tmp = t_0 / ((s * t_1) * t_1); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := 1 + t\_0\\
\frac{t\_0}{\left(s \cdot t\_1\right) \cdot t\_1}
\end{array}
\end{array}
(FPCore (x s) :precision binary32 (let* ((t_0 (/ (fabs x) s))) (/ -1.0 (* (fma s (exp t_0) s) (- -1.0 (exp (- t_0)))))))
float code(float x, float s) {
float t_0 = fabsf(x) / s;
return -1.0f / (fmaf(s, expf(t_0), s) * (-1.0f - expf(-t_0)));
}
function code(x, s) t_0 = Float32(abs(x) / s) return Float32(Float32(-1.0) / Float32(fma(s, exp(t_0), s) * Float32(Float32(-1.0) - exp(Float32(-t_0))))) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\left|x\right|}{s}\\
\frac{-1}{\mathsf{fma}\left(s, e^{t\_0}, s\right) \cdot \left(-1 - e^{-t\_0}\right)}
\end{array}
\end{array}
Initial program 99.4%
lift-/.f32N/A
lift-*.f32N/A
associate-/r*N/A
lower-/.f32N/A
Applied rewrites99.4%
Applied rewrites99.4%
Taylor expanded in s around 0
distribute-lft-inN/A
*-rgt-identityN/A
exp-negN/A
rgt-mult-inverseN/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-fma.f32N/A
lower-exp.f32N/A
lower-/.f32N/A
lower-fabs.f3299.4
Applied rewrites99.4%
Final simplification99.4%
(FPCore (x s)
:precision binary32
(let* ((t_0 (exp (- (/ (fabs x) s)))) (t_1 (+ t_0 1.0)))
(if (<= (/ t_0 (* t_1 (* s t_1))) 0.009999999776482582)
t_0
(/
1.0
(*
s
(-
(/
(/
(fma
x
(- x)
(/
(fma (* (fabs x) (* x x)) -3.0 (- (* (fabs x) (* (* x x) -3.0))))
(- s)))
(- s))
s)
-4.0))))))
float code(float x, float s) {
float t_0 = expf(-(fabsf(x) / s));
float t_1 = t_0 + 1.0f;
float tmp;
if ((t_0 / (t_1 * (s * t_1))) <= 0.009999999776482582f) {
tmp = t_0;
} else {
tmp = 1.0f / (s * (((fmaf(x, -x, (fmaf((fabsf(x) * (x * x)), -3.0f, -(fabsf(x) * ((x * x) * -3.0f))) / -s)) / -s) / s) - -4.0f));
}
return tmp;
}
function code(x, s) t_0 = exp(Float32(-Float32(abs(x) / s))) t_1 = Float32(t_0 + Float32(1.0)) tmp = Float32(0.0) if (Float32(t_0 / Float32(t_1 * Float32(s * t_1))) <= Float32(0.009999999776482582)) tmp = t_0; else tmp = Float32(Float32(1.0) / Float32(s * Float32(Float32(Float32(fma(x, Float32(-x), Float32(fma(Float32(abs(x) * Float32(x * x)), Float32(-3.0), Float32(-Float32(abs(x) * Float32(Float32(x * x) * Float32(-3.0))))) / Float32(-s))) / Float32(-s)) / s) - Float32(-4.0)))); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-\frac{\left|x\right|}{s}}\\
t_1 := t\_0 + 1\\
\mathbf{if}\;\frac{t\_0}{t\_1 \cdot \left(s \cdot t\_1\right)} \leq 0.009999999776482582:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{s \cdot \left(\frac{\frac{\mathsf{fma}\left(x, -x, \frac{\mathsf{fma}\left(\left|x\right| \cdot \left(x \cdot x\right), -3, -\left|x\right| \cdot \left(\left(x \cdot x\right) \cdot -3\right)\right)}{-s}\right)}{-s}}{s} - -4\right)}\\
\end{array}
\end{array}
if (/.f32 (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)) (*.f32 (*.f32 s (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)))) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s))))) < 0.00999999978Initial program 99.8%
Applied rewrites99.9%
Taylor expanded in s around 0
neg-mul-1N/A
lower-neg.f32N/A
lower-/.f32N/A
lower-fabs.f3299.4
Applied rewrites99.4%
if 0.00999999978 < (/.f32 (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)) (*.f32 (*.f32 s (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)))) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s))))) Initial program 99.0%
lift-/.f32N/A
lift-*.f32N/A
associate-/r*N/A
lower-/.f32N/A
Applied rewrites99.2%
Applied rewrites99.1%
Taylor expanded in s around -inf
Applied rewrites90.5%
Final simplification96.9%
herbie shell --seed 2024230
(FPCore (x s)
:name "Logistic distribution"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ (exp (/ (- (fabs x)) s)) (* (* s (+ 1.0 (exp (/ (- (fabs x)) s)))) (+ 1.0 (exp (/ (- (fabs x)) s))))))