
(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}
(FPCore (x s) :precision binary32 (/ 1.0 (* s (+ (exp (/ (fabs x) (- s))) (+ (exp (/ (fabs x) s)) 2.0)))))
float code(float x, float s) {
return 1.0f / (s * (expf((fabsf(x) / -s)) + (expf((fabsf(x) / s)) + 2.0f)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (s * (exp((abs(x) / -s)) + (exp((abs(x) / s)) + 2.0e0)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(exp(Float32(abs(x) / Float32(-s))) + Float32(exp(Float32(abs(x) / s)) + Float32(2.0))))) end
function tmp = code(x, s) tmp = single(1.0) / (s * (exp((abs(x) / -s)) + (exp((abs(x) / s)) + single(2.0)))); end
\begin{array}{l}
\\
\frac{1}{s \cdot \left(e^{\frac{\left|x\right|}{-s}} + \left(e^{\frac{\left|x\right|}{s}} + 2\right)\right)}
\end{array}
Initial program 99.4%
Simplified99.1%
expm1-log1p-u97.1%
expm1-udef96.9%
associate-/l/97.3%
Applied egg-rr97.3%
expm1-def97.5%
expm1-log1p99.5%
*-commutative99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x s) :precision binary32 (if (<= (fabs x) 1.000000031374395e-22) (/ 1.0 (fma x (/ x s) (* s 4.0))) (/ (exp (* -0.5 (/ (* x (- (* x 1.5) x)) (* s s)))) (* s 4.0))))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 1.000000031374395e-22f) {
tmp = 1.0f / fmaf(x, (x / s), (s * 4.0f));
} else {
tmp = expf((-0.5f * ((x * ((x * 1.5f) - x)) / (s * s)))) / (s * 4.0f);
}
return tmp;
}
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(1.000000031374395e-22)) tmp = Float32(Float32(1.0) / fma(x, Float32(x / s), Float32(s * Float32(4.0)))); else tmp = Float32(exp(Float32(Float32(-0.5) * Float32(Float32(x * Float32(Float32(x * Float32(1.5)) - x)) / Float32(s * s)))) / Float32(s * Float32(4.0))); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 1.000000031374395 \cdot 10^{-22}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(x, \frac{x}{s}, s \cdot 4\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{-0.5 \cdot \frac{x \cdot \left(x \cdot 1.5 - x\right)}{s \cdot s}}}{s \cdot 4}\\
\end{array}
\end{array}
if (fabs.f32 x) < 1.00000003e-22Initial program 99.4%
Simplified99.7%
expm1-log1p-u92.3%
expm1-udef91.3%
associate-/l/91.3%
Applied egg-rr91.3%
expm1-def92.3%
expm1-log1p99.7%
*-commutative99.7%
Simplified99.7%
Taylor expanded in s around inf 87.5%
+-commutative87.5%
+-commutative87.5%
associate-+l+87.5%
distribute-rgt1-in87.5%
metadata-eval87.5%
mul0-lft87.5%
+-commutative87.5%
associate-+l+87.5%
unpow287.5%
sqr-abs87.5%
associate-*l/89.1%
associate-/r/89.1%
*-commutative89.1%
metadata-eval89.1%
mul0-lft89.1%
metadata-eval89.1%
distribute-lft1-in89.1%
fma-def89.1%
distribute-lft1-in89.1%
metadata-eval89.1%
mul0-lft89.1%
Simplified89.1%
Taylor expanded in x around 0 87.5%
unpow287.5%
associate-*r/89.1%
+-commutative89.1%
fma-def89.1%
*-commutative89.1%
Simplified89.1%
if 1.00000003e-22 < (fabs.f32 x) Initial program 99.4%
add-exp-log98.7%
log-div98.7%
add-log-exp99.0%
associate-*l*99.0%
pow299.0%
Applied egg-rr99.0%
Taylor expanded in s around inf 61.9%
neg-mul-161.9%
unsub-neg61.9%
Simplified62.4%
expm1-log1p-u62.3%
expm1-udef62.3%
exp-diff62.4%
frac-times62.4%
associate-*r*62.4%
sum-log62.5%
add-exp-log62.5%
Applied egg-rr62.5%
expm1-def62.5%
expm1-log1p63.1%
*-commutative63.1%
Simplified98.5%
Final simplification96.7%
(FPCore (x s) :precision binary32 (/ (exp (* -0.25 (pow (/ x s) 2.0))) (* s 4.0)))
float code(float x, float s) {
return expf((-0.25f * powf((x / s), 2.0f))) / (s * 4.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = exp(((-0.25e0) * ((x / s) ** 2.0e0))) / (s * 4.0e0)
end function
function code(x, s) return Float32(exp(Float32(Float32(-0.25) * (Float32(x / s) ^ Float32(2.0)))) / Float32(s * Float32(4.0))) end
function tmp = code(x, s) tmp = exp((single(-0.25) * ((x / s) ^ single(2.0)))) / (s * single(4.0)); end
\begin{array}{l}
\\
\frac{e^{-0.25 \cdot {\left(\frac{x}{s}\right)}^{2}}}{s \cdot 4}
\end{array}
Initial program 99.4%
add-exp-log97.4%
log-div97.4%
add-log-exp97.6%
associate-*l*97.6%
pow297.6%
Applied egg-rr97.6%
Taylor expanded in s around inf 62.7%
neg-mul-162.7%
unsub-neg62.7%
Simplified65.7%
Taylor expanded in x around 0 91.7%
unpow291.7%
unpow291.7%
times-frac96.0%
Simplified96.0%
exp-diff96.0%
pow296.0%
sum-log96.3%
add-exp-log98.2%
Applied egg-rr98.2%
Final simplification98.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.4%
Taylor expanded in s around inf 95.4%
*-commutative95.4%
Simplified95.4%
Final simplification95.4%
(FPCore (x s) :precision binary32 (/ 1.0 (fma 4.0 s (* x (/ x s)))))
float code(float x, float s) {
return 1.0f / fmaf(4.0f, s, (x * (x / s)));
}
function code(x, s) return Float32(Float32(1.0) / fma(Float32(4.0), s, Float32(x * Float32(x / s)))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(4, s, x \cdot \frac{x}{s}\right)}
\end{array}
Initial program 99.4%
Simplified99.1%
expm1-log1p-u97.1%
expm1-udef96.9%
associate-/l/97.3%
Applied egg-rr97.3%
expm1-def97.5%
expm1-log1p99.5%
*-commutative99.5%
Simplified99.5%
Taylor expanded in s around inf 70.6%
+-commutative70.6%
+-commutative70.6%
associate-+l+70.6%
distribute-rgt1-in70.6%
metadata-eval70.6%
mul0-lft70.6%
+-commutative70.6%
associate-+l+70.6%
unpow270.6%
sqr-abs70.6%
associate-*l/70.9%
associate-/r/70.9%
*-commutative70.9%
metadata-eval70.9%
mul0-lft70.9%
metadata-eval70.9%
distribute-lft1-in70.9%
fma-def70.9%
distribute-lft1-in70.9%
metadata-eval70.9%
mul0-lft70.9%
Simplified70.9%
Taylor expanded in x around 0 70.6%
fma-def70.6%
unpow270.6%
associate-*r/70.9%
Simplified70.9%
Final simplification70.9%
(FPCore (x s) :precision binary32 (/ 1.0 (fma x (/ x s) (* s 4.0))))
float code(float x, float s) {
return 1.0f / fmaf(x, (x / s), (s * 4.0f));
}
function code(x, s) return Float32(Float32(1.0) / fma(x, Float32(x / s), Float32(s * Float32(4.0)))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(x, \frac{x}{s}, s \cdot 4\right)}
\end{array}
Initial program 99.4%
Simplified99.1%
expm1-log1p-u97.1%
expm1-udef96.9%
associate-/l/97.3%
Applied egg-rr97.3%
expm1-def97.5%
expm1-log1p99.5%
*-commutative99.5%
Simplified99.5%
Taylor expanded in s around inf 70.6%
+-commutative70.6%
+-commutative70.6%
associate-+l+70.6%
distribute-rgt1-in70.6%
metadata-eval70.6%
mul0-lft70.6%
+-commutative70.6%
associate-+l+70.6%
unpow270.6%
sqr-abs70.6%
associate-*l/70.9%
associate-/r/70.9%
*-commutative70.9%
metadata-eval70.9%
mul0-lft70.9%
metadata-eval70.9%
distribute-lft1-in70.9%
fma-def70.9%
distribute-lft1-in70.9%
metadata-eval70.9%
mul0-lft70.9%
Simplified70.9%
Taylor expanded in x around 0 70.6%
unpow270.6%
associate-*r/70.9%
+-commutative70.9%
fma-def70.9%
*-commutative70.9%
Simplified70.9%
Final simplification70.9%
(FPCore (x s) :precision binary32 (if (<= x 0.014999999664723873) (/ 0.25 s) (* 2.0 (/ 1.0 (/ x (/ s x))))))
float code(float x, float s) {
float tmp;
if (x <= 0.014999999664723873f) {
tmp = 0.25f / s;
} else {
tmp = 2.0f * (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 <= 0.014999999664723873e0) then
tmp = 0.25e0 / s
else
tmp = 2.0e0 * (1.0e0 / (x / (s / x)))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.014999999664723873)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(2.0) * Float32(Float32(1.0) / Float32(x / Float32(s / x)))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.014999999664723873)) tmp = single(0.25) / s; else tmp = single(2.0) * (single(1.0) / (x / (s / x))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.014999999664723873:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \frac{1}{\frac{x}{\frac{s}{x}}}\\
\end{array}
\end{array}
if x < 0.0149999997Initial program 99.2%
*-lft-identity99.2%
associate-*r/99.2%
associate-/l*99.2%
distribute-frac-neg99.2%
exp-neg99.2%
associate-/r/99.2%
/-rgt-identity99.2%
associate-*l*99.1%
Simplified99.3%
Taylor expanded in s around inf 39.7%
if 0.0149999997 < x Initial program 100.0%
Simplified98.5%
Taylor expanded in s around inf 50.0%
+-commutative50.0%
associate-+r+50.0%
mul-1-neg50.0%
unsub-neg50.0%
associate-*r/50.0%
*-commutative50.0%
unpow250.0%
times-frac50.0%
unpow250.0%
sqr-abs50.0%
Simplified50.0%
Taylor expanded in s around 0 80.3%
unpow280.3%
sqr-abs80.3%
unpow280.3%
unpow280.3%
sqr-abs80.3%
Simplified80.3%
clear-num80.9%
inv-pow80.9%
Applied egg-rr80.9%
unpow-180.9%
associate-/l*80.9%
Simplified80.9%
Final simplification50.3%
(FPCore (x s) :precision binary32 (if (<= x 0.014999999664723873) (/ 0.25 s) (* 2.0 (/ s (* x x)))))
float code(float x, float s) {
float tmp;
if (x <= 0.014999999664723873f) {
tmp = 0.25f / s;
} else {
tmp = 2.0f * (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 <= 0.014999999664723873e0) then
tmp = 0.25e0 / s
else
tmp = 2.0e0 * (s / (x * x))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.014999999664723873)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(2.0) * Float32(s / Float32(x * x))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.014999999664723873)) tmp = single(0.25) / s; else tmp = single(2.0) * (s / (x * x)); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.014999999664723873:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \frac{s}{x \cdot x}\\
\end{array}
\end{array}
if x < 0.0149999997Initial program 99.2%
*-lft-identity99.2%
associate-*r/99.2%
associate-/l*99.2%
distribute-frac-neg99.2%
exp-neg99.2%
associate-/r/99.2%
/-rgt-identity99.2%
associate-*l*99.1%
Simplified99.3%
Taylor expanded in s around inf 39.7%
if 0.0149999997 < x Initial program 100.0%
Simplified98.5%
Taylor expanded in s around inf 50.0%
+-commutative50.0%
associate-+r+50.0%
mul-1-neg50.0%
unsub-neg50.0%
associate-*r/50.0%
*-commutative50.0%
unpow250.0%
times-frac50.0%
unpow250.0%
sqr-abs50.0%
Simplified50.0%
Taylor expanded in s around 0 80.3%
unpow280.3%
sqr-abs80.3%
unpow280.3%
unpow280.3%
sqr-abs80.3%
Simplified80.3%
Final simplification50.1%
(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.4%
*-lft-identity99.4%
associate-*r/99.4%
associate-/l*99.4%
distribute-frac-neg99.4%
exp-neg99.4%
associate-/r/99.4%
/-rgt-identity99.4%
associate-*l*99.4%
Simplified99.5%
Taylor expanded in s around inf 30.6%
Final simplification30.6%
herbie shell --seed 2023199
(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))))))