
(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}
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= (fabs x_m) 7.0) (/ 1.0 (* s (exp (- (* 2.0 (log1p (exp (/ x_m s)))) (/ x_m s))))) (/ (exp (/ x_m (- s))) (* s 4.0))))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (fabsf(x_m) <= 7.0f) {
tmp = 1.0f / (s * expf(((2.0f * log1pf(expf((x_m / s)))) - (x_m / s))));
} else {
tmp = expf((x_m / -s)) / (s * 4.0f);
}
return tmp;
}
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (abs(x_m) <= Float32(7.0)) tmp = Float32(Float32(1.0) / Float32(s * exp(Float32(Float32(Float32(2.0) * log1p(exp(Float32(x_m / s)))) - Float32(x_m / s))))); else tmp = Float32(exp(Float32(x_m / Float32(-s))) / Float32(s * Float32(4.0))); end return tmp end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;\left|x\_m\right| \leq 7:\\
\;\;\;\;\frac{1}{s \cdot e^{2 \cdot \mathsf{log1p}\left(e^{\frac{x\_m}{s}}\right) - \frac{x\_m}{s}}}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{\frac{x\_m}{-s}}}{s \cdot 4}\\
\end{array}
\end{array}
if (fabs.f32 x) < 7Initial program 99.1%
fabs-neg99.1%
distribute-frac-neg99.1%
distribute-frac-neg299.1%
fabs-neg99.1%
*-commutative99.1%
fabs-neg99.1%
+-commutative99.1%
fabs-neg99.1%
Simplified99.1%
add-exp-log95.7%
log-div95.4%
add-log-exp95.4%
add-sqr-sqrt51.5%
fabs-sqr51.5%
add-sqr-sqrt73.0%
add-sqr-sqrt-0.0%
sqrt-unprod62.7%
sqr-neg62.7%
sqrt-unprod67.9%
add-sqr-sqrt67.9%
*-commutative67.9%
Applied egg-rr94.9%
Taylor expanded in x around -inf 95.1%
exp-neg95.1%
exp-sum94.9%
rem-exp-log99.0%
+-commutative99.0%
log1p-define99.3%
mul-1-neg99.3%
unsub-neg99.3%
Simplified99.3%
if 7 < (fabs.f32 x) Initial program 100.0%
Simplified100.0%
Taylor expanded in s around inf 100.0%
*-commutative100.0%
Simplified100.0%
add-sqr-sqrt47.0%
fabs-sqr47.0%
add-sqr-sqrt48.7%
distribute-frac-neg48.7%
rec-exp48.7%
Applied egg-rr48.7%
rec-exp48.7%
distribute-frac-neg48.7%
Simplified48.7%
Final simplification72.8%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (let* ((t_0 (exp (/ (fabs x_m) (- s)))) (t_1 (+ t_0 1.0))) (/ t_0 (* s (* t_1 t_1)))))
x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = expf((fabsf(x_m) / -s));
float t_1 = t_0 + 1.0f;
return t_0 / (s * (t_1 * t_1));
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
real(4) :: t_0
real(4) :: t_1
t_0 = exp((abs(x_m) / -s))
t_1 = t_0 + 1.0e0
code = t_0 / (s * (t_1 * t_1))
end function
x_m = abs(x) function code(x_m, s) t_0 = exp(Float32(abs(x_m) / Float32(-s))) t_1 = Float32(t_0 + Float32(1.0)) return Float32(t_0 / Float32(s * Float32(t_1 * t_1))) end
x_m = abs(x); function tmp = code(x_m, s) t_0 = exp((abs(x_m) / -s)); t_1 = t_0 + single(1.0); tmp = t_0 / (s * (t_1 * t_1)); end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := e^{\frac{\left|x\_m\right|}{-s}}\\
t_1 := t\_0 + 1\\
\frac{t\_0}{s \cdot \left(t\_1 \cdot t\_1\right)}
\end{array}
\end{array}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Final simplification99.6%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (let* ((t_0 (exp (/ (fabs x_m) (- s))))) (/ t_0 (* (+ t_0 1.0) (+ s (/ s (exp (/ x_m s))))))))
x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = expf((fabsf(x_m) / -s));
return t_0 / ((t_0 + 1.0f) * (s + (s / expf((x_m / s)))));
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
real(4) :: t_0
t_0 = exp((abs(x_m) / -s))
code = t_0 / ((t_0 + 1.0e0) * (s + (s / exp((x_m / s)))))
end function
x_m = abs(x) function code(x_m, s) t_0 = exp(Float32(abs(x_m) / Float32(-s))) return Float32(t_0 / Float32(Float32(t_0 + Float32(1.0)) * Float32(s + Float32(s / exp(Float32(x_m / s)))))) end
x_m = abs(x); function tmp = code(x_m, s) t_0 = exp((abs(x_m) / -s)); tmp = t_0 / ((t_0 + single(1.0)) * (s + (s / exp((x_m / s))))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := e^{\frac{\left|x\_m\right|}{-s}}\\
\frac{t\_0}{\left(t\_0 + 1\right) \cdot \left(s + \frac{s}{e^{\frac{x\_m}{s}}}\right)}
\end{array}
\end{array}
Initial program 99.6%
Simplified99.6%
add-cube-cbrt99.4%
associate-/l*99.4%
pow299.4%
add-sqr-sqrt50.1%
fabs-sqr50.1%
add-sqr-sqrt96.9%
Applied egg-rr96.9%
associate-*r/96.9%
unpow296.9%
rem-3cbrt-lft97.0%
Simplified97.0%
Final simplification97.0%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= (fabs x_m) 7.0) (/ (exp (+ (/ x_m s) (* (log1p (exp (/ x_m s))) -2.0))) s) (/ (exp (/ x_m (- s))) (* s 4.0))))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (fabsf(x_m) <= 7.0f) {
tmp = expf(((x_m / s) + (log1pf(expf((x_m / s))) * -2.0f))) / s;
} else {
tmp = expf((x_m / -s)) / (s * 4.0f);
}
return tmp;
}
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (abs(x_m) <= Float32(7.0)) tmp = Float32(exp(Float32(Float32(x_m / s) + Float32(log1p(exp(Float32(x_m / s))) * Float32(-2.0)))) / s); else tmp = Float32(exp(Float32(x_m / Float32(-s))) / Float32(s * Float32(4.0))); end return tmp end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;\left|x\_m\right| \leq 7:\\
\;\;\;\;\frac{e^{\frac{x\_m}{s} + \mathsf{log1p}\left(e^{\frac{x\_m}{s}}\right) \cdot -2}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{\frac{x\_m}{-s}}}{s \cdot 4}\\
\end{array}
\end{array}
if (fabs.f32 x) < 7Initial program 99.1%
fabs-neg99.1%
distribute-frac-neg99.1%
distribute-frac-neg299.1%
fabs-neg99.1%
*-commutative99.1%
fabs-neg99.1%
+-commutative99.1%
fabs-neg99.1%
Simplified99.1%
div-inv99.1%
distribute-frac-neg299.1%
distribute-frac-neg99.1%
add-sqr-sqrt-0.0%
sqrt-unprod47.8%
sqr-neg47.8%
sqrt-unprod47.4%
add-sqr-sqrt47.4%
add-sqr-sqrt25.0%
fabs-sqr25.0%
add-sqr-sqrt70.5%
add-sqr-sqrt69.9%
Applied egg-rr76.2%
associate-*r/76.3%
*-rgt-identity76.3%
associate-/r*76.2%
+-commutative76.2%
Simplified76.2%
div-inv76.2%
div-inv76.2%
associate-*l*76.2%
pow-flip76.2%
+-commutative76.2%
metadata-eval76.2%
Applied egg-rr76.2%
associate-*r*76.2%
associate-*r/76.3%
*-rgt-identity76.3%
associate-*l/76.4%
exp-to-pow76.4%
+-commutative76.4%
log1p-define76.6%
*-commutative76.6%
exp-sum99.3%
Simplified99.3%
if 7 < (fabs.f32 x) Initial program 100.0%
Simplified100.0%
Taylor expanded in s around inf 100.0%
*-commutative100.0%
Simplified100.0%
add-sqr-sqrt47.0%
fabs-sqr47.0%
add-sqr-sqrt48.7%
distribute-frac-neg48.7%
rec-exp48.7%
Applied egg-rr48.7%
rec-exp48.7%
distribute-frac-neg48.7%
Simplified48.7%
Final simplification72.8%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (exp (/ x_m (- s))) (* s 4.0)))
x_m = fabs(x);
float code(float x_m, float s) {
return expf((x_m / -s)) / (s * 4.0f);
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = exp((x_m / -s)) / (s * 4.0e0)
end function
x_m = abs(x) function code(x_m, s) return Float32(exp(Float32(x_m / Float32(-s))) / Float32(s * Float32(4.0))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = exp((x_m / -s)) / (s * single(4.0)); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{e^{\frac{x\_m}{-s}}}{s \cdot 4}
\end{array}
Initial program 99.6%
Simplified99.6%
Taylor expanded in s around inf 94.2%
*-commutative94.2%
Simplified94.2%
add-sqr-sqrt47.1%
fabs-sqr47.1%
add-sqr-sqrt58.8%
distribute-frac-neg58.8%
rec-exp58.8%
Applied egg-rr58.8%
rec-exp58.8%
distribute-frac-neg58.8%
Simplified58.8%
Final simplification58.8%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 0.25 s))
x_m = fabs(x);
float code(float x_m, float s) {
return 0.25f / s;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = 0.25e0 / s
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(0.25) / s) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(0.25) / s; end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{0.25}{s}
\end{array}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Taylor expanded in s around inf 27.2%
herbie shell --seed 2024106
(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))))))