
(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}
x_m = (fabs.f32 x)
(FPCore (x_m s)
:precision binary32
(let* ((t_0 (exp (/ x_m s))))
(if (<= (fabs x_m) 0.00019999999494757503)
(/ (exp (+ (/ x_m s) (* -2.0 (log1p t_0)))) s)
(/ (/ 0.5 s) (+ 1.0 t_0)))))x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = expf((x_m / s));
float tmp;
if (fabsf(x_m) <= 0.00019999999494757503f) {
tmp = expf(((x_m / s) + (-2.0f * log1pf(t_0)))) / s;
} else {
tmp = (0.5f / s) / (1.0f + t_0);
}
return tmp;
}
x_m = abs(x) function code(x_m, s) t_0 = exp(Float32(x_m / s)) tmp = Float32(0.0) if (abs(x_m) <= Float32(0.00019999999494757503)) tmp = Float32(exp(Float32(Float32(x_m / s) + Float32(Float32(-2.0) * log1p(t_0)))) / s); else tmp = Float32(Float32(Float32(0.5) / s) / Float32(Float32(1.0) + t_0)); end return tmp end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := e^{\frac{x_m}{s}}\\
\mathbf{if}\;\left|x_m\right| \leq 0.00019999999494757503:\\
\;\;\;\;\frac{e^{\frac{x_m}{s} + -2 \cdot \mathsf{log1p}\left(t_0\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{0.5}{s}}{1 + t_0}\\
\end{array}
\end{array}
if (fabs.f32 x) < 1.99999995e-4Initial 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%
Applied egg-rr73.2%
expm1-log1p-u69.6%
expm1-udef69.5%
pow269.5%
inv-pow69.5%
+-commutative69.5%
pow-pow69.5%
metadata-eval69.5%
Applied egg-rr69.5%
expm1-def69.6%
expm1-log1p73.3%
*-commutative73.3%
metadata-eval73.3%
pow-sqr73.2%
unpow-173.2%
unpow-173.2%
unpow273.2%
rem-exp-log73.2%
log1p-def73.2%
exp-neg73.4%
exp-prod73.5%
*-commutative73.5%
rem-exp-log69.4%
exp-diff69.9%
Simplified99.0%
if 1.99999995e-4 < (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%
Applied egg-rr79.1%
associate-*r/79.1%
*-rgt-identity79.1%
+-commutative79.1%
Simplified79.1%
Taylor expanded in x around 0 45.9%
exp-neg45.9%
rem-exp-log45.9%
metadata-eval45.9%
Simplified45.9%
Final simplification71.2%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 1.0 (* s (* (+ 1.0 (exp (/ (- (fabs x_m)) s))) (+ 1.0 (exp (/ (fabs x_m) s)))))))
x_m = fabs(x);
float code(float x_m, float s) {
return 1.0f / (s * ((1.0f + expf((-fabsf(x_m) / s))) * (1.0f + expf((fabsf(x_m) / s)))));
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = 1.0e0 / (s * ((1.0e0 + exp((-abs(x_m) / s))) * (1.0e0 + exp((abs(x_m) / s)))))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(Float32(1.0) + exp(Float32(Float32(-abs(x_m)) / s))) * Float32(Float32(1.0) + exp(Float32(abs(x_m) / s)))))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(1.0) / (s * ((single(1.0) + exp((-abs(x_m) / s))) * (single(1.0) + exp((abs(x_m) / s))))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{1}{s \cdot \left(\left(1 + e^{\frac{-\left|x_m\right|}{s}}\right) \cdot \left(1 + e^{\frac{\left|x_m\right|}{s}}\right)\right)}
\end{array}
Initial program 99.4%
Simplified99.4%
Taylor expanded in s around 0 99.4%
associate-*r/99.4%
mul-1-neg99.4%
Simplified99.4%
Final simplification99.4%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 1.0 (* (fma s (exp (/ (- (fabs x_m)) s)) s) (+ 1.0 (exp (/ x_m s))))))
x_m = fabs(x);
float code(float x_m, float s) {
return 1.0f / (fmaf(s, expf((-fabsf(x_m) / s)), s) * (1.0f + expf((x_m / s))));
}
x_m = abs(x) function code(x_m, s) return Float32(Float32(1.0) / Float32(fma(s, exp(Float32(Float32(-abs(x_m)) / s)), s) * Float32(Float32(1.0) + exp(Float32(x_m / s))))) end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{1}{\mathsf{fma}\left(s, e^{\frac{-\left|x_m\right|}{s}}, s\right) \cdot \left(1 + e^{\frac{x_m}{s}}\right)}
\end{array}
Initial program 99.4%
Simplified99.4%
add-sqr-sqrt99.4%
sqrt-unprod91.6%
sqr-neg91.6%
sqrt-unprod-0.0%
add-sqr-sqrt24.4%
expm1-log1p-u24.4%
expm1-udef24.4%
Applied egg-rr59.3%
expm1-def59.3%
expm1-log1p59.3%
+-commutative59.3%
Simplified59.3%
Final simplification59.3%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ 0.5 s) (+ 1.0 (exp (/ (* x_m (log E)) s)))))
x_m = fabs(x);
float code(float x_m, float s) {
return (0.5f / s) / (1.0f + expf(((x_m * logf(((float) M_E))) / s)));
}
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(0.5) / s) / Float32(Float32(1.0) + exp(Float32(Float32(x_m * log(Float32(exp(1)))) / s)))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(0.5) / s) / (single(1.0) + exp(((x_m * log(single(2.71828182845904523536))) / s))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{0.5}{s}}{1 + e^{\frac{x_m \cdot \log e}{s}}}
\end{array}
Initial program 99.4%
*-commutative99.4%
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%
Applied egg-rr88.4%
associate-*r/88.5%
*-rgt-identity88.5%
+-commutative88.5%
Simplified88.5%
Taylor expanded in x around 0 58.1%
exp-neg58.1%
rem-exp-log58.1%
metadata-eval58.1%
Simplified58.1%
*-un-lft-identity58.1%
exp-prod58.1%
Applied egg-rr58.1%
exp-1-e58.1%
Simplified58.1%
Taylor expanded in x around inf 58.1%
Final simplification58.1%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ 0.5 s) (+ 1.0 (pow E (/ x_m s)))))
x_m = fabs(x);
float code(float x_m, float s) {
return (0.5f / s) / (1.0f + powf(((float) M_E), (x_m / s)));
}
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(0.5) / s) / Float32(Float32(1.0) + (Float32(exp(1)) ^ Float32(x_m / s)))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(0.5) / s) / (single(1.0) + (single(2.71828182845904523536) ^ (x_m / s))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{0.5}{s}}{1 + {e}^{\left(\frac{x_m}{s}\right)}}
\end{array}
Initial program 99.4%
*-commutative99.4%
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%
Applied egg-rr88.4%
associate-*r/88.5%
*-rgt-identity88.5%
+-commutative88.5%
Simplified88.5%
Taylor expanded in x around 0 58.1%
exp-neg58.1%
rem-exp-log58.1%
metadata-eval58.1%
Simplified58.1%
*-un-lft-identity58.1%
exp-prod58.1%
Applied egg-rr58.1%
exp-1-e58.1%
Simplified58.1%
Final simplification58.1%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ 0.5 s) (+ 1.0 (exp (/ x_m s)))))
x_m = fabs(x);
float code(float x_m, float s) {
return (0.5f / s) / (1.0f + expf((x_m / s)));
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = (0.5e0 / s) / (1.0e0 + exp((x_m / s)))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(0.5) / s) / Float32(Float32(1.0) + exp(Float32(x_m / s)))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(0.5) / s) / (single(1.0) + exp((x_m / s))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{0.5}{s}}{1 + e^{\frac{x_m}{s}}}
\end{array}
Initial program 99.4%
*-commutative99.4%
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%
Applied egg-rr88.4%
associate-*r/88.5%
*-rgt-identity88.5%
+-commutative88.5%
Simplified88.5%
Taylor expanded in x around 0 58.1%
exp-neg58.1%
rem-exp-log58.1%
metadata-eval58.1%
Simplified58.1%
Final simplification58.1%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= x_m 0.00019999999494757503) (/ 0.25 s) (/ (/ 0.5 s) (/ x_m s))))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (x_m <= 0.00019999999494757503f) {
tmp = 0.25f / s;
} else {
tmp = (0.5f / s) / (x_m / s);
}
return tmp;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
real(4) :: tmp
if (x_m <= 0.00019999999494757503e0) then
tmp = 0.25e0 / s
else
tmp = (0.5e0 / s) / (x_m / s)
end if
code = tmp
end function
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (x_m <= Float32(0.00019999999494757503)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(Float32(0.5) / s) / Float32(x_m / s)); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, s) tmp = single(0.0); if (x_m <= single(0.00019999999494757503)) tmp = single(0.25) / s; else tmp = (single(0.5) / s) / (x_m / s); end tmp_2 = tmp; end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x_m \leq 0.00019999999494757503:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{0.5}{s}}{\frac{x_m}{s}}\\
\end{array}
\end{array}
if x < 1.99999995e-4Initial program 99.2%
*-commutative99.2%
distribute-lft-in99.3%
*-rgt-identity99.3%
fabs-neg99.3%
distribute-frac-neg99.3%
exp-neg99.3%
associate-*r/99.2%
*-rgt-identity99.2%
*-lft-identity99.2%
metadata-eval99.2%
times-frac99.2%
neg-mul-199.2%
neg-mul-199.2%
fabs-neg99.2%
Simplified99.2%
Taylor expanded in s around inf 33.2%
if 1.99999995e-4 < 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%
Applied egg-rr51.7%
associate-*r/51.7%
*-rgt-identity51.7%
+-commutative51.7%
Simplified51.7%
Taylor expanded in x around 0 100.0%
exp-neg100.0%
rem-exp-log100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 52.3%
+-commutative52.3%
Simplified52.3%
Taylor expanded in x around inf 52.3%
Final simplification37.5%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ 0.5 s) (+ (/ x_m s) 2.0)))
x_m = fabs(x);
float code(float x_m, float s) {
return (0.5f / s) / ((x_m / s) + 2.0f);
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = (0.5e0 / s) / ((x_m / s) + 2.0e0)
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(0.5) / s) / Float32(Float32(x_m / s) + Float32(2.0))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(0.5) / s) / ((x_m / s) + single(2.0)); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{0.5}{s}}{\frac{x_m}{s} + 2}
\end{array}
Initial program 99.4%
*-commutative99.4%
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%
Applied egg-rr88.4%
associate-*r/88.5%
*-rgt-identity88.5%
+-commutative88.5%
Simplified88.5%
Taylor expanded in x around 0 58.1%
exp-neg58.1%
rem-exp-log58.1%
metadata-eval58.1%
Simplified58.1%
Taylor expanded in x around 0 51.1%
+-commutative51.1%
Simplified51.1%
Final simplification51.1%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= x_m 0.00019999999494757503) (/ 0.25 s) (/ 0.5 x_m)))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (x_m <= 0.00019999999494757503f) {
tmp = 0.25f / s;
} else {
tmp = 0.5f / x_m;
}
return tmp;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
real(4) :: tmp
if (x_m <= 0.00019999999494757503e0) then
tmp = 0.25e0 / s
else
tmp = 0.5e0 / x_m
end if
code = tmp
end function
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (x_m <= Float32(0.00019999999494757503)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.5) / x_m); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, s) tmp = single(0.0); if (x_m <= single(0.00019999999494757503)) tmp = single(0.25) / s; else tmp = single(0.5) / x_m; end tmp_2 = tmp; end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x_m \leq 0.00019999999494757503:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x_m}\\
\end{array}
\end{array}
if x < 1.99999995e-4Initial program 99.2%
*-commutative99.2%
distribute-lft-in99.3%
*-rgt-identity99.3%
fabs-neg99.3%
distribute-frac-neg99.3%
exp-neg99.3%
associate-*r/99.2%
*-rgt-identity99.2%
*-lft-identity99.2%
metadata-eval99.2%
times-frac99.2%
neg-mul-199.2%
neg-mul-199.2%
fabs-neg99.2%
Simplified99.2%
Taylor expanded in s around inf 33.2%
if 1.99999995e-4 < 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%
Applied egg-rr51.7%
associate-*r/51.7%
*-rgt-identity51.7%
+-commutative51.7%
Simplified51.7%
Taylor expanded in x around 0 100.0%
exp-neg100.0%
rem-exp-log100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 52.3%
+-commutative52.3%
Simplified52.3%
Taylor expanded in s around 0 11.0%
Final simplification28.2%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 0.25 s))
x_m = fabs(x);
float code(float x_m, float s) {
return 0.25f / s;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = 0.25e0 / s
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(0.25) / s) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(0.25) / s; end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{0.25}{s}
\end{array}
Initial program 99.4%
*-commutative99.4%
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 26.7%
Final simplification26.7%
herbie shell --seed 2023322
(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))))))