
(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}
(FPCore (x s) :precision binary32 (/ 1.0 (* (+ 1.0 (exp (/ (- x) s))) (* s (+ 1.0 (exp (/ x s)))))))
float code(float x, float s) {
return 1.0f / ((1.0f + expf((-x / s))) * (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 / ((1.0e0 + exp((-x / s))) * (s * (1.0e0 + exp((x / s)))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))) * Float32(s * Float32(Float32(1.0) + exp(Float32(x / s)))))) end
function tmp = code(x, s) tmp = single(1.0) / ((single(1.0) + exp((-x / s))) * (s * (single(1.0) + exp((x / s))))); end
\begin{array}{l}
\\
\frac{1}{\left(1 + e^{\frac{-x}{s}}\right) \cdot \left(s \cdot \left(1 + e^{\frac{x}{s}}\right)\right)}
\end{array}
Initial program 99.5%
Simplified99.5%
fma-undefine99.5%
add-sqr-sqrt99.3%
sqrt-unprod99.5%
sqr-neg99.5%
distribute-frac-neg99.5%
distribute-frac-neg99.5%
sqrt-unprod-0.0%
add-sqr-sqrt24.9%
*-commutative24.9%
+-commutative24.9%
distribute-rgt1-in24.9%
Applied egg-rr65.9%
distribute-frac-neg65.9%
rec-exp65.9%
add-sqr-sqrt65.9%
add-sqr-sqrt65.9%
add-sqr-sqrt52.8%
fabs-sqr52.8%
add-sqr-sqrt99.5%
Applied egg-rr99.5%
rec-exp99.5%
distribute-neg-frac99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x s) :precision binary32 (/ 1.0 (* (* s (+ 1.0 (exp (/ x s)))) (+ 1.0 (- 1.0 (/ x s))))))
float code(float x, float s) {
return 1.0f / ((s * (1.0f + expf((x / s)))) * (1.0f + (1.0f - (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 + (1.0e0 - (x / s))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(s * Float32(Float32(1.0) + exp(Float32(x / s)))) * Float32(Float32(1.0) + Float32(Float32(1.0) - Float32(x / s))))) end
function tmp = code(x, s) tmp = single(1.0) / ((s * (single(1.0) + exp((x / s)))) * (single(1.0) + (single(1.0) - (x / s)))); end
\begin{array}{l}
\\
\frac{1}{\left(s \cdot \left(1 + e^{\frac{x}{s}}\right)\right) \cdot \left(1 + \left(1 - \frac{x}{s}\right)\right)}
\end{array}
Initial program 99.5%
Simplified99.5%
fma-undefine99.5%
add-sqr-sqrt99.3%
sqrt-unprod99.5%
sqr-neg99.5%
distribute-frac-neg99.5%
distribute-frac-neg99.5%
sqrt-unprod-0.0%
add-sqr-sqrt24.9%
*-commutative24.9%
+-commutative24.9%
distribute-rgt1-in24.9%
Applied egg-rr65.9%
distribute-frac-neg65.9%
rec-exp65.9%
add-sqr-sqrt65.9%
add-sqr-sqrt65.9%
add-sqr-sqrt52.8%
fabs-sqr52.8%
add-sqr-sqrt99.5%
Applied egg-rr99.5%
rec-exp99.5%
distribute-neg-frac99.5%
Simplified99.5%
Taylor expanded in x around 0 74.4%
mul-1-neg74.4%
unsub-neg74.4%
Simplified74.4%
Final simplification74.4%
(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(0.5) / Float32(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{0.5}{s \cdot \left(1 + e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.5%
Simplified99.5%
fma-undefine99.5%
add-sqr-sqrt99.3%
sqrt-unprod99.5%
sqr-neg99.5%
distribute-frac-neg99.5%
distribute-frac-neg99.5%
sqrt-unprod-0.0%
add-sqr-sqrt24.9%
*-commutative24.9%
+-commutative24.9%
distribute-rgt1-in24.9%
Applied egg-rr65.9%
Taylor expanded in s around inf 61.6%
Taylor expanded in x around inf 61.6%
Final simplification61.6%
(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.5%
Simplified99.5%
fma-undefine99.5%
add-sqr-sqrt99.3%
sqrt-unprod99.5%
sqr-neg99.5%
distribute-frac-neg99.5%
distribute-frac-neg99.5%
sqrt-unprod-0.0%
add-sqr-sqrt24.9%
*-commutative24.9%
+-commutative24.9%
distribute-rgt1-in24.9%
Applied egg-rr65.9%
Taylor expanded in s around inf 61.6%
Taylor expanded in x around inf 61.6%
associate-/r*61.6%
Simplified61.6%
Final simplification61.6%
(FPCore (x s) :precision binary32 (/ 1.0 (* (- 2.0 (/ x s)) (+ x (* s 2.0)))))
float code(float x, float s) {
return 1.0f / ((2.0f - (x / s)) * (x + (s * 2.0f)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((2.0e0 - (x / s)) * (x + (s * 2.0e0)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(2.0) - Float32(x / s)) * Float32(x + Float32(s * Float32(2.0))))) end
function tmp = code(x, s) tmp = single(1.0) / ((single(2.0) - (x / s)) * (x + (s * single(2.0)))); end
\begin{array}{l}
\\
\frac{1}{\left(2 - \frac{x}{s}\right) \cdot \left(x + s \cdot 2\right)}
\end{array}
Initial program 99.5%
Simplified99.5%
fma-undefine99.5%
add-sqr-sqrt99.3%
sqrt-unprod99.5%
sqr-neg99.5%
distribute-frac-neg99.5%
distribute-frac-neg99.5%
sqrt-unprod-0.0%
add-sqr-sqrt24.9%
*-commutative24.9%
+-commutative24.9%
distribute-rgt1-in24.9%
Applied egg-rr65.9%
distribute-frac-neg65.9%
rec-exp65.9%
add-sqr-sqrt65.9%
add-sqr-sqrt65.9%
add-sqr-sqrt52.8%
fabs-sqr52.8%
add-sqr-sqrt99.5%
Applied egg-rr99.5%
rec-exp99.5%
distribute-neg-frac99.5%
Simplified99.5%
Taylor expanded in x around 0 61.0%
*-commutative29.3%
Simplified61.0%
Taylor expanded in x around 0 65.8%
mul-1-neg65.8%
unsub-neg65.8%
Simplified65.8%
Final simplification65.8%
(FPCore (x s) :precision binary32 (/ 1.0 (* 2.0 (* s (+ (/ x s) 2.0)))))
float code(float x, float s) {
return 1.0f / (2.0f * (s * ((x / s) + 2.0f)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (2.0e0 * (s * ((x / s) + 2.0e0)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(2.0) * Float32(s * Float32(Float32(x / s) + Float32(2.0))))) end
function tmp = code(x, s) tmp = single(1.0) / (single(2.0) * (s * ((x / s) + single(2.0)))); end
\begin{array}{l}
\\
\frac{1}{2 \cdot \left(s \cdot \left(\frac{x}{s} + 2\right)\right)}
\end{array}
Initial program 99.5%
Simplified99.5%
fma-undefine99.5%
add-sqr-sqrt99.3%
sqrt-unprod99.5%
sqr-neg99.5%
distribute-frac-neg99.5%
distribute-frac-neg99.5%
sqrt-unprod-0.0%
add-sqr-sqrt24.9%
*-commutative24.9%
+-commutative24.9%
distribute-rgt1-in24.9%
Applied egg-rr65.9%
Taylor expanded in s around inf 61.6%
Taylor expanded in x around 0 54.2%
+-commutative54.2%
Simplified54.2%
Final simplification54.2%
(FPCore (x s) :precision binary32 (/ 1.0 (* 2.0 (+ x (* s 2.0)))))
float code(float x, float s) {
return 1.0f / (2.0f * (x + (s * 2.0f)));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (2.0e0 * (x + (s * 2.0e0)))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(2.0) * Float32(x + Float32(s * Float32(2.0))))) end
function tmp = code(x, s) tmp = single(1.0) / (single(2.0) * (x + (s * single(2.0)))); end
\begin{array}{l}
\\
\frac{1}{2 \cdot \left(x + s \cdot 2\right)}
\end{array}
Initial program 99.5%
Simplified99.5%
fma-undefine99.5%
add-sqr-sqrt99.3%
sqrt-unprod99.5%
sqr-neg99.5%
distribute-frac-neg99.5%
distribute-frac-neg99.5%
sqrt-unprod-0.0%
add-sqr-sqrt24.9%
*-commutative24.9%
+-commutative24.9%
distribute-rgt1-in24.9%
Applied egg-rr65.9%
distribute-frac-neg65.9%
rec-exp65.9%
add-sqr-sqrt65.9%
add-sqr-sqrt65.9%
add-sqr-sqrt52.8%
fabs-sqr52.8%
add-sqr-sqrt99.5%
Applied egg-rr99.5%
rec-exp99.5%
distribute-neg-frac99.5%
Simplified99.5%
Taylor expanded in x around 0 61.0%
*-commutative29.3%
Simplified61.0%
Taylor expanded in x around 0 29.3%
Final simplification29.3%
(FPCore (x s) :precision binary32 (if (<= x 0.05000000074505806) (/ 0.25 s) (/ 0.5 x)))
float code(float x, float s) {
float tmp;
if (x <= 0.05000000074505806f) {
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.05000000074505806e0) 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.05000000074505806)) 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.05000000074505806)) 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.05000000074505806:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if x < 0.0500000007Initial program 99.3%
Simplified99.3%
Taylor expanded in s around inf 37.3%
if 0.0500000007 < x Initial program 99.9%
Simplified99.9%
fma-undefine99.9%
add-sqr-sqrt99.9%
sqrt-unprod99.9%
sqr-neg99.9%
distribute-frac-neg99.9%
distribute-frac-neg99.9%
sqrt-unprod-0.0%
add-sqr-sqrt4.6%
*-commutative4.6%
+-commutative4.6%
distribute-rgt1-in4.6%
Applied egg-rr99.9%
Taylor expanded in s around inf 99.1%
Taylor expanded in x around 0 11.7%
*-commutative11.7%
Simplified11.7%
Taylor expanded in x around inf 11.7%
Final simplification29.4%
(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.5%
Simplified99.5%
Taylor expanded in s around inf 27.2%
Final simplification27.2%
herbie shell --seed 2024039
(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))))))