
(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 11 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 (/ (- (fabs x)) s))) (fma s (exp (/ (fabs x) s)) s))))
float code(float x, float s) {
return 1.0f / ((1.0f + expf((-fabsf(x) / s))) * fmaf(s, expf((fabsf(x) / s)), s));
}
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(Float32(-abs(x)) / s))) * fma(s, exp(Float32(abs(x) / s)), s))) end
\begin{array}{l}
\\
\frac{1}{\left(1 + e^{\frac{-\left|x\right|}{s}}\right) \cdot \mathsf{fma}\left(s, e^{\frac{\left|x\right|}{s}}, s\right)}
\end{array}
Initial program 99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (x s) :precision binary32 (/ 1.0 (* (+ 1.0 (exp (/ (- (fabs x)) s))) (fma s (pow E (/ x s)) s))))
float code(float x, float s) {
return 1.0f / ((1.0f + expf((-fabsf(x) / s))) * fmaf(s, powf(((float) M_E), (x / s)), s));
}
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(Float32(-abs(x)) / s))) * fma(s, (Float32(exp(1)) ^ Float32(x / s)), s))) end
\begin{array}{l}
\\
\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.7%
Simplified99.7%
*-un-lft-identity96.9%
exp-prod96.9%
add-sqr-sqrt49.3%
fabs-sqr49.3%
add-sqr-sqrt62.3%
Applied egg-rr63.6%
exp-1-e62.3%
Simplified63.6%
Final simplification63.6%
(FPCore (x s) :precision binary32 (/ 0.5 (+ s (* s (exp (/ (* x (log E)) s))))))
float code(float x, float s) {
return 0.5f / (s + (s * expf(((x * logf(((float) M_E))) / s))));
}
function code(x, s) return Float32(Float32(0.5) / Float32(s + Float32(s * exp(Float32(Float32(x * log(Float32(exp(1)))) / s))))) end
function tmp = code(x, s) tmp = single(0.5) / (s + (s * exp(((x * log(single(2.71828182845904523536))) / s)))); end
\begin{array}{l}
\\
\frac{0.5}{s + s \cdot e^{\frac{x \cdot \log e}{s}}}
\end{array}
Initial program 99.7%
Simplified99.7%
Taylor expanded in s around inf 96.9%
*-un-lft-identity96.9%
exp-prod96.9%
add-sqr-sqrt49.3%
fabs-sqr49.3%
add-sqr-sqrt62.3%
Applied egg-rr62.3%
exp-1-e62.3%
Simplified62.3%
Taylor expanded in x around inf 62.3%
Final simplification62.3%
(FPCore (x s) :precision binary32 (/ 1.0 (* (fma s (pow E (/ x s)) s) 2.0)))
float code(float x, float s) {
return 1.0f / (fmaf(s, powf(((float) M_E), (x / s)), s) * 2.0f);
}
function code(x, s) return Float32(Float32(1.0) / Float32(fma(s, (Float32(exp(1)) ^ Float32(x / s)), s) * Float32(2.0))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(s, {e}^{\left(\frac{x}{s}\right)}, s\right) \cdot 2}
\end{array}
Initial program 99.7%
Simplified99.7%
Taylor expanded in s around inf 96.9%
*-un-lft-identity96.9%
exp-prod96.9%
add-sqr-sqrt49.3%
fabs-sqr49.3%
add-sqr-sqrt62.3%
Applied egg-rr62.3%
exp-1-e62.3%
Simplified62.3%
Final simplification62.3%
(FPCore (x s) :precision binary32 (/ 1.0 (* 2.0 (+ s (* s (pow E (/ x s)))))))
float code(float x, float s) {
return 1.0f / (2.0f * (s + (s * powf(((float) M_E), (x / s)))));
}
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(2.0) * Float32(s + Float32(s * (Float32(exp(1)) ^ Float32(x / s)))))) end
function tmp = code(x, s) tmp = single(1.0) / (single(2.0) * (s + (s * (single(2.71828182845904523536) ^ (x / s))))); end
\begin{array}{l}
\\
\frac{1}{2 \cdot \left(s + s \cdot {e}^{\left(\frac{x}{s}\right)}\right)}
\end{array}
Initial program 99.7%
Simplified99.7%
Taylor expanded in s around inf 96.9%
fma-udef96.9%
add-sqr-sqrt49.3%
fabs-sqr49.3%
add-sqr-sqrt62.3%
Applied egg-rr62.3%
*-un-lft-identity62.3%
pow-exp62.3%
e-exp-162.3%
Applied egg-rr62.3%
Final simplification62.3%
(FPCore (x s) :precision binary32 (/ 0.5 (+ s (* s (exp (/ x s))))))
float code(float x, float s) {
return 0.5f / (s + (s * expf((x / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (s + (s * exp((x / s))))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(s + Float32(s * exp(Float32(x / s))))) end
function tmp = code(x, s) tmp = single(0.5) / (s + (s * exp((x / s)))); end
\begin{array}{l}
\\
\frac{0.5}{s + s \cdot e^{\frac{x}{s}}}
\end{array}
Initial program 99.7%
Simplified99.7%
Taylor expanded in s around inf 96.9%
fma-udef96.9%
add-sqr-sqrt49.3%
fabs-sqr49.3%
add-sqr-sqrt62.3%
Applied egg-rr62.3%
Taylor expanded in x around inf 62.3%
Final simplification62.3%
(FPCore (x s) :precision binary32 (if (<= x 1.9999999949504854e-6) (/ 0.25 s) (* (/ s x) (/ 1.0 x))))
float code(float x, float s) {
float tmp;
if (x <= 1.9999999949504854e-6f) {
tmp = 0.25f / s;
} else {
tmp = (s / x) * (1.0f / x);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 1.9999999949504854e-6) then
tmp = 0.25e0 / s
else
tmp = (s / x) * (1.0e0 / x)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.9999999949504854e-6)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(s / x) * Float32(Float32(1.0) / x)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(1.9999999949504854e-6)) tmp = single(0.25) / s; else tmp = (s / x) * (single(1.0) / x); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.9999999949504854 \cdot 10^{-6}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{s}{x} \cdot \frac{1}{x}\\
\end{array}
\end{array}
if x < 1.99999999e-6Initial program 99.6%
Simplified99.7%
Taylor expanded in s around inf 36.2%
if 1.99999999e-6 < x Initial program 99.9%
Simplified99.9%
Taylor expanded in s around -inf 27.5%
+-commutative27.5%
mul-1-neg27.5%
distribute-lft1-in73.5%
metadata-eval73.5%
associate-*r/73.5%
mul-1-neg73.5%
remove-double-neg73.5%
associate-+r+73.5%
Simplified73.5%
Taylor expanded in s around 0 71.8%
*-un-lft-identity71.8%
unpow271.8%
times-frac71.8%
Applied egg-rr71.8%
Final simplification48.3%
(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.7%
Simplified99.7%
Taylor expanded in s around -inf 40.6%
+-commutative40.6%
mul-1-neg40.6%
distribute-lft1-in65.6%
metadata-eval65.6%
associate-*r/65.6%
mul-1-neg65.6%
remove-double-neg65.6%
associate-+r+65.6%
Simplified65.6%
+-commutative65.6%
add-sqr-sqrt65.6%
fma-def65.6%
sqrt-div65.6%
sqrt-prod35.6%
add-sqr-sqrt65.5%
sqrt-div65.5%
sqrt-prod35.6%
add-sqr-sqrt65.9%
+-rgt-identity65.9%
Applied egg-rr65.9%
fma-udef65.9%
unpow265.9%
Simplified65.9%
unpow265.9%
div-inv65.9%
associate-*l*65.9%
times-frac65.9%
*-un-lft-identity65.9%
add-sqr-sqrt65.9%
Applied egg-rr65.9%
Final simplification65.9%
(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.7%
Simplified99.7%
Taylor expanded in s around -inf 40.6%
+-commutative40.6%
mul-1-neg40.6%
distribute-lft1-in65.6%
metadata-eval65.6%
associate-*r/65.6%
mul-1-neg65.6%
remove-double-neg65.6%
associate-+r+65.6%
Simplified65.6%
+-commutative65.6%
add-sqr-sqrt65.6%
fma-def65.6%
sqrt-div65.6%
sqrt-prod35.6%
add-sqr-sqrt65.5%
sqrt-div65.5%
sqrt-prod35.6%
add-sqr-sqrt65.9%
+-rgt-identity65.9%
Applied egg-rr65.9%
fma-udef65.9%
unpow265.9%
Simplified65.9%
unpow265.9%
frac-2neg65.9%
clear-num65.9%
frac-2neg65.9%
add-sqr-sqrt35.6%
sqrt-prod65.5%
unpow265.5%
sqrt-div65.5%
frac-times65.5%
*-un-lft-identity65.5%
sqrt-div65.5%
unpow265.5%
sqrt-prod35.6%
add-sqr-sqrt65.9%
Applied egg-rr65.9%
associate-/l/65.9%
associate-/r*65.9%
associate-*r/65.9%
rem-square-sqrt65.9%
Simplified65.9%
Final simplification65.9%
(FPCore (x s) :precision binary32 (if (<= x 1.9999999949504854e-6) (/ 0.25 s) (/ s (* x x))))
float code(float x, float s) {
float tmp;
if (x <= 1.9999999949504854e-6f) {
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 <= 1.9999999949504854e-6) 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(1.9999999949504854e-6)) 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(1.9999999949504854e-6)) 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 1.9999999949504854 \cdot 10^{-6}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{s}{x \cdot x}\\
\end{array}
\end{array}
if x < 1.99999999e-6Initial program 99.6%
Simplified99.7%
Taylor expanded in s around inf 36.2%
if 1.99999999e-6 < x Initial program 99.9%
Simplified99.9%
Taylor expanded in s around -inf 27.5%
+-commutative27.5%
mul-1-neg27.5%
distribute-lft1-in73.5%
metadata-eval73.5%
associate-*r/73.5%
mul-1-neg73.5%
remove-double-neg73.5%
associate-+r+73.5%
Simplified73.5%
Taylor expanded in s around 0 71.8%
unpow271.8%
Applied egg-rr71.8%
Final simplification48.3%
(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.7%
Simplified99.7%
Taylor expanded in s around inf 25.5%
Final simplification25.5%
herbie shell --seed 2024021
(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))))))