
(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}
NOTE: x should be positive before calling this function
(FPCore (x s)
:precision binary32
(let* ((t_0 (exp (/ x s))))
(if (<= (fabs x) 40.0)
(/ (exp (+ (/ x s) (* -2.0 (log1p t_0)))) s)
(/ 0.5 (* s (+ 1.0 t_0))))))x = abs(x);
float code(float x, float s) {
float t_0 = expf((x / s));
float tmp;
if (fabsf(x) <= 40.0f) {
tmp = expf(((x / s) + (-2.0f * log1pf(t_0)))) / s;
} else {
tmp = 0.5f / (s * (1.0f + t_0));
}
return tmp;
}
x = abs(x) function code(x, s) t_0 = exp(Float32(x / s)) tmp = Float32(0.0) if (abs(x) <= Float32(40.0)) tmp = Float32(exp(Float32(Float32(x / s) + Float32(Float32(-2.0) * log1p(t_0)))) / s); else tmp = Float32(Float32(0.5) / Float32(s * Float32(Float32(1.0) + t_0))); end return tmp end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
t_0 := e^{\frac{x}{s}}\\
\mathbf{if}\;\left|x\right| \leq 40:\\
\;\;\;\;\frac{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(t_0\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{s \cdot \left(1 + t_0\right)}\\
\end{array}
\end{array}
if (fabs.f32 x) < 40Initial program 99.1%
associate-/r*99.2%
Simplified99.2%
expm1-log1p-u95.8%
expm1-udef94.1%
Applied egg-rr73.1%
expm1-def74.2%
expm1-log1p77.6%
Simplified77.6%
*-un-lft-identity77.6%
*-commutative77.6%
times-frac77.5%
metadata-eval77.5%
unpow277.5%
frac-times77.7%
inv-pow77.7%
inv-pow77.7%
pow-prod-up77.8%
+-commutative77.8%
metadata-eval77.8%
Applied egg-rr77.8%
associate-*r/77.9%
pow-to-exp77.8%
prod-exp99.1%
rem-log-exp98.9%
pow-to-exp98.9%
log-pow99.1%
+-commutative99.1%
log1p-udef99.3%
Applied egg-rr99.3%
if 40 < (fabs.f32 x) Initial program 100.0%
associate-/r*100.0%
Simplified100.0%
Applied egg-rr55.0%
Taylor expanded in x around 0 47.4%
*-commutative47.4%
+-commutative47.4%
frac-times47.4%
metadata-eval47.4%
+-commutative47.4%
Applied egg-rr47.4%
Final simplification77.2%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (* (fma s (exp (/ (fabs x) (- s))) s) (+ 1.0 (exp (/ x s))))))
x = abs(x);
float code(float x, float s) {
return 1.0f / (fmaf(s, expf((fabsf(x) / -s)), s) * (1.0f + expf((x / s))));
}
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(fma(s, exp(Float32(abs(x) / Float32(-s))), s) * Float32(Float32(1.0) + exp(Float32(x / s))))) end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{\mathsf{fma}\left(s, e^{\frac{\left|x\right|}{-s}}, s\right) \cdot \left(1 + e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.5%
Simplified99.6%
*-un-lft-identity99.6%
div-inv99.6%
exp-prod81.5%
add-sqr-sqrt81.5%
sqrt-unprod81.5%
sqr-neg81.5%
sqrt-unprod-0.0%
add-sqr-sqrt28.7%
exp-prod28.4%
div-inv28.4%
frac-2neg28.4%
remove-double-neg28.4%
add-sqr-sqrt-0.0%
sqrt-unprod94.5%
sqr-neg94.5%
sqrt-unprod99.6%
add-sqr-sqrt99.6%
add-sqr-sqrt47.8%
fabs-sqr47.8%
add-sqr-sqrt61.9%
Applied egg-rr61.9%
*-lft-identity61.9%
Simplified61.9%
Final simplification61.9%
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.5%
Simplified99.6%
*-un-lft-identity99.6%
div-inv99.6%
exp-prod81.5%
add-sqr-sqrt81.5%
sqrt-unprod81.5%
sqr-neg81.5%
sqrt-unprod-0.0%
add-sqr-sqrt28.7%
exp-prod28.4%
div-inv28.4%
frac-2neg28.4%
remove-double-neg28.4%
add-sqr-sqrt-0.0%
sqrt-unprod94.5%
sqr-neg94.5%
sqrt-unprod99.6%
add-sqr-sqrt99.6%
add-sqr-sqrt47.8%
fabs-sqr47.8%
add-sqr-sqrt61.9%
Applied egg-rr61.9%
*-lft-identity61.9%
Simplified61.9%
Taylor expanded in s around 0 61.9%
mul-1-neg61.9%
Simplified61.9%
Final simplification61.9%
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.5%
associate-/r*99.6%
Simplified99.6%
Applied egg-rr68.3%
Taylor expanded in x around 0 58.9%
*-commutative58.9%
+-commutative58.9%
frac-times58.9%
metadata-eval58.9%
+-commutative58.9%
Applied egg-rr58.9%
Final simplification58.9%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= x 0.11999999731779099) (/ 0.25 s) (* (/ 0.5 s) (/ 1.0 (/ x s)))))
x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 0.11999999731779099f) {
tmp = 0.25f / s;
} else {
tmp = (0.5f / s) * (1.0f / (x / s));
}
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.11999999731779099e0) then
tmp = 0.25e0 / s
else
tmp = (0.5e0 / s) * (1.0e0 / (x / s))
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.11999999731779099)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(0.5) / s) * Float32(Float32(1.0) / Float32(x / s))); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.11999999731779099)) tmp = single(0.25) / s; else tmp = (single(0.5) / s) * (single(1.0) / (x / s)); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.11999999731779099:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{s} \cdot \frac{1}{\frac{x}{s}}\\
\end{array}
\end{array}
if x < 0.119999997Initial program 99.5%
associate-/r*99.6%
Simplified99.6%
Taylor expanded in s around inf 38.5%
if 0.119999997 < x Initial program 99.6%
associate-/r*99.6%
Simplified99.6%
Applied egg-rr3.1%
Taylor expanded in x around 0 97.5%
Taylor expanded in x around 0 43.6%
+-commutative43.6%
Simplified43.6%
Taylor expanded in x around inf 43.6%
Final simplification39.6%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= x 0.11999999731779099) (/ 0.25 s) (* (/ 0.5 s) (/ s x))))
x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 0.11999999731779099f) {
tmp = 0.25f / s;
} else {
tmp = (0.5f / s) * (s / 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.11999999731779099e0) then
tmp = 0.25e0 / s
else
tmp = (0.5e0 / s) * (s / x)
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.11999999731779099)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(0.5) / s) * Float32(s / x)); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.11999999731779099)) tmp = single(0.25) / s; else tmp = (single(0.5) / s) * (s / x); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.11999999731779099:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{s} \cdot \frac{s}{x}\\
\end{array}
\end{array}
if x < 0.119999997Initial program 99.5%
associate-/r*99.6%
Simplified99.6%
Taylor expanded in s around inf 38.5%
if 0.119999997 < x Initial program 99.6%
associate-/r*99.6%
Simplified99.6%
Applied egg-rr3.1%
Taylor expanded in x around 0 97.5%
Taylor expanded in x around 0 43.6%
+-commutative43.6%
Simplified43.6%
Taylor expanded in x around inf 34.3%
Final simplification37.5%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 0.5 (* s (+ (/ x s) 2.0))))
x = abs(x);
float code(float x, float s) {
return 0.5f / (s * ((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 / (s * ((x / s) + 2.0e0))
end function
x = abs(x) function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(x / s) + Float32(2.0)))) end
x = abs(x) function tmp = code(x, s) tmp = single(0.5) / (s * ((x / s) + single(2.0))); end
\begin{array}{l}
x = |x|\\
\\
\frac{0.5}{s \cdot \left(\frac{x}{s} + 2\right)}
\end{array}
Initial program 99.5%
associate-/r*99.6%
Simplified99.6%
Applied egg-rr68.3%
Taylor expanded in x around 0 58.9%
Taylor expanded in x around 0 49.4%
+-commutative49.4%
Simplified49.4%
frac-times49.4%
metadata-eval49.4%
Applied egg-rr49.4%
Final simplification49.4%
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.5%
associate-/r*99.6%
Simplified99.6%
Taylor expanded in s around inf 31.0%
Final simplification31.0%
herbie shell --seed 2023320
(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))))))