
(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 11 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.3%
*-commutative99.3%
distribute-lft-in99.3%
*-rgt-identity99.3%
fabs-neg99.3%
+-commutative99.3%
fma-def99.3%
fabs-neg99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (x s) :precision binary32 (/ 1.0 (* s (* (+ 1.0 (exp (/ x s))) (+ 1.0 (exp (/ (- x) s)))))))
float code(float x, float s) {
return 1.0f / (s * ((1.0f + expf((x / s))) * (1.0f + expf((-x / s)))));
}
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))) * (1.0e0 + exp((-x / s)))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(s * Float32(Float32(Float32(1.0) + exp(Float32(x / s))) * Float32(Float32(1.0) + exp(Float32(Float32(-x) / s)))))) end
function tmp = code(x, s) tmp = single(1.0) / (s * ((single(1.0) + exp((x / s))) * (single(1.0) + exp((-x / s))))); end
\begin{array}{l}
\\
\frac{1}{s \cdot \left(\left(1 + e^{\frac{x}{s}}\right) \cdot \left(1 + e^{\frac{-x}{s}}\right)\right)}
\end{array}
Initial program 99.3%
Simplified99.3%
distribute-frac-neg99.3%
rec-exp99.3%
add-sqr-sqrt51.8%
fabs-sqr51.8%
add-sqr-sqrt96.9%
Applied egg-rr96.9%
Taylor expanded in x around inf 96.9%
distribute-lft-in55.9%
div-inv55.9%
exp-prod54.0%
add-sqr-sqrt8.8%
fabs-sqr8.8%
add-sqr-sqrt54.0%
exp-prod56.0%
div-inv56.0%
Applied egg-rr58.3%
distribute-lft-out99.3%
Simplified99.3%
Taylor expanded in s around 0 99.3%
rec-exp99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ x s)))) (/ 1.0 (* (+ 1.0 t_0) (+ s (/ s t_0))))))
float code(float x, float s) {
float t_0 = expf((x / s));
return 1.0f / ((1.0f + t_0) * (s + (s / t_0)));
}
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 = 1.0e0 / ((1.0e0 + t_0) * (s + (s / t_0)))
end function
function code(x, s) t_0 = exp(Float32(x / s)) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + t_0) * Float32(s + Float32(s / t_0)))) end
function tmp = code(x, s) t_0 = exp((x / s)); tmp = single(1.0) / ((single(1.0) + t_0) * (s + (s / t_0))); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\frac{x}{s}}\\
\frac{1}{\left(1 + t_0\right) \cdot \left(s + \frac{s}{t_0}\right)}
\end{array}
\end{array}
Initial program 99.3%
Simplified99.3%
distribute-frac-neg99.3%
rec-exp99.3%
add-sqr-sqrt51.8%
fabs-sqr51.8%
add-sqr-sqrt96.9%
Applied egg-rr96.9%
Taylor expanded in x around inf 96.9%
distribute-lft-in55.9%
div-inv55.9%
exp-prod54.0%
add-sqr-sqrt8.8%
fabs-sqr8.8%
add-sqr-sqrt54.0%
exp-prod56.0%
div-inv56.0%
Applied egg-rr58.3%
distribute-lft-out99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (x s) :precision binary32 (/ 1.0 (* (+ 1.0 (exp (/ x s))) (+ s (/ s (+ 1.0 (/ x s)))))))
float code(float x, float s) {
return 1.0f / ((1.0f + expf((x / s))) * (s + (s / (1.0f + (x / s)))));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0 / ((1.0e0 + exp((x / s))) * (s + (s / (1.0e0 + (x / s)))))
end function
function code(x, s) return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + exp(Float32(x / s))) * Float32(s + Float32(s / Float32(Float32(1.0) + Float32(x / s)))))) end
function tmp = code(x, s) tmp = single(1.0) / ((single(1.0) + exp((x / s))) * (s + (s / (single(1.0) + (x / s))))); end
\begin{array}{l}
\\
\frac{1}{\left(1 + e^{\frac{x}{s}}\right) \cdot \left(s + \frac{s}{1 + \frac{x}{s}}\right)}
\end{array}
Initial program 99.3%
Simplified99.3%
distribute-frac-neg99.3%
rec-exp99.3%
add-sqr-sqrt51.8%
fabs-sqr51.8%
add-sqr-sqrt96.9%
Applied egg-rr96.9%
Taylor expanded in x around inf 96.9%
distribute-lft-in55.9%
div-inv55.9%
exp-prod54.0%
add-sqr-sqrt8.8%
fabs-sqr8.8%
add-sqr-sqrt54.0%
exp-prod56.0%
div-inv56.0%
Applied egg-rr58.3%
distribute-lft-out99.3%
Simplified99.3%
Taylor expanded in x around 0 63.0%
Final simplification63.0%
(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(0.5) / Float32(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{0.5}{s \cdot \left(1 + e^{\frac{x}{s}}\right)}
\end{array}
Initial program 99.3%
*-commutative99.3%
distribute-lft-in99.3%
*-rgt-identity99.3%
fabs-neg99.3%
+-commutative99.3%
fma-def99.3%
fabs-neg99.3%
Simplified99.3%
Applied egg-rr58.3%
associate-*r/58.3%
*-rgt-identity58.3%
distribute-neg-frac58.3%
+-commutative58.3%
distribute-neg-in58.3%
metadata-eval58.3%
Simplified58.3%
Taylor expanded in x around 0 61.4%
Taylor expanded in s around 0 61.4%
Final simplification61.4%
(FPCore (x s) :precision binary32 (let* ((t_0 (+ (/ x s) 2.0))) (/ 1.0 (* s (* t_0 t_0)))))
float code(float x, float s) {
float t_0 = (x / s) + 2.0f;
return 1.0f / (s * (t_0 * t_0));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: t_0
t_0 = (x / s) + 2.0e0
code = 1.0e0 / (s * (t_0 * t_0))
end function
function code(x, s) t_0 = Float32(Float32(x / s) + Float32(2.0)) return Float32(Float32(1.0) / Float32(s * Float32(t_0 * t_0))) end
function tmp = code(x, s) t_0 = (x / s) + single(2.0); tmp = single(1.0) / (s * (t_0 * t_0)); end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x}{s} + 2\\
\frac{1}{s \cdot \left(t_0 \cdot t_0\right)}
\end{array}
\end{array}
Initial program 99.3%
*-commutative99.3%
distribute-lft-in99.3%
*-rgt-identity99.3%
fabs-neg99.3%
+-commutative99.3%
fma-def99.3%
fabs-neg99.3%
Simplified99.3%
Applied egg-rr56.2%
expm1-def56.3%
expm1-log1p57.6%
associate-/l/58.0%
+-commutative58.0%
Simplified58.0%
Taylor expanded in x around 0 55.4%
unpow255.4%
associate-+r+55.4%
metadata-eval55.4%
associate-+r+55.4%
metadata-eval55.4%
Applied egg-rr55.4%
Taylor expanded in x around 0 73.5%
Final simplification73.5%
(FPCore (x s) :precision binary32 (/ (/ -0.5 s) (- (/ (- x) s) 2.0)))
float code(float x, float s) {
return (-0.5f / s) / ((-x / s) - 2.0f);
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = ((-0.5e0) / s) / ((-x / s) - 2.0e0)
end function
function code(x, s) return Float32(Float32(Float32(-0.5) / s) / Float32(Float32(Float32(-x) / s) - Float32(2.0))) end
function tmp = code(x, s) tmp = (single(-0.5) / s) / ((-x / s) - single(2.0)); end
\begin{array}{l}
\\
\frac{\frac{-0.5}{s}}{\frac{-x}{s} - 2}
\end{array}
Initial program 99.3%
*-commutative99.3%
distribute-lft-in99.3%
*-rgt-identity99.3%
fabs-neg99.3%
+-commutative99.3%
fma-def99.3%
fabs-neg99.3%
Simplified99.3%
Applied egg-rr58.3%
associate-*r/58.3%
*-rgt-identity58.3%
distribute-neg-frac58.3%
+-commutative58.3%
distribute-neg-in58.3%
metadata-eval58.3%
Simplified58.3%
Taylor expanded in x around 0 61.4%
Taylor expanded in x around 0 49.1%
Final simplification49.1%
(FPCore (x s) :precision binary32 (/ -0.5 (* s (+ (/ x s) -2.0))))
float code(float x, float s) {
return -0.5f / (s * ((x / s) + -2.0f));
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (-0.5e0) / (s * ((x / s) + (-2.0e0)))
end function
function code(x, s) return Float32(Float32(-0.5) / Float32(s * Float32(Float32(x / s) + Float32(-2.0)))) end
function tmp = code(x, s) tmp = single(-0.5) / (s * ((x / s) + single(-2.0))); end
\begin{array}{l}
\\
\frac{-0.5}{s \cdot \left(\frac{x}{s} + -2\right)}
\end{array}
Initial program 99.3%
*-commutative99.3%
distribute-lft-in99.3%
*-rgt-identity99.3%
fabs-neg99.3%
+-commutative99.3%
fma-def99.3%
fabs-neg99.3%
Simplified99.3%
Applied egg-rr58.3%
associate-*r/58.3%
*-rgt-identity58.3%
distribute-neg-frac58.3%
+-commutative58.3%
distribute-neg-in58.3%
metadata-eval58.3%
Simplified58.3%
Taylor expanded in x around 0 61.4%
Taylor expanded in x around 0 49.1%
expm1-log1p-u47.8%
expm1-udef64.1%
sub-neg64.1%
add-sqr-sqrt32.2%
sqrt-unprod74.2%
mul-1-neg74.2%
mul-1-neg74.2%
sqr-neg74.2%
sqrt-unprod30.0%
add-sqr-sqrt63.3%
metadata-eval63.3%
Applied egg-rr63.3%
expm1-def47.7%
expm1-log1p49.3%
associate-/l/49.3%
*-commutative49.3%
Simplified49.3%
Final simplification49.3%
(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.3%
*-commutative99.3%
distribute-lft-in99.3%
*-rgt-identity99.3%
fabs-neg99.3%
+-commutative99.3%
fma-def99.3%
fabs-neg99.3%
Simplified99.3%
Applied egg-rr58.3%
associate-*r/58.3%
*-rgt-identity58.3%
distribute-neg-frac58.3%
+-commutative58.3%
distribute-neg-in58.3%
metadata-eval58.3%
Simplified58.3%
Taylor expanded in x around 0 61.4%
Taylor expanded in x around 0 49.1%
frac-2neg49.1%
div-inv49.1%
sub-neg49.1%
add-sqr-sqrt24.7%
sqrt-unprod73.1%
mul-1-neg73.1%
mul-1-neg73.1%
sqr-neg73.1%
sqrt-unprod24.3%
add-sqr-sqrt49.3%
metadata-eval49.3%
Applied egg-rr49.3%
associate-*r/49.3%
*-rgt-identity49.3%
distribute-neg-frac49.3%
metadata-eval49.3%
+-commutative49.3%
distribute-neg-in49.3%
metadata-eval49.3%
sub-neg49.3%
Simplified49.3%
Final simplification49.3%
(FPCore (x s) :precision binary32 (if (<= x 0.00019999999494757503) (/ 0.25 s) (/ 0.5 x)))
float code(float x, float s) {
float tmp;
if (x <= 0.00019999999494757503f) {
tmp = 0.25f / s;
} else {
tmp = 0.5f / x;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 0.00019999999494757503e0) then
tmp = 0.25e0 / s
else
tmp = 0.5e0 / x
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(0.00019999999494757503)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(Float32(0.5) / x); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(0.00019999999494757503)) tmp = single(0.25) / s; else tmp = single(0.5) / x; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.00019999999494757503:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if x < 1.99999995e-4Initial program 99.0%
*-commutative99.0%
distribute-lft-in99.1%
*-rgt-identity99.1%
fabs-neg99.1%
+-commutative99.1%
fma-def99.1%
fabs-neg99.1%
Simplified99.1%
Taylor expanded in s around inf 31.9%
if 1.99999995e-4 < x Initial program 99.8%
*-commutative99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
fabs-neg99.8%
+-commutative99.8%
fma-def99.8%
fabs-neg99.8%
Simplified99.8%
Applied egg-rr1.1%
associate-*r/1.1%
*-rgt-identity1.1%
distribute-neg-frac1.1%
+-commutative1.1%
distribute-neg-in1.1%
metadata-eval1.1%
Simplified1.1%
Taylor expanded in x around 0 99.1%
Taylor expanded in x around 0 52.2%
Taylor expanded in s around 0 10.6%
Final simplification25.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.3%
*-commutative99.3%
distribute-lft-in99.3%
*-rgt-identity99.3%
fabs-neg99.3%
+-commutative99.3%
fma-def99.3%
fabs-neg99.3%
Simplified99.3%
Taylor expanded in s around inf 23.6%
Final simplification23.6%
herbie shell --seed 2024011
(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))))))