
(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 7 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 (* s (+ (+ (exp (/ x s)) 2.0) (exp (/ (fabs x) (- s)))))))
x = abs(x);
float code(float x, float s) {
return 1.0f / (s * ((expf((x / s)) + 2.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 * ((exp((x / s)) + 2.0e0) + exp((abs(x) / -s))))
end function
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(exp(Float32(x / s)) + Float32(2.0)) + exp(Float32(abs(x) / Float32(-s)))))) end
x = abs(x) function tmp = code(x, s) tmp = single(1.0) / (s * ((exp((x / s)) + single(2.0)) + exp((abs(x) / -s)))); end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{s \cdot \left(\left(e^{\frac{x}{s}} + 2\right) + e^{\frac{\left|x\right|}{-s}}\right)}
\end{array}
Initial program 99.3%
Simplified99.0%
*-un-lft-identity99.0%
exp-prod99.1%
exp-1-e99.1%
add-sqr-sqrt46.8%
fabs-sqr46.8%
add-sqr-sqrt60.3%
Applied egg-rr60.3%
expm1-log1p-u58.8%
expm1-udef58.9%
pow-to-exp58.8%
e-exp-158.8%
add-log-exp58.9%
*-un-lft-identity58.9%
+-commutative58.9%
Applied egg-rr58.9%
expm1-def58.9%
expm1-log1p60.3%
associate-/l/60.3%
*-commutative60.3%
associate-+r+60.3%
Simplified60.3%
Final simplification60.3%
NOTE: x should be positive before calling this function
(FPCore (x s)
:precision binary32
(let* ((t_0 (exp (/ x s))))
(if (<= (fabs x) 1.9999999494757503e-5)
(/ (exp (+ (/ x s) (* -2.0 (log1p t_0)))) s)
(/ (/ 1.0 s) (+ t_0 3.0)))))x = abs(x);
float code(float x, float s) {
float t_0 = expf((x / s));
float tmp;
if (fabsf(x) <= 1.9999999494757503e-5f) {
tmp = expf(((x / s) + (-2.0f * log1pf(t_0)))) / s;
} else {
tmp = (1.0f / s) / (t_0 + 3.0f);
}
return tmp;
}
x = abs(x) function code(x, s) t_0 = exp(Float32(x / s)) tmp = Float32(0.0) if (abs(x) <= Float32(1.9999999494757503e-5)) tmp = Float32(exp(Float32(Float32(x / s) + Float32(Float32(-2.0) * log1p(t_0)))) / s); else tmp = Float32(Float32(Float32(1.0) / s) / Float32(t_0 + Float32(3.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 1.9999999494757503 \cdot 10^{-5}:\\
\;\;\;\;\frac{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(t_0\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{s}}{t_0 + 3}\\
\end{array}
\end{array}
if (fabs.f32 x) < 1.99999995e-5Initial program 98.5%
associate-*l*98.5%
+-commutative98.5%
+-commutative98.5%
Simplified98.5%
add-exp-log94.5%
log-div94.5%
add-log-exp95.1%
add-sqr-sqrt-0.0%
sqrt-unprod47.4%
sqr-neg47.4%
sqrt-unprod47.1%
add-sqr-sqrt47.1%
add-sqr-sqrt22.6%
fabs-sqr22.6%
add-sqr-sqrt72.0%
*-commutative72.0%
Applied egg-rr94.3%
associate--r+94.4%
exp-diff94.4%
cancel-sign-sub-inv94.4%
metadata-eval94.4%
rem-exp-log98.8%
Simplified98.8%
if 1.99999995e-5 < (fabs.f32 x) Initial program 100.0%
Simplified100.0%
Taylor expanded in s around inf 100.0%
add-sqr-sqrt100.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-unprod-0.0%
add-sqr-sqrt4.6%
expm1-log1p-u4.6%
expm1-udef4.6%
Applied egg-rr48.6%
associate--l+48.6%
metadata-eval48.6%
+-rgt-identity48.6%
Simplified48.6%
Final simplification71.5%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (* s (+ 3.0 (exp (/ (fabs x) s))))))
x = abs(x);
float code(float x, float s) {
return 1.0f / (s * (3.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 * (3.0e0 + exp((abs(x) / s))))
end function
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(3.0) + exp(Float32(abs(x) / s))))) end
x = abs(x) function tmp = code(x, s) tmp = single(1.0) / (s * (single(3.0) + exp((abs(x) / s)))); end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{s \cdot \left(3 + e^{\frac{\left|x\right|}{s}}\right)}
\end{array}
Initial program 99.3%
Simplified99.0%
Taylor expanded in s around inf 95.1%
Taylor expanded in s around 0 95.5%
Final simplification95.5%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ (/ 1.0 s) (+ (exp (/ x s)) 3.0)))
x = abs(x);
float code(float x, float s) {
return (1.0f / s) / (expf((x / s)) + 3.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 = (1.0e0 / s) / (exp((x / s)) + 3.0e0)
end function
x = abs(x) function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(exp(Float32(x / s)) + Float32(3.0))) end
x = abs(x) function tmp = code(x, s) tmp = (single(1.0) / s) / (exp((x / s)) + single(3.0)); end
\begin{array}{l}
x = |x|\\
\\
\frac{\frac{1}{s}}{e^{\frac{x}{s}} + 3}
\end{array}
Initial program 99.3%
Simplified99.0%
Taylor expanded in s around inf 95.1%
add-sqr-sqrt95.1%
sqrt-unprod92.1%
sqr-neg92.1%
sqrt-unprod-0.0%
add-sqr-sqrt26.1%
expm1-log1p-u26.1%
expm1-udef26.1%
Applied egg-rr58.7%
associate--l+58.7%
metadata-eval58.7%
+-rgt-identity58.7%
Simplified58.7%
Final simplification58.7%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= x 1.9999999494757503e-5) (/ (+ 0.25 (* (* (/ x s) (/ x s)) -0.0625)) s) (/ 1.0 (* x (* (/ x s) 0.5)))))
x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 1.9999999494757503e-5f) {
tmp = (0.25f + (((x / s) * (x / s)) * -0.0625f)) / s;
} else {
tmp = 1.0f / (x * ((x / s) * 0.5f));
}
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 <= 1.9999999494757503e-5) then
tmp = (0.25e0 + (((x / s) * (x / s)) * (-0.0625e0))) / s
else
tmp = 1.0e0 / (x * ((x / s) * 0.5e0))
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.9999999494757503e-5)) tmp = Float32(Float32(Float32(0.25) + Float32(Float32(Float32(x / s) * Float32(x / s)) * Float32(-0.0625))) / s); else tmp = Float32(Float32(1.0) / Float32(x * Float32(Float32(x / s) * Float32(0.5)))); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(1.9999999494757503e-5)) tmp = (single(0.25) + (((x / s) * (x / s)) * single(-0.0625))) / s; else tmp = single(1.0) / (x * ((x / s) * single(0.5))); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.9999999494757503 \cdot 10^{-5}:\\
\;\;\;\;\frac{0.25 + \left(\frac{x}{s} \cdot \frac{x}{s}\right) \cdot -0.0625}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x \cdot \left(\frac{x}{s} \cdot 0.5\right)}\\
\end{array}
\end{array}
if x < 1.99999995e-5Initial program 99.1%
associate-*l*99.1%
+-commutative99.1%
+-commutative99.1%
Simplified99.1%
*-un-lft-identity99.1%
times-frac98.6%
add-sqr-sqrt-0.0%
sqrt-unprod31.5%
sqr-neg31.5%
sqrt-unprod31.2%
add-sqr-sqrt31.2%
add-sqr-sqrt14.3%
fabs-sqr14.3%
add-sqr-sqrt84.0%
pow284.0%
Applied egg-rr86.7%
Taylor expanded in x around 0 31.3%
*-commutative31.3%
unpow231.3%
unpow231.3%
Simplified31.3%
associate-*l/31.3%
*-un-lft-identity31.3%
Applied egg-rr31.3%
Taylor expanded in x around 0 31.3%
unpow231.3%
unpow231.3%
times-frac35.8%
Simplified35.8%
if 1.99999995e-5 < x Initial program 100.0%
Simplified100.0%
*-un-lft-identity100.0%
exp-prod100.0%
exp-1-e100.0%
add-sqr-sqrt100.0%
fabs-sqr100.0%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
expm1-log1p-u100.0%
expm1-udef100.0%
pow-to-exp100.0%
e-exp-1100.0%
add-log-exp100.0%
*-un-lft-identity100.0%
+-commutative100.0%
Applied egg-rr100.0%
expm1-def100.0%
expm1-log1p100.0%
associate-/l/100.0%
*-commutative100.0%
associate-+r+100.0%
Simplified100.0%
Taylor expanded in x around 0 81.4%
unpow281.4%
unpow281.4%
times-frac81.4%
Simplified81.4%
Taylor expanded in s around 0 66.5%
*-commutative66.5%
unpow266.5%
associate-*r/66.5%
associate-*l*66.5%
*-commutative66.5%
Simplified66.5%
Final simplification43.5%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= x 1.9999999494757503e-5) (/ 0.25 s) (/ 1.0 (* x (* (/ x s) 0.5)))))
x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 1.9999999494757503e-5f) {
tmp = 0.25f / s;
} else {
tmp = 1.0f / (x * ((x / s) * 0.5f));
}
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 <= 1.9999999494757503e-5) then
tmp = 0.25e0 / s
else
tmp = 1.0e0 / (x * ((x / s) * 0.5e0))
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.9999999494757503e-5)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(1.0) / Float32(x * Float32(Float32(x / s) * Float32(0.5)))); end return tmp end
x = abs(x) 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 * ((x / s) * single(0.5))); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.9999999494757503 \cdot 10^{-5}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x \cdot \left(\frac{x}{s} \cdot 0.5\right)}\\
\end{array}
\end{array}
if x < 1.99999995e-5Initial program 99.1%
associate-*l*99.1%
+-commutative99.1%
+-commutative99.1%
Simplified99.1%
Taylor expanded in s around inf 35.8%
if 1.99999995e-5 < x Initial program 100.0%
Simplified100.0%
*-un-lft-identity100.0%
exp-prod100.0%
exp-1-e100.0%
add-sqr-sqrt100.0%
fabs-sqr100.0%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
expm1-log1p-u100.0%
expm1-udef100.0%
pow-to-exp100.0%
e-exp-1100.0%
add-log-exp100.0%
*-un-lft-identity100.0%
+-commutative100.0%
Applied egg-rr100.0%
expm1-def100.0%
expm1-log1p100.0%
associate-/l/100.0%
*-commutative100.0%
associate-+r+100.0%
Simplified100.0%
Taylor expanded in x around 0 81.4%
unpow281.4%
unpow281.4%
times-frac81.4%
Simplified81.4%
Taylor expanded in s around 0 66.5%
*-commutative66.5%
unpow266.5%
associate-*r/66.5%
associate-*l*66.5%
*-commutative66.5%
Simplified66.5%
Final simplification43.5%
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.3%
associate-*l*99.3%
+-commutative99.3%
+-commutative99.3%
Simplified99.3%
Taylor expanded in s around inf 28.0%
Final simplification28.0%
herbie shell --seed 2023224
(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))))))