
(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}
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ (exp (/ (fabs x_m) (- s))) s) (pow (+ 1.0 (exp (/ x_m (- s)))) 2.0)))
x_m = fabs(x);
float code(float x_m, float s) {
return (expf((fabsf(x_m) / -s)) / s) / powf((1.0f + expf((x_m / -s))), 2.0f);
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = (exp((abs(x_m) / -s)) / s) / ((1.0e0 + exp((x_m / -s))) ** 2.0e0)
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(exp(Float32(abs(x_m) / Float32(-s))) / s) / (Float32(Float32(1.0) + exp(Float32(x_m / Float32(-s)))) ^ Float32(2.0))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (exp((abs(x_m) / -s)) / s) / ((single(1.0) + exp((x_m / -s))) ^ single(2.0)); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{e^{\frac{\left|x\_m\right|}{-s}}}{s}}{{\left(1 + e^{\frac{x\_m}{-s}}\right)}^{2}}
\end{array}
Initial program 99.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.3%
Taylor expanded in x around 0 99.3%
associate-/r*99.3%
mul-1-neg99.3%
distribute-neg-frac299.3%
+-commutative99.3%
mul-1-neg99.3%
distribute-neg-frac299.3%
Simplified99.3%
distribute-frac-neg299.3%
rec-exp99.3%
pow199.3%
pow199.3%
add-sqr-sqrt51.0%
fabs-sqr51.0%
add-sqr-sqrt97.3%
Applied egg-rr97.3%
rec-exp97.3%
distribute-neg-frac297.3%
Simplified97.3%
Final simplification97.3%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= (fabs x_m) 0.0003000000142492354) (/ 1.0 (* s (exp (- (* 2.0 (log1p (exp (/ x_m s)))) (/ x_m s))))) (exp (/ x_m (- s)))))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (fabsf(x_m) <= 0.0003000000142492354f) {
tmp = 1.0f / (s * expf(((2.0f * log1pf(expf((x_m / s)))) - (x_m / s))));
} else {
tmp = expf((x_m / -s));
}
return tmp;
}
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (abs(x_m) <= Float32(0.0003000000142492354)) tmp = Float32(Float32(1.0) / Float32(s * exp(Float32(Float32(Float32(2.0) * log1p(exp(Float32(x_m / s)))) - Float32(x_m / s))))); else tmp = exp(Float32(x_m / Float32(-s))); end return tmp end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;\left|x\_m\right| \leq 0.0003000000142492354:\\
\;\;\;\;\frac{1}{s \cdot e^{2 \cdot \mathsf{log1p}\left(e^{\frac{x\_m}{s}}\right) - \frac{x\_m}{s}}}\\
\mathbf{else}:\\
\;\;\;\;e^{\frac{x\_m}{-s}}\\
\end{array}
\end{array}
if (fabs.f32 x) < 3.00000014e-4Initial program 98.4%
fabs-neg98.4%
distribute-frac-neg98.4%
distribute-frac-neg298.4%
fabs-neg98.4%
*-commutative98.4%
fabs-neg98.4%
+-commutative98.4%
fabs-neg98.4%
Simplified98.5%
Applied egg-rr93.5%
Taylor expanded in x around -inf 93.5%
exp-neg93.6%
exp-sum94.5%
rem-exp-log98.1%
+-commutative98.1%
log1p-define98.3%
mul-1-neg98.3%
unsub-neg98.3%
Simplified98.3%
if 3.00000014e-4 < (fabs.f32 x) Initial program 100.0%
fabs-neg100.0%
distribute-frac-neg100.0%
distribute-frac-neg2100.0%
fabs-neg100.0%
*-commutative100.0%
fabs-neg100.0%
+-commutative100.0%
fabs-neg100.0%
Simplified100.0%
Applied egg-rr78.3%
Taylor expanded in x around inf 49.5%
div-inv49.5%
exp-prod49.5%
add-sqr-sqrt1.6%
fabs-sqr1.6%
add-sqr-sqrt3.1%
add-sqr-sqrt3.1%
sqrt-unprod3.1%
sqr-neg3.1%
sqrt-unprod-0.0%
add-sqr-sqrt100.0%
exp-prod100.0%
div-inv100.0%
distribute-frac-neg2100.0%
exp-neg100.0%
add-sqr-sqrt100.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-unprod-0.0%
add-sqr-sqrt3.1%
div-inv3.1%
Applied egg-rr53.7%
rec-exp53.7%
distribute-frac-neg53.7%
Simplified53.7%
Final simplification74.3%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= (fabs x_m) 0.0003000000142492354) (/ (exp (+ (/ x_m s) (* (log1p (exp (/ x_m s))) -2.0))) s) (exp (/ x_m (- s)))))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (fabsf(x_m) <= 0.0003000000142492354f) {
tmp = expf(((x_m / s) + (log1pf(expf((x_m / s))) * -2.0f))) / s;
} else {
tmp = expf((x_m / -s));
}
return tmp;
}
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (abs(x_m) <= Float32(0.0003000000142492354)) tmp = Float32(exp(Float32(Float32(x_m / s) + Float32(log1p(exp(Float32(x_m / s))) * Float32(-2.0)))) / s); else tmp = exp(Float32(x_m / Float32(-s))); end return tmp end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;\left|x\_m\right| \leq 0.0003000000142492354:\\
\;\;\;\;\frac{e^{\frac{x\_m}{s} + \mathsf{log1p}\left(e^{\frac{x\_m}{s}}\right) \cdot -2}}{s}\\
\mathbf{else}:\\
\;\;\;\;e^{\frac{x\_m}{-s}}\\
\end{array}
\end{array}
if (fabs.f32 x) < 3.00000014e-4Initial program 98.4%
fabs-neg98.4%
distribute-frac-neg98.4%
distribute-frac-neg298.4%
fabs-neg98.4%
*-commutative98.4%
fabs-neg98.4%
+-commutative98.4%
fabs-neg98.4%
Simplified98.5%
Taylor expanded in x around 0 98.5%
associate-/r*98.5%
mul-1-neg98.5%
distribute-neg-frac298.5%
+-commutative98.5%
mul-1-neg98.5%
distribute-neg-frac298.5%
Simplified98.5%
Applied egg-rr98.3%
*-lft-identity98.3%
Simplified98.3%
if 3.00000014e-4 < (fabs.f32 x) Initial program 100.0%
fabs-neg100.0%
distribute-frac-neg100.0%
distribute-frac-neg2100.0%
fabs-neg100.0%
*-commutative100.0%
fabs-neg100.0%
+-commutative100.0%
fabs-neg100.0%
Simplified100.0%
Applied egg-rr78.3%
Taylor expanded in x around inf 49.5%
div-inv49.5%
exp-prod49.5%
add-sqr-sqrt1.6%
fabs-sqr1.6%
add-sqr-sqrt3.1%
add-sqr-sqrt3.1%
sqrt-unprod3.1%
sqr-neg3.1%
sqrt-unprod-0.0%
add-sqr-sqrt100.0%
exp-prod100.0%
div-inv100.0%
distribute-frac-neg2100.0%
exp-neg100.0%
add-sqr-sqrt100.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-unprod-0.0%
add-sqr-sqrt3.1%
div-inv3.1%
Applied egg-rr53.7%
rec-exp53.7%
distribute-frac-neg53.7%
Simplified53.7%
Final simplification74.2%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= x_m 9.999999682655225e-20) (/ 0.25 s) (/ 1.0 (exp (/ x_m s)))))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (x_m <= 9.999999682655225e-20f) {
tmp = 0.25f / s;
} else {
tmp = 1.0f / expf((x_m / s));
}
return tmp;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
real(4) :: tmp
if (x_m <= 9.999999682655225e-20) then
tmp = 0.25e0 / s
else
tmp = 1.0e0 / exp((x_m / s))
end if
code = tmp
end function
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (x_m <= Float32(9.999999682655225e-20)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(1.0) / exp(Float32(x_m / s))); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, s) tmp = single(0.0); if (x_m <= single(9.999999682655225e-20)) tmp = single(0.25) / s; else tmp = single(1.0) / exp((x_m / s)); end tmp_2 = tmp; end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 9.999999682655225 \cdot 10^{-20}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{\frac{x\_m}{s}}}\\
\end{array}
\end{array}
if x < 9.99999968e-20Initial program 99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.8%
Taylor expanded in s around inf 40.2%
if 9.99999968e-20 < x Initial program 98.5%
fabs-neg98.5%
distribute-frac-neg98.5%
distribute-frac-neg298.5%
fabs-neg98.5%
*-commutative98.5%
fabs-neg98.5%
+-commutative98.5%
fabs-neg98.5%
Simplified98.6%
Applied egg-rr68.5%
Taylor expanded in x around inf 4.1%
div-inv4.1%
exp-prod4.6%
add-sqr-sqrt4.6%
fabs-sqr4.6%
add-sqr-sqrt4.6%
add-sqr-sqrt4.6%
sqrt-unprod4.6%
sqr-neg4.6%
sqrt-unprod2.1%
add-sqr-sqrt76.0%
exp-prod91.7%
div-inv91.7%
distribute-frac-neg291.7%
exp-neg91.7%
add-sqr-sqrt91.7%
sqrt-unprod91.7%
sqr-neg91.7%
sqrt-unprod-0.0%
add-sqr-sqrt4.1%
div-inv4.1%
Applied egg-rr91.7%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= x_m 9.999999682655225e-20) (/ 0.25 s) (exp (/ x_m (- s)))))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (x_m <= 9.999999682655225e-20f) {
tmp = 0.25f / s;
} else {
tmp = expf((x_m / -s));
}
return tmp;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
real(4) :: tmp
if (x_m <= 9.999999682655225e-20) then
tmp = 0.25e0 / s
else
tmp = exp((x_m / -s))
end if
code = tmp
end function
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (x_m <= Float32(9.999999682655225e-20)) tmp = Float32(Float32(0.25) / s); else tmp = exp(Float32(x_m / Float32(-s))); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, s) tmp = single(0.0); if (x_m <= single(9.999999682655225e-20)) tmp = single(0.25) / s; else tmp = exp((x_m / -s)); end tmp_2 = tmp; end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 9.999999682655225 \cdot 10^{-20}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;e^{\frac{x\_m}{-s}}\\
\end{array}
\end{array}
if x < 9.99999968e-20Initial program 99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.8%
Taylor expanded in s around inf 40.2%
if 9.99999968e-20 < x Initial program 98.5%
fabs-neg98.5%
distribute-frac-neg98.5%
distribute-frac-neg298.5%
fabs-neg98.5%
*-commutative98.5%
fabs-neg98.5%
+-commutative98.5%
fabs-neg98.5%
Simplified98.6%
Applied egg-rr68.5%
Taylor expanded in x around inf 4.1%
div-inv4.1%
exp-prod4.6%
add-sqr-sqrt4.6%
fabs-sqr4.6%
add-sqr-sqrt4.6%
add-sqr-sqrt4.6%
sqrt-unprod4.6%
sqr-neg4.6%
sqrt-unprod2.1%
add-sqr-sqrt76.0%
exp-prod91.7%
div-inv91.7%
distribute-frac-neg291.7%
exp-neg91.7%
add-sqr-sqrt91.7%
sqrt-unprod91.7%
sqr-neg91.7%
sqrt-unprod-0.0%
add-sqr-sqrt4.1%
div-inv4.1%
Applied egg-rr91.7%
rec-exp91.7%
distribute-frac-neg91.7%
Simplified91.7%
Final simplification60.7%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 0.5 (* s (+ 1.0 (exp (/ x_m s))))))
x_m = fabs(x);
float code(float x_m, float s) {
return 0.5f / (s * (1.0f + expf((x_m / s))));
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = 0.5e0 / (s * (1.0e0 + exp((x_m / s))))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(1.0) + exp(Float32(x_m / s))))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(0.5) / (s * (single(1.0) + exp((x_m / s)))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{0.5}{s \cdot \left(1 + e^{\frac{x\_m}{s}}\right)}
\end{array}
Initial program 99.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.3%
Applied egg-rr61.1%
associate-*l/61.1%
*-lft-identity61.1%
+-commutative61.1%
Simplified61.1%
Taylor expanded in x around 0 63.0%
Taylor expanded in s around 0 63.0%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ (exp (/ x_m (- s))) s) 4.0))
x_m = fabs(x);
float code(float x_m, float s) {
return (expf((x_m / -s)) / s) / 4.0f;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = (exp((x_m / -s)) / s) / 4.0e0
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(exp(Float32(x_m / Float32(-s))) / s) / Float32(4.0)) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (exp((x_m / -s)) / s) / single(4.0); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{e^{\frac{x\_m}{-s}}}{s}}{4}
\end{array}
Initial program 99.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.3%
Taylor expanded in x around 0 99.3%
associate-/r*99.3%
mul-1-neg99.3%
distribute-neg-frac299.3%
+-commutative99.3%
mul-1-neg99.3%
distribute-neg-frac299.3%
Simplified99.3%
Taylor expanded in s around inf 95.3%
distribute-frac-neg299.3%
rec-exp99.3%
pow199.3%
pow199.3%
add-sqr-sqrt51.0%
fabs-sqr51.0%
add-sqr-sqrt97.3%
Applied egg-rr62.3%
rec-exp97.3%
distribute-neg-frac297.3%
Simplified62.3%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ 0.5 s) (+ 2.0 (/ x_m s))))
x_m = fabs(x);
float code(float x_m, float s) {
return (0.5f / s) / (2.0f + (x_m / s));
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = (0.5e0 / s) / (2.0e0 + (x_m / s))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(0.5) / s) / Float32(Float32(2.0) + Float32(x_m / s))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(0.5) / s) / (single(2.0) + (x_m / s)); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{0.5}{s}}{2 + \frac{x\_m}{s}}
\end{array}
Initial program 99.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.3%
Applied egg-rr61.1%
associate-*l/61.1%
*-lft-identity61.1%
+-commutative61.1%
Simplified61.1%
Taylor expanded in x around 0 63.0%
Taylor expanded in x around 0 50.1%
+-commutative50.1%
Simplified50.1%
Final simplification50.1%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 0.25 s))
x_m = fabs(x);
float code(float x_m, float s) {
return 0.25f / s;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = 0.25e0 / s
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(0.25) / s) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(0.25) / s; end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{0.25}{s}
\end{array}
Initial program 99.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.3%
Taylor expanded in s around inf 28.3%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 1.0)
x_m = fabs(x);
float code(float x_m, float s) {
return 1.0f;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = 1.0e0
end function
x_m = abs(x) function code(x_m, s) return Float32(1.0) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(1.0); end
\begin{array}{l}
x_m = \left|x\right|
\\
1
\end{array}
Initial program 99.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.3%
Applied egg-rr85.3%
Taylor expanded in x around inf 39.0%
Taylor expanded in x around 0 8.4%
herbie shell --seed 2024108
(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))))))