
(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 12 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%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.9%
*-rgt-identity99.9%
+-commutative99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x s) :precision binary32 (if (<= x 3.999999999279835e-23) (/ (+ 0.25 (* -0.0625 (/ (/ x s) (/ s x)))) s) (exp (/ x (- s)))))
float code(float x, float s) {
float tmp;
if (x <= 3.999999999279835e-23f) {
tmp = (0.25f + (-0.0625f * ((x / s) / (s / x)))) / s;
} else {
tmp = expf((x / -s));
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 3.999999999279835e-23) then
tmp = (0.25e0 + ((-0.0625e0) * ((x / s) / (s / x)))) / s
else
tmp = exp((x / -s))
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(3.999999999279835e-23)) tmp = Float32(Float32(Float32(0.25) + Float32(Float32(-0.0625) * Float32(Float32(x / s) / Float32(s / x)))) / s); else tmp = exp(Float32(x / Float32(-s))); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(3.999999999279835e-23)) tmp = (single(0.25) + (single(-0.0625) * ((x / s) / (s / x)))) / s; else tmp = exp((x / -s)); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.999999999279835 \cdot 10^{-23}:\\
\;\;\;\;\frac{0.25 + -0.0625 \cdot \frac{\frac{x}{s}}{\frac{s}{x}}}{s}\\
\mathbf{else}:\\
\;\;\;\;e^{\frac{x}{-s}}\\
\end{array}
\end{array}
if x < 4e-23Initial 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%
*-un-lft-identity99.8%
associate-/r*99.8%
Applied egg-rr98.4%
*-lft-identity98.4%
associate-/l/98.4%
exp-to-pow98.4%
log1p-undefine98.5%
*-commutative98.5%
rem-exp-log95.8%
prod-exp95.2%
exp-diff96.5%
associate--r+96.6%
exp-diff97.1%
Simplified99.9%
Taylor expanded in x around 0 30.7%
unpow230.7%
unpow230.7%
times-frac35.9%
unpow235.9%
Simplified35.9%
unpow235.9%
clear-num35.9%
un-div-inv35.9%
Applied egg-rr35.9%
if 4e-23 < x 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 s around inf 97.5%
*-commutative97.5%
Simplified97.5%
Applied egg-rr97.5%
unpow-197.5%
Simplified97.5%
inv-pow97.5%
pow-to-exp97.3%
associate-*r*97.3%
*-commutative97.3%
log-prod97.3%
add-log-exp97.3%
Applied egg-rr97.3%
Taylor expanded in s around 0 90.4%
Final simplification60.2%
(FPCore (x s) :precision binary32 (/ 1.0 (* 4.0 (* s (exp (/ x s))))))
float code(float x, float s) {
return 1.0f / (4.0f * (s * expf((x / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (4.0e0 * (s * exp((x / s))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(4.0) * Float32(s * exp(Float32(x / s))))) end
function tmp = code(x, s) tmp = single(1.0) / (single(4.0) * (s * exp((x / s)))); end
\begin{array}{l}
\\
\frac{1}{4 \cdot \left(s \cdot e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.9%
*-rgt-identity99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in s around inf 96.8%
*-commutative96.8%
Simplified96.8%
Applied egg-rr62.7%
unpow-162.7%
Simplified62.7%
Final simplification62.7%
(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(x / Float32(-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%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.9%
*-rgt-identity99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in s around inf 96.8%
*-commutative96.8%
Simplified96.8%
distribute-frac-neg96.8%
exp-neg96.8%
add-sqr-sqrt52.0%
fabs-sqr52.0%
add-sqr-sqrt62.7%
add-sqr-sqrt52.0%
sqrt-unprod96.8%
add-sqr-sqrt52.0%
fabs-sqr52.0%
add-sqr-sqrt53.6%
add-sqr-sqrt52.0%
fabs-sqr52.0%
add-sqr-sqrt96.8%
sqr-neg96.8%
distribute-frac-neg96.8%
distribute-frac-neg96.8%
sqrt-unprod-0.0%
add-sqr-sqrt23.6%
Applied egg-rr62.7%
rec-exp62.7%
mul-1-neg62.7%
associate-*r/62.7%
mul-1-neg62.7%
Simplified62.7%
Final simplification62.7%
(FPCore (x s) :precision binary32 (/ (/ 0.25 s) (exp (/ x s))))
float code(float x, float s) {
return (0.25f / s) / expf((x / s));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.25e0 / s) / exp((x / s))
end function
function code(x, s) return Float32(Float32(Float32(0.25) / s) / exp(Float32(x / s))) end
function tmp = code(x, s) tmp = (single(0.25) / s) / exp((x / s)); end
\begin{array}{l}
\\
\frac{\frac{0.25}{s}}{e^{\frac{x}{s}}}
\end{array}
Initial program 99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.9%
*-rgt-identity99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in s around inf 96.8%
*-commutative96.8%
Simplified96.8%
Applied egg-rr62.7%
unpow-162.7%
Simplified62.7%
Taylor expanded in s around 0 62.7%
associate-/r*62.7%
Simplified62.7%
Final simplification62.7%
(FPCore (x s) :precision binary32 (/ 1.0 (* 4.0 (+ s (* x (+ 1.0 (* (/ x s) 0.5)))))))
float code(float x, float s) {
return 1.0f / (4.0f * (s + (x * (1.0f + ((x / s) * 0.5f)))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (4.0e0 * (s + (x * (1.0e0 + ((x / s) * 0.5e0)))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(4.0) * Float32(s + Float32(x * Float32(Float32(1.0) + Float32(Float32(x / s) * Float32(0.5))))))) end
function tmp = code(x, s) tmp = single(1.0) / (single(4.0) * (s + (x * (single(1.0) + ((x / s) * single(0.5)))))); end
\begin{array}{l}
\\
\frac{1}{4 \cdot \left(s + x \cdot \left(1 + \frac{x}{s} \cdot 0.5\right)\right)}
\end{array}
Initial program 99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.9%
*-rgt-identity99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in s around inf 96.8%
*-commutative96.8%
Simplified96.8%
Applied egg-rr62.7%
unpow-162.7%
Simplified62.7%
Taylor expanded in x around 0 65.4%
*-commutative65.4%
Simplified65.4%
Final simplification65.4%
(FPCore (x s) :precision binary32 (/ 1.0 (* 4.0 (* s (+ 1.0 (/ x s))))))
float code(float x, float s) {
return 1.0f / (4.0f * (s * (1.0f + (x / s))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (4.0e0 * (s * (1.0e0 + (x / s))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(4.0) * Float32(s * Float32(Float32(1.0) + Float32(x / s))))) end
function tmp = code(x, s) tmp = single(1.0) / (single(4.0) * (s * (single(1.0) + (x / s)))); end
\begin{array}{l}
\\
\frac{1}{4 \cdot \left(s \cdot \left(1 + \frac{x}{s}\right)\right)}
\end{array}
Initial program 99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.9%
*-rgt-identity99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in s around inf 96.8%
*-commutative96.8%
Simplified96.8%
Applied egg-rr62.7%
unpow-162.7%
Simplified62.7%
Taylor expanded in s around inf 52.7%
Final simplification52.7%
(FPCore (x s) :precision binary32 (/ (/ 0.25 s) (+ 1.0 (/ x s))))
float code(float x, float s) {
return (0.25f / s) / (1.0f + (x / s));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.25e0 / s) / (1.0e0 + (x / s))
end function
function code(x, s) return Float32(Float32(Float32(0.25) / s) / Float32(Float32(1.0) + Float32(x / s))) end
function tmp = code(x, s) tmp = (single(0.25) / s) / (single(1.0) + (x / s)); end
\begin{array}{l}
\\
\frac{\frac{0.25}{s}}{1 + \frac{x}{s}}
\end{array}
Initial program 99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.9%
*-rgt-identity99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in s around inf 96.8%
*-commutative96.8%
Simplified96.8%
Applied egg-rr62.7%
unpow-162.7%
Simplified62.7%
Taylor expanded in s around 0 62.7%
associate-/r*62.7%
Simplified62.7%
Taylor expanded in x around 0 52.7%
Final simplification52.7%
(FPCore (x s) :precision binary32 (if (<= x 1.0000000116860974e-7) (/ 0.25 s) (/ 0.25 x)))
float code(float x, float s) {
float tmp;
if (x <= 1.0000000116860974e-7f) {
tmp = 0.25f / s;
} else {
tmp = 0.25f / x;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 1.0000000116860974e-7) then
tmp = 0.25e0 / s
else
tmp = 0.25e0 / x
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(1.0000000116860974e-7)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.25) / x); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(1.0000000116860974e-7)) tmp = single(0.25) / s; else tmp = single(0.25) / x; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.0000000116860974 \cdot 10^{-7}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.25}{x}\\
\end{array}
\end{array}
if x < 1.00000001e-7Initial 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 37.0%
if 1.00000001e-7 < x 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 s around inf 99.1%
*-commutative99.1%
Simplified99.1%
Applied egg-rr99.1%
unpow-199.1%
Simplified99.1%
Taylor expanded in x around 0 12.1%
Taylor expanded in s around 0 10.6%
Final simplification28.2%
(FPCore (x s) :precision binary32 (/ 1.0 (* 4.0 (+ x s))))
float code(float x, float s) {
return 1.0f / (4.0f * (x + s));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / (4.0e0 * (x + s))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(4.0) * Float32(x + s))) end
function tmp = code(x, s) tmp = single(1.0) / (single(4.0) * (x + s)); end
\begin{array}{l}
\\
\frac{1}{4 \cdot \left(x + s\right)}
\end{array}
Initial program 99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.9%
*-rgt-identity99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in s around inf 96.8%
*-commutative96.8%
Simplified96.8%
Applied egg-rr62.7%
unpow-162.7%
Simplified62.7%
Taylor expanded in x around 0 29.0%
Final simplification29.0%
(FPCore (x s) :precision binary32 (/ 0.25 (+ x s)))
float code(float x, float s) {
return 0.25f / (x + s);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 0.25e0 / (x + s)
end function
function code(x, s) return Float32(Float32(0.25) / Float32(x + s)) end
function tmp = code(x, s) tmp = single(0.25) / (x + s); end
\begin{array}{l}
\\
\frac{0.25}{x + s}
\end{array}
Initial program 99.8%
*-commutative99.8%
fabs-neg99.8%
+-commutative99.8%
fabs-neg99.8%
distribute-lft-in99.9%
*-rgt-identity99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in s around inf 96.8%
*-commutative96.8%
Simplified96.8%
Applied egg-rr62.7%
unpow-162.7%
Simplified62.7%
Taylor expanded in x around 0 29.0%
inv-pow29.0%
unpow-prod-down28.5%
metadata-eval28.5%
Applied egg-rr28.5%
unpow-128.5%
associate-*r/28.5%
metadata-eval28.5%
Simplified28.5%
Final simplification28.5%
(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 26.2%
Final simplification26.2%
herbie shell --seed 2024130
(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))))))