
(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 12 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%
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 (/ (/ (exp (/ (- (fabs x)) s)) s) (pow (+ 1.0 (exp (/ (- x) s))) 2.0)))
float code(float x, float s) {
return (expf((-fabsf(x) / s)) / s) / powf((1.0f + expf((-x / s))), 2.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (exp((-abs(x) / s)) / s) / ((1.0e0 + exp((-x / s))) ** 2.0e0)
end function
function code(x, s) return Float32(Float32(exp(Float32(Float32(-abs(x)) / s)) / s) / (Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) ^ Float32(2.0))) end
function tmp = code(x, s) tmp = (exp((-abs(x) / s)) / s) / ((single(1.0) + exp((-x / s))) ^ single(2.0)); end
\begin{array}{l}
\\
\frac{\frac{e^{\frac{-\left|x\right|}{s}}}{s}}{{\left(1 + e^{\frac{-x}{s}}\right)}^{2}}
\end{array}
Initial program 99.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
fabs-neg99.6%
associate-*r*99.6%
/-rgt-identity99.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%
associate-*r/99.7%
mul-1-neg99.7%
Simplified99.6%
div-inv99.6%
exp-prod95.5%
neg-mul-195.5%
exp-prod95.5%
add-sqr-sqrt47.6%
fabs-sqr47.6%
add-sqr-sqrt95.0%
exp-prod95.0%
neg-mul-195.0%
exp-prod97.4%
div-inv97.4%
distribute-frac-neg97.4%
exp-neg97.4%
Applied egg-rr97.4%
expm1-log1p-u97.4%
expm1-udef97.4%
rec-exp97.4%
distribute-neg-frac97.4%
Applied egg-rr97.4%
expm1-def97.4%
expm1-log1p97.4%
Simplified97.4%
Final simplification97.4%
(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(abs(x) / Float32(-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%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
fabs-neg99.2%
associate-*r*99.2%
/-rgt-identity99.2%
Simplified99.2%
Applied egg-rr82.1%
associate-*r/82.0%
*-commutative82.0%
fma-udef82.0%
+-commutative82.0%
*-rgt-identity82.0%
distribute-lft-in81.9%
distribute-lft-out82.0%
*-rgt-identity82.0%
associate-*l*81.3%
distribute-lft-out81.2%
*-rgt-identity81.2%
distribute-lft-in81.1%
Simplified81.1%
Applied egg-rr81.0%
expm1-log1p-u78.0%
expm1-udef76.8%
Applied egg-rr94.7%
expm1-def95.8%
expm1-log1p99.2%
Simplified99.2%
if 1 < (fabs.f32 x) Initial program 100.0%
fabs-neg100.0%
*-commutative100.0%
fabs-neg100.0%
fabs-neg100.0%
associate-*r*100.0%
/-rgt-identity100.0%
Simplified100.0%
Taylor expanded in s around inf 100.0%
*-commutative100.0%
Simplified100.0%
Final simplification99.6%
(FPCore (x s) :precision binary32 (/ 1.0 (* (* s (+ 1.0 (exp (/ (- x) s)))) (+ 1.0 (exp (/ (fabs x) s))))))
float code(float x, float s) {
return 1.0f / ((s * (1.0f + expf((-x / s)))) * (1.0f + expf((fabsf(x) / s))));
}
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
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(s * Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))) * Float32(Float32(1.0) + exp(Float32(abs(x) / s))))) end
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}
\\
\frac{1}{\left(s \cdot \left(1 + e^{\frac{-x}{s}}\right)\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}
\end{array}
Initial program 99.6%
Simplified99.3%
*-un-lft-identity99.3%
neg-mul-199.3%
times-frac99.3%
metadata-eval99.3%
metadata-eval99.3%
times-frac99.3%
neg-mul-199.3%
*-un-lft-identity99.3%
distribute-frac-neg99.3%
rec-exp99.3%
add-sqr-sqrt50.2%
fabs-sqr50.2%
add-sqr-sqrt97.5%
Applied egg-rr97.5%
rec-exp97.5%
distribute-neg-frac97.5%
Simplified97.5%
Taylor expanded in s around 0 97.5%
associate-*r*97.5%
mul-1-neg97.5%
Simplified97.5%
Final simplification97.5%
(FPCore (x s) :precision binary32 (let* ((t_0 (/ 1.0 (exp (/ x s))))) (/ (/ t_0 s) (pow (+ 1.0 t_0) 2.0))))
float code(float x, float s) {
float t_0 = 1.0f / 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 = 1.0e0 / exp((x / s))
code = (t_0 / s) / ((1.0e0 + t_0) ** 2.0e0)
end function
function code(x, s) t_0 = Float32(Float32(1.0) / exp(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 = single(1.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 := \frac{1}{e^{\frac{x}{s}}}\\
\frac{\frac{t_0}{s}}{{\left(1 + t_0\right)}^{2}}
\end{array}
\end{array}
Initial program 99.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
fabs-neg99.6%
associate-*r*99.6%
/-rgt-identity99.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%
associate-*r/99.7%
mul-1-neg99.7%
Simplified99.6%
div-inv99.6%
exp-prod95.5%
neg-mul-195.5%
exp-prod95.5%
add-sqr-sqrt47.6%
fabs-sqr47.6%
add-sqr-sqrt95.0%
exp-prod95.0%
neg-mul-195.0%
exp-prod97.4%
div-inv97.4%
distribute-frac-neg97.4%
exp-neg97.4%
Applied egg-rr97.4%
div-inv99.6%
exp-prod95.5%
neg-mul-195.5%
exp-prod95.5%
add-sqr-sqrt47.6%
fabs-sqr47.6%
add-sqr-sqrt95.0%
exp-prod95.0%
neg-mul-195.0%
exp-prod97.4%
div-inv97.4%
distribute-frac-neg97.4%
exp-neg97.4%
Applied egg-rr61.9%
Final simplification61.9%
(FPCore (x s) :precision binary32 (/ 0.5 (* s (+ 1.0 (exp (/ (fabs x) s))))))
float code(float x, float s) {
return 0.5f / (s * (1.0f + expf((fabsf(x) / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (s * (1.0e0 + exp((abs(x) / s))))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(1.0) + exp(Float32(abs(x) / s))))) end
function tmp = code(x, s) tmp = single(0.5) / (s * (single(1.0) + exp((abs(x) / s)))); end
\begin{array}{l}
\\
\frac{0.5}{s \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}
\end{array}
Initial program 99.6%
Simplified99.3%
*-un-lft-identity99.3%
neg-mul-199.3%
times-frac99.3%
metadata-eval99.3%
metadata-eval99.3%
times-frac99.3%
neg-mul-199.3%
*-un-lft-identity99.3%
distribute-frac-neg99.3%
rec-exp99.3%
add-sqr-sqrt50.2%
fabs-sqr50.2%
add-sqr-sqrt97.5%
Applied egg-rr97.5%
rec-exp97.5%
distribute-neg-frac97.5%
Simplified97.5%
Taylor expanded in x around 0 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(abs(x) / Float32(-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%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
fabs-neg99.6%
associate-*r*99.6%
/-rgt-identity99.6%
Simplified99.6%
Taylor expanded in s around inf 94.9%
*-commutative94.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%
fabs-neg99.4%
*-commutative99.4%
fabs-neg99.4%
fabs-neg99.4%
associate-*r*99.4%
/-rgt-identity99.4%
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 (if (<= x 1.9999999494757503e-5) (/ 0.25 s) (/ (/ s x) x)))
float code(float x, float s) {
float tmp;
if (x <= 1.9999999494757503e-5f) {
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 <= 1.9999999494757503e-5) 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(1.9999999494757503e-5)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(s / x) / 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 = (s / x) / 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{\frac{s}{x}}{x}\\
\end{array}
\end{array}
if x < 1.99999995e-5Initial program 99.4%
fabs-neg99.4%
*-commutative99.4%
fabs-neg99.4%
fabs-neg99.4%
associate-*r*99.4%
/-rgt-identity99.4%
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%
associate-*l/71.1%
*-lft-identity71.1%
Simplified71.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%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
fabs-neg99.6%
associate-*r*99.6%
/-rgt-identity99.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))))))