
(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}
(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(abs(x) / Float32(-s))) return Float32(t_0 / Float32(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{t_0}{\left(t_0 + 1\right) \cdot \left(s + \frac{s}{e^{\frac{\left|x\right|}{s}}}\right)}
\end{array}
\end{array}
Initial program 99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (x s) :precision binary32 (/ (/ 1.0 (+ (exp (/ (fabs x) (- s))) 1.0)) (+ (- (* s (exp (/ x s))) s) (+ s s))))
float code(float x, float s) {
return (1.0f / (expf((fabsf(x) / -s)) + 1.0f)) / (((s * expf((x / s))) - s) + (s + s));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / (exp((abs(x) / -s)) + 1.0e0)) / (((s * exp((x / s))) - s) + (s + s))
end function
function code(x, s) return Float32(Float32(Float32(1.0) / Float32(exp(Float32(abs(x) / Float32(-s))) + Float32(1.0))) / Float32(Float32(Float32(s * exp(Float32(x / s))) - s) + Float32(s + s))) end
function tmp = code(x, s) tmp = (single(1.0) / (exp((abs(x) / -s)) + single(1.0))) / (((s * exp((x / s))) - s) + (s + s)); end
\begin{array}{l}
\\
\frac{\frac{1}{e^{\frac{\left|x\right|}{-s}} + 1}}{\left(s \cdot e^{\frac{x}{s}} - s\right) + \left(s + s\right)}
\end{array}
Initial program 99.4%
Simplified99.2%
add-sqr-sqrt98.8%
pow298.8%
add-sqr-sqrt53.7%
fabs-sqr53.7%
add-sqr-sqrt64.1%
Applied egg-rr64.1%
unpow264.1%
add-sqr-sqrt64.5%
add-sqr-sqrt64.4%
sqrt-unprod61.0%
sqr-neg61.0%
sqrt-unprod-0.0%
add-sqr-sqrt42.7%
fma-neg42.7%
*-un-lft-identity42.7%
prod-diff42.7%
add-sqr-sqrt-0.0%
sqrt-unprod61.0%
sqr-neg61.0%
sqrt-unprod64.4%
add-sqr-sqrt64.5%
Applied egg-rr64.5%
*-rgt-identity64.5%
fma-neg64.5%
fma-udef64.5%
*-rgt-identity64.5%
*-rgt-identity64.5%
Simplified64.5%
Final simplification64.5%
(FPCore (x s) :precision binary32 (/ (/ 1.0 (+ (exp (/ (fabs x) (- s))) 1.0)) (+ (+ s s) (* s (expm1 (/ x s))))))
float code(float x, float s) {
return (1.0f / (expf((fabsf(x) / -s)) + 1.0f)) / ((s + s) + (s * expm1f((x / s))));
}
function code(x, s) return Float32(Float32(Float32(1.0) / Float32(exp(Float32(abs(x) / Float32(-s))) + Float32(1.0))) / Float32(Float32(s + s) + Float32(s * expm1(Float32(x / s))))) end
\begin{array}{l}
\\
\frac{\frac{1}{e^{\frac{\left|x\right|}{-s}} + 1}}{\left(s + s\right) + s \cdot \mathsf{expm1}\left(\frac{x}{s}\right)}
\end{array}
Initial program 99.4%
Simplified99.2%
add-sqr-sqrt98.8%
pow298.8%
add-sqr-sqrt53.7%
fabs-sqr53.7%
add-sqr-sqrt64.1%
Applied egg-rr64.1%
unpow264.1%
add-sqr-sqrt64.5%
add-sqr-sqrt64.4%
sqrt-unprod61.0%
sqr-neg61.0%
sqrt-unprod-0.0%
add-sqr-sqrt42.7%
fma-neg42.7%
*-un-lft-identity42.7%
prod-diff42.7%
add-sqr-sqrt-0.0%
sqrt-unprod61.0%
sqr-neg61.0%
sqrt-unprod64.4%
add-sqr-sqrt64.5%
Applied egg-rr64.5%
+-commutative64.5%
fma-udef64.5%
*-rgt-identity64.5%
*-rgt-identity64.5%
*-rgt-identity64.5%
fma-neg64.5%
*-rgt-identity64.5%
distribute-lft-out--64.5%
expm1-def64.5%
Simplified64.5%
Final simplification64.5%
(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(s * Float32(Float32(Float32(1.0) + exp(Float32(x / s))) * Float32(Float32(1.0) + exp(Float32(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}{s \cdot \left(\left(1 + e^{\frac{x}{s}}\right) \cdot \left(1 + e^{\frac{-\left|x\right|}{s}}\right)\right)}
\end{array}
Initial program 99.4%
Simplified99.2%
add-sqr-sqrt98.8%
pow298.8%
add-sqr-sqrt53.7%
fabs-sqr53.7%
add-sqr-sqrt64.1%
Applied egg-rr64.1%
unpow264.1%
add-sqr-sqrt64.5%
add-sqr-sqrt64.4%
sqrt-unprod61.0%
sqr-neg61.0%
sqrt-unprod-0.0%
add-sqr-sqrt42.7%
fma-neg42.7%
*-un-lft-identity42.7%
prod-diff42.7%
add-sqr-sqrt-0.0%
sqrt-unprod61.0%
sqr-neg61.0%
sqrt-unprod64.4%
add-sqr-sqrt64.5%
Applied egg-rr64.5%
+-commutative64.5%
fma-udef64.5%
*-rgt-identity64.5%
*-rgt-identity64.5%
*-rgt-identity64.5%
fma-neg64.5%
*-rgt-identity64.5%
distribute-lft-out--64.5%
expm1-def64.5%
Simplified64.5%
Taylor expanded in s around 0 64.5%
*-commutative64.5%
+-commutative64.5%
mul-1-neg64.5%
Simplified64.5%
Final simplification64.5%
(FPCore (x s)
:precision binary32
(let* ((t_0 (+ 1.0 (/ x (/ s x)))))
(if (<= (fabs x) 15000000512.0)
(/ 1.0 (+ (/ (+ (* t_0 t_0) -1.0) (+ 1.0 t_0)) (* s 4.0)))
(/ 1.0 (+ (* s 4.0) (/ 1.0 (/ s (* x x))))))))
float code(float x, float s) {
float t_0 = 1.0f + (x / (s / x));
float tmp;
if (fabsf(x) <= 15000000512.0f) {
tmp = 1.0f / ((((t_0 * t_0) + -1.0f) / (1.0f + t_0)) + (s * 4.0f));
} else {
tmp = 1.0f / ((s * 4.0f) + (1.0f / (s / (x * x))));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: tmp
t_0 = 1.0e0 + (x / (s / x))
if (abs(x) <= 15000000512.0e0) then
tmp = 1.0e0 / ((((t_0 * t_0) + (-1.0e0)) / (1.0e0 + t_0)) + (s * 4.0e0))
else
tmp = 1.0e0 / ((s * 4.0e0) + (1.0e0 / (s / (x * x))))
end if
code = tmp
end function
function code(x, s) t_0 = Float32(Float32(1.0) + Float32(x / Float32(s / x))) tmp = Float32(0.0) if (abs(x) <= Float32(15000000512.0)) tmp = Float32(Float32(1.0) / Float32(Float32(Float32(Float32(t_0 * t_0) + Float32(-1.0)) / Float32(Float32(1.0) + t_0)) + Float32(s * Float32(4.0)))); else tmp = Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(Float32(1.0) / Float32(s / Float32(x * x))))); end return tmp end
function tmp_2 = code(x, s) t_0 = single(1.0) + (x / (s / x)); tmp = single(0.0); if (abs(x) <= single(15000000512.0)) tmp = single(1.0) / ((((t_0 * t_0) + single(-1.0)) / (single(1.0) + t_0)) + (s * single(4.0))); else tmp = single(1.0) / ((s * single(4.0)) + (single(1.0) / (s / (x * x)))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 1 + \frac{x}{\frac{s}{x}}\\
\mathbf{if}\;\left|x\right| \leq 15000000512:\\
\;\;\;\;\frac{1}{\frac{t_0 \cdot t_0 + -1}{1 + t_0} + s \cdot 4}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{s \cdot 4 + \frac{1}{\frac{s}{x \cdot x}}}\\
\end{array}
\end{array}
if (fabs.f32 x) < 15000000500Initial program 99.0%
Simplified98.7%
Taylor expanded in x around 0 98.6%
+-commutative98.6%
associate-*r/98.6%
mul-1-neg98.6%
Simplified98.6%
Taylor expanded in s around -inf 45.7%
+-commutative45.7%
associate-+r+45.7%
mul-1-neg45.7%
distribute-lft1-in45.7%
metadata-eval45.7%
associate-*r/45.7%
mul-1-neg45.7%
remove-double-neg45.7%
associate-+r+45.7%
Simplified45.7%
expm1-log1p-u45.7%
expm1-udef44.9%
Applied egg-rr44.9%
flip--53.7%
log1p-udef53.7%
add-exp-log53.7%
associate-/l*53.7%
log1p-udef53.7%
add-exp-log53.7%
associate-/l*53.7%
metadata-eval53.7%
log1p-udef53.7%
add-exp-log53.7%
associate-/l*53.7%
Applied egg-rr53.7%
if 15000000500 < (fabs.f32 x) Initial program 100.0%
Simplified100.0%
Taylor expanded in x around 0 100.0%
+-commutative100.0%
associate-*r/100.0%
mul-1-neg100.0%
Simplified100.0%
Taylor expanded in s around -inf 30.6%
+-commutative30.6%
associate-+r+30.6%
mul-1-neg30.6%
distribute-lft1-in92.0%
metadata-eval92.0%
associate-*r/92.0%
mul-1-neg92.0%
remove-double-neg92.0%
associate-+r+92.0%
Simplified92.0%
expm1-log1p-u92.0%
expm1-udef92.0%
Applied egg-rr92.0%
expm1-def92.0%
expm1-log1p-u92.0%
clear-num92.0%
Applied egg-rr92.0%
Final simplification68.1%
(FPCore (x s) :precision binary32 (/ 0.5 (* s (+ (expm1 (/ x s)) 2.0))))
float code(float x, float s) {
return 0.5f / (s * (expm1f((x / s)) + 2.0f));
}
function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(expm1(Float32(x / s)) + Float32(2.0)))) end
\begin{array}{l}
\\
\frac{0.5}{s \cdot \left(\mathsf{expm1}\left(\frac{x}{s}\right) + 2\right)}
\end{array}
Initial program 99.4%
Simplified99.2%
add-sqr-sqrt98.8%
pow298.8%
add-sqr-sqrt53.7%
fabs-sqr53.7%
add-sqr-sqrt64.1%
Applied egg-rr64.1%
unpow264.1%
add-sqr-sqrt64.5%
add-sqr-sqrt64.4%
sqrt-unprod61.0%
sqr-neg61.0%
sqrt-unprod-0.0%
add-sqr-sqrt42.7%
fma-neg42.7%
*-un-lft-identity42.7%
prod-diff42.7%
add-sqr-sqrt-0.0%
sqrt-unprod61.0%
sqr-neg61.0%
sqrt-unprod64.4%
add-sqr-sqrt64.5%
Applied egg-rr64.5%
+-commutative64.5%
fma-udef64.5%
*-rgt-identity64.5%
*-rgt-identity64.5%
*-rgt-identity64.5%
fma-neg64.5%
*-rgt-identity64.5%
distribute-lft-out--64.5%
expm1-def64.5%
Simplified64.5%
Taylor expanded in s around inf 61.4%
Taylor expanded in x around inf 61.4%
count-261.4%
+-commutative61.4%
*-commutative61.4%
count-261.4%
distribute-rgt-out61.4%
expm1-def61.4%
Simplified61.4%
Final simplification61.4%
(FPCore (x s) :precision binary32 (/ 1.0 (+ (* s 4.0) (* (* x x) (/ 1.0 s)))))
float code(float x, float s) {
return 1.0f / ((s * 4.0f) + ((x * x) * (1.0f / 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) * (1.0e0 / s)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(Float32(x * x) * Float32(Float32(1.0) / s)))) end
function tmp = code(x, s) tmp = single(1.0) / ((s * single(4.0)) + ((x * x) * (single(1.0) / s))); end
\begin{array}{l}
\\
\frac{1}{s \cdot 4 + \left(x \cdot x\right) \cdot \frac{1}{s}}
\end{array}
Initial program 99.4%
Simplified99.2%
Taylor expanded in x around 0 99.2%
+-commutative99.2%
associate-*r/99.2%
mul-1-neg99.2%
Simplified99.2%
Taylor expanded in s around -inf 40.0%
+-commutative40.0%
associate-+r+40.0%
mul-1-neg40.0%
distribute-lft1-in63.1%
metadata-eval63.1%
associate-*r/63.1%
mul-1-neg63.1%
remove-double-neg63.1%
associate-+r+63.1%
Simplified63.1%
div-inv63.4%
Applied egg-rr63.4%
Final simplification63.4%
(FPCore (x s) :precision binary32 (/ 1.0 (+ (/ x (/ s x)) (* s 4.0))))
float code(float x, float s) {
return 1.0f / ((x / (s / x)) + (s * 4.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((x / (s / x)) + (s * 4.0e0))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(x / Float32(s / x)) + Float32(s * Float32(4.0)))) end
function tmp = code(x, s) tmp = single(1.0) / ((x / (s / x)) + (s * single(4.0))); end
\begin{array}{l}
\\
\frac{1}{\frac{x}{\frac{s}{x}} + s \cdot 4}
\end{array}
Initial program 99.4%
Simplified99.2%
Taylor expanded in x around 0 99.2%
+-commutative99.2%
associate-*r/99.2%
mul-1-neg99.2%
Simplified99.2%
Taylor expanded in s around -inf 40.0%
+-commutative40.0%
associate-+r+40.0%
mul-1-neg40.0%
distribute-lft1-in63.1%
metadata-eval63.1%
associate-*r/63.1%
mul-1-neg63.1%
remove-double-neg63.1%
associate-+r+63.1%
Simplified63.1%
Taylor expanded in x around 0 63.1%
unpow263.1%
associate-/l*63.4%
Simplified63.4%
Final simplification63.4%
(FPCore (x s) :precision binary32 (if (<= x 9.999999747378752e-5) (/ 0.25 s) (/ s (* x x))))
float code(float x, float s) {
float tmp;
if (x <= 9.999999747378752e-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 <= 9.999999747378752e-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(9.999999747378752e-5)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(s / Float32(x * x)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(9.999999747378752e-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 9.999999747378752 \cdot 10^{-5}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{s}{x \cdot x}\\
\end{array}
\end{array}
if x < 9.99999975e-5Initial program 99.2%
Simplified99.3%
Taylor expanded in s around inf 35.8%
if 9.99999975e-5 < x Initial program 99.8%
Simplified99.8%
Taylor expanded in x around 0 99.8%
+-commutative99.8%
associate-*r/99.8%
mul-1-neg99.8%
Simplified99.8%
Taylor expanded in s around -inf 30.3%
+-commutative30.3%
associate-+r+30.3%
mul-1-neg30.3%
distribute-lft1-in69.5%
metadata-eval69.5%
associate-*r/69.5%
mul-1-neg69.5%
remove-double-neg69.5%
associate-+r+69.5%
Simplified69.5%
Taylor expanded in x around inf 68.2%
unpow268.2%
Simplified68.2%
Final simplification45.2%
(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%
Simplified99.4%
Taylor expanded in s around inf 26.9%
Final simplification26.9%
herbie shell --seed 2023271
(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))))))