
(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}
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ (/ 1.0 (expm1 (log1p (+ 1.0 (exp (/ (fabs x) (- s))))))) (* s (+ 1.0 (exp (/ x s))))))
x = abs(x);
float code(float x, float s) {
return (1.0f / expm1f(log1pf((1.0f + expf((fabsf(x) / -s)))))) / (s * (1.0f + expf((x / s))));
}
x = abs(x) function code(x, s) return Float32(Float32(Float32(1.0) / expm1(log1p(Float32(Float32(1.0) + exp(Float32(abs(x) / Float32(-s))))))) / Float32(s * Float32(Float32(1.0) + exp(Float32(x / s))))) end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{1}{\mathsf{expm1}\left(\mathsf{log1p}\left(1 + e^{\frac{\left|x\right|}{-s}}\right)\right)}}{s \cdot \left(1 + e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.2%
Simplified99.3%
fma-udef99.2%
add-sqr-sqrt46.0%
fabs-sqr46.0%
add-sqr-sqrt60.1%
Applied egg-rr60.1%
fma-def60.0%
Simplified60.0%
Taylor expanded in s around 0 60.0%
expm1-log1p-u60.1%
Applied egg-rr60.1%
Final simplification60.1%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (* s (* (+ 1.0 (exp (/ x s))) (+ 1.0 (exp (/ (- (fabs x)) s)))))))
x = abs(x);
float code(float x, float s) {
return 1.0f / (s * ((1.0f + expf((x / s))) * (1.0f + expf((-fabsf(x) / s)))));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (s * ((1.0e0 + exp((x / s))) * (1.0e0 + exp((-abs(x) / s)))))
end function
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(Float32(1.0) + exp(Float32(x / s))) * Float32(Float32(1.0) + exp(Float32(Float32(-abs(x)) / s)))))) end
x = abs(x) function tmp = code(x, s) tmp = single(1.0) / (s * ((single(1.0) + exp((x / s))) * (single(1.0) + exp((-abs(x) / s))))); end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{s \cdot \left(\left(1 + e^{\frac{x}{s}}\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)}
\end{array}
Initial program 99.2%
Simplified99.3%
fma-udef99.2%
add-sqr-sqrt46.0%
fabs-sqr46.0%
add-sqr-sqrt60.1%
Applied egg-rr60.1%
fma-def60.0%
Simplified60.0%
Taylor expanded in s around 0 60.0%
Taylor expanded in x around inf 60.0%
mul-1-neg60.0%
Simplified60.0%
Final simplification60.0%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (* (+ 1.0 (exp (/ (- (fabs x)) s))) (+ s (* s (exp (/ x s)))))))
x = abs(x);
float code(float x, float s) {
return 1.0f / ((1.0f + expf((-fabsf(x) / s))) * (s + (s * expf((x / s)))));
}
NOTE: x should be positive before calling this function
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
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(Float32(-abs(x)) / s))) * Float32(s + Float32(s * exp(Float32(x / s)))))) end
x = abs(x) 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}
x = |x|\\
\\
\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.2%
Simplified99.3%
fma-udef99.2%
add-sqr-sqrt46.0%
fabs-sqr46.0%
add-sqr-sqrt60.1%
Applied egg-rr60.1%
fma-def60.0%
Simplified60.0%
Taylor expanded in x around inf 60.1%
*-un-lft-identity60.1%
mul-1-neg60.1%
Applied egg-rr60.1%
*-lft-identity60.1%
distribute-neg-frac60.1%
Simplified60.1%
Final simplification60.1%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= (fabs x) 3.999999999279835e-23) (/ 0.25 s) (/ (/ 1.0 (+ 1.0 (+ 1.0 (* 0.5 (/ (* x x) (* s s)))))) (+ x (* s 2.0)))))
x = abs(x);
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 3.999999999279835e-23f) {
tmp = 0.25f / s;
} else {
tmp = (1.0f / (1.0f + (1.0f + (0.5f * ((x * x) / (s * s)))))) / (x + (s * 2.0f));
}
return tmp;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (abs(x) <= 3.999999999279835e-23) then
tmp = 0.25e0 / s
else
tmp = (1.0e0 / (1.0e0 + (1.0e0 + (0.5e0 * ((x * x) / (s * s)))))) / (x + (s * 2.0e0))
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(3.999999999279835e-23)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(Float32(1.0) + Float32(Float32(0.5) * Float32(Float32(x * x) / Float32(s * s)))))) / Float32(x + Float32(s * Float32(2.0)))); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (abs(x) <= single(3.999999999279835e-23)) tmp = single(0.25) / s; else tmp = (single(1.0) / (single(1.0) + (single(1.0) + (single(0.5) * ((x * x) / (s * s)))))) / (x + (s * single(2.0))); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 3.999999999279835 \cdot 10^{-23}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{1 + \left(1 + 0.5 \cdot \frac{x \cdot x}{s \cdot s}\right)}}{x + s \cdot 2}\\
\end{array}
\end{array}
if (fabs.f32 x) < 4e-23Initial program 98.9%
Simplified99.1%
Taylor expanded in s around inf 75.2%
if 4e-23 < (fabs.f32 x) Initial program 99.2%
Simplified99.4%
fma-udef99.3%
add-sqr-sqrt45.6%
fabs-sqr45.6%
add-sqr-sqrt53.2%
Applied egg-rr53.2%
fma-def53.2%
Simplified53.2%
Taylor expanded in s around inf 19.7%
*-commutative19.7%
Simplified19.7%
Taylor expanded in s around inf 50.5%
associate-+r+50.5%
mul-1-neg50.5%
unsub-neg50.5%
unpow250.5%
sqr-abs50.5%
unpow250.5%
Simplified50.5%
Taylor expanded in s around inf 82.8%
Final simplification81.3%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 0.5 (* s (+ 1.0 (exp (/ x s))))))
x = abs(x);
float code(float x, float s) {
return 0.5f / (s * (1.0f + expf((x / s))));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (s * (1.0e0 + exp((x / s))))
end function
x = abs(x) function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(1.0) + exp(Float32(x / s))))) end
x = abs(x) function tmp = code(x, s) tmp = single(0.5) / (s * (single(1.0) + exp((x / s)))); end
\begin{array}{l}
x = |x|\\
\\
\frac{0.5}{s \cdot \left(1 + e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.2%
Simplified99.3%
fma-udef99.2%
add-sqr-sqrt46.0%
fabs-sqr46.0%
add-sqr-sqrt60.1%
Applied egg-rr60.1%
fma-def60.0%
Simplified60.0%
Taylor expanded in s around 0 60.0%
Taylor expanded in s around inf 58.5%
Final simplification58.5%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= x 4.999999858590343e-10) (/ 0.25 s) (/ (* 2.0 (* (/ s x) (/ s x))) (+ x (* s 2.0)))))
x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 4.999999858590343e-10f) {
tmp = 0.25f / s;
} else {
tmp = (2.0f * ((s / x) * (s / x))) / (x + (s * 2.0f));
}
return tmp;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 4.999999858590343e-10) then
tmp = 0.25e0 / s
else
tmp = (2.0e0 * ((s / x) * (s / x))) / (x + (s * 2.0e0))
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(4.999999858590343e-10)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(2.0) * Float32(Float32(s / x) * Float32(s / x))) / Float32(x + Float32(s * Float32(2.0)))); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(4.999999858590343e-10)) tmp = single(0.25) / s; else tmp = (single(2.0) * ((s / x) * (s / x))) / (x + (s * single(2.0))); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.999999858590343 \cdot 10^{-10}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{2 \cdot \left(\frac{s}{x} \cdot \frac{s}{x}\right)}{x + s \cdot 2}\\
\end{array}
\end{array}
if x < 4.99999986e-10Initial program 98.8%
Simplified99.0%
Taylor expanded in s around inf 36.5%
if 4.99999986e-10 < x Initial program 100.0%
Simplified99.9%
fma-udef99.9%
add-sqr-sqrt99.9%
fabs-sqr99.9%
add-sqr-sqrt99.9%
Applied egg-rr99.9%
fma-def99.9%
Simplified99.9%
Taylor expanded in s around inf 12.4%
*-commutative12.4%
Simplified12.4%
Taylor expanded in s around inf 45.5%
associate-+r+45.5%
mul-1-neg45.5%
unsub-neg45.5%
unpow245.5%
sqr-abs45.5%
unpow245.5%
Simplified45.5%
Taylor expanded in x around inf 84.3%
unpow284.3%
unpow284.3%
times-frac84.3%
Simplified84.3%
Final simplification50.8%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 0.5 (+ x (* s 2.0))))
x = abs(x);
float code(float x, float s) {
return 0.5f / (x + (s * 2.0f));
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (x + (s * 2.0e0))
end function
x = abs(x) function code(x, s) return Float32(Float32(0.5) / Float32(x + Float32(s * Float32(2.0)))) end
x = abs(x) function tmp = code(x, s) tmp = single(0.5) / (x + (s * single(2.0))); end
\begin{array}{l}
x = |x|\\
\\
\frac{0.5}{x + s \cdot 2}
\end{array}
Initial program 99.2%
Simplified99.3%
fma-udef99.2%
add-sqr-sqrt46.0%
fabs-sqr46.0%
add-sqr-sqrt60.1%
Applied egg-rr60.1%
fma-def60.0%
Simplified60.0%
Taylor expanded in s around inf 29.9%
*-commutative29.9%
Simplified29.9%
Taylor expanded in s around inf 29.9%
Final simplification29.9%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= x 0.019999999552965164) (/ 0.25 s) (/ 0.5 x)))
x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 0.019999999552965164f) {
tmp = 0.25f / s;
} else {
tmp = 0.5f / x;
}
return tmp;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 0.019999999552965164e0) then
tmp = 0.25e0 / s
else
tmp = 0.5e0 / x
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.019999999552965164)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.5) / x); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.019999999552965164)) tmp = single(0.25) / s; else tmp = single(0.5) / x; end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.019999999552965164:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if x < 0.0199999996Initial program 98.9%
Simplified99.0%
Taylor expanded in s around inf 35.4%
if 0.0199999996 < x Initial program 100.0%
Simplified100.0%
fma-udef100.0%
add-sqr-sqrt100.0%
fabs-sqr100.0%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in s around inf 10.6%
*-commutative10.6%
Simplified10.6%
Taylor expanded in s around inf 10.8%
Taylor expanded in x around inf 10.8%
Final simplification29.2%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 0.25 s))
x = abs(x);
float code(float x, float s) {
return 0.25f / s;
}
NOTE: x should be positive before calling this function
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.25e0 / s
end function
x = abs(x) function code(x, s) return Float32(Float32(0.25) / s) end
x = abs(x) function tmp = code(x, s) tmp = single(0.25) / s; end
\begin{array}{l}
x = |x|\\
\\
\frac{0.25}{s}
\end{array}
Initial program 99.2%
Simplified99.3%
Taylor expanded in s around inf 27.7%
Final simplification27.7%
herbie shell --seed 2023275
(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))))))