
(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 7 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_1 (+ t_0 1.0))) (/ t_0 (* t_1 (* s t_1)))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
float t_1 = t_0 + 1.0f;
return t_0 / (t_1 * (s * 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 = t_0 + 1.0e0
code = t_0 / (t_1 * (s * t_1))
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) t_1 = Float32(t_0 + Float32(1.0)) return Float32(t_0 / Float32(t_1 * Float32(s * t_1))) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); t_1 = t_0 + single(1.0); tmp = t_0 / (t_1 * (s * t_1)); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
t_1 := t_0 + 1\\
\frac{t_0}{t_1 \cdot \left(s \cdot t_1\right)}
\end{array}
\end{array}
Initial program 99.9%
Final simplification99.9%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (fabs x) (- s))))) (/ t_0 (* (+ 1.0 t_0) (+ s (/ s (exp (/ (fabs x) s))))))))
float code(float x, float s) {
float t_0 = expf((fabsf(x) / -s));
return t_0 / ((1.0f + t_0) * (s + (s / expf((fabsf(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((abs(x) / -s))
code = t_0 / ((1.0e0 + t_0) * (s + (s / exp((abs(x) / s)))))
end function
function code(x, s) t_0 = exp(Float32(abs(x) / Float32(-s))) return Float32(t_0 / Float32(Float32(Float32(1.0) + t_0) * Float32(s + Float32(s / exp(Float32(abs(x) / s)))))) end
function tmp = code(x, s) t_0 = exp((abs(x) / -s)); tmp = t_0 / ((single(1.0) + t_0) * (s + (s / exp((abs(x) / s))))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{\left|x\right|}{-s}}\\
\frac{t_0}{\left(1 + t_0\right) \cdot \left(s + \frac{s}{e^{\frac{\left|x\right|}{s}}}\right)}
\end{array}
\end{array}
Initial program 99.9%
Simplified99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- (fabs x)) s)))) (/ (/ (/ t_0 s) (+ t_0 1.0)) (+ 1.0 (exp (/ (- x) s))))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
return ((t_0 / s) / (t_0 + 1.0f)) / (1.0f + expf((-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((-abs(x) / s))
code = ((t_0 / s) / (t_0 + 1.0e0)) / (1.0e0 + exp((-x / s)))
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) return Float32(Float32(Float32(t_0 / s) / Float32(t_0 + Float32(1.0))) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); tmp = ((t_0 / s) / (t_0 + single(1.0))) / (single(1.0) + exp((-x / s))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
\frac{\frac{\frac{t_0}{s}}{t_0 + 1}}{1 + e^{\frac{-x}{s}}}
\end{array}
\end{array}
Initial program 99.9%
associate-/r*99.9%
Simplified99.8%
remove-double-neg99.8%
frac-2neg99.8%
add-sqr-sqrt-0.0%
add-sqr-sqrt99.8%
add-sqr-sqrt99.8%
sqrt-unprod97.6%
sqr-neg97.6%
sqrt-unprod-0.0%
add-sqr-sqrt96.2%
distribute-frac-neg96.2%
exp-neg96.2%
remove-double-neg96.2%
frac-2neg96.2%
add-sqr-sqrt-0.0%
Applied egg-rr97.4%
rec-exp97.4%
Simplified97.4%
Final simplification97.4%
(FPCore (x s) :precision binary32 (if (<= (fabs x) 0.0008500000112690032) (/ 1.0 (/ s (exp (+ (/ x s) (* -2.0 (log1p (exp (/ x s)))))))) (/ (exp (/ (fabs x) (- s))) (* s 4.0))))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 0.0008500000112690032f) {
tmp = 1.0f / (s / expf(((x / s) + (-2.0f * log1pf(expf((x / s)))))));
} else {
tmp = expf((fabsf(x) / -s)) / (s * 4.0f);
}
return tmp;
}
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(0.0008500000112690032)) tmp = Float32(Float32(1.0) / Float32(s / exp(Float32(Float32(x / s) + Float32(Float32(-2.0) * log1p(exp(Float32(x / s)))))))); else tmp = Float32(exp(Float32(abs(x) / Float32(-s))) / Float32(s * Float32(4.0))); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 0.0008500000112690032:\\
\;\;\;\;\frac{1}{\frac{s}{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{\frac{\left|x\right|}{-s}}}{s \cdot 4}\\
\end{array}
\end{array}
if (fabs.f32 x) < 8.50000011e-4Initial program 99.6%
Simplified99.6%
Applied egg-rr79.7%
frac-times79.6%
*-commutative79.6%
associate-/l*79.7%
associate-/l*79.7%
un-div-inv79.7%
add-exp-log79.6%
prod-exp99.5%
pow-flip99.5%
log-pow99.5%
metadata-eval99.5%
log1p-def99.6%
Applied egg-rr99.6%
if 8.50000011e-4 < (fabs.f32 x) Initial program 100.0%
Simplified100.0%
Taylor expanded in s around inf 100.0%
*-commutative100.0%
Simplified100.0%
Final simplification99.8%
(FPCore (x s) :precision binary32 (if (<= (fabs x) 0.0008500000112690032) (/ (exp (+ (/ x s) (* -2.0 (log1p (exp (/ x s)))))) s) (/ (exp (/ (fabs x) (- s))) (* s 4.0))))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 0.0008500000112690032f) {
tmp = expf(((x / s) + (-2.0f * log1pf(expf((x / s)))))) / s;
} else {
tmp = expf((fabsf(x) / -s)) / (s * 4.0f);
}
return tmp;
}
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(0.0008500000112690032)) tmp = Float32(exp(Float32(Float32(x / s) + Float32(Float32(-2.0) * log1p(exp(Float32(x / s)))))) / s); else tmp = Float32(exp(Float32(abs(x) / Float32(-s))) / Float32(s * Float32(4.0))); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 0.0008500000112690032:\\
\;\;\;\;\frac{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{\frac{\left|x\right|}{-s}}}{s \cdot 4}\\
\end{array}
\end{array}
if (fabs.f32 x) < 8.50000011e-4Initial program 99.6%
associate-/r*99.6%
Simplified99.5%
remove-double-neg99.5%
frac-2neg99.5%
add-sqr-sqrt-0.0%
add-sqr-sqrt99.5%
add-sqr-sqrt99.5%
sqrt-unprod94.3%
sqr-neg94.3%
sqrt-unprod-0.0%
add-sqr-sqrt90.8%
distribute-frac-neg90.8%
exp-neg90.8%
remove-double-neg90.8%
frac-2neg90.8%
add-sqr-sqrt-0.0%
Applied egg-rr93.7%
rec-exp93.6%
Simplified93.6%
Applied egg-rr95.3%
expm1-def95.5%
expm1-log1p99.6%
Simplified99.6%
if 8.50000011e-4 < (fabs.f32 x) Initial program 100.0%
Simplified100.0%
Taylor expanded in s around inf 100.0%
*-commutative100.0%
Simplified100.0%
Final simplification99.8%
(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.9%
Simplified99.8%
Taylor expanded in s around inf 96.3%
*-commutative96.3%
Simplified96.3%
Final simplification96.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.9%
Simplified99.8%
Taylor expanded in s around inf 25.1%
Final simplification25.1%
herbie shell --seed 2023326
(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))))))