
(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 14 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(abs(x) / Float32(-s))) return Float32(Float32(t_0 / 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{\frac{t\_0}{t\_0 + 1}}{\mathsf{fma}\left(s, t\_0, s\right)}
\end{array}
\end{array}
Initial program 99.5%
lift-/.f32N/A
lift-*.f32N/A
associate-/l/N/A
lower-/.f32N/A
Applied rewrites99.6%
Final simplification99.6%
(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.0010000000474974513)
(/ t_0 s)
(/ 1.0 (* s (- (/ (/ (* x x) s) s) -4.0))))))
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.0010000000474974513f) {
tmp = t_0 / s;
} else {
tmp = 1.0f / (s * ((((x * x) / s) / s) - -4.0f));
}
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.0010000000474974513e0) then
tmp = t_0 / s
else
tmp = 1.0e0 / (s * ((((x * x) / s) / s) - (-4.0e0)))
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.0010000000474974513)) tmp = Float32(t_0 / s); else tmp = Float32(Float32(1.0) / Float32(s * Float32(Float32(Float32(Float32(x * x) / s) / s) - Float32(-4.0)))); 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.0010000000474974513)) tmp = t_0 / s; else tmp = single(1.0) / (s * ((((x * x) / s) / s) - single(-4.0))); 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.0010000000474974513:\\
\;\;\;\;\frac{t\_0}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{s \cdot \left(\frac{\frac{x \cdot x}{s}}{s} - -4\right)}\\
\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.00100000005Initial program 99.8%
lift-/.f32N/A
lift-*.f32N/A
associate-/l/N/A
lower-/.f32N/A
Applied rewrites99.8%
Applied rewrites99.8%
Taylor expanded in s around 0
neg-mul-1N/A
distribute-frac-neg2N/A
neg-mul-1N/A
lower-/.f32N/A
lower-fabs.f32N/A
neg-mul-1N/A
lower-neg.f3299.8
Applied rewrites99.8%
if 0.00100000005 < (/.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.1%
lift-/.f32N/A
lift-*.f32N/A
associate-/l/N/A
lower-/.f32N/A
Applied rewrites99.3%
Taylor expanded in s around inf
Applied rewrites83.7%
Applied rewrites83.8%
Taylor expanded in s around -inf
mul-1-negN/A
*-commutativeN/A
distribute-rgt-neg-inN/A
neg-mul-1N/A
lower-*.f32N/A
Applied rewrites91.0%
Final simplification97.4%
herbie shell --seed 2024228
(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))))))