
(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 (let* ((t_0 (exp (/ (- (fabs x)) s)))) (/ t_0 (* (+ t_0 1.0) (fma s t_0 s)))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
return t_0 / ((t_0 + 1.0f) * fmaf(s, t_0, s));
}
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) return Float32(t_0 / Float32(Float32(t_0 + Float32(1.0)) * fma(s, t_0, s))) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
\frac{t\_0}{\left(t\_0 + 1\right) \cdot \mathsf{fma}\left(s, t\_0, s\right)}
\end{array}
\end{array}
Initial program 99.8%
*-commutative99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fma-define99.8%
fabs-neg99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (x s)
:precision binary32
(let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ t_0 1.0)))
(if (<= (/ t_0 (* t_1 (* s t_1))) 2.0000000063421537e-28)
(/ (exp (/ (exp (log x)) (- s))) (* s 4.0))
(/ 1.0 (/ s (exp (- (/ x s) (* 2.0 (log1p (exp (/ x s)))))))))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
float t_1 = t_0 + 1.0f;
float tmp;
if ((t_0 / (t_1 * (s * t_1))) <= 2.0000000063421537e-28f) {
tmp = expf((expf(logf(x)) / -s)) / (s * 4.0f);
} else {
tmp = 1.0f / (s / expf(((x / s) - (2.0f * log1pf(expf((x / s)))))));
}
return tmp;
}
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) t_1 = Float32(t_0 + Float32(1.0)) tmp = Float32(0.0) if (Float32(t_0 / Float32(t_1 * Float32(s * t_1))) <= Float32(2.0000000063421537e-28)) tmp = Float32(exp(Float32(exp(log(x)) / Float32(-s))) / Float32(s * Float32(4.0))); else tmp = Float32(Float32(1.0) / Float32(s / exp(Float32(Float32(x / s) - Float32(Float32(2.0) * log1p(exp(Float32(x / s)))))))); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := t\_0 + 1\\
\mathbf{if}\;\frac{t\_0}{t\_1 \cdot \left(s \cdot t\_1\right)} \leq 2.0000000063421537 \cdot 10^{-28}:\\
\;\;\;\;\frac{e^{\frac{e^{\log x}}{-s}}}{s \cdot 4}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{s}{e^{\frac{x}{s} - 2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}}\\
\end{array}
\end{array}
if (/.f32 (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)) (*.f32 (*.f32 s (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)))) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s))))) < 2.00000001e-28Initial program 99.9%
*-commutative99.9%
+-commutative99.9%
fabs-neg99.9%
distribute-lft-in99.9%
*-rgt-identity99.9%
fma-define99.9%
fabs-neg99.9%
Simplified99.9%
Taylor expanded in s around inf 99.6%
*-commutative99.6%
Simplified99.6%
add-sqr-sqrt55.7%
fabs-sqr55.7%
add-sqr-sqrt57.1%
add-exp-log55.7%
Applied egg-rr55.7%
if 2.00000001e-28 < (/.f32 (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)) (*.f32 (*.f32 s (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)))) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s))))) Initial program 99.2%
*-commutative99.2%
Simplified99.2%
associate-/r*99.2%
+-commutative99.2%
distribute-lft-in99.3%
*-rgt-identity99.3%
fma-undefine99.4%
associate-/r*99.3%
div-inv99.2%
*-commutative99.2%
Applied egg-rr97.9%
associate-*l/98.1%
*-un-lft-identity98.1%
*-commutative98.1%
+-commutative98.1%
associate-/r*97.9%
+-commutative97.9%
Applied egg-rr97.9%
clear-num98.0%
inv-pow98.0%
add-exp-log98.0%
log-div97.9%
add-log-exp97.9%
log-pow99.1%
+-commutative99.1%
log1p-define99.3%
Applied egg-rr99.3%
unpow-199.3%
Simplified99.3%
Final simplification67.1%
(FPCore (x s)
:precision binary32
(let* ((t_0 (exp (/ (- (fabs x)) s))) (t_1 (+ t_0 1.0)))
(if (<= (/ t_0 (* t_1 (* s t_1))) 2.0000000063421537e-28)
(/ (exp (/ (exp (log x)) (- s))) (* s 4.0))
(/ (exp (+ (/ x s) (* (log1p (exp (/ x s))) -2.0))) s))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
float t_1 = t_0 + 1.0f;
float tmp;
if ((t_0 / (t_1 * (s * t_1))) <= 2.0000000063421537e-28f) {
tmp = expf((expf(logf(x)) / -s)) / (s * 4.0f);
} else {
tmp = expf(((x / s) + (log1pf(expf((x / s))) * -2.0f))) / s;
}
return tmp;
}
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) t_1 = Float32(t_0 + Float32(1.0)) tmp = Float32(0.0) if (Float32(t_0 / Float32(t_1 * Float32(s * t_1))) <= Float32(2.0000000063421537e-28)) tmp = Float32(exp(Float32(exp(log(x)) / Float32(-s))) / Float32(s * Float32(4.0))); else tmp = Float32(exp(Float32(Float32(x / s) + Float32(log1p(exp(Float32(x / s))) * Float32(-2.0)))) / s); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := t\_0 + 1\\
\mathbf{if}\;\frac{t\_0}{t\_1 \cdot \left(s \cdot t\_1\right)} \leq 2.0000000063421537 \cdot 10^{-28}:\\
\;\;\;\;\frac{e^{\frac{e^{\log x}}{-s}}}{s \cdot 4}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{\frac{x}{s} + \mathsf{log1p}\left(e^{\frac{x}{s}}\right) \cdot -2}}{s}\\
\end{array}
\end{array}
if (/.f32 (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)) (*.f32 (*.f32 s (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)))) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s))))) < 2.00000001e-28Initial program 99.9%
*-commutative99.9%
+-commutative99.9%
fabs-neg99.9%
distribute-lft-in99.9%
*-rgt-identity99.9%
fma-define99.9%
fabs-neg99.9%
Simplified99.9%
Taylor expanded in s around inf 99.6%
*-commutative99.6%
Simplified99.6%
add-sqr-sqrt55.7%
fabs-sqr55.7%
add-sqr-sqrt57.1%
add-exp-log55.7%
Applied egg-rr55.7%
if 2.00000001e-28 < (/.f32 (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)) (*.f32 (*.f32 s (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)))) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s))))) Initial program 99.2%
*-commutative99.2%
Simplified99.2%
associate-/r*99.2%
+-commutative99.2%
distribute-lft-in99.3%
*-rgt-identity99.3%
fma-undefine99.4%
associate-/r*99.3%
div-inv99.2%
*-commutative99.2%
Applied egg-rr97.9%
Taylor expanded in s around 0 98.1%
+-commutative98.1%
*-commutative98.1%
exp-to-pow98.0%
+-commutative98.0%
log1p-undefine98.0%
*-commutative98.0%
rem-exp-log90.9%
exp-sum91.6%
exp-diff91.1%
associate--r+91.1%
exp-diff92.1%
Simplified99.2%
Final simplification67.1%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ x (- s))))) (/ (/ t_0 s) (pow (+ 1.0 t_0) 2.0))))
float code(float x, float s) {
float t_0 = expf((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((x / -s))
code = (t_0 / s) / ((1.0e0 + t_0) ** 2.0e0)
end function
function code(x, s) t_0 = exp(Float32(x / Float32(-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((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{x}{-s}}\\
\frac{\frac{t\_0}{s}}{{\left(1 + t\_0\right)}^{2}}
\end{array}
\end{array}
Initial program 99.8%
*-commutative99.8%
Simplified99.8%
Taylor expanded in x around 0 99.8%
associate-/r*99.8%
exp-prod99.8%
rem-square-sqrt54.9%
fabs-sqr54.9%
rem-square-sqrt66.3%
exp-prod66.3%
neg-mul-166.3%
distribute-neg-frac266.3%
+-commutative66.3%
exp-prod66.3%
rem-square-sqrt54.9%
fabs-sqr54.9%
rem-square-sqrt67.3%
exp-prod67.4%
neg-mul-167.4%
distribute-neg-frac267.4%
Simplified67.4%
Final simplification67.4%
(FPCore (x s) :precision binary32 (/ (exp (/ (exp (log x)) (- s))) (* s 4.0)))
float code(float x, float s) {
return expf((expf(logf(x)) / -s)) / (s * 4.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = exp((exp(log(x)) / -s)) / (s * 4.0e0)
end function
function code(x, s) return Float32(exp(Float32(exp(log(x)) / Float32(-s))) / Float32(s * Float32(4.0))) end
function tmp = code(x, s) tmp = exp((exp(log(x)) / -s)) / (s * single(4.0)); end
\begin{array}{l}
\\
\frac{e^{\frac{e^{\log x}}{-s}}}{s \cdot 4}
\end{array}
Initial program 99.8%
*-commutative99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fma-define99.8%
fabs-neg99.8%
Simplified99.8%
Taylor expanded in s around inf 95.7%
*-commutative95.7%
Simplified95.7%
add-sqr-sqrt52.8%
fabs-sqr52.8%
add-sqr-sqrt64.2%
add-exp-log52.8%
Applied egg-rr52.8%
Final simplification52.8%
(FPCore (x s) :precision binary32 (/ (exp (* x (/ -1.0 s))) (* s 4.0)))
float code(float x, float s) {
return expf((x * (-1.0f / s))) / (s * 4.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = exp((x * ((-1.0e0) / s))) / (s * 4.0e0)
end function
function code(x, s) return Float32(exp(Float32(x * Float32(Float32(-1.0) / s))) / Float32(s * Float32(4.0))) end
function tmp = code(x, s) tmp = exp((x * (single(-1.0) / s))) / (s * single(4.0)); end
\begin{array}{l}
\\
\frac{e^{x \cdot \frac{-1}{s}}}{s \cdot 4}
\end{array}
Initial program 99.8%
*-commutative99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fma-define99.8%
fabs-neg99.8%
Simplified99.8%
Taylor expanded in s around inf 95.7%
*-commutative95.7%
Simplified95.7%
frac-2neg95.7%
div-inv95.7%
remove-double-neg95.7%
add-sqr-sqrt52.8%
fabs-sqr52.8%
add-sqr-sqrt64.2%
Applied egg-rr64.2%
Taylor expanded in s around 0 64.2%
(FPCore (x s) :precision binary32 (/ (exp (/ x (- s))) (* s 4.0)))
float code(float x, float s) {
return expf((x / -s)) / (s * 4.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = exp((x / -s)) / (s * 4.0e0)
end function
function code(x, s) return Float32(exp(Float32(x / Float32(-s))) / Float32(s * Float32(4.0))) end
function tmp = code(x, s) tmp = exp((x / -s)) / (s * single(4.0)); end
\begin{array}{l}
\\
\frac{e^{\frac{x}{-s}}}{s \cdot 4}
\end{array}
Initial program 99.8%
*-commutative99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fma-define99.8%
fabs-neg99.8%
Simplified99.8%
Taylor expanded in s around inf 95.7%
*-commutative95.7%
Simplified95.7%
add-sqr-sqrt95.7%
sqrt-unprod93.8%
sqr-abs93.8%
sqr-neg93.8%
add-sqr-sqrt51.8%
fabs-sqr51.8%
add-sqr-sqrt60.7%
add-sqr-sqrt51.8%
fabs-sqr51.8%
add-sqr-sqrt93.8%
sqrt-unprod-0.0%
add-sqr-sqrt24.3%
neg-sub024.3%
add-sqr-sqrt12.9%
fabs-sqr12.9%
add-sqr-sqrt55.8%
sub-neg55.8%
add-sqr-sqrt12.9%
fabs-sqr12.9%
add-sqr-sqrt24.3%
add-sqr-sqrt-0.0%
sqrt-unprod93.8%
Applied egg-rr64.2%
+-lft-identity64.2%
Simplified64.2%
Final simplification64.2%
(FPCore (x s) :precision binary32 (/ (/ 0.25 (exp (/ x s))) s))
float code(float x, float s) {
return (0.25f / expf((x / s))) / s;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.25e0 / exp((x / s))) / s
end function
function code(x, s) return Float32(Float32(Float32(0.25) / exp(Float32(x / s))) / s) end
function tmp = code(x, s) tmp = (single(0.25) / exp((x / s))) / s; end
\begin{array}{l}
\\
\frac{\frac{0.25}{e^{\frac{x}{s}}}}{s}
\end{array}
Initial program 99.8%
*-commutative99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fma-define99.8%
fabs-neg99.8%
Simplified99.8%
Taylor expanded in s around inf 95.7%
*-commutative95.7%
Simplified95.7%
frac-2neg95.7%
div-inv95.7%
remove-double-neg95.7%
add-sqr-sqrt52.8%
fabs-sqr52.8%
add-sqr-sqrt64.2%
Applied egg-rr64.2%
Taylor expanded in x around inf 64.2%
associate-*r/64.2%
mul-1-neg64.2%
rec-exp64.1%
associate-*r/64.1%
metadata-eval64.1%
Simplified64.1%
(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.8%
*-commutative99.8%
Simplified99.8%
Taylor expanded in s around inf 26.9%
herbie shell --seed 2024191
(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))))))