
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
return 1.0f / (1.0f + expf((-x / 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)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + exp((-x / s))); end
\begin{array}{l}
\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}
Sampling outcomes in binary32 precision:
Herbie found 12 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
float code(float x, float s) {
return 1.0f / (1.0f + expf((-x / 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)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + exp((-x / s))); end
\begin{array}{l}
\\
\frac{1}{1 + e^{\frac{-x}{s}}}
\end{array}
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ x (- s))))))
float code(float x, float s) {
return 1.0f / (1.0f + expf((x / -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)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(x / Float32(-s))))) end
function tmp = code(x, s) tmp = single(1.0) / (single(1.0) + exp((x / -s))); end
\begin{array}{l}
\\
\frac{1}{1 + e^{\frac{x}{-s}}}
\end{array}
Initial program 99.8%
Final simplification99.8%
(FPCore (x s)
:precision binary32
(if (<= (/ x (- s)) -5.0)
0.5
(/
1.0
(fma
x
(fma (/ (/ x s) s) (fma -0.16666666666666666 (/ x s) 0.5) (/ -1.0 s))
2.0))))
float code(float x, float s) {
float tmp;
if ((x / -s) <= -5.0f) {
tmp = 0.5f;
} else {
tmp = 1.0f / fmaf(x, fmaf(((x / s) / s), fmaf(-0.16666666666666666f, (x / s), 0.5f), (-1.0f / s)), 2.0f);
}
return tmp;
}
function code(x, s) tmp = Float32(0.0) if (Float32(x / Float32(-s)) <= Float32(-5.0)) tmp = Float32(0.5); else tmp = Float32(Float32(1.0) / fma(x, fma(Float32(Float32(x / s) / s), fma(Float32(-0.16666666666666666), Float32(x / s), Float32(0.5)), Float32(Float32(-1.0) / s)), Float32(2.0))); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{-s} \leq -5:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(x, \mathsf{fma}\left(\frac{\frac{x}{s}}{s}, \mathsf{fma}\left(-0.16666666666666666, \frac{x}{s}, 0.5\right), \frac{-1}{s}\right), 2\right)}\\
\end{array}
\end{array}
if (/.f32 (neg.f32 x) s) < -5Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites28.1%
if -5 < (/.f32 (neg.f32 x) s) Initial program 99.7%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f32N/A
Applied rewrites85.2%
Applied rewrites90.6%
Final simplification67.5%
herbie shell --seed 2024226
(FPCore (x s)
:name "Logistic function"
:precision binary32
:pre (and (<= 0.0 s) (<= s 1.0651631))
(/ 1.0 (+ 1.0 (exp (/ (- x) s)))))