
(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 8 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) (+ 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(Float32(-abs(x)) / 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.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x s)
:precision binary32
(let* ((t_0 (+ 1.0 (exp (/ (- x) s)))))
(/
(/ (exp (* (/ x s) -0.6666666666666666)) (cbrt (exp (/ x s))))
(* s (* t_0 t_0)))))
float code(float x, float s) {
float t_0 = 1.0f + expf((-x / s));
return (expf(((x / s) * -0.6666666666666666f)) / cbrtf(expf((x / s)))) / (s * (t_0 * t_0));
}
function code(x, s) t_0 = Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) return Float32(Float32(exp(Float32(Float32(x / s) * Float32(-0.6666666666666666))) / cbrt(exp(Float32(x / s)))) / Float32(s * Float32(t_0 * t_0))) end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 1 + e^{\frac{-x}{s}}\\
\frac{\frac{e^{\frac{x}{s} \cdot -0.6666666666666666}}{\sqrt[3]{e^{\frac{x}{s}}}}}{s \cdot \left(t\_0 \cdot t\_0\right)}
\end{array}
\end{array}
Initial 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.5%
distribute-frac-neg299.5%
rec-exp99.5%
add-cube-cbrt99.3%
associate-/r*99.3%
div-inv99.4%
exp-prod93.9%
add-sqr-sqrt93.9%
sqrt-unprod93.9%
sqr-neg93.9%
sqrt-unprod33.5%
add-sqr-sqrt33.7%
exp-prod22.0%
div-inv22.0%
Applied egg-rr65.2%
*-un-lft-identity65.2%
pow-flip65.3%
metadata-eval65.3%
Applied egg-rr65.3%
*-lft-identity65.3%
unpow1/365.3%
exp-prod65.2%
*-commutative65.2%
exp-prod65.2%
*-commutative65.2%
associate-*r*65.2%
metadata-eval65.2%
*-commutative65.2%
Simplified65.2%
distribute-frac-neg265.2%
rec-exp65.2%
div-inv65.2%
add-sqr-sqrt65.2%
sqrt-unprod63.5%
sqr-neg63.5%
sqrt-unprod-0.0%
add-sqr-sqrt59.3%
div-inv59.3%
add-sqr-sqrt-0.0%
sqrt-unprod63.5%
sqr-neg63.5%
sqrt-unprod65.2%
add-sqr-sqrt65.2%
add-sqr-sqrt52.7%
fabs-sqr52.7%
add-sqr-sqrt64.3%
Applied egg-rr64.3%
rec-exp64.3%
distribute-neg-frac264.3%
Simplified64.3%
distribute-frac-neg265.2%
rec-exp65.2%
div-inv65.2%
add-sqr-sqrt65.2%
sqrt-unprod63.5%
sqr-neg63.5%
sqrt-unprod-0.0%
add-sqr-sqrt59.3%
div-inv59.3%
add-sqr-sqrt-0.0%
sqrt-unprod63.5%
sqr-neg63.5%
sqrt-unprod65.2%
add-sqr-sqrt65.2%
add-sqr-sqrt52.7%
fabs-sqr52.7%
add-sqr-sqrt64.3%
Applied egg-rr66.1%
rec-exp64.3%
distribute-neg-frac264.3%
Simplified66.1%
Final simplification66.1%
(FPCore (x s)
:precision binary32
(let* ((t_0 (exp (/ x s))))
(if (<= (fabs x) 0.10000000149011612)
(/ 1.0 (/ s (exp (+ (/ x s) (* -2.0 (log1p t_0))))))
(/ 0.5 (fma s t_0 s)))))
float code(float x, float s) {
float t_0 = expf((x / s));
float tmp;
if (fabsf(x) <= 0.10000000149011612f) {
tmp = 1.0f / (s / expf(((x / s) + (-2.0f * log1pf(t_0)))));
} else {
tmp = 0.5f / fmaf(s, t_0, s);
}
return tmp;
}
function code(x, s) t_0 = exp(Float32(x / s)) tmp = Float32(0.0) if (abs(x) <= Float32(0.10000000149011612)) tmp = Float32(Float32(1.0) / Float32(s / exp(Float32(Float32(x / s) + Float32(Float32(-2.0) * log1p(t_0)))))); else tmp = Float32(Float32(0.5) / fma(s, t_0, s)); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{x}{s}}\\
\mathbf{if}\;\left|x\right| \leq 0.10000000149011612:\\
\;\;\;\;\frac{1}{\frac{s}{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(t\_0\right)}}}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{\mathsf{fma}\left(s, t\_0, s\right)}\\
\end{array}
\end{array}
if (fabs.f32 x) < 0.100000001Initial program 98.9%
fabs-neg98.9%
distribute-frac-neg98.9%
distribute-frac-neg298.9%
fabs-neg98.9%
*-commutative98.9%
fabs-neg98.9%
+-commutative98.9%
fabs-neg98.9%
Simplified98.9%
div-inv98.9%
distribute-frac-neg298.9%
distribute-frac-neg98.9%
add-sqr-sqrt-0.0%
sqrt-unprod46.5%
sqr-neg46.5%
sqrt-unprod46.1%
add-sqr-sqrt46.1%
add-sqr-sqrt21.2%
fabs-sqr21.2%
add-sqr-sqrt73.4%
add-sqr-sqrt72.9%
Applied egg-rr82.1%
*-commutative82.1%
exp-to-pow82.1%
+-commutative82.1%
log1p-undefine82.2%
*-commutative82.2%
rem-exp-log78.1%
prod-exp79.7%
associate-/l*79.8%
*-rgt-identity79.8%
exp-diff94.7%
associate--r+94.8%
exp-diff95.0%
Simplified98.9%
clear-num98.9%
inv-pow98.9%
sub-neg98.9%
*-commutative98.9%
distribute-rgt-neg-in98.9%
metadata-eval98.9%
Applied egg-rr98.9%
unpow-198.9%
+-commutative98.9%
+-commutative98.9%
*-commutative98.9%
Simplified98.9%
if 0.100000001 < (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%
*-un-lft-identity100.0%
associate-*r*100.0%
times-frac100.0%
Applied egg-rr41.8%
associate-*l/41.8%
*-lft-identity41.8%
+-commutative41.8%
Simplified41.8%
Taylor expanded in x around 0 60.1%
Final simplification78.6%
(FPCore (x s)
:precision binary32
(let* ((t_0 (exp (/ x s))))
(if (<= (fabs x) 0.10000000149011612)
(/ (exp (- (/ x s) (* (log1p t_0) 2.0))) s)
(/ 0.5 (fma s t_0 s)))))
float code(float x, float s) {
float t_0 = expf((x / s));
float tmp;
if (fabsf(x) <= 0.10000000149011612f) {
tmp = expf(((x / s) - (log1pf(t_0) * 2.0f))) / s;
} else {
tmp = 0.5f / fmaf(s, t_0, s);
}
return tmp;
}
function code(x, s) t_0 = exp(Float32(x / s)) tmp = Float32(0.0) if (abs(x) <= Float32(0.10000000149011612)) tmp = Float32(exp(Float32(Float32(x / s) - Float32(log1p(t_0) * Float32(2.0)))) / s); else tmp = Float32(Float32(0.5) / fma(s, t_0, s)); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{x}{s}}\\
\mathbf{if}\;\left|x\right| \leq 0.10000000149011612:\\
\;\;\;\;\frac{e^{\frac{x}{s} - \mathsf{log1p}\left(t\_0\right) \cdot 2}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{\mathsf{fma}\left(s, t\_0, s\right)}\\
\end{array}
\end{array}
if (fabs.f32 x) < 0.100000001Initial program 98.9%
fabs-neg98.9%
distribute-frac-neg98.9%
distribute-frac-neg298.9%
fabs-neg98.9%
*-commutative98.9%
fabs-neg98.9%
+-commutative98.9%
fabs-neg98.9%
Simplified98.9%
div-inv98.9%
distribute-frac-neg298.9%
distribute-frac-neg98.9%
add-sqr-sqrt-0.0%
sqrt-unprod46.5%
sqr-neg46.5%
sqrt-unprod46.1%
add-sqr-sqrt46.1%
add-sqr-sqrt21.2%
fabs-sqr21.2%
add-sqr-sqrt73.4%
add-sqr-sqrt72.9%
Applied egg-rr82.1%
*-commutative82.1%
exp-to-pow82.1%
+-commutative82.1%
log1p-undefine82.2%
*-commutative82.2%
rem-exp-log78.1%
prod-exp79.7%
associate-/l*79.8%
*-rgt-identity79.8%
exp-diff94.7%
associate--r+94.8%
exp-diff95.0%
Simplified98.9%
if 0.100000001 < (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%
*-un-lft-identity100.0%
associate-*r*100.0%
times-frac100.0%
Applied egg-rr41.8%
associate-*l/41.8%
*-lft-identity41.8%
+-commutative41.8%
Simplified41.8%
Taylor expanded in x around 0 60.1%
Final simplification78.6%
(FPCore (x s) :precision binary32 (/ 0.5 (fma s (exp (/ x s)) s)))
float code(float x, float s) {
return 0.5f / fmaf(s, expf((x / s)), s);
}
function code(x, s) return Float32(Float32(0.5) / fma(s, exp(Float32(x / s)), s)) end
\begin{array}{l}
\\
\frac{0.5}{\mathsf{fma}\left(s, e^{\frac{x}{s}}, s\right)}
\end{array}
Initial 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.5%
*-un-lft-identity99.5%
associate-*r*99.5%
times-frac99.5%
Applied egg-rr62.0%
associate-*l/62.0%
*-lft-identity62.0%
+-commutative62.0%
Simplified62.0%
Taylor expanded in x around 0 61.6%
Final simplification61.6%
(FPCore (x s) :precision binary32 (/ (/ 1.0 s) (+ 4.0 (* (/ x s) 4.0))))
float code(float x, float s) {
return (1.0f / s) / (4.0f + ((x / s) * 4.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / s) / (4.0e0 + ((x / s) * 4.0e0))
end function
function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(x / s) * Float32(4.0)))) end
function tmp = code(x, s) tmp = (single(1.0) / s) / (single(4.0) + ((x / s) * single(4.0))); end
\begin{array}{l}
\\
\frac{\frac{1}{s}}{4 + \frac{x}{s} \cdot 4}
\end{array}
Initial 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.5%
div-inv99.5%
distribute-frac-neg299.5%
distribute-frac-neg99.5%
add-sqr-sqrt-0.0%
sqrt-unprod23.8%
sqr-neg23.8%
sqrt-unprod23.6%
add-sqr-sqrt23.6%
add-sqr-sqrt11.0%
fabs-sqr11.0%
add-sqr-sqrt57.8%
add-sqr-sqrt57.6%
Applied egg-rr61.0%
associate-*r/61.0%
*-rgt-identity61.0%
associate-/r*60.3%
+-commutative60.3%
Simplified60.3%
Taylor expanded in x around 0 57.4%
*-commutative57.4%
Simplified57.4%
Taylor expanded in x around 0 53.9%
Final simplification53.9%
(FPCore (x s) :precision binary32 (if (<= x 0.05000000074505806) (/ 0.25 s) (/ 0.25 x)))
float code(float x, float s) {
float tmp;
if (x <= 0.05000000074505806f) {
tmp = 0.25f / s;
} else {
tmp = 0.25f / x;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 0.05000000074505806e0) then
tmp = 0.25e0 / s
else
tmp = 0.25e0 / x
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.05000000074505806)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.25) / x); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.05000000074505806)) tmp = single(0.25) / s; else tmp = single(0.25) / x; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.05000000074505806:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.25}{x}\\
\end{array}
\end{array}
if x < 0.0500000007Initial program 99.3%
fabs-neg99.3%
distribute-frac-neg99.3%
distribute-frac-neg299.3%
fabs-neg99.3%
*-commutative99.3%
fabs-neg99.3%
+-commutative99.3%
fabs-neg99.3%
Simplified99.3%
Taylor expanded in s around inf 37.3%
if 0.0500000007 < 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%
div-inv99.9%
distribute-frac-neg299.9%
distribute-frac-neg99.9%
add-sqr-sqrt-0.0%
sqrt-unprod3.3%
sqr-neg3.3%
sqrt-unprod3.3%
add-sqr-sqrt3.3%
add-sqr-sqrt3.3%
fabs-sqr3.3%
add-sqr-sqrt3.3%
add-sqr-sqrt3.3%
Applied egg-rr1.3%
associate-*r/1.2%
*-rgt-identity1.2%
associate-/r*1.3%
+-commutative1.3%
Simplified1.3%
Taylor expanded in x around 0 1.5%
*-commutative1.5%
Simplified1.5%
Taylor expanded in x around 0 61.0%
Taylor expanded in s around 0 12.0%
Final simplification29.5%
(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.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.5%
Taylor expanded in s around inf 27.2%
Final simplification27.2%
herbie shell --seed 2024039
(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))))))