
(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 8 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(Float32(1.0) / s) / Float32(exp(Float32(abs(x) / Float32(-s))) + Float32(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{\frac{1}{s}}{e^{\frac{\left|x\right|}{-s}} + \left(e^{\frac{\left|x\right|}{s}} + 2\right)}
\end{array}
Initial program 99.6%
Simplified99.8%
Final simplification99.8%
(FPCore (x s)
:precision binary32
(let* ((t_0 (/ (* x x) s)))
(if (<= (fabs x) 10.0)
(/ 1.0 (- (+ (* 2.0 (/ x (/ s x))) (* s 4.0)) t_0))
(/ 1.0 t_0))))
float code(float x, float s) {
float t_0 = (x * x) / s;
float tmp;
if (fabsf(x) <= 10.0f) {
tmp = 1.0f / (((2.0f * (x / (s / x))) + (s * 4.0f)) - t_0);
} else {
tmp = 1.0f / t_0;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: tmp
t_0 = (x * x) / s
if (abs(x) <= 10.0e0) then
tmp = 1.0e0 / (((2.0e0 * (x / (s / x))) + (s * 4.0e0)) - t_0)
else
tmp = 1.0e0 / t_0
end if
code = tmp
end function
function code(x, s) t_0 = Float32(Float32(x * x) / s) tmp = Float32(0.0) if (abs(x) <= Float32(10.0)) tmp = Float32(Float32(1.0) / Float32(Float32(Float32(Float32(2.0) * Float32(x / Float32(s / x))) + Float32(s * Float32(4.0))) - t_0)); else tmp = Float32(Float32(1.0) / t_0); end return tmp end
function tmp_2 = code(x, s) t_0 = (x * x) / s; tmp = single(0.0); if (abs(x) <= single(10.0)) tmp = single(1.0) / (((single(2.0) * (x / (s / x))) + (s * single(4.0))) - t_0); else tmp = single(1.0) / t_0; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x \cdot x}{s}\\
\mathbf{if}\;\left|x\right| \leq 10:\\
\;\;\;\;\frac{1}{\left(2 \cdot \frac{x}{\frac{s}{x}} + s \cdot 4\right) - t_0}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{t_0}\\
\end{array}
\end{array}
if (fabs.f32 x) < 10Initial program 99.3%
*-lft-identity99.3%
associate-*r/99.3%
associate-/l*99.2%
distribute-frac-neg99.2%
exp-neg99.3%
associate-/r/99.2%
/-rgt-identity99.2%
associate-*l*99.3%
Simplified99.5%
Taylor expanded in s around inf 55.6%
Simplified55.6%
fma-udef55.6%
associate-/l*55.7%
Applied egg-rr55.7%
if 10 < (fabs.f32 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%
Taylor expanded in s around inf 1.9%
Simplified1.9%
Taylor expanded in x around inf 85.7%
unpow285.7%
Simplified85.7%
Final simplification70.2%
(FPCore (x s) :precision binary32 (if (<= x 4.0000000126843074e-30) (/ 1.0 (fma s 4.0 (/ (* x x) s))) (/ (/ 1.0 (+ (exp (/ x s)) 3.0)) s)))
float code(float x, float s) {
float tmp;
if (x <= 4.0000000126843074e-30f) {
tmp = 1.0f / fmaf(s, 4.0f, ((x * x) / s));
} else {
tmp = (1.0f / (expf((x / s)) + 3.0f)) / s;
}
return tmp;
}
function code(x, s) tmp = Float32(0.0) if (x <= Float32(4.0000000126843074e-30)) tmp = Float32(Float32(1.0) / fma(s, Float32(4.0), Float32(Float32(x * x) / s))); else tmp = Float32(Float32(Float32(1.0) / Float32(exp(Float32(x / s)) + Float32(3.0))) / s); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.0000000126843074 \cdot 10^{-30}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(s, 4, \frac{x \cdot x}{s}\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{e^{\frac{x}{s}} + 3}}{s}\\
\end{array}
\end{array}
if x < 4e-30Initial program 99.6%
*-lft-identity99.6%
associate-*r/99.6%
associate-/l*99.5%
distribute-frac-neg99.5%
exp-neg99.6%
associate-/r/99.5%
/-rgt-identity99.5%
associate-*l*99.5%
Simplified99.7%
Taylor expanded in s around inf 38.5%
Simplified38.6%
Taylor expanded in x around 0 73.3%
*-commutative73.3%
fma-def73.3%
unpow273.3%
Simplified73.3%
if 4e-30 < x Initial program 99.7%
Simplified99.8%
Taylor expanded in s around inf 97.7%
Taylor expanded in s around 0 97.7%
+-commutative97.7%
Simplified97.7%
inv-pow97.7%
*-commutative97.7%
unpow-prod-down97.7%
inv-pow97.7%
Applied egg-rr97.7%
un-div-inv97.7%
unpow-197.7%
add-sqr-sqrt97.7%
fabs-sqr97.7%
add-sqr-sqrt97.7%
Applied egg-rr97.7%
Final simplification84.0%
(FPCore (x s) :precision binary32 (/ 1.0 (* s (+ 2.0 (* 2.0 (cosh (/ x s)))))))
float code(float x, float s) {
return 1.0f / (s * (2.0f + (2.0f * coshf((x / s)))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (s * (2.0e0 + (2.0e0 * cosh((x / s)))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(2.0) + Float32(Float32(2.0) * cosh(Float32(x / s)))))) end
function tmp = code(x, s) tmp = single(1.0) / (s * (single(2.0) + (single(2.0) * cosh((x / s))))); end
\begin{array}{l}
\\
\frac{1}{s \cdot \left(2 + 2 \cdot \cosh \left(\frac{x}{s}\right)\right)}
\end{array}
Initial program 99.6%
Simplified99.8%
Taylor expanded in s around 0 99.7%
+-commutative99.7%
mul-1-neg99.7%
distribute-frac-neg99.7%
Simplified99.7%
expm1-log1p-u99.7%
expm1-udef79.1%
Applied egg-rr79.1%
expm1-def99.8%
expm1-log1p99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (/ 1.0 (fma s 4.0 (/ (* x x) s))))
float code(float x, float s) {
return 1.0f / fmaf(s, 4.0f, ((x * x) / s));
}
function code(x, s) return Float32(Float32(1.0) / fma(s, Float32(4.0), Float32(Float32(x * x) / s))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(s, 4, \frac{x \cdot x}{s}\right)}
\end{array}
Initial program 99.6%
*-lft-identity99.6%
associate-*r/99.6%
associate-/l*99.6%
distribute-frac-neg99.6%
exp-neg99.6%
associate-/r/99.6%
/-rgt-identity99.6%
associate-*l*99.7%
Simplified99.7%
Taylor expanded in s around inf 29.6%
Simplified29.6%
Taylor expanded in x around 0 70.2%
*-commutative70.2%
fma-def70.2%
unpow270.2%
Simplified70.2%
Final simplification70.2%
(FPCore (x s) :precision binary32 (if (or (<= x -2.0000000233721948e-7) (not (<= x 1.999999987845058e-8))) (/ 1.0 (/ (* x x) s)) (/ 0.25 s)))
float code(float x, float s) {
float tmp;
if ((x <= -2.0000000233721948e-7f) || !(x <= 1.999999987845058e-8f)) {
tmp = 1.0f / ((x * x) / s);
} else {
tmp = 0.25f / s;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if ((x <= (-2.0000000233721948e-7)) .or. (.not. (x <= 1.999999987845058e-8))) then
tmp = 1.0e0 / ((x * x) / s)
else
tmp = 0.25e0 / s
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if ((x <= Float32(-2.0000000233721948e-7)) || !(x <= Float32(1.999999987845058e-8))) tmp = Float32(Float32(1.0) / Float32(Float32(x * x) / s)); else tmp = Float32(Float32(0.25) / s); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if ((x <= single(-2.0000000233721948e-7)) || ~((x <= single(1.999999987845058e-8)))) tmp = single(1.0) / ((x * x) / s); else tmp = single(0.25) / s; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.0000000233721948 \cdot 10^{-7} \lor \neg \left(x \leq 1.999999987845058 \cdot 10^{-8}\right):\\
\;\;\;\;\frac{1}{\frac{x \cdot x}{s}}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.25}{s}\\
\end{array}
\end{array}
if x < -2.00000002e-7 or 1.99999999e-8 < x Initial program 99.9%
*-lft-identity99.9%
associate-*r/99.9%
associate-/l*100.0%
distribute-frac-neg100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
associate-*l*100.0%
Simplified100.0%
Taylor expanded in s around inf 3.0%
Simplified3.0%
Taylor expanded in x around inf 75.2%
unpow275.2%
Simplified75.2%
if -2.00000002e-7 < x < 1.99999999e-8Initial program 99.3%
Simplified99.5%
Taylor expanded in s around inf 62.0%
Final simplification69.4%
(FPCore (x s) :precision binary32 (if (or (<= x -2.0000000233721948e-7) (not (<= x 1.999999987845058e-8))) (/ s (* x x)) (/ 0.25 s)))
float code(float x, float s) {
float tmp;
if ((x <= -2.0000000233721948e-7f) || !(x <= 1.999999987845058e-8f)) {
tmp = s / (x * x);
} else {
tmp = 0.25f / s;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if ((x <= (-2.0000000233721948e-7)) .or. (.not. (x <= 1.999999987845058e-8))) then
tmp = s / (x * x)
else
tmp = 0.25e0 / s
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if ((x <= Float32(-2.0000000233721948e-7)) || !(x <= Float32(1.999999987845058e-8))) tmp = Float32(s / Float32(x * x)); else tmp = Float32(Float32(0.25) / s); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if ((x <= single(-2.0000000233721948e-7)) || ~((x <= single(1.999999987845058e-8)))) tmp = s / (x * x); else tmp = single(0.25) / s; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.0000000233721948 \cdot 10^{-7} \lor \neg \left(x \leq 1.999999987845058 \cdot 10^{-8}\right):\\
\;\;\;\;\frac{s}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.25}{s}\\
\end{array}
\end{array}
if x < -2.00000002e-7 or 1.99999999e-8 < x Initial program 99.9%
*-lft-identity99.9%
associate-*r/99.9%
associate-/l*100.0%
distribute-frac-neg100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
associate-*l*100.0%
Simplified100.0%
Taylor expanded in s around inf 3.0%
Simplified3.0%
Taylor expanded in x around inf 73.4%
unpow273.4%
Simplified73.4%
if -2.00000002e-7 < x < 1.99999999e-8Initial program 99.3%
Simplified99.5%
Taylor expanded in s around inf 62.0%
Final simplification68.4%
(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%
Simplified99.8%
Taylor expanded in s around inf 29.9%
Final simplification29.9%
herbie shell --seed 2023194
(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))))))