
(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 7 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 (* (* s (+ 1.0 (exp (- (/ x s))))) (+ 1.0 (exp (/ x s))))))
float code(float x, float s) {
return 1.0f / ((s * (1.0f + expf(-(x / 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 / ((s * (1.0e0 + exp(-(x / s)))) * (1.0e0 + exp((x / s))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(s * Float32(Float32(1.0) + exp(Float32(-Float32(x / s))))) * Float32(Float32(1.0) + exp(Float32(x / s))))) end
function tmp = code(x, s) tmp = single(1.0) / ((s * (single(1.0) + exp(-(x / s)))) * (single(1.0) + exp((x / s)))); end
\begin{array}{l}
\\
\frac{1}{\left(s \cdot \left(1 + e^{-\frac{x}{s}}\right)\right) \cdot \left(1 + e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.8%
Simplified99.9%
distribute-frac-neg99.9%
rec-exp99.8%
pow199.8%
pow199.8%
frac-2neg99.8%
add-sqr-sqrt-0.0%
sqrt-unprod96.4%
sqr-neg96.4%
sqrt-unprod96.3%
add-sqr-sqrt96.3%
remove-double-neg96.3%
frac-2neg96.3%
add-sqr-sqrt-0.0%
sqrt-unprod97.8%
sqr-neg97.8%
sqrt-unprod99.8%
add-sqr-sqrt99.8%
add-sqr-sqrt51.5%
fabs-sqr51.5%
add-sqr-sqrt99.0%
Applied egg-rr99.0%
rec-exp99.0%
Simplified99.0%
add-sqr-sqrt99.0%
add-sqr-sqrt99.0%
add-sqr-sqrt51.5%
fabs-sqr51.5%
add-sqr-sqrt99.9%
expm1-log1p-u99.8%
+-commutative99.8%
expm1-udef99.7%
+-commutative99.7%
Applied egg-rr99.7%
expm1-def99.8%
expm1-log1p99.9%
Simplified99.9%
Taylor expanded in s around 0 99.8%
associate-*r*99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x s) :precision binary32 (/ 1.0 (/ (+ 1.0 (exp (/ x s))) (/ 1.0 (+ s s)))))
float code(float x, float s) {
return 1.0f / ((1.0f + expf((x / s))) / (1.0f / (s + s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((1.0e0 + exp((x / s))) / (1.0e0 / (s + s)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(x / s))) / Float32(Float32(1.0) / Float32(s + s)))) end
function tmp = code(x, s) tmp = single(1.0) / ((single(1.0) + exp((x / s))) / (single(1.0) / (s + s))); end
\begin{array}{l}
\\
\frac{1}{\frac{1 + e^{\frac{x}{s}}}{\frac{1}{s + s}}}
\end{array}
Initial program 99.8%
Simplified99.9%
Taylor expanded in s around inf 96.6%
/-rgt-identity96.6%
*-commutative96.6%
associate-/l*96.6%
add-sqr-sqrt96.6%
add-sqr-sqrt96.6%
add-sqr-sqrt49.0%
fabs-sqr49.0%
add-sqr-sqrt62.5%
fma-udef62.5%
*-rgt-identity62.5%
Applied egg-rr62.5%
Final simplification62.5%
(FPCore (x s) :precision binary32 (/ (/ 1.0 (* s 2.0)) (+ 1.0 (exp (/ x s)))))
float code(float x, float s) {
return (1.0f / (s * 2.0f)) / (1.0f + expf((x / s)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / (s * 2.0e0)) / (1.0e0 + exp((x / s)))
end function
function code(x, s) return Float32(Float32(Float32(1.0) / Float32(s * Float32(2.0))) / Float32(Float32(1.0) + exp(Float32(x / s)))) end
function tmp = code(x, s) tmp = (single(1.0) / (s * single(2.0))) / (single(1.0) + exp((x / s))); end
\begin{array}{l}
\\
\frac{\frac{1}{s \cdot 2}}{1 + e^{\frac{x}{s}}}
\end{array}
Initial program 99.8%
Simplified99.9%
Taylor expanded in s around inf 96.6%
expm1-log1p-u95.5%
expm1-udef95.5%
associate-/r*95.5%
fma-udef95.5%
*-rgt-identity95.5%
add-sqr-sqrt95.5%
add-sqr-sqrt95.5%
add-sqr-sqrt48.6%
fabs-sqr48.6%
add-sqr-sqrt61.4%
Applied egg-rr61.4%
expm1-def61.4%
expm1-log1p62.5%
count-262.5%
Simplified62.5%
Final simplification62.5%
(FPCore (x s) :precision binary32 (/ 1.0 (+ (* s 4.0) (* x (/ x s)))))
float code(float x, float s) {
return 1.0f / ((s * 4.0f) + (x * (x / 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)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(x * Float32(x / s)))) end
function tmp = code(x, s) tmp = single(1.0) / ((s * single(4.0)) + (x * (x / s))); end
\begin{array}{l}
\\
\frac{1}{s \cdot 4 + x \cdot \frac{x}{s}}
\end{array}
Initial program 99.8%
Simplified99.9%
distribute-frac-neg99.9%
rec-exp99.8%
pow199.8%
pow199.8%
frac-2neg99.8%
add-sqr-sqrt-0.0%
sqrt-unprod96.4%
sqr-neg96.4%
sqrt-unprod96.3%
add-sqr-sqrt96.3%
remove-double-neg96.3%
frac-2neg96.3%
add-sqr-sqrt-0.0%
sqrt-unprod97.8%
sqr-neg97.8%
sqrt-unprod99.8%
add-sqr-sqrt99.8%
add-sqr-sqrt51.5%
fabs-sqr51.5%
add-sqr-sqrt99.0%
Applied egg-rr99.0%
rec-exp99.0%
Simplified99.0%
add-sqr-sqrt99.0%
add-sqr-sqrt99.0%
add-sqr-sqrt51.5%
fabs-sqr51.5%
add-sqr-sqrt99.9%
expm1-log1p-u99.8%
+-commutative99.8%
expm1-udef99.7%
+-commutative99.7%
Applied egg-rr99.7%
expm1-def99.8%
expm1-log1p99.9%
Simplified99.9%
Taylor expanded in x around 0 65.8%
unpow265.8%
*-un-lft-identity65.8%
times-frac66.5%
Applied egg-rr66.5%
Final simplification66.5%
(FPCore (x s) :precision binary32 (/ 1.0 (+ (* s 4.0) (* x 2.0))))
float code(float x, float s) {
return 1.0f / ((s * 4.0f) + (x * 2.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((s * 4.0e0) + (x * 2.0e0))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(s * Float32(4.0)) + Float32(x * Float32(2.0)))) end
function tmp = code(x, s) tmp = single(1.0) / ((s * single(4.0)) + (x * single(2.0))); end
\begin{array}{l}
\\
\frac{1}{s \cdot 4 + x \cdot 2}
\end{array}
Initial program 99.8%
Simplified99.9%
Taylor expanded in s around inf 96.6%
distribute-lft-in96.6%
*-rgt-identity96.6%
fma-udef96.6%
*-rgt-identity96.6%
fma-udef96.6%
*-rgt-identity96.6%
add-sqr-sqrt96.6%
add-sqr-sqrt96.6%
add-sqr-sqrt49.0%
fabs-sqr49.0%
add-sqr-sqrt62.5%
Applied egg-rr62.5%
Taylor expanded in s around inf 28.9%
Final simplification28.9%
(FPCore (x s) :precision binary32 (if (<= x 0.004999999888241291) (/ 0.25 s) (/ 0.5 x)))
float code(float x, float s) {
float tmp;
if (x <= 0.004999999888241291f) {
tmp = 0.25f / s;
} else {
tmp = 0.5f / x;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 0.004999999888241291e0) then
tmp = 0.25e0 / s
else
tmp = 0.5e0 / x
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.004999999888241291)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.5) / x); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.004999999888241291)) tmp = single(0.25) / s; else tmp = single(0.5) / x; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.004999999888241291:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if x < 0.00499999989Initial program 99.7%
Simplified99.8%
Taylor expanded in s around inf 35.6%
if 0.00499999989 < x Initial program 100.0%
Simplified100.0%
Taylor expanded in s around inf 100.0%
distribute-lft-in100.0%
*-rgt-identity100.0%
fma-udef100.0%
*-rgt-identity100.0%
fma-udef100.0%
*-rgt-identity100.0%
add-sqr-sqrt100.0%
add-sqr-sqrt100.0%
add-sqr-sqrt100.0%
fabs-sqr100.0%
add-sqr-sqrt100.0%
Applied egg-rr100.0%
Taylor expanded in s around inf 10.3%
Taylor expanded in x around inf 10.3%
Final simplification28.3%
(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.8%
Simplified99.9%
Taylor expanded in s around inf 26.7%
Final simplification26.7%
herbie shell --seed 2023333
(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))))))