
(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}
(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.3%
*-commutative99.3%
distribute-lft-in99.4%
*-rgt-identity99.4%
fabs-neg99.4%
distribute-frac-neg99.4%
exp-neg99.4%
associate-*r/99.4%
*-rgt-identity99.4%
*-lft-identity99.4%
metadata-eval99.4%
times-frac99.4%
neg-mul-199.4%
neg-mul-199.4%
fabs-neg99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (x s) :precision binary32 (/ 1.0 (* (+ (exp (/ (- (fabs x)) s)) 1.0) (fma s (exp (/ (fabs x) s)) s))))
float code(float x, float s) {
return 1.0f / ((expf((-fabsf(x) / s)) + 1.0f) * fmaf(s, expf((fabsf(x) / s)), s));
}
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(exp(Float32(Float32(-abs(x)) / s)) + Float32(1.0)) * fma(s, exp(Float32(abs(x) / s)), s))) end
\begin{array}{l}
\\
\frac{1}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \mathsf{fma}\left(s, e^{\frac{\left|x\right|}{s}}, s\right)}
\end{array}
Initial program 99.3%
Simplified99.1%
Final simplification99.1%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- (fabs x)) s)))) (/ (/ t_0 s) (pow (+ t_0 1.0) 2.0))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
return (t_0 / s) / powf((t_0 + 1.0f), 2.0f);
}
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 / s) / ((t_0 + 1.0e0) ** 2.0e0)
end function
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) return Float32(Float32(t_0 / s) / (Float32(t_0 + Float32(1.0)) ^ Float32(2.0))) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); tmp = (t_0 / s) / ((t_0 + single(1.0)) ^ single(2.0)); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-\left|x\right|}{s}}\\
\frac{\frac{t\_0}{s}}{{\left(t\_0 + 1\right)}^{2}}
\end{array}
\end{array}
Initial program 99.3%
*-commutative99.3%
distribute-lft-in99.4%
*-rgt-identity99.4%
fabs-neg99.4%
distribute-frac-neg99.4%
exp-neg99.4%
associate-*r/99.4%
*-rgt-identity99.4%
*-lft-identity99.4%
metadata-eval99.4%
times-frac99.4%
neg-mul-199.4%
neg-mul-199.4%
fabs-neg99.4%
Simplified99.4%
Taylor expanded in s around 0 99.3%
associate-/r*99.3%
associate-*r/99.3%
mul-1-neg99.3%
rec-exp99.3%
mul-1-neg99.3%
unpow299.3%
associate-*r/99.3%
mul-1-neg99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (x s)
:precision binary32
(let* ((t_0 (exp (/ x s))))
(if (<= (fabs x) 0.009999999776482582)
(/ (exp (+ (/ x s) (* (log1p t_0) -2.0))) s)
(/ (/ 0.5 s) (+ 1.0 t_0)))))
float code(float x, float s) {
float t_0 = expf((x / s));
float tmp;
if (fabsf(x) <= 0.009999999776482582f) {
tmp = expf(((x / s) + (log1pf(t_0) * -2.0f))) / s;
} else {
tmp = (0.5f / s) / (1.0f + t_0);
}
return tmp;
}
function code(x, s) t_0 = exp(Float32(x / s)) tmp = Float32(0.0) if (abs(x) <= Float32(0.009999999776482582)) tmp = Float32(exp(Float32(Float32(x / s) + Float32(log1p(t_0) * Float32(-2.0)))) / s); else tmp = Float32(Float32(Float32(0.5) / s) / Float32(Float32(1.0) + t_0)); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{x}{s}}\\
\mathbf{if}\;\left|x\right| \leq 0.009999999776482582:\\
\;\;\;\;\frac{e^{\frac{x}{s} + \mathsf{log1p}\left(t\_0\right) \cdot -2}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{0.5}{s}}{1 + t\_0}\\
\end{array}
\end{array}
if (fabs.f32 x) < 0.00999999978Initial program 98.7%
*-commutative98.7%
distribute-lft-in98.8%
*-rgt-identity98.8%
fabs-neg98.8%
distribute-frac-neg98.8%
exp-neg98.8%
associate-*r/98.8%
*-rgt-identity98.8%
*-lft-identity98.8%
metadata-eval98.8%
times-frac98.8%
neg-mul-198.8%
neg-mul-198.8%
fabs-neg98.8%
Simplified98.8%
associate-/r*98.7%
+-commutative98.7%
div-inv98.7%
fma-define98.6%
rec-exp98.6%
distribute-frac-neg98.6%
associate-/r*98.7%
clear-num98.7%
associate-/r/97.9%
Applied egg-rr75.5%
Applied egg-rr75.5%
pow175.5%
add-exp-log75.5%
log-prod75.5%
add-log-exp98.6%
log-pow98.6%
log1p-define98.6%
Applied egg-rr98.6%
unpow198.6%
*-commutative98.6%
Simplified98.6%
if 0.00999999978 < (fabs.f32 x) Initial program 100.0%
*-commutative100.0%
distribute-lft-in100.0%
*-rgt-identity100.0%
fabs-neg100.0%
distribute-frac-neg100.0%
exp-neg100.0%
associate-*r/100.0%
*-rgt-identity100.0%
*-lft-identity100.0%
metadata-eval100.0%
times-frac100.0%
neg-mul-1100.0%
neg-mul-1100.0%
fabs-neg100.0%
Simplified100.0%
associate-/r*100.0%
+-commutative100.0%
div-inv100.0%
fma-define100.0%
rec-exp100.0%
distribute-frac-neg100.0%
associate-/r*100.0%
clear-num100.0%
associate-/r/100.0%
Applied egg-rr44.4%
Applied egg-rr44.4%
Taylor expanded in x around 0 57.6%
Final simplification78.4%
(FPCore (x s) :precision binary32 (/ 1.0 (* (+ (exp (/ (- (fabs x)) s)) 1.0) (* s (+ 1.0 (exp (/ x s)))))))
float code(float x, float s) {
return 1.0f / ((expf((-fabsf(x) / s)) + 1.0f) * (s * (1.0f + expf((x / s)))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((exp((-abs(x) / s)) + 1.0e0) * (s * (1.0e0 + exp((x / s)))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(exp(Float32(Float32(-abs(x)) / s)) + Float32(1.0)) * Float32(s * Float32(Float32(1.0) + exp(Float32(x / s)))))) end
function tmp = code(x, s) tmp = single(1.0) / ((exp((-abs(x) / s)) + single(1.0)) * (s * (single(1.0) + exp((x / s))))); end
\begin{array}{l}
\\
\frac{1}{\left(e^{\frac{-\left|x\right|}{s}} + 1\right) \cdot \left(s \cdot \left(1 + e^{\frac{x}{s}}\right)\right)}
\end{array}
Initial program 99.3%
Simplified99.1%
add-sqr-sqrt98.9%
sqrt-unprod94.0%
sqr-neg94.0%
sqrt-unprod-0.0%
add-sqr-sqrt28.0%
frac-2neg28.0%
frac-2neg28.0%
fma-undefine28.0%
*-un-lft-identity28.0%
*-commutative28.0%
distribute-lft-in28.0%
+-commutative28.0%
*-commutative28.0%
*-un-lft-identity28.0%
Applied egg-rr68.0%
Final simplification68.0%
(FPCore (x s) :precision binary32 (if (<= (fabs x) 3.000000106112566e-7) (/ 0.25 s) (/ 0.5 (fabs x))))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 3.000000106112566e-7f) {
tmp = 0.25f / s;
} else {
tmp = 0.5f / fabsf(x);
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (abs(x) <= 3.000000106112566e-7) then
tmp = 0.25e0 / s
else
tmp = 0.5e0 / abs(x)
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(3.000000106112566e-7)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.5) / abs(x)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (abs(x) <= single(3.000000106112566e-7)) tmp = single(0.25) / s; else tmp = single(0.5) / abs(x); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 3.000000106112566 \cdot 10^{-7}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{\left|x\right|}\\
\end{array}
\end{array}
if (fabs.f32 x) < 3.0000001e-7Initial program 98.4%
*-commutative98.4%
distribute-lft-in98.6%
*-rgt-identity98.6%
fabs-neg98.6%
distribute-frac-neg98.6%
exp-neg98.6%
associate-*r/98.6%
*-rgt-identity98.6%
*-lft-identity98.6%
metadata-eval98.6%
times-frac98.6%
neg-mul-198.6%
neg-mul-198.6%
fabs-neg98.6%
Simplified98.6%
Taylor expanded in s around inf 60.7%
if 3.0000001e-7 < (fabs.f32 x) Initial program 100.0%
Simplified100.0%
Taylor expanded in s around inf 99.0%
Taylor expanded in s around inf 13.3%
*-commutative13.3%
Simplified13.3%
Taylor expanded in s around 0 11.8%
Final simplification33.2%
(FPCore (x s) :precision binary32 (/ (/ 0.5 s) (+ 1.0 (exp (/ x s)))))
float code(float x, float s) {
return (0.5f / s) / (1.0f + expf((x / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.5e0 / s) / (1.0e0 + exp((x / s)))
end function
function code(x, s) return Float32(Float32(Float32(0.5) / s) / Float32(Float32(1.0) + exp(Float32(x / s)))) end
function tmp = code(x, s) tmp = (single(0.5) / s) / (single(1.0) + exp((x / s))); end
\begin{array}{l}
\\
\frac{\frac{0.5}{s}}{1 + e^{\frac{x}{s}}}
\end{array}
Initial program 99.3%
*-commutative99.3%
distribute-lft-in99.4%
*-rgt-identity99.4%
fabs-neg99.4%
distribute-frac-neg99.4%
exp-neg99.4%
associate-*r/99.4%
*-rgt-identity99.4%
*-lft-identity99.4%
metadata-eval99.4%
times-frac99.4%
neg-mul-199.4%
neg-mul-199.4%
fabs-neg99.4%
Simplified99.4%
associate-/r*99.3%
+-commutative99.3%
div-inv99.3%
fma-define99.3%
rec-exp99.3%
distribute-frac-neg99.3%
associate-/r*99.4%
clear-num99.3%
associate-/r/98.9%
Applied egg-rr60.2%
Applied egg-rr60.2%
Taylor expanded in x around 0 65.7%
Final simplification65.7%
(FPCore (x s) :precision binary32 (/ 1.0 (* 2.0 (fma s 2.0 x))))
float code(float x, float s) {
return 1.0f / (2.0f * fmaf(s, 2.0f, x));
}
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(2.0) * fma(s, Float32(2.0), x))) end
\begin{array}{l}
\\
\frac{1}{2 \cdot \mathsf{fma}\left(s, 2, x\right)}
\end{array}
Initial program 99.3%
Simplified99.1%
Taylor expanded in s around inf 94.4%
*-un-lft-identity94.4%
exp-prod94.4%
add-sqr-sqrt94.4%
sqrt-unprod92.2%
sqr-neg92.2%
sqrt-unprod-0.0%
add-sqr-sqrt28.2%
add-sqr-sqrt-0.0%
sqrt-unprod92.2%
sqr-neg92.2%
sqrt-unprod94.4%
add-sqr-sqrt94.4%
add-sqr-sqrt51.5%
fabs-sqr51.5%
add-sqr-sqrt65.7%
Applied egg-rr65.7%
exp-1-e65.7%
Simplified65.7%
Taylor expanded in s around inf 32.8%
*-commutative32.8%
fma-define32.8%
log-E32.8%
*-rgt-identity32.8%
Simplified32.8%
Final simplification32.8%
(FPCore (x s) :precision binary32 (/ 0.5 (fma s 2.0 x)))
float code(float x, float s) {
return 0.5f / fmaf(s, 2.0f, x);
}
function code(x, s) return Float32(Float32(0.5) / fma(s, Float32(2.0), x)) end
\begin{array}{l}
\\
\frac{0.5}{\mathsf{fma}\left(s, 2, x\right)}
\end{array}
Initial program 99.3%
Simplified99.1%
Taylor expanded in s around inf 94.4%
Taylor expanded in s around inf 33.2%
*-commutative33.2%
Simplified33.2%
*-un-lft-identity33.2%
associate-/r*32.4%
metadata-eval32.4%
+-commutative32.4%
fma-define32.4%
add-sqr-sqrt16.0%
fabs-sqr16.0%
add-sqr-sqrt32.0%
Applied egg-rr32.0%
*-lft-identity32.0%
Simplified32.0%
Final simplification32.0%
(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.3%
*-commutative99.3%
distribute-lft-in99.4%
*-rgt-identity99.4%
fabs-neg99.4%
distribute-frac-neg99.4%
exp-neg99.4%
associate-*r/99.4%
*-rgt-identity99.4%
*-lft-identity99.4%
metadata-eval99.4%
times-frac99.4%
neg-mul-199.4%
neg-mul-199.4%
fabs-neg99.4%
Simplified99.4%
Taylor expanded in s around inf 29.3%
Final simplification29.3%
herbie shell --seed 2024035
(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))))))