
(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 10 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) (fma s t_0 s)))))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
return t_0 / ((t_0 + 1.0f) * fmaf(s, t_0, s));
}
function code(x, s) t_0 = exp(Float32(Float32(-abs(x)) / s)) return Float32(t_0 / Float32(Float32(t_0 + Float32(1.0)) * fma(s, t_0, 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 \mathsf{fma}\left(s, t\_0, s\right)}
\end{array}
\end{array}
Initial program 99.8%
*-commutative99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fma-define99.8%
fabs-neg99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ x (- s))))) (/ (/ t_0 s) (pow (+ 1.0 t_0) 2.0))))
float code(float x, float s) {
float t_0 = expf((x / -s));
return (t_0 / s) / powf((1.0f + t_0), 2.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
t_0 = exp((x / -s))
code = (t_0 / s) / ((1.0e0 + t_0) ** 2.0e0)
end function
function code(x, s) t_0 = exp(Float32(x / Float32(-s))) return Float32(Float32(t_0 / s) / (Float32(Float32(1.0) + t_0) ^ Float32(2.0))) end
function tmp = code(x, s) t_0 = exp((x / -s)); tmp = (t_0 / s) / ((single(1.0) + t_0) ^ single(2.0)); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{x}{-s}}\\
\frac{\frac{t\_0}{s}}{{\left(1 + t\_0\right)}^{2}}
\end{array}
\end{array}
Initial program 99.8%
*-commutative99.8%
Simplified99.8%
Taylor expanded in x around 0 99.7%
associate-/r*99.8%
exp-prod99.8%
rem-square-sqrt48.7%
fabs-sqr48.7%
rem-square-sqrt62.7%
exp-prod62.6%
neg-mul-162.6%
distribute-neg-frac262.6%
Simplified63.5%
Final simplification63.5%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ x (- s))))) (/ (/ t_0 (+ 1.0 t_0)) (+ s (/ s (+ 1.0 (/ x s)))))))
float code(float x, float s) {
float t_0 = expf((x / -s));
return (t_0 / (1.0f + t_0)) / (s + (s / (1.0f + (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((x / -s))
code = (t_0 / (1.0e0 + t_0)) / (s + (s / (1.0e0 + (x / s))))
end function
function code(x, s) t_0 = exp(Float32(x / Float32(-s))) return Float32(Float32(t_0 / Float32(Float32(1.0) + t_0)) / Float32(s + Float32(s / Float32(Float32(1.0) + Float32(x / s))))) end
function tmp = code(x, s) t_0 = exp((x / -s)); tmp = (t_0 / (single(1.0) + t_0)) / (s + (s / (single(1.0) + (x / s)))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{x}{-s}}\\
\frac{\frac{t\_0}{1 + t\_0}}{s + \frac{s}{1 + \frac{x}{s}}}
\end{array}
\end{array}
Initial program 99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
+-commutative99.8%
Simplified99.8%
Taylor expanded in x around 0 99.8%
associate-/r*99.8%
Simplified64.2%
Taylor expanded in x around 0 62.6%
Final simplification62.6%
(FPCore (x s) :precision binary32 (/ (/ (exp (* x (/ -1.0 s))) 2.0) (+ s (/ s (exp (/ x s))))))
float code(float x, float s) {
return (expf((x * (-1.0f / s))) / 2.0f) / (s + (s / expf((x / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (exp((x * ((-1.0e0) / s))) / 2.0e0) / (s + (s / exp((x / s))))
end function
function code(x, s) return Float32(Float32(exp(Float32(x * Float32(Float32(-1.0) / s))) / Float32(2.0)) / Float32(s + Float32(s / exp(Float32(x / s))))) end
function tmp = code(x, s) tmp = (exp((x * (single(-1.0) / s))) / single(2.0)) / (s + (s / exp((x / s)))); end
\begin{array}{l}
\\
\frac{\frac{e^{x \cdot \frac{-1}{s}}}{2}}{s + \frac{s}{e^{\frac{x}{s}}}}
\end{array}
Initial program 99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
+-commutative99.8%
Simplified99.8%
Taylor expanded in x around 0 99.8%
associate-/r*99.8%
Simplified64.2%
Taylor expanded in x around 0 60.8%
distribute-frac-neg260.8%
div-inv60.8%
distribute-lft-neg-in60.8%
Applied egg-rr60.8%
Final simplification60.8%
(FPCore (x s) :precision binary32 (/ (/ (exp (/ x (- s))) 2.0) (+ s (/ s (exp (/ x s))))))
float code(float x, float s) {
return (expf((x / -s)) / 2.0f) / (s + (s / expf((x / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (exp((x / -s)) / 2.0e0) / (s + (s / exp((x / s))))
end function
function code(x, s) return Float32(Float32(exp(Float32(x / Float32(-s))) / Float32(2.0)) / Float32(s + Float32(s / exp(Float32(x / s))))) end
function tmp = code(x, s) tmp = (exp((x / -s)) / single(2.0)) / (s + (s / exp((x / s)))); end
\begin{array}{l}
\\
\frac{\frac{e^{\frac{x}{-s}}}{2}}{s + \frac{s}{e^{\frac{x}{s}}}}
\end{array}
Initial program 99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
+-commutative99.8%
Simplified99.8%
Taylor expanded in x around 0 99.8%
associate-/r*99.8%
Simplified64.2%
Taylor expanded in x around 0 60.8%
(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.8%
*-commutative99.8%
Simplified99.8%
add-sqr-sqrt99.7%
times-frac99.7%
+-commutative99.7%
distribute-lft-in99.7%
*-rgt-identity99.7%
fma-undefine99.8%
Applied egg-rr61.8%
associate-*l/61.9%
/-rgt-identity61.9%
times-frac61.8%
rem-square-sqrt61.9%
*-lft-identity61.9%
+-commutative61.9%
Simplified61.9%
Taylor expanded in x around 0 62.4%
(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(Float32(exp(Float32(x / Float32(-s))) / s) / Float32(4.0)) end
function tmp = code(x, s) tmp = (exp((x / -s)) / s) / single(4.0); end
\begin{array}{l}
\\
\frac{\frac{e^{\frac{x}{-s}}}{s}}{4}
\end{array}
Initial program 99.8%
*-commutative99.8%
Simplified99.8%
Taylor expanded in x around 0 99.7%
associate-/r*99.8%
exp-prod99.8%
rem-square-sqrt48.7%
fabs-sqr48.7%
rem-square-sqrt62.7%
exp-prod62.6%
neg-mul-162.6%
distribute-neg-frac262.6%
Simplified63.5%
Taylor expanded in x around 0 61.6%
(FPCore (x s) :precision binary32 (if (<= s 9.999999960041972e-13) (/ (/ 0.5 s) (+ 2.0 (/ x s))) (/ (+ 0.5 (* (/ x s) -0.25)) (+ s (+ s (* x (+ (* (/ x s) 0.5) -1.0)))))))
float code(float x, float s) {
float tmp;
if (s <= 9.999999960041972e-13f) {
tmp = (0.5f / s) / (2.0f + (x / s));
} else {
tmp = (0.5f + ((x / s) * -0.25f)) / (s + (s + (x * (((x / s) * 0.5f) + -1.0f))));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (s <= 9.999999960041972e-13) then
tmp = (0.5e0 / s) / (2.0e0 + (x / s))
else
tmp = (0.5e0 + ((x / s) * (-0.25e0))) / (s + (s + (x * (((x / s) * 0.5e0) + (-1.0e0)))))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (s <= Float32(9.999999960041972e-13)) tmp = Float32(Float32(Float32(0.5) / s) / Float32(Float32(2.0) + Float32(x / s))); else tmp = Float32(Float32(Float32(0.5) + Float32(Float32(x / s) * Float32(-0.25))) / Float32(s + Float32(s + Float32(x * Float32(Float32(Float32(x / s) * Float32(0.5)) + Float32(-1.0)))))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (s <= single(9.999999960041972e-13)) tmp = (single(0.5) / s) / (single(2.0) + (x / s)); else tmp = (single(0.5) + ((x / s) * single(-0.25))) / (s + (s + (x * (((x / s) * single(0.5)) + single(-1.0))))); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;s \leq 9.999999960041972 \cdot 10^{-13}:\\
\;\;\;\;\frac{\frac{0.5}{s}}{2 + \frac{x}{s}}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 + \frac{x}{s} \cdot -0.25}{s + \left(s + x \cdot \left(\frac{x}{s} \cdot 0.5 + -1\right)\right)}\\
\end{array}
\end{array}
if s < 9.99999996e-13Initial program 99.7%
*-commutative99.7%
Simplified99.7%
add-sqr-sqrt99.7%
times-frac99.7%
+-commutative99.7%
distribute-lft-in99.7%
*-rgt-identity99.7%
fma-undefine99.7%
Applied egg-rr51.7%
associate-*l/51.7%
/-rgt-identity51.7%
times-frac51.7%
rem-square-sqrt51.7%
*-lft-identity51.7%
+-commutative51.7%
Simplified51.7%
Taylor expanded in s around -inf 24.2%
associate-*r/24.2%
sub-neg24.2%
mul-1-neg24.2%
distribute-rgt-out--24.2%
metadata-eval24.2%
*-commutative24.2%
associate-*r/24.2%
distribute-lft-neg-in24.2%
metadata-eval24.2%
metadata-eval24.2%
distribute-lft-in24.2%
neg-mul-124.2%
distribute-lft-neg-in24.2%
metadata-eval24.2%
metadata-eval24.2%
Simplified24.2%
Taylor expanded in x around 0 15.2%
Taylor expanded in x around 0 49.2%
if 9.99999996e-13 < s Initial program 99.9%
*-commutative99.9%
fabs-neg99.9%
+-commutative99.9%
fabs-neg99.9%
distribute-lft-in99.9%
*-rgt-identity99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in x around 0 99.9%
associate-/r*99.9%
Simplified65.4%
Taylor expanded in x around 0 80.8%
*-commutative80.8%
Simplified80.8%
Taylor expanded in x around 0 73.2%
Final simplification56.8%
(FPCore (x s) :precision binary32 (/ (/ 0.5 s) (+ 2.0 (/ x s))))
float code(float x, float s) {
return (0.5f / s) / (2.0f + (x / s));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.5e0 / s) / (2.0e0 + (x / s))
end function
function code(x, s) return Float32(Float32(Float32(0.5) / s) / Float32(Float32(2.0) + Float32(x / s))) end
function tmp = code(x, s) tmp = (single(0.5) / s) / (single(2.0) + (x / s)); end
\begin{array}{l}
\\
\frac{\frac{0.5}{s}}{2 + \frac{x}{s}}
\end{array}
Initial program 99.8%
*-commutative99.8%
Simplified99.8%
add-sqr-sqrt99.7%
times-frac99.7%
+-commutative99.7%
distribute-lft-in99.7%
*-rgt-identity99.7%
fma-undefine99.8%
Applied egg-rr61.8%
associate-*l/61.9%
/-rgt-identity61.9%
times-frac61.8%
rem-square-sqrt61.9%
*-lft-identity61.9%
+-commutative61.9%
Simplified61.9%
Taylor expanded in s around -inf 35.7%
associate-*r/35.7%
sub-neg35.7%
mul-1-neg35.7%
distribute-rgt-out--35.7%
metadata-eval35.7%
*-commutative35.7%
associate-*r/35.7%
distribute-lft-neg-in35.7%
metadata-eval35.7%
metadata-eval35.7%
distribute-lft-in35.7%
neg-mul-135.7%
distribute-lft-neg-in35.7%
metadata-eval35.7%
metadata-eval35.7%
Simplified35.7%
Taylor expanded in x around 0 26.6%
Taylor expanded in x around 0 51.2%
(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%
*-commutative99.8%
Simplified99.8%
Taylor expanded in s around inf 27.9%
herbie shell --seed 2024155
(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))))))