
(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 (let* ((t_0 (exp (/ (- (fabs x)) s)))) (/ (/ t_0 (+ t_0 1.0)) (+ s (/ s (exp (/ (fabs x) s)))))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
return (t_0 / (t_0 + 1.0f)) / (s + (s / expf((fabsf(x) / s))));
}
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 / (t_0 + 1.0e0)) / (s + (s / exp((abs(x) / s))))
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) return Float32(Float32(t_0 / Float32(t_0 + Float32(1.0))) / Float32(s + Float32(s / exp(Float32(abs(x) / s))))) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); tmp = (t_0 / (t_0 + single(1.0))) / (s + (s / exp((abs(x) / s)))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
\frac{\frac{t_0}{t_0 + 1}}{s + \frac{s}{e^{\frac{\left|x\right|}{s}}}}
\end{array}
\end{array}
Initial program 99.6%
*-commutative99.6%
distribute-lft-in99.6%
*-rgt-identity99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
exp-neg99.6%
associate-*r/99.6%
*-rgt-identity99.6%
*-lft-identity99.6%
metadata-eval99.6%
times-frac99.6%
neg-mul-199.6%
neg-mul-199.6%
fabs-neg99.6%
Simplified99.6%
Taylor expanded in x around 0 99.6%
associate-/r*99.6%
associate-*r/99.6%
mul-1-neg99.6%
+-commutative99.6%
associate-*r/99.6%
mul-1-neg99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x s) :precision binary32 (if (<= (fabs x) 1.0) (/ (exp (+ (/ x s) (* -2.0 (log1p (exp (/ x s)))))) s) (/ (exp (/ (- (fabs x)) s)) (* s 4.0))))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 1.0f) {
tmp = expf(((x / s) + (-2.0f * log1pf(expf((x / s)))))) / s;
} else {
tmp = expf((-fabsf(x) / s)) / (s * 4.0f);
}
return tmp;
}
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(1.0)) tmp = Float32(exp(Float32(Float32(x / s) + Float32(Float32(-2.0) * log1p(exp(Float32(x / s)))))) / s); else tmp = Float32(exp(Float32(Float32(-abs(x)) / s)) / Float32(s * Float32(4.0))); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 1:\\
\;\;\;\;\frac{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{\frac{-\left|x\right|}{s}}}{s \cdot 4}\\
\end{array}
\end{array}
if (fabs.f32 x) < 1Initial program 99.2%
*-commutative99.2%
distribute-lft-in99.1%
*-rgt-identity99.1%
fabs-neg99.1%
+-commutative99.1%
fma-def99.2%
fabs-neg99.2%
Simplified99.2%
Applied egg-rr76.8%
expm1-def78.0%
expm1-log1p81.3%
associate-/l/81.1%
+-commutative81.1%
Simplified81.1%
expm1-log1p-u78.0%
expm1-udef76.9%
Applied egg-rr94.7%
expm1-def95.8%
expm1-log1p99.2%
cancel-sign-sub-inv99.2%
metadata-eval99.2%
Simplified99.2%
if 1 < (fabs.f32 x) Initial program 100.0%
*-commutative100.0%
distribute-lft-in100.0%
*-rgt-identity100.0%
fabs-neg100.0%
+-commutative100.0%
fma-def100.0%
fabs-neg100.0%
Simplified100.0%
Taylor expanded in s around inf 100.0%
*-commutative100.0%
Simplified100.0%
Final simplification99.6%
(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(Float32(-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((-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.6%
*-commutative99.6%
distribute-lft-in99.6%
*-rgt-identity99.6%
fabs-neg99.6%
+-commutative99.6%
fma-def99.6%
fabs-neg99.6%
Simplified99.6%
Taylor expanded in x around 0 99.6%
*-commutative99.6%
*-rgt-identity99.6%
distribute-lft-in99.6%
mul-1-neg99.6%
rec-exp99.6%
associate-*r*99.6%
associate-/r*99.7%
Simplified99.6%
distribute-frac-neg99.6%
rec-exp99.6%
add-sqr-sqrt50.2%
fabs-sqr50.2%
add-sqr-sqrt97.4%
Applied egg-rr97.4%
rec-exp97.4%
distribute-neg-frac97.4%
Simplified97.4%
distribute-frac-neg99.6%
rec-exp99.6%
add-sqr-sqrt50.2%
fabs-sqr50.2%
add-sqr-sqrt97.4%
Applied egg-rr61.8%
rec-exp97.4%
distribute-neg-frac97.4%
Simplified61.8%
Final simplification61.8%
(FPCore (x s) :precision binary32 (/ 1.0 (* (fma s 1.0 s) (+ 1.0 (exp (/ (fabs x) s))))))
float code(float x, float s) {
return 1.0f / (fmaf(s, 1.0f, s) * (1.0f + expf((fabsf(x) / s))));
}
function code(x, s) return Float32(Float32(1.0) / Float32(fma(s, Float32(1.0), s) * Float32(Float32(1.0) + exp(Float32(abs(x) / s))))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(s, 1, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}
\end{array}
Initial program 99.6%
Simplified99.3%
Taylor expanded in s around inf 95.2%
Final simplification95.2%
(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.6%
*-commutative99.6%
distribute-lft-in99.6%
*-rgt-identity99.6%
fabs-neg99.6%
+-commutative99.6%
fma-def99.6%
fabs-neg99.6%
Simplified99.6%
Taylor expanded in s around inf 94.9%
*-commutative94.9%
Simplified94.9%
Final simplification94.9%
(FPCore (x s) :precision binary32 (/ (* (exp (/ (- (fabs x)) s)) 0.25) s))
float code(float x, float s) {
return (expf((-fabsf(x) / s)) * 0.25f) / s;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (exp((-abs(x) / s)) * 0.25e0) / s
end function
function code(x, s) return Float32(Float32(exp(Float32(Float32(-abs(x)) / s)) * Float32(0.25)) / s) end
function tmp = code(x, s) tmp = (exp((-abs(x) / s)) * single(0.25)) / s; end
\begin{array}{l}
\\
\frac{e^{\frac{-\left|x\right|}{s}} \cdot 0.25}{s}
\end{array}
Initial program 99.6%
*-commutative99.6%
distribute-lft-in99.6%
*-rgt-identity99.6%
fabs-neg99.6%
+-commutative99.6%
fma-def99.6%
fabs-neg99.6%
Simplified99.6%
Taylor expanded in x around 0 99.6%
*-commutative99.6%
*-rgt-identity99.6%
distribute-lft-in99.6%
mul-1-neg99.6%
rec-exp99.6%
associate-*r*99.6%
associate-/r*99.7%
Simplified99.6%
distribute-frac-neg99.6%
rec-exp99.6%
add-sqr-sqrt50.2%
fabs-sqr50.2%
add-sqr-sqrt97.4%
Applied egg-rr97.4%
rec-exp97.4%
distribute-neg-frac97.4%
Simplified97.4%
Taylor expanded in x around 0 94.9%
associate-*r/94.9%
associate-*r/94.9%
mul-1-neg94.9%
Simplified94.9%
Final simplification94.9%
(FPCore (x s) :precision binary32 (/ 1.0 (+ (* s 4.0) (/ 1.0 (/ s (pow x 2.0))))))
float code(float x, float s) {
return 1.0f / ((s * 4.0f) + (1.0f / (s / powf(x, 2.0f))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((s * 4.0e0) + (1.0e0 / (s / (x ** 2.0e0))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(Float32(1.0) / Float32(s / (x ^ Float32(2.0)))))) end
function tmp = code(x, s) tmp = single(1.0) / ((s * single(4.0)) + (single(1.0) / (s / (x ^ single(2.0))))); end
\begin{array}{l}
\\
\frac{1}{s \cdot 4 + \frac{1}{\frac{s}{{x}^{2}}}}
\end{array}
Initial program 99.6%
Simplified99.3%
Taylor expanded in s around -inf 43.0%
+-commutative43.0%
mul-1-neg43.0%
distribute-lft1-in64.9%
metadata-eval64.9%
associate-*r/64.9%
mul-1-neg64.9%
remove-double-neg64.9%
associate-+r+64.9%
Simplified65.3%
clear-num65.3%
inv-pow65.3%
pow265.3%
Applied egg-rr65.3%
unpow-165.3%
Simplified65.3%
Final simplification65.3%
(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(Float32(x * 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 + \frac{x \cdot x}{s}}
\end{array}
Initial program 99.6%
Simplified99.3%
Taylor expanded in s around -inf 43.0%
+-commutative43.0%
mul-1-neg43.0%
distribute-lft1-in64.9%
metadata-eval64.9%
associate-*r/64.9%
mul-1-neg64.9%
remove-double-neg64.9%
associate-+r+64.9%
Simplified65.3%
Final simplification65.3%
(FPCore (x s) :precision binary32 (if (<= x 1.9999999494757503e-5) (/ 0.25 s) (* (/ 1.0 x) (/ s x))))
float code(float x, float s) {
float tmp;
if (x <= 1.9999999494757503e-5f) {
tmp = 0.25f / s;
} else {
tmp = (1.0f / x) * (s / x);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 1.9999999494757503e-5) then
tmp = 0.25e0 / s
else
tmp = (1.0e0 / x) * (s / x)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.9999999494757503e-5)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(1.0) / x) * Float32(s / x)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(1.9999999494757503e-5)) tmp = single(0.25) / s; else tmp = (single(1.0) / x) * (s / x); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.9999999494757503 \cdot 10^{-5}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x} \cdot \frac{s}{x}\\
\end{array}
\end{array}
if x < 1.99999995e-5Initial program 99.4%
*-commutative99.4%
distribute-lft-in99.4%
*-rgt-identity99.4%
fabs-neg99.4%
+-commutative99.4%
fma-def99.5%
fabs-neg99.5%
Simplified99.5%
Taylor expanded in s around inf 35.3%
if 1.99999995e-5 < x Initial program 100.0%
Simplified100.0%
Taylor expanded in s around -inf 36.2%
+-commutative36.2%
mul-1-neg36.2%
distribute-lft1-in72.7%
metadata-eval72.7%
associate-*r/72.7%
mul-1-neg72.7%
remove-double-neg72.7%
associate-+r+72.7%
Simplified74.1%
Taylor expanded in s around 0 71.1%
*-un-lft-identity71.1%
unpow271.1%
times-frac71.1%
Applied egg-rr71.1%
Final simplification45.6%
(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%
*-commutative99.6%
distribute-lft-in99.6%
*-rgt-identity99.6%
fabs-neg99.6%
+-commutative99.6%
fma-def99.6%
fabs-neg99.6%
Simplified99.6%
Taylor expanded in s around inf 26.3%
Final simplification26.3%
herbie shell --seed 2023319
(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))))))