
(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}
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ 1.0 (exp (/ x_m s))) (* (+ 1.0 (exp (/ (- (fabs x_m)) s))) (+ s (/ s (exp (/ (fabs x_m) s)))))))
x_m = fabs(x);
float code(float x_m, float s) {
return (1.0f / expf((x_m / s))) / ((1.0f + expf((-fabsf(x_m) / s))) * (s + (s / 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 / exp((x_m / s))) / ((1.0e0 + exp((-abs(x_m) / s))) * (s + (s / exp((abs(x_m) / s)))))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(1.0) / exp(Float32(x_m / s))) / Float32(Float32(Float32(1.0) + exp(Float32(Float32(-abs(x_m)) / s))) * Float32(s + Float32(s / exp(Float32(abs(x_m) / s)))))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(1.0) / exp((x_m / s))) / ((single(1.0) + exp((-abs(x_m) / s))) * (s + (s / exp((abs(x_m) / s))))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{1}{e^{\frac{x_m}{s}}}}{\left(1 + e^{\frac{-\left|x_m\right|}{s}}\right) \cdot \left(s + \frac{s}{e^{\frac{\left|x_m\right|}{s}}}\right)}
\end{array}
Initial program 99.7%
*-commutative99.7%
distribute-lft-in99.7%
*-rgt-identity99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
exp-neg99.7%
associate-*r/99.7%
*-rgt-identity99.7%
*-lft-identity99.7%
metadata-eval99.7%
times-frac99.7%
neg-mul-199.7%
neg-mul-199.7%
fabs-neg99.7%
Simplified99.7%
distribute-frac-neg99.7%
rec-exp99.7%
add-sqr-sqrt46.2%
fabs-sqr46.2%
add-sqr-sqrt58.2%
Applied egg-rr58.2%
Final simplification58.2%
x_m = (fabs.f32 x)
(FPCore (x_m s)
:precision binary32
(let* ((t_0 (exp (/ x_m s))))
(if (<= (fabs x_m) 3.5)
(/ (exp (+ (/ x_m s) (* -2.0 (log1p t_0)))) s)
(/ (/ 1.0 (* s t_0)) 4.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) <= 3.5f) {
tmp = expf(((x_m / s) + (-2.0f * log1pf(t_0)))) / s;
} else {
tmp = (1.0f / (s * t_0)) / 4.0f;
}
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(3.5)) tmp = Float32(exp(Float32(Float32(x_m / s) + Float32(Float32(-2.0) * log1p(t_0)))) / s); else tmp = Float32(Float32(Float32(1.0) / Float32(s * t_0)) / Float32(4.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 3.5:\\
\;\;\;\;\frac{e^{\frac{x_m}{s} + -2 \cdot \mathsf{log1p}\left(t_0\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{s \cdot t_0}}{4}\\
\end{array}
\end{array}
if (fabs.f32 x) < 3.5Initial program 99.3%
*-commutative99.3%
Simplified99.3%
Applied egg-rr80.9%
unpow280.9%
frac-times80.9%
add-sqr-sqrt81.5%
pow281.5%
+-commutative81.5%
clear-num81.5%
frac-2neg81.5%
metadata-eval81.5%
Applied egg-rr81.5%
associate-/r/82.2%
distribute-rgt-neg-in82.2%
Simplified82.2%
Taylor expanded in x around inf 82.3%
*-commutative82.3%
associate-/r*82.3%
exp-to-pow82.2%
log1p-def82.4%
*-commutative82.4%
exp-diff99.4%
cancel-sign-sub-inv99.4%
metadata-eval99.4%
Simplified99.4%
if 3.5 < (fabs.f32 x) Initial program 100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 100.0%
associate-/r*100.0%
associate-*r/100.0%
mul-1-neg100.0%
+-commutative100.0%
associate-*r/100.0%
mul-1-neg100.0%
Simplified100.0%
clear-num100.0%
inv-pow100.0%
Applied egg-rr48.3%
unpow-148.3%
Simplified48.3%
distribute-frac-neg100.0%
rec-exp100.0%
add-sqr-sqrt46.6%
fabs-sqr46.6%
add-sqr-sqrt48.3%
Applied egg-rr46.6%
rec-exp46.6%
distribute-neg-frac46.6%
Simplified46.6%
Taylor expanded in x around 0 48.3%
Final simplification72.8%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ 1.0 (* s (exp (/ x_m s)))) (pow (+ 1.0 (exp (/ (- x_m) s))) 2.0)))
x_m = fabs(x);
float code(float x_m, float s) {
return (1.0f / (s * expf((x_m / s)))) / powf((1.0f + expf((-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 = (1.0e0 / (s * exp((x_m / s)))) / ((1.0e0 + exp((-x_m / s))) ** 2.0e0)
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(1.0) / Float32(s * exp(Float32(x_m / s)))) / (Float32(Float32(1.0) + exp(Float32(Float32(-x_m) / s))) ^ Float32(2.0))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(1.0) / (s * exp((x_m / s)))) / ((single(1.0) + exp((-x_m / s))) ^ single(2.0)); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{1}{s \cdot e^{\frac{x_m}{s}}}}{{\left(1 + e^{\frac{-x_m}{s}}\right)}^{2}}
\end{array}
Initial program 99.7%
*-commutative99.7%
Simplified99.7%
Taylor expanded in x around 0 99.7%
associate-/r*99.7%
associate-*r/99.7%
mul-1-neg99.7%
+-commutative99.7%
associate-*r/99.7%
mul-1-neg99.7%
Simplified99.7%
clear-num99.7%
inv-pow99.7%
Applied egg-rr58.2%
unpow-158.2%
Simplified58.2%
distribute-frac-neg99.7%
rec-exp99.7%
add-sqr-sqrt46.2%
fabs-sqr46.2%
add-sqr-sqrt58.2%
Applied egg-rr58.7%
rec-exp58.7%
distribute-neg-frac58.7%
Simplified58.7%
Final simplification58.7%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= (fabs x_m) 5.000000058430487e-8) (/ 0.25 s) (/ 0.5 (fabs x_m))))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (fabsf(x_m) <= 5.000000058430487e-8f) {
tmp = 0.25f / s;
} else {
tmp = 0.5f / fabsf(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 (abs(x_m) <= 5.000000058430487e-8) then
tmp = 0.25e0 / s
else
tmp = 0.5e0 / abs(x_m)
end if
code = tmp
end function
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (abs(x_m) <= Float32(5.000000058430487e-8)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.5) / abs(x_m)); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, s) tmp = single(0.0); if (abs(x_m) <= single(5.000000058430487e-8)) tmp = single(0.25) / s; else tmp = single(0.5) / abs(x_m); end tmp_2 = tmp; end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;\left|x_m\right| \leq 5.000000058430487 \cdot 10^{-8}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{\left|x_m\right|}\\
\end{array}
\end{array}
if (fabs.f32 x) < 5.00000006e-8Initial program 99.3%
Simplified99.3%
Taylor expanded in s around inf 57.4%
if 5.00000006e-8 < (fabs.f32 x) Initial program 99.9%
Simplified100.0%
Taylor expanded in s around inf 99.1%
Taylor expanded in s around inf 11.2%
Taylor expanded in s around 0 10.3%
Final simplification29.3%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 1.0 (+ (+ s s) (* (exp (/ x_m s)) (+ s s)))))
x_m = fabs(x);
float code(float x_m, float s) {
return 1.0f / ((s + s) + (expf((x_m / s)) * (s + 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 + s) + (exp((x_m / s)) * (s + s)))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(1.0) / Float32(Float32(s + s) + Float32(exp(Float32(x_m / s)) * Float32(s + s)))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(1.0) / ((s + s) + (exp((x_m / s)) * (s + s))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{1}{\left(s + s\right) + e^{\frac{x_m}{s}} \cdot \left(s + s\right)}
\end{array}
Initial program 99.7%
Simplified99.7%
Taylor expanded in s around inf 94.7%
distribute-lft-in94.7%
*-rgt-identity94.7%
fma-udef94.7%
*-rgt-identity94.7%
fma-udef94.7%
*-rgt-identity94.7%
add-sqr-sqrt43.0%
fabs-sqr43.0%
add-sqr-sqrt55.7%
Applied egg-rr55.7%
Final simplification55.7%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 1.0 (* s (* 2.0 (+ 1.0 (exp (/ x_m s)))))))
x_m = fabs(x);
float code(float x_m, float s) {
return 1.0f / (s * (2.0f * (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 = 1.0e0 / (s * (2.0e0 * (1.0e0 + exp((x_m / s)))))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(2.0) * Float32(Float32(1.0) + exp(Float32(x_m / s)))))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(1.0) / (s * (single(2.0) * (single(1.0) + exp((x_m / s))))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{1}{s \cdot \left(2 \cdot \left(1 + e^{\frac{x_m}{s}}\right)\right)}
\end{array}
Initial program 99.7%
Simplified99.7%
Taylor expanded in s around inf 94.7%
distribute-rgt-in94.7%
*-un-lft-identity94.7%
fma-udef94.7%
*-rgt-identity94.7%
associate-+l+94.7%
add-sqr-sqrt43.0%
fabs-sqr43.0%
add-sqr-sqrt55.7%
fma-udef55.7%
*-rgt-identity55.7%
Applied egg-rr55.7%
associate-+r+55.7%
distribute-rgt1-in55.7%
+-commutative55.7%
distribute-lft-out55.7%
distribute-rgt-out55.7%
count-255.7%
Simplified55.7%
Final simplification55.7%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ 1.0 (* s (exp (/ x_m s)))) 4.0))
x_m = fabs(x);
float code(float x_m, float s) {
return (1.0f / (s * expf((x_m / s)))) / 4.0f;
}
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 * exp((x_m / s)))) / 4.0e0
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(1.0) / Float32(s * exp(Float32(x_m / s)))) / Float32(4.0)) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(1.0) / (s * exp((x_m / s)))) / single(4.0); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{1}{s \cdot e^{\frac{x_m}{s}}}}{4}
\end{array}
Initial program 99.7%
*-commutative99.7%
Simplified99.7%
Taylor expanded in x around 0 99.7%
associate-/r*99.7%
associate-*r/99.7%
mul-1-neg99.7%
+-commutative99.7%
associate-*r/99.7%
mul-1-neg99.7%
Simplified99.7%
clear-num99.7%
inv-pow99.7%
Applied egg-rr58.2%
unpow-158.2%
Simplified58.2%
distribute-frac-neg99.7%
rec-exp99.7%
add-sqr-sqrt46.2%
fabs-sqr46.2%
add-sqr-sqrt58.2%
Applied egg-rr58.7%
rec-exp58.7%
distribute-neg-frac58.7%
Simplified58.7%
Taylor expanded in x around 0 54.9%
Final simplification54.9%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 1.0 (fma x_m 2.0 (* s 4.0))))
x_m = fabs(x);
float code(float x_m, float s) {
return 1.0f / fmaf(x_m, 2.0f, (s * 4.0f));
}
x_m = abs(x) function code(x_m, s) return Float32(Float32(1.0) / fma(x_m, Float32(2.0), Float32(s * Float32(4.0)))) end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{1}{\mathsf{fma}\left(x_m, 2, s \cdot 4\right)}
\end{array}
Initial program 99.7%
Simplified99.7%
Taylor expanded in s around inf 94.7%
Taylor expanded in s around inf 28.9%
expm1-log1p-u27.7%
expm1-udef63.0%
*-commutative63.0%
fma-def63.0%
add-sqr-sqrt30.2%
fabs-sqr30.2%
add-sqr-sqrt61.8%
*-commutative61.8%
Applied egg-rr61.8%
expm1-def26.9%
expm1-log1p28.7%
Simplified28.7%
Final simplification28.7%
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.7%
Simplified99.7%
Taylor expanded in s around inf 26.0%
Final simplification26.0%
herbie shell --seed 2024010
(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))))))