
(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 10 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
(let* ((t_0 (exp (/ x_m s))))
(if (<= (fabs x_m) 500000000.0)
(/ (exp (+ (/ x_m s) (* -2.0 (log1p t_0)))) s)
(exp
(*
x_m
(- (/ 1.0 s) (+ (* 2.0 (/ (log (+ 1.0 t_0)) x_m)) (/ (log s) x_m))))))))x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = expf((x_m / s));
float tmp;
if (fabsf(x_m) <= 500000000.0f) {
tmp = expf(((x_m / s) + (-2.0f * log1pf(t_0)))) / s;
} else {
tmp = expf((x_m * ((1.0f / s) - ((2.0f * (logf((1.0f + t_0)) / x_m)) + (logf(s) / x_m)))));
}
return tmp;
}
x_m = abs(x) function code(x_m, s) t_0 = exp(Float32(x_m / s)) tmp = Float32(0.0) if (abs(x_m) <= Float32(500000000.0)) tmp = Float32(exp(Float32(Float32(x_m / s) + Float32(Float32(-2.0) * log1p(t_0)))) / s); else tmp = exp(Float32(x_m * Float32(Float32(Float32(1.0) / s) - Float32(Float32(Float32(2.0) * Float32(log(Float32(Float32(1.0) + t_0)) / x_m)) + Float32(log(s) / x_m))))); end return tmp end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := e^{\frac{x\_m}{s}}\\
\mathbf{if}\;\left|x\_m\right| \leq 500000000:\\
\;\;\;\;\frac{e^{\frac{x\_m}{s} + -2 \cdot \mathsf{log1p}\left(t\_0\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;e^{x\_m \cdot \left(\frac{1}{s} - \left(2 \cdot \frac{\log \left(1 + t\_0\right)}{x\_m} + \frac{\log s}{x\_m}\right)\right)}\\
\end{array}
\end{array}
if (fabs.f32 x) < 5e8Initial 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.5%
Applied egg-rr72.7%
*-lft-identity72.7%
*-commutative72.7%
exp-to-pow72.6%
log1p-undefine72.7%
*-commutative72.7%
rem-exp-log69.2%
exp-sum68.5%
exp-diff95.4%
associate--r+95.5%
exp-diff95.9%
cancel-sign-sub-inv95.9%
metadata-eval95.9%
Simplified99.6%
if 5e8 < (fabs.f32 x) Initial program 100.0%
fabs-neg100.0%
distribute-frac-neg100.0%
distribute-frac-neg2100.0%
fabs-neg100.0%
*-commutative100.0%
fabs-neg100.0%
+-commutative100.0%
fabs-neg100.0%
Simplified100.0%
Applied egg-rr69.3%
Taylor expanded in x around -inf 100.0%
Final simplification99.8%
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) (fma s t_0 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) * fmaf(s, t_0, s));
}
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)) * fma(s, t_0, 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 \mathsf{fma}\left(s, t\_0, s\right)}
\end{array}
\end{array}
Initial program 99.8%
*-commutative99.8%
distribute-lft-in99.7%
*-rgt-identity99.7%
fabs-neg99.7%
+-commutative99.7%
fma-define99.8%
fabs-neg99.8%
Simplified99.8%
Final simplification99.8%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (let* ((t_0 (exp (/ (fabs x_m) (- s)))) (t_1 (- -1.0 t_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 = -1.0f - t_0;
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 = (-1.0e0) - t_0
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(Float32(-1.0) - t_0) return Float32(t_0 / Float32(Float32(s * t_1) * t_1)) end
x_m = abs(x); function tmp = code(x_m, s) t_0 = exp((abs(x_m) / -s)); t_1 = single(-1.0) - t_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 := -1 - t\_0\\
\frac{t\_0}{\left(s \cdot t\_1\right) \cdot t\_1}
\end{array}
\end{array}
Initial program 99.8%
Final simplification99.8%
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 (/ (fabs 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((fabsf(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((abs(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(abs(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((abs(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{\left|x\_m\right|}{s}}}\right)}
\end{array}
\end{array}
Initial program 99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.7%
*-rgt-identity99.7%
+-commutative99.7%
Simplified99.7%
Final simplification99.7%
x_m = (fabs.f32 x)
(FPCore (x_m s)
:precision binary32
(let* ((t_0 (log1p (exp (/ x_m s)))))
(if (<= (fabs x_m) 500000000.0)
(/ (exp (+ (/ x_m s) (* -2.0 t_0))) s)
(exp (* x_m (- (/ 1.0 s) (+ (/ (log s) x_m) (* 2.0 (/ t_0 x_m)))))))))x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = log1pf(expf((x_m / s)));
float tmp;
if (fabsf(x_m) <= 500000000.0f) {
tmp = expf(((x_m / s) + (-2.0f * t_0))) / s;
} else {
tmp = expf((x_m * ((1.0f / s) - ((logf(s) / x_m) + (2.0f * (t_0 / x_m))))));
}
return tmp;
}
x_m = abs(x) function code(x_m, s) t_0 = log1p(exp(Float32(x_m / s))) tmp = Float32(0.0) if (abs(x_m) <= Float32(500000000.0)) tmp = Float32(exp(Float32(Float32(x_m / s) + Float32(Float32(-2.0) * t_0))) / s); else tmp = exp(Float32(x_m * Float32(Float32(Float32(1.0) / s) - Float32(Float32(log(s) / x_m) + Float32(Float32(2.0) * Float32(t_0 / x_m)))))); end return tmp end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := \mathsf{log1p}\left(e^{\frac{x\_m}{s}}\right)\\
\mathbf{if}\;\left|x\_m\right| \leq 500000000:\\
\;\;\;\;\frac{e^{\frac{x\_m}{s} + -2 \cdot t\_0}}{s}\\
\mathbf{else}:\\
\;\;\;\;e^{x\_m \cdot \left(\frac{1}{s} - \left(\frac{\log s}{x\_m} + 2 \cdot \frac{t\_0}{x\_m}\right)\right)}\\
\end{array}
\end{array}
if (fabs.f32 x) < 5e8Initial 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.5%
Applied egg-rr72.7%
*-lft-identity72.7%
*-commutative72.7%
exp-to-pow72.6%
log1p-undefine72.7%
*-commutative72.7%
rem-exp-log69.2%
exp-sum68.5%
exp-diff95.4%
associate--r+95.5%
exp-diff95.9%
cancel-sign-sub-inv95.9%
metadata-eval95.9%
Simplified99.6%
if 5e8 < (fabs.f32 x) Initial program 100.0%
fabs-neg100.0%
distribute-frac-neg100.0%
distribute-frac-neg2100.0%
fabs-neg100.0%
*-commutative100.0%
fabs-neg100.0%
+-commutative100.0%
fabs-neg100.0%
Simplified100.0%
Applied egg-rr69.3%
Taylor expanded in x around -inf 100.0%
+-commutative100.0%
*-un-lft-identity100.0%
log-prod100.0%
metadata-eval100.0%
+-commutative100.0%
log1p-define100.0%
Applied egg-rr100.0%
+-lft-identity100.0%
Simplified100.0%
Final simplification99.8%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= (fabs x_m) 500000000.0) (/ (exp (+ (/ x_m s) (* -2.0 (log1p (exp (/ x_m s)))))) s) (/ (exp (/ (- (fabs x_m)) s)) (* s 4.0))))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (fabsf(x_m) <= 500000000.0f) {
tmp = expf(((x_m / s) + (-2.0f * log1pf(expf((x_m / s)))))) / s;
} else {
tmp = expf((-fabsf(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(500000000.0)) tmp = Float32(exp(Float32(Float32(x_m / s) + Float32(Float32(-2.0) * log1p(exp(Float32(x_m / s)))))) / s); else tmp = Float32(exp(Float32(Float32(-abs(x_m)) / 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 500000000:\\
\;\;\;\;\frac{e^{\frac{x\_m}{s} + -2 \cdot \mathsf{log1p}\left(e^{\frac{x\_m}{s}}\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{\frac{-\left|x\_m\right|}{s}}}{s \cdot 4}\\
\end{array}
\end{array}
if (fabs.f32 x) < 5e8Initial 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.5%
Applied egg-rr72.7%
*-lft-identity72.7%
*-commutative72.7%
exp-to-pow72.6%
log1p-undefine72.7%
*-commutative72.7%
rem-exp-log69.2%
exp-sum68.5%
exp-diff95.4%
associate--r+95.5%
exp-diff95.9%
cancel-sign-sub-inv95.9%
metadata-eval95.9%
Simplified99.6%
if 5e8 < (fabs.f32 x) Initial program 100.0%
fabs-neg100.0%
distribute-frac-neg100.0%
distribute-frac-neg2100.0%
fabs-neg100.0%
*-commutative100.0%
fabs-neg100.0%
+-commutative100.0%
fabs-neg100.0%
Simplified100.0%
Taylor expanded in s around inf 100.0%
Final simplification99.8%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= x_m 3.999999935100636e-17) (/ 0.25 s) (exp (/ x_m (- s)))))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (x_m <= 3.999999935100636e-17f) {
tmp = 0.25f / s;
} else {
tmp = expf((x_m / -s));
}
return tmp;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
real(4) :: tmp
if (x_m <= 3.999999935100636e-17) then
tmp = 0.25e0 / s
else
tmp = exp((x_m / -s))
end if
code = tmp
end function
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (x_m <= Float32(3.999999935100636e-17)) tmp = Float32(Float32(0.25) / s); else tmp = exp(Float32(x_m / Float32(-s))); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, s) tmp = single(0.0); if (x_m <= single(3.999999935100636e-17)) tmp = single(0.25) / s; else tmp = exp((x_m / -s)); end tmp_2 = tmp; end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 3.999999935100636 \cdot 10^{-17}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;e^{\frac{x\_m}{-s}}\\
\end{array}
\end{array}
if x < 3.99999994e-17Initial program 99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.6%
Taylor expanded in s around inf 41.4%
if 3.99999994e-17 < x Initial program 99.9%
fabs-neg99.9%
distribute-frac-neg99.9%
distribute-frac-neg299.9%
fabs-neg99.9%
*-commutative99.9%
fabs-neg99.9%
+-commutative99.9%
fabs-neg99.9%
Simplified99.9%
Applied egg-rr63.0%
Taylor expanded in x around -inf 99.7%
Taylor expanded in x around inf 4.0%
associate-*r/4.0%
neg-mul-14.0%
Simplified4.0%
exp-prod4.0%
div-inv4.0%
pow-unpow4.2%
add-sqr-sqrt-0.0%
sqrt-unprod88.7%
sqr-neg88.7%
sqrt-unprod88.7%
add-sqr-sqrt88.7%
exp-prod88.7%
neg-mul-188.7%
exp-prod96.6%
div-inv96.6%
distribute-frac-neg96.6%
exp-neg96.6%
Applied egg-rr96.6%
rec-exp96.6%
distribute-frac-neg96.6%
Simplified96.6%
Final simplification61.9%
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.8%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.7%
Taylor expanded in s around inf 95.3%
distribute-frac-neg295.3%
rec-exp95.3%
pow195.3%
pow195.3%
frac-2neg95.3%
frac-2neg95.3%
add-sqr-sqrt50.5%
fabs-sqr50.5%
add-sqr-sqrt63.4%
Applied egg-rr63.4%
rec-exp63.5%
distribute-neg-frac263.5%
Simplified63.5%
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.8%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.7%
Taylor expanded in s around inf 28.6%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 1.0)
x_m = fabs(x);
float code(float x_m, float s) {
return 1.0f;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = 1.0e0
end function
x_m = abs(x) function code(x_m, s) return Float32(1.0) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(1.0); end
\begin{array}{l}
x_m = \left|x\right|
\\
1
\end{array}
Initial program 99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.7%
Applied egg-rr83.8%
Taylor expanded in x around -inf 95.9%
Taylor expanded in x around inf 38.0%
associate-*r/38.0%
neg-mul-138.0%
Simplified38.0%
Taylor expanded in x around 0 8.5%
herbie shell --seed 2024103
(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))))))