
(FPCore (x y z) :precision binary64 (+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))
double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + log(z)));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x * 0.5d0) + (y * ((1.0d0 - z) + log(z)))
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + Math.log(z)));
}
def code(x, y, z): return (x * 0.5) + (y * ((1.0 - z) + math.log(z)))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(y * Float64(Float64(1.0 - z) + log(z)))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + (y * ((1.0 - z) + log(z))); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(\left(1 - z\right) + \log z\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))
double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + log(z)));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x * 0.5d0) + (y * ((1.0d0 - z) + log(z)))
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + Math.log(z)));
}
def code(x, y, z): return (x * 0.5) + (y * ((1.0 - z) + math.log(z)))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(y * Float64(Float64(1.0 - z) + log(z)))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + (y * ((1.0 - z) + log(z))); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(\left(1 - z\right) + \log z\right)
\end{array}
(FPCore (x y z) :precision binary64 (fma (- 1.0 z) y (fma (log z) y (* x 0.5))))
double code(double x, double y, double z) {
return fma((1.0 - z), y, fma(log(z), y, (x * 0.5)));
}
function code(x, y, z) return fma(Float64(1.0 - z), y, fma(log(z), y, Float64(x * 0.5))) end
code[x_, y_, z_] := N[(N[(1.0 - z), $MachinePrecision] * y + N[(N[Log[z], $MachinePrecision] * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(1 - z, y, \mathsf{fma}\left(\log z, y, x \cdot 0.5\right)\right)
\end{array}
Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-+.f64N/A
distribute-rgt-inN/A
associate-+l+N/A
lower-fma.f64N/A
lower-fma.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Final simplification99.9%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (- (log z) z)))
(if (<= y -2.1e+126)
(+ (* t_0 y) y)
(if (<= y 3.5e+58) (fma (- z) y (* x 0.5)) (fma t_0 y y)))))
double code(double x, double y, double z) {
double t_0 = log(z) - z;
double tmp;
if (y <= -2.1e+126) {
tmp = (t_0 * y) + y;
} else if (y <= 3.5e+58) {
tmp = fma(-z, y, (x * 0.5));
} else {
tmp = fma(t_0, y, y);
}
return tmp;
}
function code(x, y, z) t_0 = Float64(log(z) - z) tmp = 0.0 if (y <= -2.1e+126) tmp = Float64(Float64(t_0 * y) + y); elseif (y <= 3.5e+58) tmp = fma(Float64(-z), y, Float64(x * 0.5)); else tmp = fma(t_0, y, y); end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision]}, If[LessEqual[y, -2.1e+126], N[(N[(t$95$0 * y), $MachinePrecision] + y), $MachinePrecision], If[LessEqual[y, 3.5e+58], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * y + y), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \log z - z\\
\mathbf{if}\;y \leq -2.1 \cdot 10^{+126}:\\
\;\;\;\;t\_0 \cdot y + y\\
\mathbf{elif}\;y \leq 3.5 \cdot 10^{+58}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t\_0, y, y\right)\\
\end{array}
\end{array}
if y < -2.0999999999999999e126Initial program 99.7%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-+.f64N/A
distribute-rgt-inN/A
associate-+l+N/A
lower-fma.f64N/A
lower-fma.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in y around inf
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f6495.0
Applied rewrites95.0%
Applied rewrites95.1%
if -2.0999999999999999e126 < y < 3.4999999999999997e58Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6483.9
Applied rewrites83.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6483.9
Applied rewrites83.9%
if 3.4999999999999997e58 < y Initial program 100.0%
Taylor expanded in y around inf
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f6486.4
Applied rewrites86.4%
(FPCore (x y z) :precision binary64 (let* ((t_0 (fma (- (log z) z) y y))) (if (<= y -1.1e+126) t_0 (if (<= y 3.5e+58) (fma (- z) y (* x 0.5)) t_0))))
double code(double x, double y, double z) {
double t_0 = fma((log(z) - z), y, y);
double tmp;
if (y <= -1.1e+126) {
tmp = t_0;
} else if (y <= 3.5e+58) {
tmp = fma(-z, y, (x * 0.5));
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = fma(Float64(log(z) - z), y, y) tmp = 0.0 if (y <= -1.1e+126) tmp = t_0; elseif (y <= 3.5e+58) tmp = fma(Float64(-z), y, Float64(x * 0.5)); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] * y + y), $MachinePrecision]}, If[LessEqual[y, -1.1e+126], t$95$0, If[LessEqual[y, 3.5e+58], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\log z - z, y, y\right)\\
\mathbf{if}\;y \leq -1.1 \cdot 10^{+126}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 3.5 \cdot 10^{+58}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -1.09999999999999999e126 or 3.4999999999999997e58 < y Initial program 99.9%
Taylor expanded in y around inf
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f6490.1
Applied rewrites90.1%
if -1.09999999999999999e126 < y < 3.4999999999999997e58Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6483.9
Applied rewrites83.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6483.9
Applied rewrites83.9%
(FPCore (x y z) :precision binary64 (if (<= z 0.55) (fma x 0.5 (fma (log z) y y)) (fma (- 1.0 z) y (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.55) {
tmp = fma(x, 0.5, fma(log(z), y, y));
} else {
tmp = fma((1.0 - z), y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 0.55) tmp = fma(x, 0.5, fma(log(z), y, y)); else tmp = fma(Float64(1.0 - z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 0.55], N[(x * 0.5 + N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 - z), $MachinePrecision] * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.55:\\
\;\;\;\;\mathsf{fma}\left(x, 0.5, \mathsf{fma}\left(\log z, y, y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(1 - z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 0.55000000000000004Initial program 99.8%
Taylor expanded in z around 0
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
lower-log.f6498.8
Applied rewrites98.8%
if 0.55000000000000004 < z Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-+.f64N/A
distribute-rgt-inN/A
associate-+l+N/A
lower-fma.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6499.3
Applied rewrites99.3%
(FPCore (x y z) :precision binary64 (fma (- (+ (log z) 1.0) z) y (* x 0.5)))
double code(double x, double y, double z) {
return fma(((log(z) + 1.0) - z), y, (x * 0.5));
}
function code(x, y, z) return fma(Float64(Float64(log(z) + 1.0) - z), y, Float64(x * 0.5)) end
code[x_, y_, z_] := N[(N[(N[(N[Log[z], $MachinePrecision] + 1.0), $MachinePrecision] - z), $MachinePrecision] * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\left(\log z + 1\right) - z, y, x \cdot 0.5\right)
\end{array}
Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Final simplification99.9%
(FPCore (x y z) :precision binary64 (if (<= z 5.2e-103) (+ (* (log z) y) y) (fma (- z) y (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if (z <= 5.2e-103) {
tmp = (log(z) * y) + y;
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 5.2e-103) tmp = Float64(Float64(log(z) * y) + y); else tmp = fma(Float64(-z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 5.2e-103], N[(N[(N[Log[z], $MachinePrecision] * y), $MachinePrecision] + y), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 5.2 \cdot 10^{-103}:\\
\;\;\;\;\log z \cdot y + y\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 5.19999999999999993e-103Initial program 99.7%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-+.f64N/A
distribute-rgt-inN/A
associate-+l+N/A
lower-fma.f64N/A
lower-fma.f6499.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.8
Applied rewrites99.8%
Taylor expanded in y around inf
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f6453.4
Applied rewrites53.4%
Taylor expanded in z around 0
Applied rewrites53.4%
Applied rewrites53.5%
if 5.19999999999999993e-103 < z Initial program 100.0%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6491.8
Applied rewrites91.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6491.8
Applied rewrites91.8%
(FPCore (x y z) :precision binary64 (if (<= z 5.2e-103) (fma (log z) y y) (fma (- z) y (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if (z <= 5.2e-103) {
tmp = fma(log(z), y, y);
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 5.2e-103) tmp = fma(log(z), y, y); else tmp = fma(Float64(-z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 5.2e-103], N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 5.2 \cdot 10^{-103}:\\
\;\;\;\;\mathsf{fma}\left(\log z, y, y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 5.19999999999999993e-103Initial program 99.7%
Taylor expanded in z around 0
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
lower-log.f6499.7
Applied rewrites99.7%
Taylor expanded in y around inf
Applied rewrites53.4%
if 5.19999999999999993e-103 < z Initial program 100.0%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6491.8
Applied rewrites91.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6491.8
Applied rewrites91.8%
(FPCore (x y z) :precision binary64 (if (<= z 6.3e+40) (* x 0.5) (* (- z) y)))
double code(double x, double y, double z) {
double tmp;
if (z <= 6.3e+40) {
tmp = x * 0.5;
} else {
tmp = -z * y;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if (z <= 6.3d+40) then
tmp = x * 0.5d0
else
tmp = -z * y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= 6.3e+40) {
tmp = x * 0.5;
} else {
tmp = -z * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 6.3e+40: tmp = x * 0.5 else: tmp = -z * y return tmp
function code(x, y, z) tmp = 0.0 if (z <= 6.3e+40) tmp = Float64(x * 0.5); else tmp = Float64(Float64(-z) * y); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= 6.3e+40) tmp = x * 0.5; else tmp = -z * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 6.3e+40], N[(x * 0.5), $MachinePrecision], N[((-z) * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 6.3 \cdot 10^{+40}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\left(-z\right) \cdot y\\
\end{array}
\end{array}
if z < 6.30000000000000006e40Initial program 99.8%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6452.9
Applied rewrites52.9%
if 6.30000000000000006e40 < z Initial program 100.0%
Taylor expanded in z around inf
mul-1-negN/A
*-commutativeN/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6485.6
Applied rewrites85.6%
(FPCore (x y z) :precision binary64 (fma (- z) y (* x 0.5)))
double code(double x, double y, double z) {
return fma(-z, y, (x * 0.5));
}
function code(x, y, z) return fma(Float64(-z), y, Float64(x * 0.5)) end
code[x_, y_, z_] := N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-z, y, x \cdot 0.5\right)
\end{array}
Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6476.4
Applied rewrites76.4%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6476.4
Applied rewrites76.4%
(FPCore (x y z) :precision binary64 (* x 0.5))
double code(double x, double y, double z) {
return x * 0.5;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = x * 0.5d0
end function
public static double code(double x, double y, double z) {
return x * 0.5;
}
def code(x, y, z): return x * 0.5
function code(x, y, z) return Float64(x * 0.5) end
function tmp = code(x, y, z) tmp = x * 0.5; end
code[x_, y_, z_] := N[(x * 0.5), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5
\end{array}
Initial program 99.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6436.1
Applied rewrites36.1%
(FPCore (x y z) :precision binary64 (- (+ y (* 0.5 x)) (* y (- z (log z)))))
double code(double x, double y, double z) {
return (y + (0.5 * x)) - (y * (z - log(z)));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (y + (0.5d0 * x)) - (y * (z - log(z)))
end function
public static double code(double x, double y, double z) {
return (y + (0.5 * x)) - (y * (z - Math.log(z)));
}
def code(x, y, z): return (y + (0.5 * x)) - (y * (z - math.log(z)))
function code(x, y, z) return Float64(Float64(y + Float64(0.5 * x)) - Float64(y * Float64(z - log(z)))) end
function tmp = code(x, y, z) tmp = (y + (0.5 * x)) - (y * (z - log(z))); end
code[x_, y_, z_] := N[(N[(y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision] - N[(y * N[(z - N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(y + 0.5 \cdot x\right) - y \cdot \left(z - \log z\right)
\end{array}
herbie shell --seed 2024277
(FPCore (x y z)
:name "System.Random.MWC.Distributions:gamma from mwc-random-0.13.3.2"
:precision binary64
:alt
(! :herbie-platform default (- (+ y (* 1/2 x)) (* y (- z (log z)))))
(+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))