
(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 7 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_1 (+ t_0 1.0))) (/ (/ (/ t_0 s) t_1) t_1)))
float code(float x, float s) {
float t_0 = expf((-fabsf(x) / s));
float t_1 = t_0 + 1.0f;
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 = t_0 + 1.0e0
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(t_0 + Float32(1.0)) return Float32(Float32(Float32(t_0 / s) / t_1) / t_1) end
function tmp = code(x, s) t_0 = exp((-abs(x) / s)); t_1 = t_0 + single(1.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 := t_0 + 1\\
\frac{\frac{\frac{t_0}{s}}{t_1}}{t_1}
\end{array}
\end{array}
(FPCore (x s) :precision binary32 (let* ((t_0 (exp (/ (fabs x) (- s))))) (/ t_0 (* (+ 1.0 t_0) (+ s (/ s (exp (/ (fabs x) s))))))))
float code(float x, float s) {
float t_0 = expf((fabsf(x) / -s));
return t_0 / ((1.0f + t_0) * (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 / ((1.0e0 + t_0) * (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(Float32(1.0) + t_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 / ((single(1.0) + t_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(1 + t_0\right) \cdot \left(s + \frac{s}{e^{\frac{\left|x\right|}{s}}}\right)}
\end{array}
\end{array}
(FPCore (x s) :precision binary32 (/ (/ 0.25 (exp (/ x s))) s))
float code(float x, float s) {
return (0.25f / expf((x / s))) / s;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.25e0 / exp((x / s))) / s
end function
function code(x, s) return Float32(Float32(Float32(0.25) / exp(Float32(x / s))) / s) end
function tmp = code(x, s) tmp = (single(0.25) / exp((x / s))) / s; end
\begin{array}{l}
\\
\frac{\frac{0.25}{e^{\frac{x}{s}}}}{s}
\end{array}
(FPCore (x s) :precision binary32 (/ (/ 0.25 (+ 1.0 (/ x s))) s))
float code(float x, float s) {
return (0.25f / (1.0f + (x / s))) / s;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = (0.25e0 / (1.0e0 + (x / s))) / s
end function
function code(x, s) return Float32(Float32(Float32(0.25) / Float32(Float32(1.0) + Float32(x / s))) / s) end
function tmp = code(x, s) tmp = (single(0.25) / (single(1.0) + (x / s))) / s; end
\begin{array}{l}
\\
\frac{\frac{0.25}{1 + \frac{x}{s}}}{s}
\end{array}
(FPCore (x s) :precision binary32 (if (<= x 4.999999987376214e-7) (/ 0.25 s) (/ s 4.0)))
float code(float x, float s) {
float tmp;
if (x <= 4.999999987376214e-7f) {
tmp = 0.25f / s;
} else {
tmp = s / 4.0f;
}
return tmp;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
real(4) :: tmp
if (x <= 4.999999987376214e-7) then
tmp = 0.25e0 / s
else
tmp = s / 4.0e0
end if
code = tmp
end function
function code(x, s) tmp = Float32(0.0) if (x <= Float32(4.999999987376214e-7)) tmp = Float32(Float32(0.25) / s); else tmp = Float32(s / Float32(4.0)); end return tmp end
function tmp_2 = code(x, s) tmp = single(0.0); if (x <= single(4.999999987376214e-7)) tmp = single(0.25) / s; else tmp = s / single(4.0); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.999999987376214 \cdot 10^{-7}:\\
\;\;\;\;\frac{0.25}{s}\\
\mathbf{else}:\\
\;\;\;\;\frac{s}{4}\\
\end{array}
\end{array}
(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}
(FPCore (x s) :precision binary32 1.0)
float code(float x, float s) {
return 1.0f;
}
real(4) function code(x, s)
real(4), intent (in) :: x
real(4), intent (in) :: s
code = 1.0e0
end function
function code(x, s) return Float32(1.0) end
function tmp = code(x, s) tmp = single(1.0); end
\begin{array}{l}
\\
1
\end{array}
herbie shell --seed 2024006
(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))))))