
(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 (exp (/ (- (fabs x)) s)))) (/ t_0 (* (+ t_0 1.0) (+ s (/ s (exp (/ (fabs x) s))))))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
return t_0 / ((t_0 + 1.0f) * (s + (s / expf((fabsf(x) / s)))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
t_0 = exp((-abs(x) / s))
code = t_0 / ((t_0 + 1.0e0) * (s + (s / exp((abs(x) / s)))))
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) return Float32(t_0 / Float32(Float32(t_0 + Float32(1.0)) * Float32(s + Float32(s / exp(Float32(abs(x) / s)))))) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); tmp = t_0 / ((t_0 + single(1.0)) * (s + (s / exp((abs(x) / s))))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
\frac{t\_0}{\left(t\_0 + 1\right) \cdot \left(s + \frac{s}{e^{\frac{\left|x\right|}{s}}}\right)}
\end{array}
\end{array}
Initial program 99.6%
*-commutative99.6%
distribute-lft-in99.6%
*-rgt-identity99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
exp-neg99.6%
associate-*r/99.6%
*-rgt-identity99.6%
*-lft-identity99.6%
metadata-eval99.6%
times-frac99.6%
neg-mul-199.6%
neg-mul-199.6%
fabs-neg99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- (fabs x)) s)))) (/ t_0 (* s (pow (+ t_0 1.0) 2.0)))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
return t_0 / (s * powf((t_0 + 1.0f), 2.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
t_0 = exp((-abs(x) / s))
code = t_0 / (s * ((t_0 + 1.0e0) ** 2.0e0))
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) return Float32(t_0 / Float32(s * (Float32(t_0 + Float32(1.0)) ^ Float32(2.0)))) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); tmp = t_0 / (s * ((t_0 + single(1.0)) ^ single(2.0))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
\frac{t\_0}{s \cdot {\left(t\_0 + 1\right)}^{2}}
\end{array}
\end{array}
Initial program 99.6%
associate-*l*99.6%
Simplified99.6%
Taylor expanded in x around 0 99.6%
associate-*r/99.6%
mul-1-neg99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x s) :precision binary32 (if (<= (fabs x) 0.006000000052154064) (/ (exp (+ (/ x s) (* -2.0 (log1p (exp (/ x s)))))) s) (* (/ (exp (/ (- x) s)) s) 0.25)))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 0.006000000052154064f) {
tmp = expf(((x / s) + (-2.0f * log1pf(expf((x / s)))))) / s;
} else {
tmp = (expf((-x / s)) / s) * 0.25f;
}
return tmp;
}
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(0.006000000052154064)) tmp = Float32(exp(Float32(Float32(x / s) + Float32(Float32(-2.0) * log1p(exp(Float32(x / s)))))) / s); else tmp = Float32(Float32(exp(Float32(Float32(-x) / s)) / s) * Float32(0.25)); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 0.006000000052154064:\\
\;\;\;\;\frac{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{\frac{-x}{s}}}{s} \cdot 0.25\\
\end{array}
\end{array}
if (fabs.f32 x) < 0.00600000005Initial program 99.2%
associate-*l*99.2%
Simplified99.2%
Taylor expanded in x around 0 99.2%
associate-*r/99.2%
mul-1-neg99.2%
Simplified99.2%
Applied egg-rr94.7%
expm1-def95.8%
expm1-log1p99.5%
Simplified99.5%
if 0.00600000005 < (fabs.f32 x) Initial program 100.0%
associate-*l*100.0%
Simplified100.0%
associate-/r*100.0%
div-inv100.0%
Applied egg-rr52.1%
Taylor expanded in x around 0 27.6%
*-commutative27.6%
Simplified27.6%
add-sqr-sqrt1.5%
sqrt-unprod3.1%
add-sqr-sqrt1.5%
fabs-sqr1.5%
add-sqr-sqrt1.5%
add-sqr-sqrt1.5%
fabs-sqr1.5%
add-sqr-sqrt3.1%
sqr-neg3.1%
distribute-frac-neg3.1%
distribute-frac-neg3.1%
sqrt-unprod-0.0%
add-sqr-sqrt53.8%
distribute-frac-neg53.8%
exp-neg53.8%
Applied egg-rr29.4%
rec-exp29.4%
distribute-neg-frac29.4%
Simplified29.4%
Taylor expanded in x around 0 49.5%
Final simplification76.3%
(FPCore (x s) :precision binary32 (/ 1.0 (* (+ (exp (/ (- (fabs x)) s)) 1.0) (fma s (pow E (/ x s)) s))))
float code(float x, float s) {
return 1.0f / ((expf((-fabsf(x) / s)) + 1.0f) * fmaf(s, powf(((float) M_E), (x / s)), s));
}
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(exp(Float32(Float32(-abs(x)) / s)) + Float32(1.0)) * fma(s, (Float32(exp(1)) ^ Float32(x / s)), s))) end
\begin{array}{l}
\\
\frac{1}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \mathsf{fma}\left(s, {e}^{\left(\frac{x}{s}\right)}, s\right)}
\end{array}
Initial program 99.6%
Simplified99.5%
frac-2neg99.5%
frac-2neg99.5%
add-sqr-sqrt99.4%
sqrt-unprod95.5%
sqr-neg95.5%
sqrt-unprod-0.0%
add-sqr-sqrt26.9%
*-un-lft-identity26.9%
exp-prod26.9%
add-sqr-sqrt-0.0%
sqrt-unprod95.5%
sqr-neg95.5%
sqrt-unprod99.4%
add-sqr-sqrt99.5%
add-sqr-sqrt46.4%
fabs-sqr46.4%
add-sqr-sqrt62.5%
Applied egg-rr62.5%
exp-1-e62.5%
Simplified62.5%
Final simplification62.5%
(FPCore (x s) :precision binary32 (/ 1.0 (* (+ (exp (/ (- (fabs x)) s)) 1.0) (+ s (* s (exp (/ x s)))))))
float code(float x, float s) {
return 1.0f / ((expf((-fabsf(x) / s)) + 1.0f) * (s + (s * expf((x / s)))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((exp((-abs(x) / s)) + 1.0e0) * (s + (s * exp((x / s)))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(exp(Float32(Float32(-abs(x)) / s)) + Float32(1.0)) * Float32(s + Float32(s * exp(Float32(x / s)))))) end
function tmp = code(x, s) tmp = single(1.0) / ((exp((-abs(x) / s)) + single(1.0)) * (s + (s * exp((x / s))))); end
\begin{array}{l}
\\
\frac{1}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(s + s \cdot e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.6%
Simplified99.5%
fma-udef99.5%
frac-2neg99.5%
frac-2neg99.5%
add-sqr-sqrt46.4%
fabs-sqr46.4%
add-sqr-sqrt62.5%
Applied egg-rr62.5%
Final simplification62.5%
(FPCore (x s) :precision binary32 (* (/ (exp (/ (- x) s)) s) 0.25))
float code(float x, float s) {
return (expf((-x / s)) / s) * 0.25f;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (exp((-x / s)) / s) * 0.25e0
end function
function code(x, s) return Float32(Float32(exp(Float32(Float32(-x) / s)) / s) * Float32(0.25)) end
function tmp = code(x, s) tmp = (exp((-x / s)) / s) * single(0.25); end
\begin{array}{l}
\\
\frac{e^{\frac{-x}{s}}}{s} \cdot 0.25
\end{array}
Initial program 99.6%
associate-*l*99.6%
Simplified99.6%
associate-/r*99.6%
div-inv99.6%
Applied egg-rr64.5%
Taylor expanded in x around 0 50.7%
*-commutative50.7%
Simplified50.7%
add-sqr-sqrt11.4%
sqrt-unprod26.8%
add-sqr-sqrt11.4%
fabs-sqr11.4%
add-sqr-sqrt16.1%
add-sqr-sqrt11.4%
fabs-sqr11.4%
add-sqr-sqrt26.8%
sqr-neg26.8%
distribute-frac-neg26.8%
distribute-frac-neg26.8%
sqrt-unprod-0.0%
add-sqr-sqrt74.2%
distribute-frac-neg74.2%
exp-neg74.2%
Applied egg-rr50.3%
rec-exp50.3%
distribute-neg-frac50.3%
Simplified50.3%
Taylor expanded in x around 0 60.1%
Final simplification60.1%
(FPCore (x s) :precision binary32 (/ 0.25 s))
float code(float x, float s) {
return 0.25f / s;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.25e0 / s
end function
function code(x, s) return Float32(Float32(0.25) / s) end
function tmp = code(x, s) tmp = single(0.25) / s; end
\begin{array}{l}
\\
\frac{0.25}{s}
\end{array}
Initial program 99.6%
associate-*l*99.6%
Simplified99.6%
Taylor expanded in s around inf 29.5%
Final simplification29.5%
herbie shell --seed 2024031
(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))))))