
(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 (pow (+ t_0 1.0) -2.0)) s)))
float code(float x, float s) {
float t_0 = expf(-(fabsf(x) / s));
return (t_0 * powf((t_0 + 1.0f), -2.0f)) / 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) ** (-2.0e0))) / s
end function
function code(x, s) t_0 = exp(Float32(-Float32(abs(x) / s))) return Float32(Float32(t_0 * (Float32(t_0 + Float32(1.0)) ^ Float32(-2.0))) / s) end
function tmp = code(x, s) t_0 = exp(-(abs(x) / s)); tmp = (t_0 * ((t_0 + single(1.0)) ^ single(-2.0))) / s; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-\frac{\left|x\right|}{s}}\\
\frac{t\_0 \cdot {\left(t\_0 + 1\right)}^{-2}}{s}
\end{array}
\end{array}
Initial program 99.7%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x s)
:precision binary32
(let* ((t_0 (exp (- (/ (fabs x) s)))) (t_1 (+ t_0 1.0)))
(if (<= (/ t_0 (* t_1 (* s t_1))) 0.0)
0.0
(/ (fma (/ (* x (/ x s)) s) -0.0625 0.25) s))))
float code(float x, float s) {
float t_0 = expf(-(fabsf(x) / s));
float t_1 = t_0 + 1.0f;
float tmp;
if ((t_0 / (t_1 * (s * t_1))) <= 0.0f) {
tmp = 0.0f;
} else {
tmp = fmaf(((x * (x / s)) / s), -0.0625f, 0.25f) / s;
}
return tmp;
}
function code(x, s) t_0 = exp(Float32(-Float32(abs(x) / s))) t_1 = Float32(t_0 + Float32(1.0)) tmp = Float32(0.0) if (Float32(t_0 / Float32(t_1 * Float32(s * t_1))) <= Float32(0.0)) tmp = Float32(0.0); else tmp = Float32(fma(Float32(Float32(x * Float32(x / s)) / s), Float32(-0.0625), Float32(0.25)) / s); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-\frac{\left|x\right|}{s}}\\
t_1 := t\_0 + 1\\
\mathbf{if}\;\frac{t\_0}{t\_1 \cdot \left(s \cdot t\_1\right)} \leq 0:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{x \cdot \frac{x}{s}}{s}, -0.0625, 0.25\right)}{s}\\
\end{array}
\end{array}
if (/.f32 (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)) (*.f32 (*.f32 s (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)))) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s))))) < 0.0Initial program 99.9%
Taylor expanded in s around -inf
Simplified0.2%
Taylor expanded in s around 0
div-subN/A
*-commutativeN/A
metadata-evalN/A
distribute-rgt-outN/A
+-inversesN/A
metadata-eval99.9
Simplified99.9%
if 0.0 < (/.f32 (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)) (*.f32 (*.f32 s (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)))) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s))))) Initial program 99.2%
Taylor expanded in s around -inf
Simplified61.4%
Taylor expanded in x around 0
+-commutativeN/A
unpow2N/A
associate-*l*N/A
accelerator-lowering-fma.f32N/A
Simplified61.4%
Taylor expanded in s around inf
/-lowering-/.f32N/A
+-commutativeN/A
*-commutativeN/A
accelerator-lowering-fma.f32N/A
/-lowering-/.f32N/A
unpow2N/A
*-lowering-*.f32N/A
unpow2N/A
*-lowering-*.f3279.4
Simplified79.4%
times-fracN/A
associate-*r/N/A
/-lowering-/.f32N/A
*-lowering-*.f32N/A
/-lowering-/.f3295.9
Applied egg-rr95.9%
Final simplification98.8%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (- (/ (fabs x) s)))) (t_1 (+ t_0 1.0))) (if (<= (/ t_0 (* t_1 (* s t_1))) 0.0) 0.0 (/ 0.25 s))))
float code(float x, float s) {
float t_0 = expf(-(fabsf(x) / s));
float t_1 = t_0 + 1.0f;
float tmp;
if ((t_0 / (t_1 * (s * t_1))) <= 0.0f) {
tmp = 0.0f;
} else {
tmp = 0.25f / s;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: t_1
real(4) :: tmp
t_0 = exp(-(abs(x) / s))
t_1 = t_0 + 1.0e0
if ((t_0 / (t_1 * (s * t_1))) <= 0.0e0) then
tmp = 0.0e0
else
tmp = 0.25e0 / s
end if
code = tmp
end function
function code(x, s) t_0 = exp(Float32(-Float32(abs(x) / s))) t_1 = Float32(t_0 + Float32(1.0)) tmp = Float32(0.0) if (Float32(t_0 / Float32(t_1 * Float32(s * t_1))) <= Float32(0.0)) tmp = Float32(0.0); else tmp = Float32(Float32(0.25) / s); end return tmp end
function tmp_2 = code(x, s) t_0 = exp(-(abs(x) / s)); t_1 = t_0 + single(1.0); tmp = single(0.0); if ((t_0 / (t_1 * (s * t_1))) <= single(0.0)) tmp = single(0.0); else tmp = single(0.25) / s; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-\frac{\left|x\right|}{s}}\\
t_1 := t\_0 + 1\\
\mathbf{if}\;\frac{t\_0}{t\_1 \cdot \left(s \cdot t\_1\right)} \leq 0:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;\frac{0.25}{s}\\
\end{array}
\end{array}
if (/.f32 (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)) (*.f32 (*.f32 s (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)))) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s))))) < 0.0Initial program 99.9%
Taylor expanded in s around -inf
Simplified0.2%
Taylor expanded in s around 0
div-subN/A
*-commutativeN/A
metadata-evalN/A
distribute-rgt-outN/A
+-inversesN/A
metadata-eval99.9
Simplified99.9%
if 0.0 < (/.f32 (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)) (*.f32 (*.f32 s (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s)))) (+.f32 #s(literal 1 binary32) (exp.f32 (/.f32 (neg.f32 (fabs.f32 x)) s))))) Initial program 99.2%
Taylor expanded in s around inf
/-lowering-/.f3293.5
Simplified93.5%
Final simplification98.1%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (- (/ (fabs x) s))))) (* t_0 (/ (pow (+ t_0 1.0) -2.0) s))))
float code(float x, float s) {
float t_0 = expf(-(fabsf(x) / s));
return t_0 * (powf((t_0 + 1.0f), -2.0f) / 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) ** (-2.0e0)) / s)
end function
function code(x, s) t_0 = exp(Float32(-Float32(abs(x) / s))) return Float32(t_0 * Float32((Float32(t_0 + Float32(1.0)) ^ Float32(-2.0)) / s)) end
function tmp = code(x, s) t_0 = exp(-(abs(x) / s)); tmp = t_0 * (((t_0 + single(1.0)) ^ single(-2.0)) / s); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-\frac{\left|x\right|}{s}}\\
t\_0 \cdot \frac{{\left(t\_0 + 1\right)}^{-2}}{s}
\end{array}
\end{array}
Initial program 99.7%
clear-numN/A
associate-/r/N/A
*-lowering-*.f32N/A
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (let* ((t_0 (- (/ (fabs x) s)))) (/ (exp (fma -2.0 (log1p (exp t_0)) t_0)) s)))
float code(float x, float s) {
float t_0 = -(fabsf(x) / s);
return expf(fmaf(-2.0f, log1pf(expf(t_0)), t_0)) / s;
}
function code(x, s) t_0 = Float32(-Float32(abs(x) / s)) return Float32(exp(fma(Float32(-2.0), log1p(exp(t_0)), t_0)) / s) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := -\frac{\left|x\right|}{s}\\
\frac{e^{\mathsf{fma}\left(-2, \mathsf{log1p}\left(e^{t\_0}\right), t\_0\right)}}{s}
\end{array}
\end{array}
Initial program 99.7%
Applied egg-rr99.9%
/-lowering-/.f32N/A
Applied egg-rr99.8%
(FPCore (x s) :precision binary32 (/ (exp (fma (- (/ (* x 0.25) s)) (/ x s) (* -2.0 (log 2.0)))) s))
float code(float x, float s) {
return expf(fmaf(-((x * 0.25f) / s), (x / s), (-2.0f * logf(2.0f)))) / s;
}
function code(x, s) return Float32(exp(fma(Float32(-Float32(Float32(x * Float32(0.25)) / s)), Float32(x / s), Float32(Float32(-2.0) * log(Float32(2.0))))) / s) end
\begin{array}{l}
\\
\frac{e^{\mathsf{fma}\left(-\frac{x \cdot 0.25}{s}, \frac{x}{s}, -2 \cdot \log 2\right)}}{s}
\end{array}
Initial program 99.7%
Applied egg-rr99.9%
/-lowering-/.f32N/A
Applied egg-rr99.8%
Taylor expanded in s around inf
accelerator-lowering-fma.f32N/A
log-lowering-log.f32N/A
mul-1-negN/A
distribute-neg-frac2N/A
/-lowering-/.f32N/A
distribute-rgt-outN/A
unpow2N/A
sqr-absN/A
unpow2N/A
metadata-evalN/A
metadata-evalN/A
associate-*l*N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
metadata-evalN/A
*-lowering-*.f32N/A
unpow2N/A
*-lowering-*.f32N/A
neg-lowering-neg.f32N/A
unpow2N/A
*-lowering-*.f3290.3
Simplified90.3%
+-commutativeN/A
associate-*r*N/A
distribute-lft-neg-inN/A
times-fracN/A
accelerator-lowering-fma.f32N/A
/-lowering-/.f32N/A
*-commutativeN/A
*-lowering-*.f32N/A
neg-lowering-neg.f32N/A
/-lowering-/.f32N/A
*-lowering-*.f32N/A
log-lowering-log.f3298.9
Applied egg-rr98.9%
Final simplification98.9%
(FPCore (x s) :precision binary32 (/ (exp (- (/ (fabs x) s))) (* s 4.0)))
float code(float x, float s) {
return expf(-(fabsf(x) / s)) / (s * 4.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = exp(-(abs(x) / s)) / (s * 4.0e0)
end function
function code(x, s) return Float32(exp(Float32(-Float32(abs(x) / s))) / Float32(s * Float32(4.0))) end
function tmp = code(x, s) tmp = exp(-(abs(x) / s)) / (s * single(4.0)); end
\begin{array}{l}
\\
\frac{e^{-\frac{\left|x\right|}{s}}}{s \cdot 4}
\end{array}
Initial program 99.7%
Taylor expanded in s around inf
*-commutativeN/A
*-lowering-*.f3296.2
Simplified96.2%
Final simplification96.2%
(FPCore (x s) :precision binary32 0.0)
float code(float x, float s) {
return 0.0f;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.0e0
end function
function code(x, s) return Float32(0.0) end
function tmp = code(x, s) tmp = single(0.0); end
\begin{array}{l}
\\
0
\end{array}
Initial program 99.7%
Taylor expanded in s around -inf
Simplified17.4%
Taylor expanded in s around 0
div-subN/A
*-commutativeN/A
metadata-evalN/A
distribute-rgt-outN/A
+-inversesN/A
metadata-eval73.3
Simplified73.3%
herbie shell --seed 2024204
(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))))))