
(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 6 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.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
distribute-lft-in99.8%
*-rgt-identity99.8%
+-commutative99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (pow (cbrt (/ (exp (/ x (- s))) (* s (pow (+ (exp (/ (- (fabs x)) s)) 1.0) 2.0)))) 3.0))
float code(float x, float s) {
return powf(cbrtf((expf((x / -s)) / (s * powf((expf((-fabsf(x) / s)) + 1.0f), 2.0f)))), 3.0f);
}
function code(x, s) return cbrt(Float32(exp(Float32(x / Float32(-s))) / Float32(s * (Float32(exp(Float32(Float32(-abs(x)) / s)) + Float32(1.0)) ^ Float32(2.0))))) ^ Float32(3.0) end
\begin{array}{l}
\\
{\left(\sqrt[3]{\frac{e^{\frac{x}{-s}}}{s \cdot {\left(e^{\frac{-\left|x\right|}{s}} + 1\right)}^{2}}}\right)}^{3}
\end{array}
Initial program 99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.8%
distribute-frac-neg299.8%
rec-exp99.8%
add-sqr-sqrt99.7%
associate-/r*99.7%
Applied egg-rr65.0%
Taylor expanded in x around 0 65.0%
associate-*r/65.0%
mul-1-neg65.0%
Simplified65.0%
add-cube-cbrt64.6%
pow364.6%
Applied egg-rr64.6%
Final simplification64.6%
(FPCore (x s)
:precision binary32
(let* ((t_0 (exp (/ x s))))
(if (<= (fabs x) 0.0010000000474974513)
(/ (exp (- (/ x s) (* 2.0 (log1p t_0)))) s)
(* (/ 1.0 (* s (+ 1.0 t_0))) 0.5))))
float code(float x, float s) {
float t_0 = expf((x / s));
float tmp;
if (fabsf(x) <= 0.0010000000474974513f) {
tmp = expf(((x / s) - (2.0f * log1pf(t_0)))) / s;
} else {
tmp = (1.0f / (s * (1.0f + t_0))) * 0.5f;
}
return tmp;
}
function code(x, s) t_0 = exp(Float32(x / s)) tmp = Float32(0.0) if (abs(x) <= Float32(0.0010000000474974513)) tmp = Float32(exp(Float32(Float32(x / s) - Float32(Float32(2.0) * log1p(t_0)))) / s); else tmp = Float32(Float32(Float32(1.0) / Float32(s * Float32(Float32(1.0) + t_0))) * Float32(0.5)); end return tmp end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{x}{s}}\\
\mathbf{if}\;\left|x\right| \leq 0.0010000000474974513:\\
\;\;\;\;\frac{e^{\frac{x}{s} - 2 \cdot \mathsf{log1p}\left(t\_0\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{s \cdot \left(1 + t\_0\right)} \cdot 0.5\\
\end{array}
\end{array}
if (fabs.f32 x) < 0.00100000005Initial 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-rr79.9%
*-lft-identity79.9%
*-commutative79.9%
exp-to-pow79.8%
log1p-undefine80.0%
*-commutative80.0%
rem-exp-log75.7%
exp-sum75.4%
exp-diff94.1%
associate--r+94.3%
exp-diff95.3%
rem-exp-log99.6%
Simplified99.6%
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%
distribute-frac-neg2100.0%
rec-exp100.0%
add-sqr-sqrt100.0%
associate-/r*100.0%
Applied egg-rr53.4%
Applied egg-rr48.1%
Taylor expanded in x around 0 54.1%
(FPCore (x s) :precision binary32 (* (/ 1.0 (* s (+ 1.0 (exp (/ x s))))) 0.5))
float code(float x, float s) {
return (1.0f / (s * (1.0f + expf((x / s))))) * 0.5f;
}
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))))) * 0.5e0
end function
function code(x, s) return Float32(Float32(Float32(1.0) / Float32(s * Float32(Float32(1.0) + exp(Float32(x / s))))) * Float32(0.5)) end
function tmp = code(x, s) tmp = (single(1.0) / (s * (single(1.0) + exp((x / s))))) * single(0.5); end
\begin{array}{l}
\\
\frac{1}{s \cdot \left(1 + e^{\frac{x}{s}}\right)} \cdot 0.5
\end{array}
Initial program 99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.8%
distribute-frac-neg299.8%
rec-exp99.8%
add-sqr-sqrt99.7%
associate-/r*99.7%
Applied egg-rr65.0%
Applied egg-rr64.2%
Taylor expanded in x around 0 62.5%
(FPCore (x s) :precision binary32 (* (/ 1.0 (+ (* s 2.0) (* x (+ 1.0 (* (/ x s) 0.5))))) (+ 0.5 (* (/ x s) 0.25))))
float code(float x, float s) {
return (1.0f / ((s * 2.0f) + (x * (1.0f + ((x / s) * 0.5f))))) * (0.5f + ((x / s) * 0.25f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (1.0e0 / ((s * 2.0e0) + (x * (1.0e0 + ((x / s) * 0.5e0))))) * (0.5e0 + ((x / s) * 0.25e0))
end function
function code(x, s) return Float32(Float32(Float32(1.0) / Float32(Float32(s * Float32(2.0)) + Float32(x * Float32(Float32(1.0) + Float32(Float32(x / s) * Float32(0.5)))))) * Float32(Float32(0.5) + Float32(Float32(x / s) * Float32(0.25)))) end
function tmp = code(x, s) tmp = (single(1.0) / ((s * single(2.0)) + (x * (single(1.0) + ((x / s) * single(0.5)))))) * (single(0.5) + ((x / s) * single(0.25))); end
\begin{array}{l}
\\
\frac{1}{s \cdot 2 + x \cdot \left(1 + \frac{x}{s} \cdot 0.5\right)} \cdot \left(0.5 + \frac{x}{s} \cdot 0.25\right)
\end{array}
Initial program 99.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.8%
distribute-frac-neg299.8%
rec-exp99.8%
add-sqr-sqrt99.7%
associate-/r*99.7%
Applied egg-rr65.0%
Applied egg-rr64.2%
Taylor expanded in x around 0 53.0%
*-commutative53.0%
Simplified53.0%
Taylor expanded in x around 0 39.4%
Final simplification39.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.7%
fabs-neg99.7%
distribute-frac-neg99.7%
distribute-frac-neg299.7%
fabs-neg99.7%
*-commutative99.7%
fabs-neg99.7%
+-commutative99.7%
fabs-neg99.7%
Simplified99.8%
Taylor expanded in s around inf 30.0%
herbie shell --seed 2024085
(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))))))