
(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 (* (fma s (exp (/ (- (fabs x)) s)) s) (+ 1.0 (exp (/ (fabs x) s))))))
float code(float x, float s) {
return 1.0f / (fmaf(s, expf((-fabsf(x) / s)), s) * (1.0f + expf((fabsf(x) / s))));
}
function code(x, s) return Float32(Float32(1.0) / Float32(fma(s, exp(Float32(Float32(-abs(x)) / s)), s) * Float32(Float32(1.0) + exp(Float32(abs(x) / s))))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(s, e^{\frac{-\left|x\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{\left|x\right|}{s}}\right)}
\end{array}
Initial program 99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (/ (/ 1.0 (+ 1.0 (exp (/ x s)))) (- (* s 2.0) x)))
float code(float x, float s) {
return (1.0f / (1.0f + expf((x / s)))) / ((s * 2.0f) - x);
}
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 * 2.0e0) - x)
end function
function code(x, s) return Float32(Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(x / s)))) / Float32(Float32(s * Float32(2.0)) - x)) end
function tmp = code(x, s) tmp = (single(1.0) / (single(1.0) + exp((x / s)))) / ((s * single(2.0)) - x); end
\begin{array}{l}
\\
\frac{\frac{1}{1 + e^{\frac{x}{s}}}}{s \cdot 2 - x}
\end{array}
Initial program 99.8%
*-commutative99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
exp-neg99.8%
associate-*r/99.8%
*-rgt-identity99.8%
*-lft-identity99.8%
metadata-eval99.8%
times-frac99.8%
neg-mul-199.8%
neg-mul-199.8%
fabs-neg99.8%
Simplified99.8%
*-un-lft-identity99.8%
clear-num99.8%
associate-/r/99.8%
rec-exp99.8%
distribute-frac-neg99.8%
distribute-rgt-in99.8%
*-commutative99.8%
associate-/l/99.8%
associate-/l/99.8%
Applied egg-rr58.8%
clear-num58.8%
frac-times58.8%
*-un-lft-identity58.8%
+-commutative58.8%
+-commutative58.8%
Applied egg-rr58.8%
Taylor expanded in x around 0 65.0%
Final simplification65.0%
(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.8%
Simplified99.8%
Taylor expanded in s around inf 96.9%
distribute-rgt-in96.9%
*-un-lft-identity96.9%
add-sqr-sqrt51.8%
fabs-sqr51.8%
add-sqr-sqrt62.1%
add-sqr-sqrt51.8%
add-sqr-sqrt62.1%
Applied egg-rr62.1%
distribute-rgt1-in62.1%
+-commutative62.1%
fma-udef62.1%
*-rgt-identity62.1%
Simplified62.1%
Taylor expanded in x around inf 62.1%
Final simplification62.1%
(FPCore (x s) :precision binary32 (/ (/ 1.0 (+ (/ x s) 2.0)) (- (* s 2.0) x)))
float code(float x, float s) {
return (1.0f / ((x / s) + 2.0f)) / ((s * 2.0f) - x);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / ((x / s) + 2.0e0)) / ((s * 2.0e0) - x)
end function
function code(x, s) return Float32(Float32(Float32(1.0) / Float32(Float32(x / s) + Float32(2.0))) / Float32(Float32(s * Float32(2.0)) - x)) end
function tmp = code(x, s) tmp = (single(1.0) / ((x / s) + single(2.0))) / ((s * single(2.0)) - x); end
\begin{array}{l}
\\
\frac{\frac{1}{\frac{x}{s} + 2}}{s \cdot 2 - x}
\end{array}
Initial program 99.8%
*-commutative99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
exp-neg99.8%
associate-*r/99.8%
*-rgt-identity99.8%
*-lft-identity99.8%
metadata-eval99.8%
times-frac99.8%
neg-mul-199.8%
neg-mul-199.8%
fabs-neg99.8%
Simplified99.8%
*-un-lft-identity99.8%
clear-num99.8%
associate-/r/99.8%
rec-exp99.8%
distribute-frac-neg99.8%
distribute-rgt-in99.8%
*-commutative99.8%
associate-/l/99.8%
associate-/l/99.8%
Applied egg-rr58.8%
clear-num58.8%
frac-times58.8%
*-un-lft-identity58.8%
+-commutative58.8%
+-commutative58.8%
Applied egg-rr58.8%
Taylor expanded in x around 0 65.0%
Taylor expanded in x around 0 58.9%
Final simplification58.9%
(FPCore (x s) :precision binary32 (/ 0.5 (* s (+ (/ x s) 2.0))))
float code(float x, float s) {
return 0.5f / (s * ((x / s) + 2.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (s * ((x / s) + 2.0e0))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(s * Float32(Float32(x / s) + Float32(2.0)))) end
function tmp = code(x, s) tmp = single(0.5) / (s * ((x / s) + single(2.0))); end
\begin{array}{l}
\\
\frac{0.5}{s \cdot \left(\frac{x}{s} + 2\right)}
\end{array}
Initial program 99.8%
Simplified99.8%
Taylor expanded in s around inf 96.9%
distribute-rgt-in96.9%
*-un-lft-identity96.9%
add-sqr-sqrt51.8%
fabs-sqr51.8%
add-sqr-sqrt62.1%
add-sqr-sqrt51.8%
add-sqr-sqrt62.1%
Applied egg-rr62.1%
distribute-rgt1-in62.1%
+-commutative62.1%
fma-udef62.1%
*-rgt-identity62.1%
Simplified62.1%
Taylor expanded in x around inf 62.1%
Taylor expanded in x around 0 46.2%
Final simplification46.2%
(FPCore (x s) :precision binary32 (if (<= x 0.00019999999494757503) (/ 0.25 s) (/ 0.5 (+ x -2.0))))
float code(float x, float s) {
float tmp;
if (x <= 0.00019999999494757503f) {
tmp = 0.25f / s;
} else {
tmp = 0.5f / (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.00019999999494757503e0) then
tmp = 0.25e0 / s
else
tmp = 0.5e0 / (x + (-2.0e0))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.00019999999494757503)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.5) / Float32(x + Float32(-2.0))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.00019999999494757503)) tmp = single(0.25) / s; else tmp = single(0.5) / (x + single(-2.0)); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.00019999999494757503:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x + -2}\\
\end{array}
\end{array}
if x < 1.99999995e-4Initial program 99.7%
*-commutative99.7%
distribute-lft-in99.8%
*-rgt-identity99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
exp-neg99.7%
associate-*r/99.7%
*-rgt-identity99.7%
*-lft-identity99.7%
metadata-eval99.7%
times-frac99.7%
neg-mul-199.7%
neg-mul-199.7%
fabs-neg99.7%
Simplified99.7%
Taylor expanded in s around inf 33.0%
if 1.99999995e-4 < x Initial program 99.9%
Simplified99.9%
Taylor expanded in s around inf 99.1%
distribute-rgt-in99.1%
*-un-lft-identity99.1%
add-sqr-sqrt99.1%
fabs-sqr99.1%
add-sqr-sqrt99.1%
add-sqr-sqrt99.1%
add-sqr-sqrt99.1%
Applied egg-rr99.1%
distribute-rgt1-in99.1%
+-commutative99.1%
fma-udef99.1%
*-rgt-identity99.1%
Simplified99.1%
Taylor expanded in x around inf 99.1%
Taylor expanded in s around inf 10.4%
Simplified10.3%
Final simplification26.0%
(FPCore (x s) :precision binary32 (/ 0.5 (+ x (* s 2.0))))
float code(float x, float s) {
return 0.5f / (x + (s * 2.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.5e0 / (x + (s * 2.0e0))
end function
function code(x, s) return Float32(Float32(0.5) / Float32(x + Float32(s * Float32(2.0)))) end
function tmp = code(x, s) tmp = single(0.5) / (x + (s * single(2.0))); end
\begin{array}{l}
\\
\frac{0.5}{x + s \cdot 2}
\end{array}
Initial program 99.8%
Simplified99.8%
Taylor expanded in s around inf 96.9%
distribute-rgt-in96.9%
*-un-lft-identity96.9%
add-sqr-sqrt51.8%
fabs-sqr51.8%
add-sqr-sqrt62.1%
add-sqr-sqrt51.8%
add-sqr-sqrt62.1%
Applied egg-rr62.1%
distribute-rgt1-in62.1%
+-commutative62.1%
fma-udef62.1%
*-rgt-identity62.1%
Simplified62.1%
Taylor expanded in x around inf 62.1%
Taylor expanded in s around inf 26.4%
Final simplification26.4%
(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.8%
*-commutative99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
exp-neg99.8%
associate-*r/99.8%
*-rgt-identity99.8%
*-lft-identity99.8%
metadata-eval99.8%
times-frac99.8%
neg-mul-199.8%
neg-mul-199.8%
fabs-neg99.8%
Simplified99.8%
Taylor expanded in s around inf 24.3%
Final simplification24.3%
(FPCore (x s) :precision binary32 0.25)
float code(float x, float s) {
return 0.25f;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.25e0
end function
function code(x, s) return Float32(0.25) end
function tmp = code(x, s) tmp = single(0.25); end
\begin{array}{l}
\\
0.25
\end{array}
Initial program 99.8%
*-commutative99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
exp-neg99.8%
associate-*r/99.8%
*-rgt-identity99.8%
*-lft-identity99.8%
metadata-eval99.8%
times-frac99.8%
neg-mul-199.8%
neg-mul-199.8%
fabs-neg99.8%
Simplified99.8%
*-un-lft-identity99.8%
clear-num99.8%
associate-/r/99.8%
rec-exp99.8%
distribute-frac-neg99.8%
distribute-rgt-in99.8%
*-commutative99.8%
associate-/l/99.8%
associate-/l/99.8%
Applied egg-rr58.8%
clear-num58.8%
frac-times58.8%
*-un-lft-identity58.8%
+-commutative58.8%
+-commutative58.8%
Applied egg-rr58.8%
Taylor expanded in x around 0 65.0%
Taylor expanded in x around 0 24.3%
Simplified7.7%
Final simplification7.7%
herbie shell --seed 2023332
(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))))))