
(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 5 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) (+ 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(Float32(-abs(x)) / 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.8%
Simplified99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (if (<= (fabs x) 0.0012000000569969416) (/ (exp (+ (/ x s) (* -2.0 (log1p (exp (/ x s)))))) s) (/ (exp (/ (- x) s)) (* s 4.0))))
float code(float x, float s) {
float tmp;
if (fabsf(x) <= 0.0012000000569969416f) {
tmp = expf(((x / s) + (-2.0f * log1pf(expf((x / s)))))) / s;
} else {
tmp = expf((-x / s)) / (s * 4.0f);
}
return tmp;
}
function code(x, s) tmp = Float32(0.0) if (abs(x) <= Float32(0.0012000000569969416)) tmp = Float32(exp(Float32(Float32(x / s) + Float32(Float32(-2.0) * log1p(exp(Float32(x / s)))))) / s); else tmp = Float32(exp(Float32(Float32(-x) / s)) / Float32(s * Float32(4.0))); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| \leq 0.0012000000569969416:\\
\;\;\;\;\frac{e^{\frac{x}{s} + -2 \cdot \mathsf{log1p}\left(e^{\frac{x}{s}}\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{\frac{-x}{s}}}{s \cdot 4}\\
\end{array}
\end{array}
if (fabs.f32 x) < 0.00120000006Initial 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.6%
Applied egg-rr78.2%
*-lft-identity78.2%
rem-exp-log73.7%
exp-to-pow73.8%
log1p-undefine73.8%
*-commutative73.8%
exp-sum73.1%
+-commutative73.1%
exp-diff93.9%
associate--r+94.2%
exp-diff95.0%
Simplified99.5%
if 0.00120000006 < (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 100.0%
distribute-frac-neg2100.0%
rec-exp100.0%
add-sqr-sqrt100.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-unprod-0.0%
add-sqr-sqrt3.1%
add-sqr-sqrt-0.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-unprod100.0%
add-sqr-sqrt100.0%
add-sqr-sqrt52.3%
fabs-sqr52.3%
add-sqr-sqrt53.8%
Applied egg-rr53.8%
rec-exp53.8%
distribute-neg-frac253.8%
Simplified53.8%
Final simplification76.3%
(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(exp(Float32(Float32(-x) / s)) / Float32(s * Float32(4.0))) end
function tmp = code(x, s) tmp = exp((-x / s)) / (s * single(4.0)); end
\begin{array}{l}
\\
\frac{e^{\frac{-x}{s}}}{s \cdot 4}
\end{array}
Initial program 99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.8%
Taylor expanded in s around inf 93.8%
distribute-frac-neg293.8%
rec-exp93.8%
add-sqr-sqrt93.8%
sqrt-unprod93.7%
sqr-neg93.7%
sqrt-unprod-0.0%
add-sqr-sqrt27.4%
add-sqr-sqrt-0.0%
sqrt-unprod93.7%
sqr-neg93.7%
sqrt-unprod93.8%
add-sqr-sqrt93.8%
add-sqr-sqrt48.0%
fabs-sqr48.0%
add-sqr-sqrt63.1%
Applied egg-rr63.1%
rec-exp63.1%
distribute-neg-frac263.1%
Simplified63.1%
Final simplification63.1%
(FPCore (x s) :precision binary32 (/ (+ (* (* (/ x s) (/ x s)) -0.0625) 0.25) s))
float code(float x, float s) {
return ((((x / s) * (x / s)) * -0.0625f) + 0.25f) / s;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = ((((x / s) * (x / s)) * (-0.0625e0)) + 0.25e0) / s
end function
function code(x, s) return Float32(Float32(Float32(Float32(Float32(x / s) * Float32(x / s)) * Float32(-0.0625)) + Float32(0.25)) / s) end
function tmp = code(x, s) tmp = ((((x / s) * (x / s)) * single(-0.0625)) + single(0.25)) / s; end
\begin{array}{l}
\\
\frac{\left(\frac{x}{s} \cdot \frac{x}{s}\right) \cdot -0.0625 + 0.25}{s}
\end{array}
Initial program 99.8%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.8%
Applied egg-rr62.7%
*-lft-identity62.7%
rem-exp-log60.5%
exp-to-pow60.5%
log1p-undefine60.5%
*-commutative60.5%
exp-sum60.2%
+-commutative60.2%
exp-diff84.9%
associate--r+85.0%
exp-diff85.4%
Simplified87.6%
Taylor expanded in x around 0 26.2%
+-commutative26.2%
*-commutative26.2%
fma-define26.2%
unpow226.2%
unpow226.2%
times-frac31.9%
unpow231.9%
Simplified31.9%
fma-undefine31.9%
Applied egg-rr31.9%
unpow231.9%
Applied egg-rr31.9%
Final simplification31.9%
(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%
fabs-neg99.8%
distribute-frac-neg99.8%
distribute-frac-neg299.8%
fabs-neg99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
Simplified99.8%
Taylor expanded in s around inf 31.1%
Final simplification31.1%
herbie shell --seed 2024046
(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))))))