
(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 6 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 (/ (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(abs(x) / Float32(-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.4%
lift-fabs.f32N/A
remove-double-negN/A
lift-neg.f32N/A
remove-double-negN/A
frac-2negN/A
frac-2negN/A
lift-/.f32N/A
lift-exp.f32N/A
lift-+.f32N/A
lift-*.f32N/A
lift-fabs.f32N/A
remove-double-negN/A
lift-neg.f32N/A
remove-double-negN/A
Applied rewrites99.5%
Applied rewrites99.5%
Final simplification99.5%
(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 s) (* (/ x 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 / s), ((x / s) * -0.0625f), 0.25f) / s;
}
return tmp;
}
function code(x, s) t_0 = exp(Float32(abs(x) / Float32(-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(x / s), Float32(Float32(x / 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}{s}, \frac{x}{s} \cdot -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.5%
Taylor expanded in s around -inf
Applied rewrites0.5%
Taylor expanded in s around 0
div-subN/A
distribute-rgt-outN/A
metadata-evalN/A
*-commutativeN/A
+-inverses99.5
Applied rewrites99.5%
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
Applied rewrites57.8%
Taylor expanded in s around inf
lower-/.f32N/A
+-commutativeN/A
*-commutativeN/A
unpow2N/A
sqr-absN/A
unpow2N/A
lower-fma.f32N/A
unpow2N/A
sqr-absN/A
unpow2N/A
lower-/.f32N/A
unpow2N/A
lower-*.f32N/A
unpow2N/A
lower-*.f3281.5
Applied rewrites81.5%
lift-*.f32N/A
lift-*.f32N/A
lift-/.f32N/A
lift-/.f32N/A
lift-*.f32N/A
lift-*.f32N/A
times-fracN/A
associate-*l*N/A
lower-fma.f32N/A
lower-/.f32N/A
lower-*.f32N/A
lower-/.f3293.2
Applied rewrites93.2%
Final simplification97.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(abs(x) / Float32(-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.5%
Taylor expanded in s around -inf
Applied rewrites0.5%
Taylor expanded in s around 0
div-subN/A
distribute-rgt-outN/A
metadata-evalN/A
*-commutativeN/A
+-inverses99.5
Applied rewrites99.5%
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
lower-/.f3291.7
Applied rewrites91.7%
Final simplification97.4%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (fabs x) (- s))))) (/ t_0 (* (* s 2.0) (+ t_0 1.0)))))
float code(float x, float s) {
float t_0 = expf((fabsf(x) / -s));
return t_0 / ((s * 2.0f) * (t_0 + 1.0f));
}
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 / ((s * 2.0e0) * (t_0 + 1.0e0))
end function
function code(x, s) t_0 = exp(Float32(abs(x) / Float32(-s))) return Float32(t_0 / Float32(Float32(s * Float32(2.0)) * Float32(t_0 + Float32(1.0)))) end
function tmp = code(x, s) t_0 = exp((abs(x) / -s)); tmp = t_0 / ((s * single(2.0)) * (t_0 + single(1.0))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{\left|x\right|}{-s}}\\
\frac{t\_0}{\left(s \cdot 2\right) \cdot \left(t\_0 + 1\right)}
\end{array}
\end{array}
Initial program 99.4%
Taylor expanded in s around inf
*-commutativeN/A
lower-*.f3296.2
Applied rewrites96.2%
Final simplification96.2%
(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(abs(x) / Float32(-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.4%
Taylor expanded in s around inf
*-commutativeN/A
lower-*.f3295.9
Applied rewrites95.9%
Final simplification95.9%
(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.4%
Taylor expanded in s around -inf
Applied rewrites16.1%
Taylor expanded in s around 0
div-subN/A
distribute-rgt-outN/A
metadata-evalN/A
*-commutativeN/A
+-inverses73.7
Applied rewrites73.7%
herbie shell --seed 2024214
(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))))))