
(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 (/ (/ 1.0 s) (+ (exp (/ (- x) s)) (+ 2.0 (exp (/ x s))))))
float code(float x, float s) {
return (1.0f / s) / (expf((-x / s)) + (2.0f + expf((x / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / s) / (exp((-x / s)) + (2.0e0 + exp((x / s))))
end function
function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(exp(Float32(Float32(-x) / s)) + Float32(Float32(2.0) + exp(Float32(x / s))))) end
function tmp = code(x, s) tmp = (single(1.0) / s) / (exp((-x / s)) + (single(2.0) + exp((x / s)))); end
\begin{array}{l}
\\
\frac{\frac{1}{s}}{e^{\frac{-x}{s}} + \left(2 + e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.4%
Simplified99.5%
*-un-lft-identity99.5%
neg-mul-199.5%
times-frac99.5%
metadata-eval99.5%
metadata-eval99.5%
times-frac99.5%
neg-mul-199.5%
*-un-lft-identity99.5%
distribute-frac-neg99.5%
rec-exp99.5%
div-inv99.5%
exp-prod97.1%
add-sqr-sqrt97.1%
sqrt-unprod97.1%
sqr-neg97.1%
sqrt-unprod-0.0%
add-sqr-sqrt96.8%
exp-prod96.0%
div-inv96.0%
Applied egg-rr97.9%
rec-exp97.9%
distribute-neg-frac97.9%
Simplified97.9%
Taylor expanded in s around 0 97.8%
+-commutative97.8%
associate-+r+97.8%
+-commutative97.8%
+-commutative97.8%
associate-+r+97.8%
+-commutative97.8%
associate-*r/97.8%
mul-1-neg97.8%
Simplified97.8%
Taylor expanded in s around 0 97.8%
associate-/r*97.9%
mul-1-neg97.9%
*-lft-identity97.9%
*-lft-identity97.9%
unpow197.9%
sqr-pow47.9%
fabs-sqr47.9%
sqr-pow99.5%
unpow199.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x s) :precision binary32 (/ 1.0 (* s (+ (exp (/ (- x) s)) (+ 2.0 (exp (/ x s)))))))
float code(float x, float s) {
return 1.0f / (s * (expf((-x / s)) + (2.0f + expf((x / s)))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (s * (exp((-x / s)) + (2.0e0 + exp((x / s)))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(exp(Float32(Float32(-x) / s)) + Float32(Float32(2.0) + exp(Float32(x / s)))))) end
function tmp = code(x, s) tmp = single(1.0) / (s * (exp((-x / s)) + (single(2.0) + exp((x / s))))); end
\begin{array}{l}
\\
\frac{1}{s \cdot \left(e^{\frac{-x}{s}} + \left(2 + e^{\frac{x}{s}}\right)\right)}
\end{array}
Initial program 99.4%
Simplified99.5%
*-un-lft-identity99.5%
neg-mul-199.5%
times-frac99.5%
metadata-eval99.5%
metadata-eval99.5%
times-frac99.5%
neg-mul-199.5%
*-un-lft-identity99.5%
distribute-frac-neg99.5%
rec-exp99.5%
div-inv99.5%
exp-prod97.1%
add-sqr-sqrt97.1%
sqrt-unprod97.1%
sqr-neg97.1%
sqrt-unprod-0.0%
add-sqr-sqrt96.8%
exp-prod96.0%
div-inv96.0%
Applied egg-rr97.9%
rec-exp97.9%
distribute-neg-frac97.9%
Simplified97.9%
Taylor expanded in s around 0 97.8%
+-commutative97.8%
associate-+r+97.8%
+-commutative97.8%
+-commutative97.8%
associate-+r+97.8%
+-commutative97.8%
associate-*r/97.8%
mul-1-neg97.8%
Simplified97.8%
Taylor expanded in s around 0 97.8%
+-commutative97.8%
associate-+r+97.8%
mul-1-neg97.8%
distribute-frac-neg97.8%
+-commutative97.8%
distribute-frac-neg97.8%
*-lft-identity97.8%
*-lft-identity97.8%
unpow197.8%
sqr-pow47.9%
fabs-sqr47.9%
sqr-pow99.5%
unpow199.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x s) :precision binary32 (/ 1.0 (fma s (+ (exp (/ x s)) 3.0) (- x))))
float code(float x, float s) {
return 1.0f / fmaf(s, (expf((x / s)) + 3.0f), -x);
}
function code(x, s) return Float32(Float32(1.0) / fma(s, Float32(exp(Float32(x / s)) + Float32(3.0)), Float32(-x))) end
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(s, e^{\frac{x}{s}} + 3, -x\right)}
\end{array}
Initial program 99.4%
Simplified99.5%
*-un-lft-identity99.5%
neg-mul-199.5%
times-frac99.5%
metadata-eval99.5%
metadata-eval99.5%
times-frac99.5%
neg-mul-199.5%
*-un-lft-identity99.5%
distribute-frac-neg99.5%
rec-exp99.5%
div-inv99.5%
exp-prod97.1%
add-sqr-sqrt97.1%
sqrt-unprod97.1%
sqr-neg97.1%
sqrt-unprod-0.0%
add-sqr-sqrt96.8%
exp-prod96.0%
div-inv96.0%
Applied egg-rr97.9%
rec-exp97.9%
distribute-neg-frac97.9%
Simplified97.9%
Taylor expanded in s around 0 97.8%
+-commutative97.8%
associate-+r+97.8%
+-commutative97.8%
+-commutative97.8%
associate-+r+97.8%
+-commutative97.8%
associate-*r/97.8%
mul-1-neg97.8%
Simplified97.8%
Taylor expanded in x around 0 97.3%
fma-def97.3%
*-lft-identity97.3%
*-lft-identity97.3%
unpow197.3%
sqr-pow47.1%
fabs-sqr47.1%
sqr-pow62.2%
unpow162.2%
neg-mul-162.2%
Simplified62.2%
Final simplification62.2%
(FPCore (x s) :precision binary32 (if (<= (fabs x) 1.9999999996399175e-23) (/ 0.25 s) (/ (/ 1.0 s) (+ 4.0 (* 0.5 (/ (* x x) (* s s)))))))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 1.9999999996399175e-23f) {
tmp = 0.25f / s;
} else {
tmp = (1.0f / s) / (4.0f + (0.5f * ((x * x) / (s * s))));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (abs(x) <= 1.9999999996399175e-23) then
tmp = 0.25e0 / s
else
tmp = (1.0e0 / s) / (4.0e0 + (0.5e0 * ((x * x) / (s * s))))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(1.9999999996399175e-23)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(0.5) * Float32(Float32(x * x) / Float32(s * s))))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (abs(x) <= single(1.9999999996399175e-23)) tmp = single(0.25) / s; else tmp = (single(1.0) / s) / (single(4.0) + (single(0.5) * ((x * x) / (s * s)))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 1.9999999996399175 \cdot 10^{-23}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{s}}{4 + 0.5 \cdot \frac{x \cdot x}{s \cdot s}}\\
\end{array}
\end{array}
if (fabs.f32 x) < 2e-23Initial program 97.7%
/-rgt-identity97.7%
associate-/l/97.7%
*-lft-identity97.7%
+-commutative97.7%
distribute-rgt-in97.8%
*-lft-identity97.8%
+-commutative97.8%
distribute-rgt-in97.8%
*-lft-identity97.8%
Simplified97.7%
Taylor expanded in s around inf 76.4%
if 2e-23 < (fabs.f32 x) Initial program 99.9%
Simplified99.9%
Taylor expanded in s around inf 64.3%
mul-1-neg64.3%
unsub-neg64.3%
Simplified64.3%
associate-+r-64.3%
add-sqr-sqrt30.2%
fabs-sqr30.2%
add-sqr-sqrt55.9%
add-sqr-sqrt30.3%
fabs-sqr30.3%
add-sqr-sqrt56.2%
Applied egg-rr56.2%
Taylor expanded in x around 0 85.0%
unpow285.0%
unpow285.0%
Simplified85.0%
Final simplification83.3%
(FPCore (x s) :precision binary32 (/ (/ 1.0 s) (+ (exp (/ x s)) 3.0)))
float code(float x, float s) {
return (1.0f / s) / (expf((x / s)) + 3.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / s) / (exp((x / s)) + 3.0e0)
end function
function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(exp(Float32(x / s)) + Float32(3.0))) end
function tmp = code(x, s) tmp = (single(1.0) / s) / (exp((x / s)) + single(3.0)); end
\begin{array}{l}
\\
\frac{\frac{1}{s}}{e^{\frac{x}{s}} + 3}
\end{array}
Initial program 99.4%
Simplified99.5%
Taylor expanded in s around inf 97.0%
expm1-log1p-u95.8%
expm1-udef95.1%
clear-num95.1%
div-inv95.1%
add-sqr-sqrt45.7%
fabs-sqr45.7%
add-sqr-sqrt57.7%
clear-num57.7%
/-rgt-identity57.7%
Applied egg-rr57.7%
expm1-def58.1%
expm1-log1p59.3%
associate-/r*59.3%
*-lft-identity59.3%
associate-*l/59.3%
associate-*r/59.3%
*-rgt-identity59.3%
+-commutative59.3%
Simplified59.3%
Final simplification59.3%
(FPCore (x s) :precision binary32 (if (<= x 2.8500001008069376e-6) (/ 0.25 s) (/ (/ 1.0 s) (/ x s))))
float code(float x, float s) {
float tmp;
if (x <= 2.8500001008069376e-6f) {
tmp = 0.25f / s;
} else {
tmp = (1.0f / s) / (x / s);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 2.8500001008069376e-6) then
tmp = 0.25e0 / s
else
tmp = (1.0e0 / s) / (x / s)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(2.8500001008069376e-6)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(1.0) / s) / Float32(x / s)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(2.8500001008069376e-6)) tmp = single(0.25) / s; else tmp = (single(1.0) / s) / (x / s); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.8500001008069376 \cdot 10^{-6}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{s}}{\frac{x}{s}}\\
\end{array}
\end{array}
if x < 2.8500001e-6Initial program 99.2%
/-rgt-identity99.2%
associate-/l/99.2%
*-lft-identity99.2%
+-commutative99.2%
distribute-rgt-in99.3%
*-lft-identity99.3%
+-commutative99.3%
distribute-rgt-in99.3%
*-lft-identity99.3%
Simplified99.3%
Taylor expanded in s around inf 32.6%
if 2.8500001e-6 < x Initial program 100.0%
Simplified100.0%
Taylor expanded in s around inf 100.0%
Taylor expanded in s around inf 54.1%
+-commutative54.1%
Simplified54.1%
Taylor expanded in s around 0 54.1%
unpow154.1%
sqr-pow54.1%
fabs-sqr54.1%
sqr-pow54.1%
unpow154.1%
Simplified54.1%
Final simplification38.1%
(FPCore (x s) :precision binary32 (/ (/ 1.0 s) (+ (/ x s) 4.0)))
float code(float x, float s) {
return (1.0f / s) / ((x / s) + 4.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / s) / ((x / s) + 4.0e0)
end function
function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(Float32(x / s) + Float32(4.0))) end
function tmp = code(x, s) tmp = (single(1.0) / s) / ((x / s) + single(4.0)); end
\begin{array}{l}
\\
\frac{\frac{1}{s}}{\frac{x}{s} + 4}
\end{array}
Initial program 99.4%
Simplified99.5%
Taylor expanded in s around inf 97.0%
Taylor expanded in s around inf 52.4%
+-commutative52.4%
Simplified52.4%
Taylor expanded in x around 0 52.4%
associate-/r*52.4%
+-commutative52.4%
*-lft-identity52.4%
*-lft-identity52.4%
unpow152.4%
sqr-pow25.0%
fabs-sqr25.0%
sqr-pow52.3%
unpow152.3%
Simplified52.3%
Final simplification52.3%
(FPCore (x s) :precision binary32 (if (<= x 2.8500001008069376e-6) (/ 0.25 s) (/ 1.0 x)))
float code(float x, float s) {
float tmp;
if (x <= 2.8500001008069376e-6f) {
tmp = 0.25f / s;
} else {
tmp = 1.0f / x;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 2.8500001008069376e-6) then
tmp = 0.25e0 / s
else
tmp = 1.0e0 / x
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(2.8500001008069376e-6)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(1.0) / x); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(2.8500001008069376e-6)) tmp = single(0.25) / s; else tmp = single(1.0) / x; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.8500001008069376 \cdot 10^{-6}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x}\\
\end{array}
\end{array}
if x < 2.8500001e-6Initial program 99.2%
/-rgt-identity99.2%
associate-/l/99.2%
*-lft-identity99.2%
+-commutative99.2%
distribute-rgt-in99.3%
*-lft-identity99.3%
+-commutative99.3%
distribute-rgt-in99.3%
*-lft-identity99.3%
Simplified99.3%
Taylor expanded in s around inf 32.6%
if 2.8500001e-6 < x Initial program 100.0%
Simplified100.0%
Taylor expanded in s around inf 100.0%
Taylor expanded in s around inf 54.1%
+-commutative54.1%
Simplified54.1%
Taylor expanded in s around 0 10.4%
unpow110.4%
sqr-pow10.4%
fabs-sqr10.4%
sqr-pow10.4%
unpow110.4%
Simplified10.4%
Final simplification26.8%
(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.4%
/-rgt-identity99.4%
associate-/l/99.4%
*-lft-identity99.4%
+-commutative99.4%
distribute-rgt-in99.5%
*-lft-identity99.5%
+-commutative99.5%
distribute-rgt-in99.5%
*-lft-identity99.5%
Simplified99.5%
Taylor expanded in s around inf 25.4%
Final simplification25.4%
herbie shell --seed 2023215
(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))))))