
(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 8 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}
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (if (<= (fabs x_m) 0.5) (/ (exp (+ (/ x_m s) (* -2.0 (log1p (exp (/ x_m s)))))) s) (/ (exp (/ x_m (- s))) (* s 4.0))))
x_m = fabs(x);
float code(float x_m, float s) {
float tmp;
if (fabsf(x_m) <= 0.5f) {
tmp = expf(((x_m / s) + (-2.0f * log1pf(expf((x_m / s)))))) / s;
} else {
tmp = expf((x_m / -s)) / (s * 4.0f);
}
return tmp;
}
x_m = abs(x) function code(x_m, s) tmp = Float32(0.0) if (abs(x_m) <= Float32(0.5)) tmp = Float32(exp(Float32(Float32(x_m / s) + Float32(Float32(-2.0) * log1p(exp(Float32(x_m / s)))))) / s); else tmp = Float32(exp(Float32(x_m / Float32(-s))) / Float32(s * Float32(4.0))); end return tmp end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;\left|x\_m\right| \leq 0.5:\\
\;\;\;\;\frac{e^{\frac{x\_m}{s} + -2 \cdot \mathsf{log1p}\left(e^{\frac{x\_m}{s}}\right)}}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{\frac{x\_m}{-s}}}{s \cdot 4}\\
\end{array}
\end{array}
if (fabs.f32 x) < 0.5Initial program 99.1%
fabs-neg99.1%
distribute-frac-neg99.1%
distribute-frac-neg299.1%
fabs-neg99.1%
*-commutative99.1%
fabs-neg99.1%
+-commutative99.1%
fabs-neg99.1%
Simplified99.2%
Applied egg-rr84.1%
*-lft-identity84.1%
*-commutative84.1%
exp-to-pow84.0%
log1p-undefine84.1%
*-commutative84.1%
rem-exp-log79.0%
prod-exp79.3%
exp-diff93.5%
associate--r+93.7%
exp-diff94.2%
cancel-sign-sub-inv94.2%
metadata-eval94.2%
rem-exp-log99.3%
Simplified99.3%
if 0.5 < (fabs.f32 x) Initial program 100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in s around inf 100.0%
*-commutative100.0%
Simplified100.0%
distribute-frac-neg100.0%
exp-neg100.0%
add-sqr-sqrt53.2%
fabs-sqr53.2%
add-sqr-sqrt54.7%
add-sqr-sqrt53.2%
sqrt-unprod100.0%
add-sqr-sqrt53.2%
fabs-sqr53.2%
add-sqr-sqrt53.2%
add-sqr-sqrt53.2%
fabs-sqr53.2%
add-sqr-sqrt100.0%
sqr-neg100.0%
distribute-frac-neg100.0%
distribute-frac-neg100.0%
sqrt-unprod-0.0%
add-sqr-sqrt3.1%
Applied egg-rr54.7%
rec-exp54.7%
distribute-frac-neg54.7%
Simplified54.7%
Final simplification75.1%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (let* ((t_0 (exp (/ (fabs x_m) (- s))))) (/ t_0 (* (+ t_0 1.0) (fma s t_0 s)))))
x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = expf((fabsf(x_m) / -s));
return t_0 / ((t_0 + 1.0f) * fmaf(s, t_0, s));
}
x_m = abs(x) function code(x_m, s) t_0 = exp(Float32(abs(x_m) / Float32(-s))) return Float32(t_0 / Float32(Float32(t_0 + Float32(1.0)) * fma(s, t_0, s))) end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := e^{\frac{\left|x\_m\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.6%
*-commutative99.6%
distribute-lft-in99.6%
*-rgt-identity99.6%
fabs-neg99.6%
+-commutative99.6%
fma-define99.7%
fabs-neg99.7%
Simplified99.7%
Final simplification99.7%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (let* ((t_0 (exp (/ (fabs x_m) (- s))))) (/ t_0 (* (+ t_0 1.0) (+ s (/ s (exp (/ (fabs x_m) s))))))))
x_m = fabs(x);
float code(float x_m, float s) {
float t_0 = expf((fabsf(x_m) / -s));
return t_0 / ((t_0 + 1.0f) * (s + (s / expf((fabsf(x_m) / s)))));
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
real(4) :: t_0
t_0 = exp((abs(x_m) / -s))
code = t_0 / ((t_0 + 1.0e0) * (s + (s / exp((abs(x_m) / s)))))
end function
x_m = abs(x) function code(x_m, s) t_0 = exp(Float32(abs(x_m) / Float32(-s))) return Float32(t_0 / Float32(Float32(t_0 + Float32(1.0)) * Float32(s + Float32(s / exp(Float32(abs(x_m) / s)))))) end
x_m = abs(x); function tmp = code(x_m, s) t_0 = exp((abs(x_m) / -s)); tmp = t_0 / ((t_0 + single(1.0)) * (s + (s / exp((abs(x_m) / s))))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := e^{\frac{\left|x\_m\right|}{-s}}\\
\frac{t\_0}{\left(t\_0 + 1\right) \cdot \left(s + \frac{s}{e^{\frac{\left|x\_m\right|}{s}}}\right)}
\end{array}
\end{array}
Initial program 99.6%
Simplified99.6%
Final simplification99.6%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ (+ (fma (* x_m s) 0.5 0.5) (* (* x_m s) -0.25)) s) (+ (exp (/ x_m s)) 1.0)))
x_m = fabs(x);
float code(float x_m, float s) {
return ((fmaf((x_m * s), 0.5f, 0.5f) + ((x_m * s) * -0.25f)) / s) / (expf((x_m / s)) + 1.0f);
}
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(fma(Float32(x_m * s), Float32(0.5), Float32(0.5)) + Float32(Float32(x_m * s) * Float32(-0.25))) / s) / Float32(exp(Float32(x_m / s)) + Float32(1.0))) end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{\mathsf{fma}\left(x\_m \cdot s, 0.5, 0.5\right) + \left(x\_m \cdot s\right) \cdot -0.25}{s}}{e^{\frac{x\_m}{s}} + 1}
\end{array}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Applied egg-rr82.6%
add-log-exp61.5%
log1p-undefine61.5%
log-pow61.2%
sum-log61.5%
*-commutative61.5%
log-div61.6%
associate-/r*61.6%
add-exp-log63.8%
unpow263.8%
associate-/r*63.7%
Applied egg-rr63.7%
Taylor expanded in s around inf 34.8%
cancel-sign-sub-inv34.8%
+-commutative34.8%
*-commutative34.8%
fma-define34.8%
div-inv34.8%
add-exp-log34.8%
neg-log34.8%
add-sqr-sqrt34.8%
sqrt-unprod34.8%
sqr-neg34.8%
sqrt-unprod-0.0%
add-sqr-sqrt32.3%
add-exp-log32.3%
metadata-eval32.3%
div-inv32.3%
add-exp-log32.3%
neg-log32.3%
add-sqr-sqrt32.3%
sqrt-unprod32.3%
Applied egg-rr59.0%
Final simplification59.0%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (/ 0.5 s) (+ 1.0 (exp (/ x_m s)))))
x_m = fabs(x);
float code(float x_m, float s) {
return (0.5f / s) / (1.0f + expf((x_m / s)));
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = (0.5e0 / s) / (1.0e0 + exp((x_m / s)))
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(0.5) / s) / Float32(Float32(1.0) + exp(Float32(x_m / s)))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = (single(0.5) / s) / (single(1.0) + exp((x_m / s))); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\frac{0.5}{s}}{1 + e^{\frac{x\_m}{s}}}
\end{array}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Applied egg-rr82.6%
add-log-exp61.5%
log1p-undefine61.5%
log-pow61.2%
sum-log61.5%
*-commutative61.5%
log-div61.6%
associate-/r*61.6%
add-exp-log63.8%
unpow263.8%
associate-/r*63.7%
Applied egg-rr63.7%
Taylor expanded in x around 0 59.2%
Final simplification59.2%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (exp (/ x_m (- s))) (* s 4.0)))
x_m = fabs(x);
float code(float x_m, float s) {
return expf((x_m / -s)) / (s * 4.0f);
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = exp((x_m / -s)) / (s * 4.0e0)
end function
x_m = abs(x) function code(x_m, s) return Float32(exp(Float32(x_m / Float32(-s))) / Float32(s * Float32(4.0))) end
x_m = abs(x); function tmp = code(x_m, s) tmp = exp((x_m / -s)) / (s * single(4.0)); end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{e^{\frac{x\_m}{-s}}}{s \cdot 4}
\end{array}
Initial program 99.6%
*-commutative99.6%
Simplified99.6%
Taylor expanded in s around inf 93.3%
*-commutative93.3%
Simplified93.3%
distribute-frac-neg93.3%
exp-neg93.3%
add-sqr-sqrt45.8%
fabs-sqr45.8%
add-sqr-sqrt58.3%
add-sqr-sqrt45.8%
sqrt-unprod93.3%
add-sqr-sqrt45.8%
fabs-sqr45.8%
add-sqr-sqrt47.0%
add-sqr-sqrt45.8%
fabs-sqr45.8%
add-sqr-sqrt93.3%
sqr-neg93.3%
distribute-frac-neg93.3%
distribute-frac-neg93.3%
sqrt-unprod-0.0%
add-sqr-sqrt23.7%
Applied egg-rr58.3%
rec-exp58.3%
distribute-frac-neg58.3%
Simplified58.3%
Final simplification58.3%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ (- (+ 0.25 (/ (* x_m -0.125) s)) (* 0.5 (* (/ x_m s) -0.25))) s))
x_m = fabs(x);
float code(float x_m, float s) {
return ((0.25f + ((x_m * -0.125f) / s)) - (0.5f * ((x_m / s) * -0.25f))) / s;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = ((0.25e0 + ((x_m * (-0.125e0)) / s)) - (0.5e0 * ((x_m / s) * (-0.25e0)))) / s
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(Float32(Float32(0.25) + Float32(Float32(x_m * Float32(-0.125)) / s)) - Float32(Float32(0.5) * Float32(Float32(x_m / s) * Float32(-0.25)))) / s) end
x_m = abs(x); function tmp = code(x_m, s) tmp = ((single(0.25) + ((x_m * single(-0.125)) / s)) - (single(0.5) * ((x_m / s) * single(-0.25)))) / s; end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{\left(0.25 + \frac{x\_m \cdot -0.125}{s}\right) - 0.5 \cdot \left(\frac{x\_m}{s} \cdot -0.25\right)}{s}
\end{array}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Applied egg-rr82.6%
add-log-exp61.5%
log1p-undefine61.5%
log-pow61.2%
sum-log61.5%
*-commutative61.5%
log-div61.6%
associate-/r*61.6%
add-exp-log63.8%
unpow263.8%
associate-/r*63.7%
Applied egg-rr63.7%
Taylor expanded in s around inf 34.8%
Taylor expanded in s around -inf 62.9%
mul-1-neg62.9%
distribute-rgt-out--62.9%
metadata-eval62.9%
*-commutative62.9%
associate-*r/62.9%
associate-*r/62.9%
*-commutative62.9%
Simplified62.9%
Final simplification62.9%
x_m = (fabs.f32 x) (FPCore (x_m s) :precision binary32 (/ 0.25 s))
x_m = fabs(x);
float code(float x_m, float s) {
return 0.25f / s;
}
x_m = abs(x)
real(4) function code(x_m, s)
real(4), intent (in) :: x_m
real(4), intent (in) :: s
code = 0.25e0 / s
end function
x_m = abs(x) function code(x_m, s) return Float32(Float32(0.25) / s) end
x_m = abs(x); function tmp = code(x_m, s) tmp = single(0.25) / s; end
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{0.25}{s}
\end{array}
Initial program 99.6%
fabs-neg99.6%
distribute-frac-neg99.6%
distribute-frac-neg299.6%
fabs-neg99.6%
*-commutative99.6%
fabs-neg99.6%
+-commutative99.6%
fabs-neg99.6%
Simplified99.6%
Taylor expanded in s around inf 26.4%
Final simplification26.4%
herbie shell --seed 2024071
(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))))))