
(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 10 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%
rec-exp99.4%
mul-1-neg99.4%
unpow299.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(Float32(1.0) / 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{\frac{1}{1 + e^{\frac{\left|x\right|}{-s}}}}{s + s \cdot e^{\frac{x}{s}}}
\end{array}
Initial program 99.4%
Simplified99.4%
expm1-log1p-u99.5%
Applied egg-rr99.5%
fma-def99.4%
expm1-log1p-u99.4%
add-sqr-sqrt47.6%
fabs-sqr47.6%
add-sqr-sqrt62.7%
Applied egg-rr62.7%
Final simplification62.7%
(FPCore (x s) :precision binary32 (if (<= (fabs x) 1.0999999986962872e-16) (/ 1.0 (+ (* s 4.0) (* x (/ x s)))) (/ (/ (/ 1.0 s) (expm1 (/ x s))) 2.0)))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 1.0999999986962872e-16f) {
tmp = 1.0f / ((s * 4.0f) + (x * (x / s)));
} else {
tmp = ((1.0f / s) / expm1f((x / s))) / 2.0f;
}
return tmp;
}
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(1.0999999986962872e-16)) tmp = Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(x * Float32(x / s)))); else tmp = Float32(Float32(Float32(Float32(1.0) / s) / expm1(Float32(x / s))) / Float32(2.0)); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 1.0999999986962872 \cdot 10^{-16}:\\
\;\;\;\;\frac{1}{s \cdot 4 + x \cdot \frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{\frac{1}{s}}{\mathsf{expm1}\left(\frac{x}{s}\right)}}{2}\\
\end{array}
\end{array}
if (fabs.f32 x) < 1.1e-16Initial program 98.1%
Simplified98.3%
Taylor expanded in s around 0 98.3%
*-commutative98.3%
mul-1-neg98.3%
distribute-frac-neg98.3%
Simplified98.3%
Taylor expanded in s around inf 76.8%
+-commutative76.8%
+-commutative76.8%
associate-+r+76.8%
metadata-eval76.8%
associate-*r*76.8%
*-commutative76.8%
metadata-eval76.8%
associate-*r*76.8%
*-commutative76.8%
associate-+r+76.8%
Simplified76.8%
Taylor expanded in x around 0 76.8%
unpow276.8%
associate-*l/77.7%
*-commutative77.7%
Simplified77.7%
if 1.1e-16 < (fabs.f32 x) Initial program 99.9%
Simplified99.9%
expm1-log1p-u99.7%
expm1-udef99.3%
associate-/l/99.3%
Applied egg-rr99.3%
expm1-def99.7%
expm1-log1p99.9%
associate-/r*99.9%
Simplified99.9%
expm1-log1p-u99.6%
expm1-udef99.2%
Applied egg-rr50.2%
expm1-def50.2%
expm1-log1p52.3%
associate-/r*51.8%
Simplified51.8%
Taylor expanded in s around inf 51.8%
Final simplification58.9%
(FPCore (x s) :precision binary32 (/ 0.5 (+ s (* s (exp (/ (fabs x) s))))))
float code(float x, float s) {
return 0.5f / (s + (s * expf((fabsf(x) / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (s + (s * exp((abs(x) / s))))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(s + Float32(s * exp(Float32(abs(x) / s))))) end
function tmp = code(x, s) tmp = single(0.5) / (s + (s * exp((abs(x) / s)))); end
\begin{array}{l}
\\
\frac{0.5}{s + s \cdot e^{\frac{\left|x\right|}{s}}}
\end{array}
Initial program 99.4%
Simplified99.4%
fma-udef99.4%
Applied egg-rr99.4%
Taylor expanded in s around inf 96.0%
Final simplification96.0%
(FPCore (x s) :precision binary32 (/ (exp (/ (- (fabs x)) s)) (* s 4.0)))
float code(float x, float s) {
return expf((-fabsf(x) / s)) / (s * 4.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = exp((-abs(x) / s)) / (s * 4.0e0)
end function
function code(x, s) return Float32(exp(Float32(Float32(-abs(x)) / s)) / Float32(s * Float32(4.0))) end
function tmp = code(x, s) tmp = exp((-abs(x) / s)) / (s * single(4.0)); end
\begin{array}{l}
\\
\frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot 4}
\end{array}
Initial program 99.4%
associate-*l*99.4%
Simplified99.4%
Taylor expanded in s around inf 95.7%
Final simplification95.7%
(FPCore (x s) :precision binary32 (/ 1.0 (+ (* s 4.0) (* x (/ x s)))))
float code(float x, float s) {
return 1.0f / ((s * 4.0f) + (x * (x / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((s * 4.0e0) + (x * (x / s)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(x * Float32(x / s)))) end
function tmp = code(x, s) tmp = single(1.0) / ((s * single(4.0)) + (x * (x / s))); end
\begin{array}{l}
\\
\frac{1}{s \cdot 4 + x \cdot \frac{x}{s}}
\end{array}
Initial program 99.4%
Simplified99.4%
Taylor expanded in s around 0 99.4%
*-commutative99.4%
mul-1-neg99.4%
distribute-frac-neg99.4%
Simplified99.4%
Taylor expanded in s around inf 27.6%
+-commutative27.6%
+-commutative27.6%
associate-+r+27.6%
metadata-eval27.6%
associate-*r*27.6%
*-commutative27.6%
metadata-eval27.6%
associate-*r*27.6%
*-commutative27.6%
associate-+r+27.6%
Simplified66.0%
Taylor expanded in x around 0 66.0%
unpow266.0%
associate-*l/66.3%
*-commutative66.3%
Simplified66.3%
Final simplification66.3%
(FPCore (x s) :precision binary32 (if (<= x 2.0000000233721948e-7) (/ 0.25 s) (/ s (* x x))))
float code(float x, float s) {
float tmp;
if (x <= 2.0000000233721948e-7f) {
tmp = 0.25f / s;
} else {
tmp = s / (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 <= 2.0000000233721948e-7) then
tmp = 0.25e0 / s
else
tmp = s / (x * x)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(2.0000000233721948e-7)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(s / Float32(x * x)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(2.0000000233721948e-7)) tmp = single(0.25) / s; else tmp = s / (x * x); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.0000000233721948 \cdot 10^{-7}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{s}{x \cdot x}\\
\end{array}
\end{array}
if x < 2.00000002e-7Initial program 99.1%
Simplified99.2%
Taylor expanded in s around inf 37.1%
if 2.00000002e-7 < x Initial program 100.0%
Simplified100.0%
Taylor expanded in s around 0 100.0%
*-commutative100.0%
mul-1-neg100.0%
distribute-frac-neg100.0%
Simplified100.0%
Taylor expanded in s around inf 4.4%
+-commutative4.4%
+-commutative4.4%
associate-+r+4.4%
metadata-eval4.4%
associate-*r*4.4%
*-commutative4.4%
metadata-eval4.4%
associate-*r*4.4%
*-commutative4.4%
associate-+r+4.4%
Simplified76.4%
Taylor expanded in s around 0 73.3%
unpow273.3%
Simplified73.3%
Final simplification48.5%
(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))))))