
(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) (fma s t_0 s)))))
float code(float x, float s) {
float t_0 = expf((fabsf(x) / -s));
return t_0 / ((t_0 + 1.0f) * fmaf(s, t_0, s));
}
function code(x, s) t_0 = exp(Float32(abs(x) / Float32(-s))) return Float32(t_0 / Float32(Float32(t_0 + Float32(1.0)) * fma(s, t_0, 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 \mathsf{fma}\left(s, t\_0, s\right)}
\end{array}
\end{array}
Initial program 99.5%
*-commutative99.5%
+-commutative99.5%
fabs-neg99.5%
distribute-lft-in99.6%
*-rgt-identity99.6%
fma-define99.6%
fabs-neg99.6%
Simplified99.6%
Final simplification99.6%
(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(abs(x) / Float32(-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.5%
*-commutative99.5%
fabs-neg99.5%
+-commutative99.5%
fabs-neg99.5%
distribute-lft-in99.6%
*-rgt-identity99.6%
+-commutative99.6%
Simplified99.5%
Final simplification99.5%
(FPCore (x s) :precision binary32 (if (<= (fabs x) 0.0010000000474974513) (/ (exp (+ (/ x s) (* -2.0 (log1p (exp (/ x s)))))) s) 0.0))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 0.0010000000474974513f) {
tmp = expf(((x / s) + (-2.0f * log1pf(expf((x / s)))))) / s;
} else {
tmp = 0.0f;
}
return tmp;
}
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(0.0010000000474974513)) tmp = Float32(exp(Float32(Float32(x / s) + Float32(Float32(-2.0) * log1p(exp(Float32(x / s)))))) / s); else tmp = Float32(0.0); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 0.0010000000474974513:\\
\;\;\;\;\frac{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if (fabs.f32 x) < 0.00100000005Initial program 99.0%
fabs-neg99.0%
distribute-frac-neg99.0%
distribute-frac-neg299.0%
fabs-neg99.0%
*-commutative99.0%
fabs-neg99.0%
+-commutative99.0%
fabs-neg99.0%
Simplified99.0%
Applied egg-rr80.4%
*-lft-identity80.4%
*-commutative80.4%
exp-to-pow80.4%
log1p-undefine80.6%
*-commutative80.6%
rem-exp-log76.7%
exp-sum77.0%
exp-diff94.3%
associate--r+94.3%
exp-diff94.9%
cancel-sign-sub-inv94.9%
metadata-eval94.9%
Simplified98.7%
if 0.00100000005 < (fabs.f32 x) Initial program 100.0%
fabs-neg100.0%
distribute-frac-neg100.0%
distribute-frac-neg2100.0%
fabs-neg100.0%
*-commutative100.0%
fabs-neg100.0%
+-commutative100.0%
fabs-neg100.0%
Simplified100.0%
Taylor expanded in s around inf 4.6%
expm1-log1p-u4.6%
expm1-undefine4.6%
Applied egg-rr4.6%
Taylor expanded in s around inf 100.0%
metadata-eval100.0%
Applied egg-rr100.0%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (- x) s)))) (/ (/ t_0 s) (pow (+ 1.0 t_0) 2.0))))
float code(float x, float s) {
float t_0 = expf((-x / s));
return (t_0 / s) / powf((1.0f + t_0), 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((-x / s))
code = (t_0 / s) / ((1.0e0 + t_0) ** 2.0e0)
end function
function code(x, s) t_0 = exp(Float32(Float32(-x) / s)) return Float32(Float32(t_0 / s) / (Float32(Float32(1.0) + t_0) ^ Float32(2.0))) end
function tmp = code(x, s) t_0 = exp((-x / s)); tmp = (t_0 / s) / ((single(1.0) + t_0) ^ single(2.0)); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{-x}{s}}\\
\frac{\frac{t\_0}{s}}{{\left(1 + t\_0\right)}^{2}}
\end{array}
\end{array}
Initial program 99.5%
fabs-neg99.5%
distribute-frac-neg99.5%
distribute-frac-neg299.5%
fabs-neg99.5%
*-commutative99.5%
fabs-neg99.5%
+-commutative99.5%
fabs-neg99.5%
Simplified99.5%
Taylor expanded in x around 0 99.5%
associate-/r*99.5%
exp-prod99.5%
rem-square-sqrt46.9%
fabs-sqr46.9%
rem-square-sqrt59.6%
exp-prod59.6%
neg-mul-159.6%
distribute-neg-frac259.6%
+-commutative59.6%
exp-prod59.6%
rem-square-sqrt46.9%
fabs-sqr46.9%
rem-square-sqrt61.9%
exp-prod61.9%
neg-mul-161.9%
distribute-neg-frac261.9%
Simplified61.9%
Final simplification61.9%
(FPCore (x s) :precision binary32 (/ (/ (exp (/ (- x) s)) s) (pow (- 2.0 (/ x s)) 2.0)))
float code(float x, float s) {
return (expf((-x / s)) / s) / powf((2.0f - (x / s)), 2.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (exp((-x / s)) / s) / ((2.0e0 - (x / s)) ** 2.0e0)
end function
function code(x, s) return Float32(Float32(exp(Float32(Float32(-x) / s)) / s) / (Float32(Float32(2.0) - Float32(x / s)) ^ Float32(2.0))) end
function tmp = code(x, s) tmp = (exp((-x / s)) / s) / ((single(2.0) - (x / s)) ^ single(2.0)); end
\begin{array}{l}
\\
\frac{\frac{e^{\frac{-x}{s}}}{s}}{{\left(2 - \frac{x}{s}\right)}^{2}}
\end{array}
Initial program 99.5%
fabs-neg99.5%
distribute-frac-neg99.5%
distribute-frac-neg299.5%
fabs-neg99.5%
*-commutative99.5%
fabs-neg99.5%
+-commutative99.5%
fabs-neg99.5%
Simplified99.5%
Taylor expanded in x around 0 99.5%
associate-/r*99.5%
exp-prod99.5%
rem-square-sqrt46.9%
fabs-sqr46.9%
rem-square-sqrt59.6%
exp-prod59.6%
neg-mul-159.6%
distribute-neg-frac259.6%
+-commutative59.6%
exp-prod59.6%
rem-square-sqrt46.9%
fabs-sqr46.9%
rem-square-sqrt61.9%
exp-prod61.9%
neg-mul-161.9%
distribute-neg-frac261.9%
Simplified61.9%
Taylor expanded in x around 0 59.0%
neg-mul-159.0%
unsub-neg59.0%
Simplified59.0%
Final simplification59.0%
(FPCore (x s) :precision binary32 (/ (/ (exp (/ (- x) s)) s) 4.0))
float code(float x, float s) {
return (expf((-x / s)) / s) / 4.0f;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (exp((-x / s)) / s) / 4.0e0
end function
function code(x, s) return Float32(Float32(exp(Float32(Float32(-x) / s)) / s) / Float32(4.0)) end
function tmp = code(x, s) tmp = (exp((-x / s)) / s) / single(4.0); end
\begin{array}{l}
\\
\frac{\frac{e^{\frac{-x}{s}}}{s}}{4}
\end{array}
Initial program 99.5%
fabs-neg99.5%
distribute-frac-neg99.5%
distribute-frac-neg299.5%
fabs-neg99.5%
*-commutative99.5%
fabs-neg99.5%
+-commutative99.5%
fabs-neg99.5%
Simplified99.5%
Taylor expanded in x around 0 99.5%
associate-/r*99.5%
exp-prod99.5%
rem-square-sqrt46.9%
fabs-sqr46.9%
rem-square-sqrt59.6%
exp-prod59.6%
neg-mul-159.6%
distribute-neg-frac259.6%
+-commutative59.6%
exp-prod59.6%
rem-square-sqrt46.9%
fabs-sqr46.9%
rem-square-sqrt61.9%
exp-prod61.9%
neg-mul-161.9%
distribute-neg-frac261.9%
Simplified61.9%
Taylor expanded in x around 0 57.2%
Final simplification57.2%
(FPCore (x s) :precision binary32 (if (<= x 1.999999936531045e-20) (/ 1.0 (/ s (- 0.25 (/ (* 0.0625 (* x (/ x s))) s)))) 0.0))
float code(float x, float s) {
float tmp;
if (x <= 1.999999936531045e-20f) {
tmp = 1.0f / (s / (0.25f - ((0.0625f * (x * (x / s))) / s)));
} else {
tmp = 0.0f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 1.999999936531045e-20) then
tmp = 1.0e0 / (s / (0.25e0 - ((0.0625e0 * (x * (x / s))) / s)))
else
tmp = 0.0e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.999999936531045e-20)) tmp = Float32(Float32(1.0) / Float32(s / Float32(Float32(0.25) - Float32(Float32(Float32(0.0625) * Float32(x * Float32(x / s))) / s)))); else tmp = Float32(0.0); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(1.999999936531045e-20)) tmp = single(1.0) / (s / (single(0.25) - ((single(0.0625) * (x * (x / s))) / s))); else tmp = single(0.0); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.999999936531045 \cdot 10^{-20}:\\
\;\;\;\;\frac{1}{\frac{s}{0.25 - \frac{0.0625 \cdot \left(x \cdot \frac{x}{s}\right)}{s}}}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 1.99999994e-20Initial program 99.3%
fabs-neg99.3%
distribute-frac-neg99.3%
distribute-frac-neg299.3%
fabs-neg99.3%
*-commutative99.3%
fabs-neg99.3%
+-commutative99.3%
fabs-neg99.3%
Simplified99.3%
Applied egg-rr97.2%
*-lft-identity97.2%
*-commutative97.2%
exp-to-pow97.2%
log1p-undefine97.3%
*-commutative97.3%
rem-exp-log94.5%
exp-sum94.7%
exp-diff95.9%
associate--r+95.9%
exp-diff96.3%
cancel-sign-sub-inv96.3%
metadata-eval96.3%
Simplified99.1%
Taylor expanded in s around -inf 37.2%
Simplified37.8%
clear-num37.8%
inv-pow37.8%
*-commutative37.8%
Applied egg-rr37.8%
unpow-137.8%
fma-define37.8%
+-rgt-identity37.8%
associate-*r*37.8%
metadata-eval37.8%
Simplified37.8%
unpow237.8%
*-un-lft-identity37.8%
times-frac38.1%
Applied egg-rr38.1%
if 1.99999994e-20 < x Initial program 99.9%
fabs-neg99.9%
distribute-frac-neg99.9%
distribute-frac-neg299.9%
fabs-neg99.9%
*-commutative99.9%
fabs-neg99.9%
+-commutative99.9%
fabs-neg99.9%
Simplified99.9%
Taylor expanded in s around inf 12.4%
expm1-log1p-u12.2%
expm1-undefine12.2%
Applied egg-rr12.2%
Taylor expanded in s around inf 90.8%
metadata-eval90.8%
Applied egg-rr90.8%
Final simplification57.1%
(FPCore (x s) :precision binary32 (if (<= x 1.999999936531045e-20) (/ 1.0 (/ s (- 0.25 (/ (* 0.0625 (/ (* x x) s)) s)))) 0.0))
float code(float x, float s) {
float tmp;
if (x <= 1.999999936531045e-20f) {
tmp = 1.0f / (s / (0.25f - ((0.0625f * ((x * x) / s)) / s)));
} else {
tmp = 0.0f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 1.999999936531045e-20) then
tmp = 1.0e0 / (s / (0.25e0 - ((0.0625e0 * ((x * x) / s)) / s)))
else
tmp = 0.0e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.999999936531045e-20)) tmp = Float32(Float32(1.0) / Float32(s / Float32(Float32(0.25) - Float32(Float32(Float32(0.0625) * Float32(Float32(x * x) / s)) / s)))); else tmp = Float32(0.0); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(1.999999936531045e-20)) tmp = single(1.0) / (s / (single(0.25) - ((single(0.0625) * ((x * x) / s)) / s))); else tmp = single(0.0); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.999999936531045 \cdot 10^{-20}:\\
\;\;\;\;\frac{1}{\frac{s}{0.25 - \frac{0.0625 \cdot \frac{x \cdot x}{s}}{s}}}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 1.99999994e-20Initial program 99.3%
fabs-neg99.3%
distribute-frac-neg99.3%
distribute-frac-neg299.3%
fabs-neg99.3%
*-commutative99.3%
fabs-neg99.3%
+-commutative99.3%
fabs-neg99.3%
Simplified99.3%
Applied egg-rr97.2%
*-lft-identity97.2%
*-commutative97.2%
exp-to-pow97.2%
log1p-undefine97.3%
*-commutative97.3%
rem-exp-log94.5%
exp-sum94.7%
exp-diff95.9%
associate--r+95.9%
exp-diff96.3%
cancel-sign-sub-inv96.3%
metadata-eval96.3%
Simplified99.1%
Taylor expanded in s around -inf 37.2%
Simplified37.8%
clear-num37.8%
inv-pow37.8%
*-commutative37.8%
Applied egg-rr37.8%
unpow-137.8%
fma-define37.8%
+-rgt-identity37.8%
associate-*r*37.8%
metadata-eval37.8%
Simplified37.8%
unpow237.8%
Applied egg-rr37.8%
if 1.99999994e-20 < x Initial program 99.9%
fabs-neg99.9%
distribute-frac-neg99.9%
distribute-frac-neg299.9%
fabs-neg99.9%
*-commutative99.9%
fabs-neg99.9%
+-commutative99.9%
fabs-neg99.9%
Simplified99.9%
Taylor expanded in s around inf 12.4%
expm1-log1p-u12.2%
expm1-undefine12.2%
Applied egg-rr12.2%
Taylor expanded in s around inf 90.8%
metadata-eval90.8%
Applied egg-rr90.8%
(FPCore (x s) :precision binary32 (if (<= x 1.999999936531045e-20) (/ 0.25 s) 0.0))
float code(float x, float s) {
float tmp;
if (x <= 1.999999936531045e-20f) {
tmp = 0.25f / s;
} else {
tmp = 0.0f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 1.999999936531045e-20) then
tmp = 0.25e0 / s
else
tmp = 0.0e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.999999936531045e-20)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(0.0); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(1.999999936531045e-20)) tmp = single(0.25) / s; else tmp = single(0.0); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.999999936531045 \cdot 10^{-20}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < 1.99999994e-20Initial program 99.3%
fabs-neg99.3%
distribute-frac-neg99.3%
distribute-frac-neg299.3%
fabs-neg99.3%
*-commutative99.3%
fabs-neg99.3%
+-commutative99.3%
fabs-neg99.3%
Simplified99.3%
Taylor expanded in s around inf 38.0%
if 1.99999994e-20 < x Initial program 99.9%
fabs-neg99.9%
distribute-frac-neg99.9%
distribute-frac-neg299.9%
fabs-neg99.9%
*-commutative99.9%
fabs-neg99.9%
+-commutative99.9%
fabs-neg99.9%
Simplified99.9%
Taylor expanded in s around inf 12.4%
expm1-log1p-u12.2%
expm1-undefine12.2%
Applied egg-rr12.2%
Taylor expanded in s around inf 90.8%
metadata-eval90.8%
Applied egg-rr90.8%
(FPCore (x s) :precision binary32 0.0)
float code(float x, float s) {
return 0.0f;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.0e0
end function
function code(x, s) return Float32(0.0) end
function tmp = code(x, s) tmp = single(0.0); end
\begin{array}{l}
\\
0
\end{array}
Initial program 99.5%
fabs-neg99.5%
distribute-frac-neg99.5%
distribute-frac-neg299.5%
fabs-neg99.5%
*-commutative99.5%
fabs-neg99.5%
+-commutative99.5%
fabs-neg99.5%
Simplified99.5%
Taylor expanded in s around inf 28.8%
expm1-log1p-u27.2%
expm1-undefine27.2%
Applied egg-rr27.2%
Taylor expanded in s around inf 72.2%
metadata-eval72.2%
Applied egg-rr72.2%
herbie shell --seed 2024166
(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))))))