
(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 9 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 (if (<= (fabs x) 5.0) (/ (exp (+ (/ x s) (* (log1p (exp (/ x s))) -2.0))) s) (/ 0.0 s)))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 5.0f) {
tmp = expf(((x / s) + (log1pf(expf((x / s))) * -2.0f))) / s;
} else {
tmp = 0.0f / s;
}
return tmp;
}
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(5.0)) tmp = Float32(exp(Float32(Float32(x / s) + Float32(log1p(exp(Float32(x / s))) * Float32(-2.0)))) / s); else tmp = Float32(Float32(0.0) / s); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 5:\\
\;\;\;\;\frac{e^{\frac{x}{s} + \mathsf{log1p}\left(e^{\frac{x}{s}}\right) \cdot -2}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0}{s}\\
\end{array}
\end{array}
if (fabs.f32 x) < 5Initial program 99.2%
fabs-neg99.2%
distribute-frac-neg99.2%
distribute-frac-neg299.2%
fabs-neg99.2%
*-commutative99.2%
fabs-neg99.2%
+-commutative99.2%
fabs-neg99.2%
Simplified99.4%
Applied egg-rr77.5%
*-lft-identity77.5%
Simplified77.5%
add-exp-log73.5%
log-div73.4%
add-log-exp94.5%
*-commutative94.5%
sum-log93.8%
log-pow94.3%
log1p-undefine94.3%
*-un-lft-identity94.3%
associate--r+94.5%
exp-diff95.2%
Applied egg-rr99.4%
*-lft-identity99.4%
Simplified99.4%
if 5 < (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-rr53.1%
*-lft-identity53.1%
Simplified53.1%
*-un-lft-identity53.1%
exp-prod53.1%
Applied egg-rr53.1%
exp-1-e53.1%
Simplified53.1%
Taylor expanded in s around inf 7.2%
Taylor expanded in x around inf 31.4%
log-E100.0%
+-inverses100.0%
metadata-eval100.0%
distribute-rgt-out--100.0%
+-inverses100.0%
Simplified100.0%
Final simplification99.7%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (fabs x) (- s)))) (t_1 (+ t_0 1.0))) (/ t_0 (* s (* t_1 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 / (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 = t_0 + 1.0e0
code = t_0 / (s * (t_1 * t_1))
end function
function code(x, s) t_0 = exp(Float32(abs(x) / Float32(-s))) t_1 = Float32(t_0 + Float32(1.0)) return Float32(t_0 / Float32(s * Float32(t_1 * t_1))) end
function tmp = code(x, s) t_0 = exp((abs(x) / -s)); t_1 = t_0 + single(1.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 := 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.7%
Final simplification99.7%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (fabs x) (- s))))) (/ t_0 (* (+ t_0 1.0) (+ s (/ s (exp (/ (fabs x) s))))))))
float code(float x, float s) {
float t_0 = expf((fabsf(x) / -s));
return t_0 / ((t_0 + 1.0f) * (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 / ((t_0 + 1.0e0) * (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(t_0 + Float32(1.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 / ((t_0 + single(1.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(t\_0 + 1\right) \cdot \left(s + \frac{s}{e^{\frac{\left|x\right|}{s}}}\right)}
\end{array}
\end{array}
Initial program 99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x s) :precision binary32 (exp (fma x (/ 1.0 s) (- (- (log s)) (* 2.0 (log1p (exp (/ x s))))))))
float code(float x, float s) {
return expf(fmaf(x, (1.0f / s), (-logf(s) - (2.0f * log1pf(expf((x / s)))))));
}
function code(x, s) return exp(fma(x, Float32(Float32(1.0) / s), Float32(Float32(-log(s)) - Float32(Float32(2.0) * log1p(exp(Float32(x / s))))))) end
\begin{array}{l}
\\
e^{\mathsf{fma}\left(x, \frac{1}{s}, \left(-\log s\right) - 2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)\right)}
\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.7%
Applied egg-rr87.0%
div-inv86.9%
fma-neg97.1%
*-un-lft-identity97.1%
*-commutative97.1%
*-commutative97.1%
*-un-lft-identity97.1%
fma-define97.1%
Applied egg-rr97.1%
fma-undefine97.1%
Applied egg-rr97.1%
Final simplification97.1%
(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.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.7%
Taylor expanded in s around inf 94.3%
Final simplification94.3%
(FPCore (x s) :precision binary32 (if (<= x 6.000000212225132e-6) (/ (- (/ (* 0.25 (+ x s)) s) (* (/ x s) 0.25)) s) (/ 0.0 s)))
float code(float x, float s) {
float tmp;
if (x <= 6.000000212225132e-6f) {
tmp = (((0.25f * (x + s)) / s) - ((x / s) * 0.25f)) / s;
} else {
tmp = 0.0f / s;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 6.000000212225132e-6) then
tmp = (((0.25e0 * (x + s)) / s) - ((x / s) * 0.25e0)) / s
else
tmp = 0.0e0 / s
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(6.000000212225132e-6)) tmp = Float32(Float32(Float32(Float32(Float32(0.25) * Float32(x + s)) / s) - Float32(Float32(x / s) * Float32(0.25))) / s); else tmp = Float32(Float32(0.0) / s); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(6.000000212225132e-6)) tmp = (((single(0.25) * (x + s)) / s) - ((x / s) * single(0.25))) / s; else tmp = single(0.0) / s; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 6.000000212225132 \cdot 10^{-6}:\\
\;\;\;\;\frac{\frac{0.25 \cdot \left(x + s\right)}{s} - \frac{x}{s} \cdot 0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0}{s}\\
\end{array}
\end{array}
if x < 6.00000021e-6Initial program 99.5%
fabs-neg99.5%
distribute-frac-neg99.5%
distribute-frac-neg299.5%
fabs-neg99.5%
*-commutative99.5%
fabs-neg99.5%
+-commutative99.5%
fabs-neg99.5%
Simplified99.6%
Applied egg-rr89.8%
*-lft-identity89.8%
Simplified89.8%
*-un-lft-identity89.8%
exp-prod89.7%
Applied egg-rr89.7%
exp-1-e89.7%
Simplified89.7%
Taylor expanded in s around inf 37.5%
Taylor expanded in s around 0 37.5%
+-commutative37.5%
log-E67.9%
associate-*r*67.9%
*-rgt-identity67.9%
distribute-lft-out67.9%
Simplified67.9%
if 6.00000021e-6 < x 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.8%
Applied egg-rr2.8%
*-lft-identity2.8%
Simplified2.8%
*-un-lft-identity2.8%
exp-prod2.8%
Applied egg-rr2.8%
exp-1-e2.8%
Simplified2.8%
Taylor expanded in s around inf 10.1%
Taylor expanded in x around inf 31.8%
log-E96.3%
+-inverses96.3%
metadata-eval96.3%
distribute-rgt-out--96.3%
+-inverses96.3%
Simplified96.3%
Final simplification75.9%
(FPCore (x s) :precision binary32 (if (<= x 1.999999936531045e-20) (/ 0.25 s) (/ 0.0 s)))
float code(float x, float s) {
float tmp;
if (x <= 1.999999936531045e-20f) {
tmp = 0.25f / s;
} else {
tmp = 0.0f / s;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 1.999999936531045e-20) then
tmp = 0.25e0 / s
else
tmp = 0.0e0 / s
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.999999936531045e-20)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.0) / s); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(1.999999936531045e-20)) tmp = single(0.25) / s; else tmp = single(0.0) / s; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.999999936531045 \cdot 10^{-20}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0}{s}\\
\end{array}
\end{array}
if x < 1.99999994e-20Initial program 99.5%
fabs-neg99.5%
distribute-frac-neg99.5%
distribute-frac-neg299.5%
fabs-neg99.5%
*-commutative99.5%
fabs-neg99.5%
+-commutative99.5%
fabs-neg99.5%
Simplified99.7%
Taylor expanded in s around inf 34.9%
if 1.99999994e-20 < x Initial 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.7%
Applied egg-rr12.7%
*-lft-identity12.7%
Simplified12.7%
*-un-lft-identity12.7%
exp-prod12.8%
Applied egg-rr12.8%
exp-1-e12.8%
Simplified12.8%
Taylor expanded in s around inf 17.9%
Taylor expanded in x around inf 33.7%
log-E87.0%
+-inverses87.0%
metadata-eval87.0%
distribute-rgt-out--87.0%
+-inverses87.0%
Simplified87.0%
Final simplification53.8%
(FPCore (x s) :precision binary32 (/ 0.0 s))
float code(float x, float s) {
return 0.0f / s;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.0e0 / s
end function
function code(x, s) return Float32(Float32(0.0) / s) end
function tmp = code(x, s) tmp = single(0.0) / s; end
\begin{array}{l}
\\
\frac{0}{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.7%
Applied egg-rr65.3%
*-lft-identity65.3%
Simplified65.3%
*-un-lft-identity65.3%
exp-prod65.3%
Applied egg-rr65.3%
exp-1-e65.3%
Simplified65.3%
Taylor expanded in s around inf 29.8%
Taylor expanded in x around inf 27.2%
log-E73.0%
+-inverses73.0%
metadata-eval73.0%
distribute-rgt-out--73.0%
+-inverses73.0%
Simplified73.0%
Final simplification73.0%
(FPCore (x s) :precision binary32 1.0)
float code(float x, float s) {
return 1.0f;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0
end function
function code(x, s) return Float32(1.0) end
function tmp = code(x, s) tmp = single(1.0); end
\begin{array}{l}
\\
1
\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.7%
Applied egg-rr87.0%
Taylor expanded in x around inf 42.2%
Taylor expanded in x around 0 8.2%
Final simplification8.2%
herbie shell --seed 2024055
(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))))))