
(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 (* (+ 1.0 (exp (/ (fabs x) (- s)))) (fma s (exp (/ (fabs x) s)) s))))
float code(float x, float s) {
return 1.0f / ((1.0f + expf((fabsf(x) / -s))) * fmaf(s, expf((fabsf(x) / s)), s));
}
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(abs(x) / Float32(-s)))) * fma(s, exp(Float32(abs(x) / s)), s))) end
\begin{array}{l}
\\
\frac{1}{\left(1 + e^{\frac{\left|x\right|}{-s}}\right) \cdot \mathsf{fma}\left(s, e^{\frac{\left|x\right|}{s}}, s\right)}
\end{array}
Initial program 99.4%
*-lft-identity99.4%
associate-*r/99.4%
associate-/l*99.4%
distribute-frac-neg99.4%
exp-neg99.4%
associate-/r/99.4%
/-rgt-identity99.4%
associate-*l*99.4%
Simplified99.6%
Final simplification99.6%
(FPCore (x s) :precision binary32 (/ (/ 1.0 s) (+ (exp (/ (fabs x) s)) (+ (exp (/ (fabs x) (- s))) 2.0))))
float code(float x, float s) {
return (1.0f / s) / (expf((fabsf(x) / s)) + (expf((fabsf(x) / -s)) + 2.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / s) / (exp((abs(x) / s)) + (exp((abs(x) / -s)) + 2.0e0))
end function
function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(exp(Float32(abs(x) / s)) + Float32(exp(Float32(abs(x) / Float32(-s))) + Float32(2.0)))) end
function tmp = code(x, s) tmp = (single(1.0) / s) / (exp((abs(x) / s)) + (exp((abs(x) / -s)) + single(2.0))); end
\begin{array}{l}
\\
\frac{\frac{1}{s}}{e^{\frac{\left|x\right|}{s}} + \left(e^{\frac{\left|x\right|}{-s}} + 2\right)}
\end{array}
Initial program 99.4%
Simplified99.5%
Final simplification99.5%
(FPCore (x s)
:precision binary32
(let* ((t_0 (exp (/ x s))))
(if (<= (fabs x) 0.10000000149011612)
(/ (exp (- (/ x s) (* 2.0 (log1p t_0)))) s)
(/ (/ 1.0 s) (+ t_0 3.0)))))
float code(float x, float s) {
float t_0 = expf((x / s));
float tmp;
if (fabsf(x) <= 0.10000000149011612f) {
tmp = expf(((x / s) - (2.0f * log1pf(t_0)))) / s;
} else {
tmp = (1.0f / s) / (t_0 + 3.0f);
}
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(Float32(2.0) * log1p(t_0)))) / s); else tmp = Float32(Float32(Float32(1.0) / s) / Float32(t_0 + Float32(3.0))); 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} - 2 \cdot \mathsf{log1p}\left(t_0\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{s}}{t_0 + 3}\\
\end{array}
\end{array}
if (fabs.f32 x) < 0.100000001Initial program 98.9%
add-exp-log95.5%
*-commutative95.5%
log-prod94.8%
log1p-udef94.9%
*-commutative94.9%
log-prod93.4%
log1p-udef93.4%
Applied egg-rr93.4%
associate-+r+95.2%
exp-sum95.2%
count-295.2%
rem-exp-log99.1%
Simplified99.1%
expm1-log1p-u95.5%
expm1-udef95.5%
Applied egg-rr95.5%
expm1-def95.4%
expm1-log1p99.0%
Simplified99.0%
if 0.100000001 < (fabs.f32 x) Initial program 100.0%
Simplified100.0%
Taylor expanded in s around inf 100.0%
add-sqr-sqrt100.0%
sqrt-unprod100.0%
sqr-neg100.0%
distribute-frac-neg100.0%
distribute-frac-neg100.0%
sqrt-unprod-0.0%
add-sqr-sqrt4.5%
*-un-lft-identity4.5%
add-sqr-sqrt-0.0%
sqrt-unprod100.0%
distribute-frac-neg100.0%
distribute-frac-neg100.0%
sqr-neg100.0%
sqrt-unprod100.0%
add-sqr-sqrt100.0%
Applied egg-rr56.1%
*-lft-identity56.1%
Simplified56.1%
Final simplification77.9%
(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 95.9%
add-sqr-sqrt95.9%
sqrt-unprod95.9%
sqr-neg95.9%
distribute-frac-neg95.9%
distribute-frac-neg95.9%
sqrt-unprod-0.0%
add-sqr-sqrt27.0%
*-un-lft-identity27.0%
add-sqr-sqrt-0.0%
sqrt-unprod95.9%
distribute-frac-neg95.9%
distribute-frac-neg95.9%
sqr-neg95.9%
sqrt-unprod95.9%
add-sqr-sqrt95.9%
Applied egg-rr63.0%
*-lft-identity63.0%
Simplified63.0%
Final simplification63.0%
(FPCore (x s) :precision binary32 (/ (/ 1.0 s) (+ 4.0 (/ (* x x) (* s s)))))
float code(float x, float s) {
return (1.0f / s) / (4.0f + ((x * x) / (s * s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / s) / (4.0e0 + ((x * x) / (s * s)))
end function
function code(x, s) return Float32(Float32(Float32(1.0) / s) / Float32(Float32(4.0) + Float32(Float32(x * x) / Float32(s * s)))) end
function tmp = code(x, s) tmp = (single(1.0) / s) / (single(4.0) + ((x * x) / (s * s))); end
\begin{array}{l}
\\
\frac{\frac{1}{s}}{4 + \frac{x \cdot x}{s \cdot s}}
\end{array}
Initial program 99.4%
Simplified99.5%
Taylor expanded in s around inf 53.3%
associate-+r+53.3%
distribute-lft1-in53.3%
metadata-eval53.3%
mul0-lft82.6%
metadata-eval82.6%
associate-+r+82.6%
metadata-eval82.6%
metadata-eval82.6%
+-commutative82.6%
unpow282.6%
sqr-abs82.6%
unpow282.6%
Simplified82.6%
Final simplification82.6%
(FPCore (x s) :precision binary32 (/ 1.0 (+ (* x (/ x s)) (* s 4.0))))
float code(float x, float s) {
return 1.0f / ((x * (x / s)) + (s * 4.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((x * (x / s)) + (s * 4.0e0))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(x * Float32(x / s)) + Float32(s * Float32(4.0)))) end
function tmp = code(x, s) tmp = single(1.0) / ((x * (x / s)) + (s * single(4.0))); end
\begin{array}{l}
\\
\frac{1}{x \cdot \frac{x}{s} + s \cdot 4}
\end{array}
Initial program 99.4%
*-lft-identity99.4%
associate-*r/99.4%
associate-/l*99.4%
distribute-frac-neg99.4%
exp-neg99.4%
associate-/r/99.4%
/-rgt-identity99.4%
associate-*l*99.4%
Simplified99.6%
Taylor expanded in s around -inf 41.0%
associate-+r+41.0%
distribute-rgt-out41.0%
metadata-eval41.0%
mul0-rgt41.0%
fma-def41.0%
mul-1-neg41.0%
distribute-rgt1-in68.7%
metadata-eval68.7%
associate-*r/68.7%
mul-1-neg68.7%
remove-double-neg68.7%
unpow268.7%
sqr-abs68.7%
Simplified68.7%
expm1-log1p-u67.1%
expm1-udef81.7%
+-lft-identity81.7%
associate-/l*81.8%
Applied egg-rr81.8%
expm1-def67.3%
expm1-log1p68.9%
fma-def68.9%
+-commutative68.9%
associate-/r/68.9%
*-commutative68.9%
fma-def68.9%
*-commutative68.9%
Simplified68.9%
fma-udef68.9%
Applied egg-rr68.9%
Final simplification68.9%
(FPCore (x s) :precision binary32 (if (<= x 9.999999747378752e-5) (/ 0.25 s) (/ 1.0 (/ x (/ s x)))))
float code(float x, float s) {
float tmp;
if (x <= 9.999999747378752e-5f) {
tmp = 0.25f / s;
} else {
tmp = 1.0f / (x / (s / x));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 9.999999747378752e-5) then
tmp = 0.25e0 / s
else
tmp = 1.0e0 / (x / (s / x))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(9.999999747378752e-5)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(1.0) / Float32(x / Float32(s / x))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(9.999999747378752e-5)) tmp = single(0.25) / s; else tmp = single(1.0) / (x / (s / x)); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 9.999999747378752 \cdot 10^{-5}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x}{\frac{s}{x}}}\\
\end{array}
\end{array}
if x < 9.99999975e-5Initial program 99.2%
*-lft-identity99.2%
associate-*r/99.2%
associate-/l*99.2%
distribute-frac-neg99.2%
exp-neg99.2%
associate-/r/99.2%
/-rgt-identity99.2%
associate-*l*99.2%
Simplified99.4%
Taylor expanded in s around inf 37.6%
if 9.99999975e-5 < x Initial program 100.0%
*-lft-identity100.0%
associate-*r/100.0%
associate-/l*100.0%
distribute-frac-neg100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
associate-*l*100.0%
Simplified100.0%
Taylor expanded in s around -inf 25.6%
associate-+r+25.6%
distribute-rgt-out25.6%
metadata-eval25.6%
mul0-rgt25.6%
fma-def25.6%
mul-1-neg25.6%
distribute-rgt1-in84.5%
metadata-eval84.5%
associate-*r/84.5%
mul-1-neg84.5%
remove-double-neg84.5%
unpow284.5%
sqr-abs84.5%
Simplified84.5%
expm1-log1p-u84.5%
expm1-udef99.2%
+-lft-identity99.2%
associate-/l*99.2%
Applied egg-rr99.2%
expm1-def84.5%
expm1-log1p84.5%
fma-def84.5%
+-commutative84.5%
associate-/r/84.5%
*-commutative84.5%
fma-def84.5%
*-commutative84.5%
Simplified84.5%
Taylor expanded in x around inf 84.3%
unpow284.3%
associate-/l*84.3%
Simplified84.3%
Final simplification50.9%
(FPCore (x s) :precision binary32 (if (<= x 9.999999747378752e-5) (/ 0.25 s) (/ s (* x x))))
float code(float x, float s) {
float tmp;
if (x <= 9.999999747378752e-5f) {
tmp = 0.25f / s;
} else {
tmp = s / (x * x);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 9.999999747378752e-5) then
tmp = 0.25e0 / s
else
tmp = s / (x * x)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(9.999999747378752e-5)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(s / Float32(x * x)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(9.999999747378752e-5)) tmp = single(0.25) / s; else tmp = s / (x * x); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 9.999999747378752 \cdot 10^{-5}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{s}{x \cdot x}\\
\end{array}
\end{array}
if x < 9.99999975e-5Initial program 99.2%
*-lft-identity99.2%
associate-*r/99.2%
associate-/l*99.2%
distribute-frac-neg99.2%
exp-neg99.2%
associate-/r/99.2%
/-rgt-identity99.2%
associate-*l*99.2%
Simplified99.4%
Taylor expanded in s around inf 37.6%
if 9.99999975e-5 < x Initial program 100.0%
*-lft-identity100.0%
associate-*r/100.0%
associate-/l*100.0%
distribute-frac-neg100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
associate-*l*100.0%
Simplified100.0%
Taylor expanded in s around -inf 25.6%
associate-+r+25.6%
distribute-rgt-out25.6%
metadata-eval25.6%
mul0-rgt25.6%
fma-def25.6%
mul-1-neg25.6%
distribute-rgt1-in84.5%
metadata-eval84.5%
associate-*r/84.5%
mul-1-neg84.5%
remove-double-neg84.5%
unpow284.5%
sqr-abs84.5%
Simplified84.5%
Taylor expanded in s around 0 83.3%
unpow283.3%
Simplified83.3%
Final simplification50.6%
(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%
*-lft-identity99.4%
associate-*r/99.4%
associate-/l*99.4%
distribute-frac-neg99.4%
exp-neg99.4%
associate-/r/99.4%
/-rgt-identity99.4%
associate-*l*99.4%
Simplified99.6%
Taylor expanded in s around inf 28.3%
Final simplification28.3%
herbie shell --seed 2023230
(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))))))