
(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 9 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 (+ 1.0 (exp (/ (fabs x) (- s))))) (expm1 (log1p (fma s (exp (/ (fabs x) s)) s)))))
float code(float x, float s) {
return (1.0f / (1.0f + expf((fabsf(x) / -s)))) / expm1f(log1pf(fmaf(s, expf((fabsf(x) / s)), s)));
}
function code(x, s) return Float32(Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(abs(x) / Float32(-s))))) / expm1(log1p(fma(s, exp(Float32(abs(x) / s)), s)))) end
\begin{array}{l}
\\
\frac{\frac{1}{1 + e^{\frac{\left|x\right|}{-s}}}}{\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(s, e^{\frac{\left|x\right|}{s}}, s\right)\right)\right)}
\end{array}
Initial program 99.4%
Simplified99.4%
expm1-log1p-u99.5%
Applied egg-rr99.5%
Final simplification99.5%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- (fabs x)) s)))) (/ (/ t_0 s) (pow (+ 1.0 t_0) 2.0))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
return (t_0 / s) / powf((1.0f + t_0), 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) / ((1.0e0 + t_0) ** 2.0e0)
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) return Float32(Float32(t_0 / s) / (Float32(Float32(1.0) + t_0) ^ Float32(2.0))) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); tmp = (t_0 / s) / ((single(1.0) + t_0) ^ single(2.0)); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
\frac{\frac{t_0}{s}}{{\left(1 + t_0\right)}^{2}}
\end{array}
\end{array}
Initial program 99.4%
Simplified99.4%
Taylor expanded in s around 0 99.4%
associate-/r*99.4%
mul-1-neg99.4%
distribute-frac-neg99.4%
+-commutative99.4%
rec-exp99.4%
mul-1-neg99.4%
unpow299.4%
+-commutative99.4%
mul-1-neg99.4%
distribute-frac-neg99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (x s) :precision binary32 (/ (/ 1.0 (+ 1.0 (exp (/ (fabs x) (- s))))) (fma (exp (/ x s)) s s)))
float code(float x, float s) {
return (1.0f / (1.0f + expf((fabsf(x) / -s)))) / fmaf(expf((x / s)), s, s);
}
function code(x, s) return Float32(Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(abs(x) / Float32(-s))))) / fma(exp(Float32(x / s)), s, s)) end
\begin{array}{l}
\\
\frac{\frac{1}{1 + e^{\frac{\left|x\right|}{-s}}}}{\mathsf{fma}\left(e^{\frac{x}{s}}, s, s\right)}
\end{array}
Initial program 99.4%
Simplified99.4%
expm1-log1p-u99.5%
Applied egg-rr99.5%
fma-def99.4%
expm1-log1p-u99.4%
*-commutative99.4%
fma-def99.4%
add-sqr-sqrt47.7%
fabs-sqr47.7%
add-sqr-sqrt62.7%
Applied egg-rr62.7%
Final simplification62.7%
(FPCore (x s) :precision binary32 (/ 1.0 (* (+ 1.0 (exp (/ (fabs x) (- s)))) (+ s (* s (exp (/ x s)))))))
float code(float x, float s) {
return 1.0f / ((1.0f + expf((fabsf(x) / -s))) * (s + (s * expf((x / s)))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((1.0e0 + exp((abs(x) / -s))) * (s + (s * exp((x / s)))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(abs(x) / Float32(-s)))) * Float32(s + Float32(s * exp(Float32(x / s)))))) end
function tmp = code(x, s) tmp = single(1.0) / ((single(1.0) + exp((abs(x) / -s))) * (s + (s * exp((x / s))))); end
\begin{array}{l}
\\
\frac{1}{\left(1 + e^{\frac{\left|x\right|}{-s}}\right) \cdot \left(s + s \cdot e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.4%
Simplified99.4%
fma-udef99.4%
Applied egg-rr99.4%
add-exp-log98.0%
*-commutative98.0%
log-prod97.8%
add-log-exp98.1%
add-sqr-sqrt47.4%
fabs-sqr47.4%
add-sqr-sqrt61.9%
Applied egg-rr61.9%
*-un-lft-identity61.9%
+-commutative61.9%
exp-sum61.7%
add-exp-log62.7%
Applied egg-rr62.7%
*-lft-identity62.7%
associate-/l/62.7%
*-commutative62.7%
Simplified62.7%
Final simplification62.7%
(FPCore (x s) :precision binary32 (/ 0.5 (+ s (* s (exp (/ x s))))))
float code(float x, float s) {
return 0.5f / (s + (s * expf((x / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (s + (s * exp((x / s))))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(s + Float32(s * exp(Float32(x / s))))) end
function tmp = code(x, s) tmp = single(0.5) / (s + (s * exp((x / s)))); end
\begin{array}{l}
\\
\frac{0.5}{s + s \cdot e^{\frac{x}{s}}}
\end{array}
Initial program 99.4%
Simplified99.4%
fma-udef99.4%
Applied egg-rr99.4%
Taylor expanded in s around inf 96.0%
expm1-log1p-u96.0%
expm1-udef84.6%
add-sqr-sqrt42.7%
fabs-sqr42.7%
add-sqr-sqrt50.5%
Applied egg-rr50.5%
expm1-def61.8%
expm1-log1p61.8%
Simplified61.8%
Final simplification61.8%
(FPCore (x s) :precision binary32 (/ 0.5 (+ (fabs x) (* s 2.0))))
float code(float x, float s) {
return 0.5f / (fabsf(x) + (s * 2.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (abs(x) + (s * 2.0e0))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(abs(x) + Float32(s * Float32(2.0)))) end
function tmp = code(x, s) tmp = single(0.5) / (abs(x) + (s * single(2.0))); end
\begin{array}{l}
\\
\frac{0.5}{\left|x\right| + s \cdot 2}
\end{array}
Initial program 99.4%
Simplified99.4%
Taylor expanded in s around inf 30.2%
*-commutative30.2%
Simplified30.2%
Taylor expanded in s around inf 28.9%
Final simplification28.9%
(FPCore (x s) :precision binary32 (/ 0.5 (+ s (+ (fabs x) s))))
float code(float x, float s) {
return 0.5f / (s + (fabsf(x) + s));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (s + (abs(x) + s))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(s + Float32(abs(x) + s))) end
function tmp = code(x, s) tmp = single(0.5) / (s + (abs(x) + s)); end
\begin{array}{l}
\\
\frac{0.5}{s + \left(\left|x\right| + s\right)}
\end{array}
Initial program 99.4%
Simplified99.4%
fma-udef99.4%
Applied egg-rr99.4%
Taylor expanded in s around inf 96.0%
Taylor expanded in s around inf 28.9%
Final simplification28.9%
(FPCore (x s) :precision binary32 (if (<= (fabs x) 9.999999747378752e-5) (/ 0.25 s) (/ 0.5 x)))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 9.999999747378752e-5f) {
tmp = 0.25f / s;
} else {
tmp = 0.5f / x;
}
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.999999747378752e-5) then
tmp = 0.25e0 / s
else
tmp = 0.5e0 / x
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(9.999999747378752e-5)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.5) / x); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (abs(x) <= single(9.999999747378752e-5)) tmp = single(0.25) / s; else tmp = single(0.5) / x; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 9.999999747378752 \cdot 10^{-5}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if (fabs.f32 x) < 9.99999975e-5Initial program 98.6%
Simplified98.8%
Taylor expanded in s around inf 53.6%
if 9.99999975e-5 < (fabs.f32 x) Initial program 100.0%
Simplified100.0%
expm1-log1p-u100.0%
expm1-udef100.0%
associate-/l/100.0%
Applied egg-rr100.0%
expm1-def100.0%
expm1-log1p100.0%
associate-/r*100.0%
Simplified100.0%
expm1-log1p-u100.0%
expm1-udef100.0%
Applied egg-rr53.6%
expm1-def53.6%
expm1-log1p55.7%
associate-/r*55.0%
Simplified55.0%
Taylor expanded in s around inf 10.8%
Final simplification30.2%
(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.4%
Simplified99.4%
Taylor expanded in s around inf 26.8%
Final simplification26.8%
herbie shell --seed 2023279
(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))))))