
(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}
(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(abs(x) / Float32(-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%
distribute-lft-in99.8%
*-rgt-identity99.8%
fabs-neg99.8%
distribute-rgt-in99.8%
cancel-sign-sub99.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(t_0 / Float32(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{t\_0}{s \cdot {\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.8%
+-commutative99.8%
exp-prod99.8%
rem-square-sqrt49.5%
fabs-sqr49.5%
rem-square-sqrt97.4%
exp-prod97.4%
neg-mul-197.4%
distribute-neg-frac297.4%
Simplified97.4%
Taylor expanded in x around 0 97.4%
mul-1-neg97.4%
rem-square-sqrt49.5%
fabs-sqr49.5%
rem-square-sqrt59.6%
Simplified59.6%
Final simplification59.6%
(FPCore (x s)
:precision binary32
(/
(exp
(*
x
(-
(/ 1.0 s)
(- (/ (* 2.0 (log 2.0)) x) (/ (+ (/ (* x -0.25) s) -1.0) s)))))
s))
float code(float x, float s) {
return expf((x * ((1.0f / s) - (((2.0f * logf(2.0f)) / x) - ((((x * -0.25f) / s) + -1.0f) / s))))) / 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 * log(2.0e0)) / x) - ((((x * (-0.25e0)) / s) + (-1.0e0)) / s))))) / s
end function
function code(x, s) return Float32(exp(Float32(x * Float32(Float32(Float32(1.0) / s) - Float32(Float32(Float32(Float32(2.0) * log(Float32(2.0))) / x) - Float32(Float32(Float32(Float32(x * Float32(-0.25)) / s) + Float32(-1.0)) / s))))) / s) end
function tmp = code(x, s) tmp = exp((x * ((single(1.0) / s) - (((single(2.0) * log(single(2.0))) / x) - ((((x * single(-0.25)) / s) + single(-1.0)) / s))))) / s; end
\begin{array}{l}
\\
\frac{e^{x \cdot \left(\frac{1}{s} - \left(\frac{2 \cdot \log 2}{x} - \frac{\frac{x \cdot -0.25}{s} + -1}{s}\right)\right)}}{s}
\end{array}
Initial program 99.8%
*-commutative99.8%
Simplified99.8%
clear-num99.7%
associate-/r/99.4%
Applied egg-rr63.4%
Taylor expanded in s around 0 63.8%
rem-exp-log62.3%
exp-to-pow62.3%
log1p-undefine62.3%
*-commutative62.3%
exp-sum62.0%
+-commutative62.0%
exp-diff87.0%
associate--r+87.2%
exp-diff87.3%
Simplified88.9%
Taylor expanded in x around -inf 99.6%
associate-*r*99.6%
mul-1-neg99.6%
log1p-define99.7%
associate-*r/99.7%
Simplified99.7%
Taylor expanded in s around -inf 98.4%
+-commutative98.4%
mul-1-neg98.4%
unsub-neg98.4%
associate-*r/98.4%
sub-neg98.4%
associate-*r/98.4%
distribute-rgt-out98.4%
metadata-eval98.4%
*-commutative98.4%
neg-mul-198.4%
distribute-lft-neg-in98.4%
metadata-eval98.4%
*-commutative98.4%
metadata-eval98.4%
Simplified98.4%
Final simplification98.4%
(FPCore (x s) :precision binary32 (/ (exp (/ x (- s))) (+ (* s 4.0) (* x (- (/ (* x 3.0) s) 4.0)))))
float code(float x, float s) {
return expf((x / -s)) / ((s * 4.0f) + (x * (((x * 3.0f) / 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) + (x * (((x * 3.0e0) / s) - 4.0e0)))
end function
function code(x, s) return Float32(exp(Float32(x / Float32(-s))) / Float32(Float32(s * Float32(4.0)) + Float32(x * Float32(Float32(Float32(x * Float32(3.0)) / s) - Float32(4.0))))) end
function tmp = code(x, s) tmp = exp((x / -s)) / ((s * single(4.0)) + (x * (((x * single(3.0)) / s) - single(4.0)))); end
\begin{array}{l}
\\
\frac{e^{\frac{x}{-s}}}{s \cdot 4 + x \cdot \left(\frac{x \cdot 3}{s} - 4\right)}
\end{array}
Initial program 99.8%
*-commutative99.8%
Simplified99.8%
Taylor expanded in x around 0 99.8%
+-commutative99.8%
exp-prod99.8%
rem-square-sqrt49.5%
fabs-sqr49.5%
rem-square-sqrt97.4%
exp-prod97.4%
neg-mul-197.4%
distribute-neg-frac297.4%
Simplified97.4%
Taylor expanded in x around 0 97.4%
mul-1-neg97.4%
rem-square-sqrt49.5%
fabs-sqr49.5%
rem-square-sqrt59.6%
Simplified59.6%
Taylor expanded in x around 0 55.9%
Taylor expanded in x around 0 58.5%
associate-*r/58.5%
*-commutative58.5%
Simplified58.5%
Final simplification58.5%
(FPCore (x s) :precision binary32 (/ (exp (/ x (- s))) (+ (* s 4.0) (* x (- (* 3.0 (/ x s)) 4.0)))))
float code(float x, float s) {
return expf((x / -s)) / ((s * 4.0f) + (x * ((3.0f * (x / 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) + (x * ((3.0e0 * (x / s)) - 4.0e0)))
end function
function code(x, s) return Float32(exp(Float32(x / Float32(-s))) / Float32(Float32(s * Float32(4.0)) + Float32(x * Float32(Float32(Float32(3.0) * Float32(x / s)) - Float32(4.0))))) end
function tmp = code(x, s) tmp = exp((x / -s)) / ((s * single(4.0)) + (x * ((single(3.0) * (x / s)) - single(4.0)))); end
\begin{array}{l}
\\
\frac{e^{\frac{x}{-s}}}{s \cdot 4 + x \cdot \left(3 \cdot \frac{x}{s} - 4\right)}
\end{array}
Initial program 99.8%
*-commutative99.8%
Simplified99.8%
Taylor expanded in x around 0 99.8%
+-commutative99.8%
exp-prod99.8%
rem-square-sqrt49.5%
fabs-sqr49.5%
rem-square-sqrt97.4%
exp-prod97.4%
neg-mul-197.4%
distribute-neg-frac297.4%
Simplified97.4%
Taylor expanded in x around 0 97.4%
mul-1-neg97.4%
rem-square-sqrt49.5%
fabs-sqr49.5%
rem-square-sqrt59.6%
Simplified59.6%
Taylor expanded in x around 0 58.5%
Final simplification58.5%
(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%
Simplified99.8%
Taylor expanded in x around 0 99.8%
+-commutative99.8%
exp-prod99.8%
rem-square-sqrt49.5%
fabs-sqr49.5%
rem-square-sqrt97.4%
exp-prod97.4%
neg-mul-197.4%
distribute-neg-frac297.4%
Simplified97.4%
Taylor expanded in x around 0 97.4%
mul-1-neg97.4%
rem-square-sqrt49.5%
fabs-sqr49.5%
rem-square-sqrt59.6%
Simplified59.6%
Taylor expanded in s around inf 57.0%
*-commutative57.0%
Simplified57.0%
Final simplification57.0%
(FPCore (x s) :precision binary32 (/ (+ (+ (/ (* x -0.25) s) 0.25) (* -0.25 (* x (/ -1.0 s)))) s))
float code(float x, float s) {
return ((((x * -0.25f) / s) + 0.25f) + (-0.25f * (x * (-1.0f / s)))) / s;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = ((((x * (-0.25e0)) / s) + 0.25e0) + ((-0.25e0) * (x * ((-1.0e0) / s)))) / s
end function
function code(x, s) return Float32(Float32(Float32(Float32(Float32(x * Float32(-0.25)) / s) + Float32(0.25)) + Float32(Float32(-0.25) * Float32(x * Float32(Float32(-1.0) / s)))) / s) end
function tmp = code(x, s) tmp = ((((x * single(-0.25)) / s) + single(0.25)) + (single(-0.25) * (x * (single(-1.0) / s)))) / s; end
\begin{array}{l}
\\
\frac{\left(\frac{x \cdot -0.25}{s} + 0.25\right) + -0.25 \cdot \left(x \cdot \frac{-1}{s}\right)}{s}
\end{array}
Initial program 99.8%
*-commutative99.8%
Simplified99.8%
clear-num99.7%
associate-/r/99.4%
Applied egg-rr63.4%
*-un-lft-identity63.4%
exp-prod63.4%
Applied egg-rr63.4%
exp-1-e63.4%
Simplified63.4%
Taylor expanded in s around -inf 27.6%
mul-1-neg27.6%
log-E63.7%
associate-/l*54.3%
associate-*r/54.3%
*-commutative54.3%
Simplified54.3%
Final simplification54.3%
(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 25.0%
herbie shell --seed 2024135
(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))))))