
(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 (/ 1.0 (* s (+ (+ (exp (/ (fabs x) (- s))) (exp (/ (fabs x) s))) 2.0))))
float code(float x, float s) {
return 1.0f / (s * ((expf((fabsf(x) / -s)) + expf((fabsf(x) / s))) + 2.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (s * ((exp((abs(x) / -s)) + exp((abs(x) / s))) + 2.0e0))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(exp(Float32(abs(x) / Float32(-s))) + exp(Float32(abs(x) / s))) + Float32(2.0)))) end
function tmp = code(x, s) tmp = single(1.0) / (s * ((exp((abs(x) / -s)) + exp((abs(x) / s))) + single(2.0))); end
\begin{array}{l}
\\
\frac{1}{s \cdot \left(\left(e^{\frac{\left|x\right|}{-s}} + e^{\frac{\left|x\right|}{s}}\right) + 2\right)}
\end{array}
Initial program 99.7%
Simplified99.0%
expm1-log1p-u97.1%
expm1-udef96.8%
associate-/l/97.6%
Applied egg-rr97.6%
expm1-def97.8%
expm1-log1p99.8%
*-commutative99.8%
associate-+r+99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (/ 1.0 (* (+ 1.0 (exp (/ (- x) s))) (fma s (exp (/ x s)) s))))
float code(float x, float s) {
return 1.0f / ((1.0f + expf((-x / s))) * fmaf(s, expf((x / s)), s));
}
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) * fma(s, exp(Float32(x / s)), s))) end
\begin{array}{l}
\\
\frac{1}{\left(1 + e^{\frac{-x}{s}}\right) \cdot \mathsf{fma}\left(s, e^{\frac{x}{s}}, s\right)}
\end{array}
Initial program 99.7%
*-lft-identity99.7%
associate-*r/99.7%
associate-/l*99.7%
distribute-frac-neg99.7%
exp-neg99.7%
associate-/r/99.7%
/-rgt-identity99.7%
associate-*l*99.7%
Simplified99.8%
fma-udef99.7%
Applied egg-rr99.7%
*-commutative99.7%
fma-def99.8%
add-sqr-sqrt51.8%
fabs-sqr51.8%
add-sqr-sqrt64.3%
Applied egg-rr64.3%
fma-udef64.3%
Applied egg-rr64.3%
Taylor expanded in x around inf 64.3%
+-commutative64.3%
fma-udef64.3%
associate-*r/64.3%
mul-1-neg64.3%
unpow164.3%
sqr-pow51.9%
fabs-sqr51.9%
sqr-pow99.8%
unpow199.8%
Simplified99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (if (<= (fabs x) 9.99999993922529e-9) (/ 0.25 s) (* (/ 1.0 s) (/ 1.0 (/ (- x) s)))))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 9.99999993922529e-9f) {
tmp = 0.25f / s;
} else {
tmp = (1.0f / s) * (1.0f / (-x / s));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (abs(x) <= 9.99999993922529e-9) then
tmp = 0.25e0 / s
else
tmp = (1.0e0 / s) * (1.0e0 / (-x / s))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(9.99999993922529e-9)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(1.0) / s) * Float32(Float32(1.0) / Float32(Float32(-x) / s))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (abs(x) <= single(9.99999993922529e-9)) tmp = single(0.25) / s; else tmp = (single(1.0) / s) * (single(1.0) / (-x / s)); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 9.99999993922529 \cdot 10^{-9}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{s} \cdot \frac{1}{\frac{-x}{s}}\\
\end{array}
\end{array}
if (fabs.f32 x) < 9.99999994e-9Initial program 99.3%
Simplified99.4%
Taylor expanded in s around inf 57.1%
if 9.99999994e-9 < (fabs.f32 x) Initial program 100.0%
Simplified98.6%
Taylor expanded in s around inf 59.2%
mul-1-neg59.2%
unsub-neg59.2%
Simplified59.2%
div-inv59.2%
+-commutative59.2%
Applied egg-rr59.2%
Taylor expanded in s around 0 44.0%
associate-*r/44.0%
mul-1-neg44.0%
unpow144.0%
sqr-pow25.2%
fabs-sqr25.2%
sqr-pow44.0%
unpow144.0%
Simplified44.0%
Final simplification49.6%
(FPCore (x s) :precision binary32 (/ (/ 1.0 (+ (exp (/ x s)) 3.0)) s))
float code(float x, float s) {
return (1.0f / (expf((x / s)) + 3.0f)) / s;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / (exp((x / s)) + 3.0e0)) / s
end function
function code(x, s) return Float32(Float32(Float32(1.0) / Float32(exp(Float32(x / s)) + Float32(3.0))) / s) end
function tmp = code(x, s) tmp = (single(1.0) / (exp((x / s)) + single(3.0))) / s; end
\begin{array}{l}
\\
\frac{\frac{1}{e^{\frac{x}{s}} + 3}}{s}
\end{array}
Initial program 99.7%
Simplified99.0%
add-sqr-sqrt98.6%
pow298.6%
associate-/l/99.4%
Applied egg-rr99.4%
unpow299.4%
add-sqr-sqrt99.8%
associate-/r*99.8%
Applied egg-rr62.2%
Taylor expanded in x around 0 62.9%
Final simplification62.9%
(FPCore (x s) :precision binary32 (if (<= x 9.99999993922529e-9) (/ 0.25 s) (* (/ s x) (/ -1.0 s))))
float code(float x, float s) {
float tmp;
if (x <= 9.99999993922529e-9f) {
tmp = 0.25f / s;
} else {
tmp = (s / x) * (-1.0f / s);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 9.99999993922529e-9) then
tmp = 0.25e0 / s
else
tmp = (s / x) * ((-1.0e0) / s)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(9.99999993922529e-9)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(s / x) * Float32(Float32(-1.0) / s)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(9.99999993922529e-9)) tmp = single(0.25) / s; else tmp = (s / x) * (single(-1.0) / s); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 9.99999993922529 \cdot 10^{-9}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{s}{x} \cdot \frac{-1}{s}\\
\end{array}
\end{array}
if x < 9.99999994e-9Initial program 99.6%
Simplified99.1%
Taylor expanded in s around inf 36.2%
if 9.99999994e-9 < x Initial program 100.0%
Simplified98.7%
Taylor expanded in s around inf 53.3%
mul-1-neg53.3%
unsub-neg53.3%
Simplified53.3%
div-inv53.3%
+-commutative53.3%
Applied egg-rr53.3%
Taylor expanded in s around 0 42.4%
mul-1-neg42.4%
distribute-frac-neg42.4%
unpow142.4%
sqr-pow42.4%
fabs-sqr42.4%
sqr-pow42.4%
unpow142.4%
Simplified42.4%
Final simplification38.0%
(FPCore (x s) :precision binary32 (if (<= x 9.99999993922529e-9) (/ 0.25 s) (/ 1.0 (+ x x))))
float code(float x, float s) {
float tmp;
if (x <= 9.99999993922529e-9f) {
tmp = 0.25f / s;
} else {
tmp = 1.0f / (x + x);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 9.99999993922529e-9) then
tmp = 0.25e0 / s
else
tmp = 1.0e0 / (x + x)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(9.99999993922529e-9)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(1.0) / Float32(x + x)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(9.99999993922529e-9)) tmp = single(0.25) / s; else tmp = single(1.0) / (x + x); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 9.99999993922529 \cdot 10^{-9}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x + x}\\
\end{array}
\end{array}
if x < 9.99999994e-9Initial program 99.6%
Simplified99.1%
Taylor expanded in s around inf 36.2%
if 9.99999994e-9 < x Initial program 100.0%
*-lft-identity100.0%
associate-*r/100.0%
associate-/l*100.0%
distribute-frac-neg100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
associate-*l*100.0%
Simplified100.0%
fma-udef100.0%
Applied egg-rr100.0%
*-commutative100.0%
fma-def100.0%
add-sqr-sqrt100.0%
fabs-sqr100.0%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Taylor expanded in s around inf 3.2%
Taylor expanded in x around inf 11.0%
*-commutative11.0%
rem-log-exp78.6%
exp-lft-sqr78.6%
prod-exp78.6%
rem-log-exp11.0%
Simplified11.0%
Final simplification28.8%
(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.7%
Simplified99.0%
Taylor expanded in s around inf 26.9%
Final simplification26.9%
herbie shell --seed 2023208
(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))))))