
(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}
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (* (+ 1.0 (exp (/ (fabs x) (- s)))) (fma s (pow E (/ x s)) s))))
x = abs(x);
float code(float x, float s) {
return 1.0f / ((1.0f + expf((fabsf(x) / -s))) * fmaf(s, powf(((float) M_E), (x / s)), s));
}
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(abs(x) / Float32(-s)))) * fma(s, (Float32(exp(1)) ^ Float32(x / s)), s))) end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{\left(1 + e^{\frac{\left|x\right|}{-s}}\right) \cdot \mathsf{fma}\left(s, {e}^{\left(\frac{x}{s}\right)}, s\right)}
\end{array}
Initial program 99.3%
*-lft-identity99.3%
associate-*r/99.3%
associate-/l*99.4%
distribute-frac-neg99.4%
exp-neg99.3%
associate-/r/99.3%
/-rgt-identity99.3%
associate-*l*99.3%
Simplified99.4%
div-inv99.4%
exp-prod67.4%
add-sqr-sqrt67.4%
sqrt-unprod67.4%
sqr-neg67.4%
sqrt-unprod-0.0%
add-sqr-sqrt13.3%
exp-prod13.0%
div-inv13.0%
add-sqr-sqrt-0.0%
sqrt-unprod99.4%
frac-times80.7%
sqr-neg80.7%
sqr-neg80.7%
frac-times99.4%
sqrt-unprod-0.0%
Applied egg-rr56.3%
Final simplification56.3%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (* (+ 1.0 (exp (/ (fabs x) (- s)))) (+ s (* s (pow E (/ x s)))))))
x = abs(x);
float code(float x, float s) {
return 1.0f / ((1.0f + expf((fabsf(x) / -s))) * (s + (s * powf(((float) M_E), (x / s)))));
}
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(abs(x) / Float32(-s)))) * Float32(s + Float32(s * (Float32(exp(1)) ^ Float32(x / s)))))) end
x = abs(x) function tmp = code(x, s) tmp = single(1.0) / ((single(1.0) + exp((abs(x) / -s))) * (s + (s * (single(2.71828182845904523536) ^ (x / s))))); end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{\left(1 + e^{\frac{\left|x\right|}{-s}}\right) \cdot \left(s + s \cdot {e}^{\left(\frac{x}{s}\right)}\right)}
\end{array}
Initial program 99.3%
*-lft-identity99.3%
associate-*r/99.3%
associate-/l*99.4%
distribute-frac-neg99.4%
exp-neg99.3%
associate-/r/99.3%
/-rgt-identity99.3%
associate-*l*99.3%
Simplified99.4%
fma-udef99.4%
Applied egg-rr56.2%
*-un-lft-identity56.2%
pow-exp56.2%
e-exp-156.2%
Applied egg-rr56.2%
Final simplification56.2%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (* s (+ (exp (/ x s)) (+ (exp (/ (fabs x) (- s))) 2.0)))))
x = abs(x);
float code(float x, float s) {
return 1.0f / (s * (expf((x / s)) + (expf((fabsf(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 = 1.0e0 / (s * (exp((x / s)) + (exp((abs(x) / -s)) + 2.0e0)))
end function
x = abs(x) function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(exp(Float32(x / s)) + Float32(exp(Float32(abs(x) / Float32(-s))) + Float32(2.0))))) end
x = abs(x) function tmp = code(x, s) tmp = single(1.0) / (s * (exp((x / s)) + (exp((abs(x) / -s)) + single(2.0)))); end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{s \cdot \left(e^{\frac{x}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)\right)}
\end{array}
Initial program 99.3%
Simplified99.0%
div-inv99.4%
exp-prod67.4%
add-sqr-sqrt67.4%
sqrt-unprod67.4%
sqr-neg67.4%
sqrt-unprod-0.0%
add-sqr-sqrt13.3%
exp-prod13.0%
div-inv13.0%
add-sqr-sqrt-0.0%
sqrt-unprod99.4%
frac-times80.7%
sqr-neg80.7%
sqr-neg80.7%
frac-times99.4%
sqrt-unprod-0.0%
Applied egg-rr56.0%
expm1-log1p-u54.8%
expm1-udef54.8%
associate-/l/55.2%
pow-to-exp55.2%
e-exp-155.2%
add-log-exp55.2%
*-un-lft-identity55.2%
Applied egg-rr55.2%
expm1-def55.2%
expm1-log1p56.4%
*-commutative56.4%
+-commutative56.4%
Simplified56.4%
Final simplification56.4%
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 94.6%
Taylor expanded in s around 0 95.0%
Final simplification95.0%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (/ 1.0 (fma s 4.0 (* x (/ x s)))))
x = abs(x);
float code(float x, float s) {
return 1.0f / fmaf(s, 4.0f, (x * (x / s)));
}
x = abs(x) function code(x, s) return Float32(Float32(1.0) / fma(s, Float32(4.0), Float32(x * Float32(x / s)))) end
\begin{array}{l}
x = |x|\\
\\
\frac{1}{\mathsf{fma}\left(s, 4, x \cdot \frac{x}{s}\right)}
\end{array}
Initial program 99.3%
*-lft-identity99.3%
associate-*r/99.3%
associate-/l*99.4%
distribute-frac-neg99.4%
exp-neg99.3%
associate-/r/99.3%
/-rgt-identity99.3%
associate-*l*99.3%
Simplified99.4%
Taylor expanded in s around -inf 37.1%
associate-+r+37.1%
distribute-rgt-out37.1%
metadata-eval37.1%
*-commutative37.1%
fma-def37.1%
mul-1-neg37.1%
distribute-rgt1-in60.9%
metadata-eval60.9%
associate-*r/60.9%
mul-1-neg60.9%
remove-double-neg60.9%
unpow260.9%
sqr-abs60.9%
Simplified60.9%
expm1-log1p-u60.0%
expm1-udef73.4%
add-log-exp68.2%
mul0-rgt68.2%
exp-sum68.2%
1-exp68.2%
*-un-lft-identity68.2%
add-log-exp73.4%
associate-/l*74.3%
Applied egg-rr74.3%
expm1-def61.0%
expm1-log1p62.3%
associate-/r/62.3%
*-commutative62.3%
Simplified62.3%
Final simplification62.3%
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 94.6%
div-inv94.6%
exp-prod69.1%
add-sqr-sqrt69.1%
sqrt-unprod69.1%
sqr-neg69.1%
sqrt-unprod-0.0%
add-sqr-sqrt15.4%
exp-prod13.6%
div-inv13.6%
distribute-frac-neg13.6%
rec-exp13.6%
*-un-lft-identity13.6%
rec-exp13.6%
distribute-frac-neg13.6%
add-sqr-sqrt-0.0%
sqrt-unprod88.3%
sqr-neg88.3%
sqrt-unprod94.6%
add-sqr-sqrt94.6%
Applied egg-rr53.6%
*-lft-identity53.6%
Simplified53.6%
Final simplification53.6%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= x 3.7000000840947678e-9) (/ 0.25 s) (* s (/ 1.0 (* x x)))))
x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 3.7000000840947678e-9f) {
tmp = 0.25f / s;
} else {
tmp = s * (1.0f / (x * 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 <= 3.7000000840947678e-9) then
tmp = 0.25e0 / s
else
tmp = s * (1.0e0 / (x * x))
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(3.7000000840947678e-9)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(s * Float32(Float32(1.0) / Float32(x * x))); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(3.7000000840947678e-9)) tmp = single(0.25) / s; else tmp = s * (single(1.0) / (x * x)); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.7000000840947678 \cdot 10^{-9}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;s \cdot \frac{1}{x \cdot x}\\
\end{array}
\end{array}
if x < 3.70000008e-9Initial program 99.0%
Taylor expanded in s around inf 20.2%
if 3.70000008e-9 < x Initial program 100.0%
*-lft-identity100.0%
associate-*r/100.0%
associate-/l*100.0%
distribute-frac-neg100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
associate-*l*100.0%
Simplified100.0%
Taylor expanded in s around -inf 42.9%
associate-+r+42.9%
distribute-rgt-out42.9%
metadata-eval42.9%
*-commutative42.9%
fma-def42.9%
mul-1-neg42.9%
distribute-rgt1-in80.2%
metadata-eval80.2%
associate-*r/80.2%
mul-1-neg80.2%
remove-double-neg80.2%
unpow280.2%
sqr-abs80.2%
Simplified80.2%
Taylor expanded in x around inf 77.6%
unpow277.6%
Simplified77.6%
div-inv77.6%
Applied egg-rr77.6%
Final simplification37.0%
NOTE: x should be positive before calling this function (FPCore (x s) :precision binary32 (if (<= x 3.7000000840947678e-9) (/ 0.25 s) (/ s (* x x))))
x = abs(x);
float code(float x, float s) {
float tmp;
if (x <= 3.7000000840947678e-9f) {
tmp = 0.25f / s;
} else {
tmp = s / (x * 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 <= 3.7000000840947678e-9) then
tmp = 0.25e0 / s
else
tmp = s / (x * x)
end if
code = tmp
end function
x = abs(x) function code(x, s) tmp = Float32(0.0) if (x <= Float32(3.7000000840947678e-9)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(s / Float32(x * x)); end return tmp end
x = abs(x) function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(3.7000000840947678e-9)) tmp = single(0.25) / s; else tmp = s / (x * x); end tmp_2 = tmp; end
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.7000000840947678 \cdot 10^{-9}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{s}{x \cdot x}\\
\end{array}
\end{array}
if x < 3.70000008e-9Initial program 99.0%
Taylor expanded in s around inf 20.2%
if 3.70000008e-9 < x Initial program 100.0%
*-lft-identity100.0%
associate-*r/100.0%
associate-/l*100.0%
distribute-frac-neg100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
associate-*l*100.0%
Simplified100.0%
Taylor expanded in s around -inf 42.9%
associate-+r+42.9%
distribute-rgt-out42.9%
metadata-eval42.9%
*-commutative42.9%
fma-def42.9%
mul-1-neg42.9%
distribute-rgt1-in80.2%
metadata-eval80.2%
associate-*r/80.2%
mul-1-neg80.2%
remove-double-neg80.2%
unpow280.2%
sqr-abs80.2%
Simplified80.2%
Taylor expanded in x around inf 77.6%
unpow277.6%
Simplified77.6%
Final simplification37.0%
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%
Taylor expanded in s around inf 15.4%
Final simplification15.4%
herbie shell --seed 2023278
(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))))))