
(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 8 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 (* (+ 1.0 (exp (/ x s))) (+ s (* s (exp (/ (- x) s)))))))
float code(float x, float s) {
return 1.0f / ((1.0f + expf((x / s))) * (s + (s * expf((-x / s)))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((1.0e0 + exp((x / s))) * (s + (s * exp((-x / s)))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(x / s))) * Float32(s + Float32(s * exp(Float32(Float32(-x) / s)))))) end
function tmp = code(x, s) tmp = single(1.0) / ((single(1.0) + exp((x / s))) * (s + (s * exp((-x / s))))); end
\begin{array}{l}
\\
\frac{1}{\left(1 + e^{\frac{x}{s}}\right) \cdot \left(s + s \cdot e^{\frac{-x}{s}}\right)}
\end{array}
Initial program 99.4%
Simplified99.5%
add-sqr-sqrt99.4%
sqrt-unprod94.0%
sqr-neg94.0%
sqrt-unprod-0.0%
add-sqr-sqrt23.8%
expm1-log1p-u23.8%
expm1-udef23.8%
Applied egg-rr65.3%
expm1-def65.3%
expm1-log1p65.3%
+-commutative65.3%
Simplified65.3%
distribute-frac-neg65.3%
rec-exp65.3%
add-sqr-sqrt55.8%
fabs-sqr55.8%
add-sqr-sqrt99.5%
Applied egg-rr99.5%
rec-exp99.5%
distribute-neg-frac99.5%
Simplified99.5%
Taylor expanded in x around inf 99.5%
mul-1-neg99.5%
exp-neg99.5%
Applied egg-rr99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x s) :precision binary32 (/ 1.0 (* s (* (+ 1.0 (exp (/ x s))) (+ 1.0 (exp (/ (- x) s)))))))
float code(float x, float s) {
return 1.0f / (s * ((1.0f + expf((x / s))) * (1.0f + expf((-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((-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(-x) / s)))))) end
function tmp = code(x, s) tmp = single(1.0) / (s * ((single(1.0) + exp((x / s))) * (single(1.0) + exp((-x / s))))); end
\begin{array}{l}
\\
\frac{1}{s \cdot \left(\left(1 + e^{\frac{x}{s}}\right) \cdot \left(1 + e^{\frac{-x}{s}}\right)\right)}
\end{array}
Initial program 99.4%
Simplified99.5%
add-sqr-sqrt99.4%
sqrt-unprod94.0%
sqr-neg94.0%
sqrt-unprod-0.0%
add-sqr-sqrt23.8%
expm1-log1p-u23.8%
expm1-udef23.8%
Applied egg-rr65.3%
expm1-def65.3%
expm1-log1p65.3%
+-commutative65.3%
Simplified65.3%
distribute-frac-neg65.3%
rec-exp65.3%
add-sqr-sqrt55.8%
fabs-sqr55.8%
add-sqr-sqrt99.5%
Applied egg-rr99.5%
rec-exp99.5%
distribute-neg-frac99.5%
Simplified99.5%
Taylor expanded in s around 0 99.5%
mul-1-neg99.5%
exp-neg99.5%
Applied egg-rr99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x s) :precision binary32 (/ 0.5 (* s (+ 1.0 (exp (/ x s))))))
float code(float x, float s) {
return 0.5f / (s * (1.0f + expf((x / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (s * (1.0e0 + exp((x / s))))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(1.0) + exp(Float32(x / s))))) end
function tmp = code(x, s) tmp = single(0.5) / (s * (single(1.0) + exp((x / s)))); end
\begin{array}{l}
\\
\frac{0.5}{s \cdot \left(1 + e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.4%
Simplified99.5%
add-sqr-sqrt99.4%
sqrt-unprod94.0%
sqr-neg94.0%
sqrt-unprod-0.0%
add-sqr-sqrt23.8%
expm1-log1p-u23.8%
expm1-udef23.8%
Applied egg-rr65.3%
expm1-def65.3%
expm1-log1p65.3%
+-commutative65.3%
Simplified65.3%
Taylor expanded in s around inf 62.3%
Taylor expanded in s around 0 62.3%
Final simplification62.3%
(FPCore (x s) :precision binary32 (if (<= x 1.999999936531045e-21) (/ 1.0 (+ (* s 4.0) (* x (/ x s)))) (/ 1.0 (exp (/ x s)))))
float code(float x, float s) {
float tmp;
if (x <= 1.999999936531045e-21f) {
tmp = 1.0f / ((s * 4.0f) + (x * (x / s)));
} else {
tmp = 1.0f / expf((x / s));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 1.999999936531045e-21) then
tmp = 1.0e0 / ((s * 4.0e0) + (x * (x / s)))
else
tmp = 1.0e0 / exp((x / s))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.999999936531045e-21)) tmp = Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(x * Float32(x / s)))); else tmp = Float32(Float32(1.0) / exp(Float32(x / s))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(1.999999936531045e-21)) tmp = single(1.0) / ((s * single(4.0)) + (x * (x / s))); else tmp = single(1.0) / exp((x / s)); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.999999936531045 \cdot 10^{-21}:\\
\;\;\;\;\frac{1}{s \cdot 4 + x \cdot \frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{\frac{x}{s}}}\\
\end{array}
\end{array}
if x < 1.9999999e-21Initial program 99.1%
Simplified99.2%
add-sqr-sqrt99.0%
sqrt-unprod89.8%
sqr-neg89.8%
sqrt-unprod-0.0%
add-sqr-sqrt34.4%
expm1-log1p-u34.4%
expm1-udef34.4%
Applied egg-rr40.4%
expm1-def40.4%
expm1-log1p40.4%
+-commutative40.4%
Simplified40.4%
distribute-frac-neg40.4%
rec-exp40.5%
add-sqr-sqrt24.0%
fabs-sqr24.0%
add-sqr-sqrt99.2%
Applied egg-rr99.2%
rec-exp99.2%
distribute-neg-frac99.2%
Simplified99.2%
Taylor expanded in x around 0 70.7%
div-inv70.7%
unpow270.7%
associate-*l*71.7%
div-inv71.7%
Applied egg-rr71.7%
if 1.9999999e-21 < x Initial program 99.9%
Simplified99.9%
add-sqr-sqrt99.9%
sqrt-unprod99.9%
sqr-neg99.9%
sqrt-unprod-0.0%
add-sqr-sqrt9.0%
expm1-log1p-u9.0%
expm1-udef9.0%
Applied egg-rr99.9%
expm1-def99.8%
expm1-log1p99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in s around inf 96.8%
/-rgt-identity96.8%
clear-num96.8%
associate-/r*95.9%
fma-udef95.9%
*-rgt-identity95.9%
Applied egg-rr95.9%
associate-/r/95.9%
remove-double-div96.8%
distribute-lft-in96.8%
metadata-eval96.8%
div-inv96.8%
/-rgt-identity96.8%
flip-+-0.0%
+-inverses-0.0%
+-inverses-0.0%
flip-+-0.0%
+-inverses-0.0%
+-inverses-0.0%
Applied egg-rr-0.0%
Simplified93.0%
Final simplification80.6%
(FPCore (x s) :precision binary32 (/ 1.0 (+ (* s 4.0) (* x (/ x s)))))
float code(float x, float s) {
return 1.0f / ((s * 4.0f) + (x * (x / 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 / s)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(x * Float32(x / s)))) end
function tmp = code(x, s) tmp = single(1.0) / ((s * single(4.0)) + (x * (x / s))); end
\begin{array}{l}
\\
\frac{1}{s \cdot 4 + x \cdot \frac{x}{s}}
\end{array}
Initial program 99.4%
Simplified99.5%
add-sqr-sqrt99.4%
sqrt-unprod94.0%
sqr-neg94.0%
sqrt-unprod-0.0%
add-sqr-sqrt23.8%
expm1-log1p-u23.8%
expm1-udef23.8%
Applied egg-rr65.3%
expm1-def65.3%
expm1-log1p65.3%
+-commutative65.3%
Simplified65.3%
distribute-frac-neg65.3%
rec-exp65.3%
add-sqr-sqrt55.8%
fabs-sqr55.8%
add-sqr-sqrt99.5%
Applied egg-rr99.5%
rec-exp99.5%
distribute-neg-frac99.5%
Simplified99.5%
Taylor expanded in x around 0 63.9%
div-inv63.9%
unpow263.9%
associate-*l*64.5%
div-inv64.5%
Applied egg-rr64.5%
Final simplification64.5%
(FPCore (x s) :precision binary32 (/ 1.0 (* s (+ (/ x s) 4.0))))
float code(float x, float s) {
return 1.0f / (s * ((x / s) + 4.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (s * ((x / s) + 4.0e0))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(x / s) + Float32(4.0)))) end
function tmp = code(x, s) tmp = single(1.0) / (s * ((x / s) + single(4.0))); end
\begin{array}{l}
\\
\frac{1}{s \cdot \left(\frac{x}{s} + 4\right)}
\end{array}
Initial program 99.4%
Simplified99.5%
add-sqr-sqrt99.4%
sqrt-unprod94.0%
sqr-neg94.0%
sqrt-unprod-0.0%
add-sqr-sqrt23.8%
expm1-log1p-u23.8%
expm1-udef23.8%
Applied egg-rr65.3%
expm1-def65.3%
expm1-log1p65.3%
+-commutative65.3%
Simplified65.3%
distribute-frac-neg65.3%
rec-exp65.3%
add-sqr-sqrt55.8%
fabs-sqr55.8%
add-sqr-sqrt99.5%
Applied egg-rr99.5%
rec-exp99.5%
distribute-neg-frac99.5%
Simplified99.5%
Taylor expanded in s around 0 99.5%
Taylor expanded in s around inf 24.9%
Simplified49.1%
Final simplification49.1%
(FPCore (x s) :precision binary32 (if (<= x 0.006000000052154064) (/ 0.25 s) (/ 1.0 (* x 2.0))))
float code(float x, float s) {
float tmp;
if (x <= 0.006000000052154064f) {
tmp = 0.25f / s;
} else {
tmp = 1.0f / (x * 2.0f);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 0.006000000052154064e0) then
tmp = 0.25e0 / s
else
tmp = 1.0e0 / (x * 2.0e0)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.006000000052154064)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(1.0) / Float32(x * Float32(2.0))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.006000000052154064)) tmp = single(0.25) / s; else tmp = single(1.0) / (x * single(2.0)); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.006000000052154064:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x \cdot 2}\\
\end{array}
\end{array}
if x < 0.00600000005Initial program 99.2%
*-commutative99.2%
distribute-lft-in99.3%
*-rgt-identity99.3%
fabs-neg99.3%
distribute-frac-neg99.3%
exp-neg99.2%
associate-*r/99.2%
*-rgt-identity99.2%
*-lft-identity99.2%
metadata-eval99.2%
times-frac99.2%
neg-mul-199.2%
neg-mul-199.2%
fabs-neg99.2%
Simplified99.2%
Taylor expanded in s around inf 34.0%
if 0.00600000005 < x Initial program 100.0%
Simplified100.0%
add-sqr-sqrt100.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-unprod-0.0%
add-sqr-sqrt4.5%
expm1-log1p-u4.5%
expm1-udef4.5%
Applied egg-rr100.0%
expm1-def100.0%
expm1-log1p100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in s around inf 3.0%
Taylor expanded in x around inf 14.4%
*-commutative14.4%
Simplified14.4%
Final simplification28.6%
(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%
*-commutative99.4%
distribute-lft-in99.5%
*-rgt-identity99.5%
fabs-neg99.5%
distribute-frac-neg99.5%
exp-neg99.4%
associate-*r/99.4%
*-rgt-identity99.4%
*-lft-identity99.4%
metadata-eval99.4%
times-frac99.4%
neg-mul-199.4%
neg-mul-199.4%
fabs-neg99.4%
Simplified99.4%
Taylor expanded in s around inf 25.9%
Final simplification25.9%
herbie shell --seed 2024019
(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))))))