
(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 (let* ((t_0 (exp (/ (- (fabs x)) s)))) (/ t_0 (* (+ t_0 1.0) (fma s t_0 s)))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
return t_0 / ((t_0 + 1.0f) * fmaf(s, t_0, s));
}
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) return Float32(t_0 / Float32(Float32(t_0 + Float32(1.0)) * fma(s, t_0, 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 \mathsf{fma}\left(s, t\_0, s\right)}
\end{array}
\end{array}
Initial program 99.3%
*-commutative99.3%
+-commutative99.3%
fabs-neg99.3%
distribute-lft-in99.3%
*-rgt-identity99.3%
fma-define99.3%
fabs-neg99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (x s) :precision binary32 (if (<= (fabs x) 0.019999999552965164) (/ (exp (- (/ x s) (* 2.0 (log1p (exp (/ x s)))))) s) (/ (exp (/ (- x) s)) (* s 4.0))))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 0.019999999552965164f) {
tmp = expf(((x / s) - (2.0f * log1pf(expf((x / s)))))) / s;
} else {
tmp = expf((-x / s)) / (s * 4.0f);
}
return tmp;
}
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(0.019999999552965164)) tmp = Float32(exp(Float32(Float32(x / s) - Float32(Float32(2.0) * log1p(exp(Float32(x / s)))))) / s); else tmp = Float32(exp(Float32(Float32(-x) / s)) / Float32(s * Float32(4.0))); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 0.019999999552965164:\\
\;\;\;\;\frac{e^{\frac{x}{s} - 2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{\frac{-x}{s}}}{s \cdot 4}\\
\end{array}
\end{array}
if (fabs.f32 x) < 0.0199999996Initial program 98.5%
fabs-neg98.5%
distribute-frac-neg98.5%
distribute-frac-neg298.5%
fabs-neg98.5%
*-commutative98.5%
fabs-neg98.5%
+-commutative98.5%
fabs-neg98.5%
Simplified98.5%
distribute-frac-neg298.5%
distribute-frac-neg98.5%
*-un-lft-identity98.5%
times-frac97.6%
Applied egg-rr76.5%
associate-*l/76.6%
*-un-lft-identity76.6%
add-exp-log76.6%
log-div76.5%
add-log-exp97.0%
log-pow98.4%
log1p-define98.4%
Applied egg-rr98.4%
if 0.0199999996 < (fabs.f32 x) Initial program 100.0%
*-commutative100.0%
fabs-neg100.0%
+-commutative100.0%
fabs-neg100.0%
distribute-lft-in100.0%
*-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 100.0%
exp-prod100.0%
rem-square-sqrt50.7%
fabs-sqr50.7%
rem-square-sqrt52.3%
exp-prod52.3%
neg-mul-152.3%
distribute-neg-frac252.3%
+-commutative52.3%
exp-prod52.3%
rem-square-sqrt50.7%
fabs-sqr50.7%
rem-square-sqrt50.7%
exp-prod50.7%
neg-mul-150.7%
distribute-neg-frac250.7%
Simplified50.7%
Taylor expanded in x around 0 52.3%
*-commutative52.3%
Simplified52.3%
Final simplification74.3%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- x) s)))) (/ t_0 (* s (pow (+ 1.0 t_0) 2.0)))))
float code(float x, float s) {
float t_0 = expf((-x / s));
return t_0 / (s * powf((1.0f + t_0), 2.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
t_0 = exp((-x / s))
code = t_0 / (s * ((1.0e0 + t_0) ** 2.0e0))
end function
function code(x, s) t_0 = exp(Float32(Float32(-x) / s)) return Float32(t_0 / Float32(s * (Float32(Float32(1.0) + t_0) ^ Float32(2.0)))) end
function tmp = code(x, s) t_0 = exp((-x / s)); tmp = t_0 / (s * ((single(1.0) + t_0) ^ single(2.0))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-x}{s}}\\
\frac{t\_0}{s \cdot {\left(1 + t\_0\right)}^{2}}
\end{array}
\end{array}
Initial program 99.3%
*-commutative99.3%
+-commutative99.3%
fabs-neg99.3%
distribute-lft-in99.3%
*-rgt-identity99.3%
fma-define99.3%
fabs-neg99.3%
Simplified99.3%
Taylor expanded in x around 0 99.3%
*-commutative99.3%
*-rgt-identity99.3%
distribute-lft-in99.3%
mul-1-neg99.3%
rec-exp99.3%
associate-*r*99.3%
distribute-lft-in99.2%
rec-exp99.3%
mul-1-neg99.3%
Simplified96.2%
Taylor expanded in x around 0 96.2%
rem-square-sqrt53.3%
fabs-sqr53.3%
rem-square-sqrt66.1%
mul-1-neg66.1%
Simplified66.1%
Final simplification66.1%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- x) s)))) (/ t_0 (* (+ 1.0 t_0) (+ s (/ s (+ 1.0 (/ x s))))))))
float code(float x, float s) {
float t_0 = expf((-x / s));
return t_0 / ((1.0f + t_0) * (s + (s / (1.0f + (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((-x / s))
code = t_0 / ((1.0e0 + t_0) * (s + (s / (1.0e0 + (x / s)))))
end function
function code(x, s) t_0 = exp(Float32(Float32(-x) / s)) return Float32(t_0 / Float32(Float32(Float32(1.0) + t_0) * Float32(s + Float32(s / Float32(Float32(1.0) + Float32(x / s)))))) end
function tmp = code(x, s) t_0 = exp((-x / s)); tmp = t_0 / ((single(1.0) + t_0) * (s + (s / (single(1.0) + (x / s))))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-x}{s}}\\
\frac{t\_0}{\left(1 + t\_0\right) \cdot \left(s + \frac{s}{1 + \frac{x}{s}}\right)}
\end{array}
\end{array}
Initial program 99.3%
*-commutative99.3%
fabs-neg99.3%
+-commutative99.3%
fabs-neg99.3%
distribute-lft-in99.3%
*-rgt-identity99.3%
+-commutative99.3%
Simplified99.2%
Taylor expanded in x around 0 99.2%
exp-prod99.2%
rem-square-sqrt53.2%
fabs-sqr53.2%
rem-square-sqrt64.3%
exp-prod64.3%
neg-mul-164.3%
distribute-neg-frac264.3%
+-commutative64.3%
exp-prod64.3%
rem-square-sqrt53.3%
fabs-sqr53.3%
rem-square-sqrt63.4%
exp-prod63.4%
neg-mul-163.4%
distribute-neg-frac263.4%
Simplified66.0%
Taylor expanded in x around 0 62.3%
Final simplification62.3%
(FPCore (x s) :precision binary32 (/ (exp (/ (- x) s)) (+ (* s 4.0) (* x (- (* (/ x s) 3.0) 4.0)))))
float code(float x, float s) {
return expf((-x / s)) / ((s * 4.0f) + (x * (((x / s) * 3.0f) - 4.0f)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = exp((-x / s)) / ((s * 4.0e0) + (x * (((x / s) * 3.0e0) - 4.0e0)))
end function
function code(x, s) return Float32(exp(Float32(Float32(-x) / s)) / Float32(Float32(s * Float32(4.0)) + Float32(x * Float32(Float32(Float32(x / s) * Float32(3.0)) - Float32(4.0))))) end
function tmp = code(x, s) tmp = exp((-x / s)) / ((s * single(4.0)) + (x * (((x / s) * single(3.0)) - single(4.0)))); end
\begin{array}{l}
\\
\frac{e^{\frac{-x}{s}}}{s \cdot 4 + x \cdot \left(\frac{x}{s} \cdot 3 - 4\right)}
\end{array}
Initial program 99.3%
*-commutative99.3%
fabs-neg99.3%
+-commutative99.3%
fabs-neg99.3%
distribute-lft-in99.3%
*-rgt-identity99.3%
+-commutative99.3%
Simplified99.2%
Taylor expanded in x around 0 99.2%
exp-prod99.2%
rem-square-sqrt53.2%
fabs-sqr53.2%
rem-square-sqrt64.3%
exp-prod64.3%
neg-mul-164.3%
distribute-neg-frac264.3%
+-commutative64.3%
exp-prod64.3%
rem-square-sqrt53.3%
fabs-sqr53.3%
rem-square-sqrt63.4%
exp-prod63.4%
neg-mul-163.4%
distribute-neg-frac263.4%
Simplified66.0%
Taylor expanded in x around 0 63.1%
Final simplification63.1%
(FPCore (x s) :precision binary32 (* (/ 1.0 (+ 1.0 (exp (/ x s)))) (/ 0.5 s)))
float code(float x, float s) {
return (1.0f / (1.0f + expf((x / s)))) * (0.5f / 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)))) * (0.5e0 / s)
end function
function code(x, s) return Float32(Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(x / s)))) * Float32(Float32(0.5) / s)) end
function tmp = code(x, s) tmp = (single(1.0) / (single(1.0) + exp((x / s)))) * (single(0.5) / s); end
\begin{array}{l}
\\
\frac{1}{1 + e^{\frac{x}{s}}} \cdot \frac{0.5}{s}
\end{array}
Initial program 99.3%
fabs-neg99.3%
distribute-frac-neg99.3%
distribute-frac-neg299.3%
fabs-neg99.3%
*-commutative99.3%
fabs-neg99.3%
+-commutative99.3%
fabs-neg99.3%
Simplified99.3%
Applied egg-rr62.9%
Taylor expanded in x around 0 62.1%
(FPCore (x s) :precision binary32 (/ (exp (/ (- x) s)) (* s 4.0)))
float code(float x, float s) {
return expf((-x / s)) / (s * 4.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = exp((-x / s)) / (s * 4.0e0)
end function
function code(x, s) return Float32(exp(Float32(Float32(-x) / s)) / Float32(s * Float32(4.0))) end
function tmp = code(x, s) tmp = exp((-x / s)) / (s * single(4.0)); end
\begin{array}{l}
\\
\frac{e^{\frac{-x}{s}}}{s \cdot 4}
\end{array}
Initial program 99.3%
*-commutative99.3%
fabs-neg99.3%
+-commutative99.3%
fabs-neg99.3%
distribute-lft-in99.3%
*-rgt-identity99.3%
+-commutative99.3%
Simplified99.2%
Taylor expanded in x around 0 99.2%
exp-prod99.2%
rem-square-sqrt53.2%
fabs-sqr53.2%
rem-square-sqrt64.3%
exp-prod64.3%
neg-mul-164.3%
distribute-neg-frac264.3%
+-commutative64.3%
exp-prod64.3%
rem-square-sqrt53.3%
fabs-sqr53.3%
rem-square-sqrt63.4%
exp-prod63.4%
neg-mul-163.4%
distribute-neg-frac263.4%
Simplified66.0%
Taylor expanded in x around 0 61.3%
*-commutative61.3%
Simplified61.3%
Final simplification61.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.3%
fabs-neg99.3%
distribute-frac-neg99.3%
distribute-frac-neg299.3%
fabs-neg99.3%
*-commutative99.3%
fabs-neg99.3%
+-commutative99.3%
fabs-neg99.3%
Simplified99.3%
Taylor expanded in s around inf 28.9%
herbie shell --seed 2024157
(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))))))