
(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 (- z) y (fma 0.5 x (fma y (log z) y))))
double code(double x, double y, double z) {
return fma(-z, y, fma(0.5, x, fma(y, log(z), y)));
}
function code(x, y, z) return fma(Float64(-z), y, fma(0.5, x, fma(y, log(z), y))) end
code[x_, y_, z_] := N[((-z) * y + N[(0.5 * x + N[(y * N[Log[z], $MachinePrecision] + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-z, y, \mathsf{fma}\left(0.5, x, \mathsf{fma}\left(y, \log z, y\right)\right)\right)
\end{array}
Initial program 99.9%
lift-*.f64N/A
lift-+.f64N/A
distribute-lft-inN/A
+-commutativeN/A
*-commutativeN/A
lift--.f64N/A
sub-negN/A
distribute-rgt-inN/A
*-lft-identityN/A
associate-+r+N/A
lower-+.f64N/A
lower-fma.f64N/A
lower-*.f64N/A
lower-neg.f6499.9
Applied rewrites99.9%
lift-+.f64N/A
lift-+.f64N/A
associate-+r+N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-fma.f6499.9
lift-fma.f64N/A
*-commutativeN/A
lower-fma.f6499.9
Applied rewrites99.9%
(FPCore (x y z) :precision binary64 (let* ((t_0 (* (+ (- 1.0 z) (log z)) y)) (t_1 (* y (- z)))) (if (<= t_0 -1e+22) t_1 (if (<= t_0 2e+157) (* 0.5 x) t_1))))
double code(double x, double y, double z) {
double t_0 = ((1.0 - z) + log(z)) * y;
double t_1 = y * -z;
double tmp;
if (t_0 <= -1e+22) {
tmp = t_1;
} else if (t_0 <= 2e+157) {
tmp = 0.5 * x;
} else {
tmp = t_1;
}
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) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = ((1.0d0 - z) + log(z)) * y
t_1 = y * -z
if (t_0 <= (-1d+22)) then
tmp = t_1
else if (t_0 <= 2d+157) then
tmp = 0.5d0 * x
else
tmp = t_1
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = ((1.0 - z) + Math.log(z)) * y;
double t_1 = y * -z;
double tmp;
if (t_0 <= -1e+22) {
tmp = t_1;
} else if (t_0 <= 2e+157) {
tmp = 0.5 * x;
} else {
tmp = t_1;
}
return tmp;
}
def code(x, y, z): t_0 = ((1.0 - z) + math.log(z)) * y t_1 = y * -z tmp = 0 if t_0 <= -1e+22: tmp = t_1 elif t_0 <= 2e+157: tmp = 0.5 * x else: tmp = t_1 return tmp
function code(x, y, z) t_0 = Float64(Float64(Float64(1.0 - z) + log(z)) * y) t_1 = Float64(y * Float64(-z)) tmp = 0.0 if (t_0 <= -1e+22) tmp = t_1; elseif (t_0 <= 2e+157) tmp = Float64(0.5 * x); else tmp = t_1; end return tmp end
function tmp_2 = code(x, y, z) t_0 = ((1.0 - z) + log(z)) * y; t_1 = y * -z; tmp = 0.0; if (t_0 <= -1e+22) tmp = t_1; elseif (t_0 <= 2e+157) tmp = 0.5 * x; else tmp = t_1; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision]}, Block[{t$95$1 = N[(y * (-z)), $MachinePrecision]}, If[LessEqual[t$95$0, -1e+22], t$95$1, If[LessEqual[t$95$0, 2e+157], N[(0.5 * x), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\left(1 - z\right) + \log z\right) \cdot y\\
t_1 := y \cdot \left(-z\right)\\
\mathbf{if}\;t\_0 \leq -1 \cdot 10^{+22}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 2 \cdot 10^{+157}:\\
\;\;\;\;0.5 \cdot x\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) < -1e22 or 1.99999999999999997e157 < (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) Initial program 99.8%
Taylor expanded in z around inf
mul-1-negN/A
*-commutativeN/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6459.9
Applied rewrites59.9%
if -1e22 < (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) < 1.99999999999999997e157Initial program 99.9%
Taylor expanded in z around 0
lower-fma.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
lower-log.f6487.0
Applied rewrites87.0%
Taylor expanded in x around inf
Applied rewrites83.4%
Taylor expanded in x around inf
Applied rewrites64.8%
Final simplification62.5%
(FPCore (x y z) :precision binary64 (if (<= (+ (- 1.0 z) (log z)) -210.0) (fma x 0.5 (* (- z) y)) (fma (log z) y y)))
double code(double x, double y, double z) {
double tmp;
if (((1.0 - z) + log(z)) <= -210.0) {
tmp = fma(x, 0.5, (-z * y));
} else {
tmp = fma(log(z), y, y);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(Float64(1.0 - z) + log(z)) <= -210.0) tmp = fma(x, 0.5, Float64(Float64(-z) * y)); else tmp = fma(log(z), y, y); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision], -210.0], N[(x * 0.5 + N[((-z) * y), $MachinePrecision]), $MachinePrecision], N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(1 - z\right) + \log z \leq -210:\\
\;\;\;\;\mathsf{fma}\left(x, 0.5, \left(-z\right) \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\log z, y, y\right)\\
\end{array}
\end{array}
if (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) < -210Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6481.7
Applied rewrites81.7%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6481.7
lift-*.f64N/A
*-commutativeN/A
lower-*.f6481.7
Applied rewrites81.7%
if -210 < (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) Initial program 99.6%
Taylor expanded in z around 0
lower-fma.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
lower-log.f6490.6
Applied rewrites90.6%
Taylor expanded in x around 0
Applied rewrites59.3%
(FPCore (x y z) :precision binary64 (let* ((t_0 (fma (- (log z) z) y y))) (if (<= y -1.2) t_0 (if (<= y 5.4e+47) (fma x 0.5 (* (- z) y)) t_0))))
double code(double x, double y, double z) {
double t_0 = fma((log(z) - z), y, y);
double tmp;
if (y <= -1.2) {
tmp = t_0;
} else if (y <= 5.4e+47) {
tmp = fma(x, 0.5, (-z * y));
} 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.2) tmp = t_0; elseif (y <= 5.4e+47) tmp = fma(x, 0.5, Float64(Float64(-z) * y)); 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.2], t$95$0, If[LessEqual[y, 5.4e+47], N[(x * 0.5 + N[((-z) * y), $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.2:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 5.4 \cdot 10^{+47}:\\
\;\;\;\;\mathsf{fma}\left(x, 0.5, \left(-z\right) \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -1.19999999999999996 or 5.39999999999999991e47 < y Initial program 99.8%
Taylor expanded in x around 0
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.f6487.9
Applied rewrites87.9%
if -1.19999999999999996 < y < 5.39999999999999991e47Initial program 100.0%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6487.4
Applied rewrites87.4%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6487.4
lift-*.f64N/A
*-commutativeN/A
lower-*.f6487.4
Applied rewrites87.4%
(FPCore (x y z) :precision binary64 (if (<= z 0.55) (fma (log z) y (fma 0.5 x y)) (fma (- z) y (fma 0.5 x y))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.55) {
tmp = fma(log(z), y, fma(0.5, x, y));
} else {
tmp = fma(-z, y, fma(0.5, x, y));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 0.55) tmp = fma(log(z), y, fma(0.5, x, y)); else tmp = fma(Float64(-z), y, fma(0.5, x, y)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 0.55], N[(N[Log[z], $MachinePrecision] * y + N[(0.5 * x + y), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(0.5 * x + y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.55:\\
\;\;\;\;\mathsf{fma}\left(\log z, y, \mathsf{fma}\left(0.5, x, y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, \mathsf{fma}\left(0.5, x, y\right)\right)\\
\end{array}
\end{array}
if z < 0.55000000000000004Initial program 99.8%
Taylor expanded in z around 0
lower-fma.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
lower-log.f6498.6
Applied rewrites98.6%
Applied rewrites98.6%
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 x around 0
associate-+r+N/A
+-commutativeN/A
distribute-lft-out--N/A
*-rgt-identityN/A
unsub-negN/A
mul-1-negN/A
+-commutativeN/A
associate-+r+N/A
+-commutativeN/A
associate-+r+N/A
+-commutativeN/A
associate-+l+N/A
associate-+r+N/A
+-commutativeN/A
mul-1-negN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
distribute-lft-inN/A
Applied rewrites100.0%
Taylor expanded in z around inf
Applied rewrites98.6%
(FPCore (x y z) :precision binary64 (if (<= z 0.55) (fma 0.5 x (fma (log z) y y)) (fma (- z) y (fma 0.5 x y))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.55) {
tmp = fma(0.5, x, fma(log(z), y, y));
} else {
tmp = fma(-z, y, fma(0.5, x, y));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 0.55) tmp = fma(0.5, x, fma(log(z), y, y)); else tmp = fma(Float64(-z), y, fma(0.5, x, y)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 0.55], N[(0.5 * x + N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(0.5 * x + y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.55:\\
\;\;\;\;\mathsf{fma}\left(0.5, x, \mathsf{fma}\left(\log z, y, y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, \mathsf{fma}\left(0.5, x, y\right)\right)\\
\end{array}
\end{array}
if z < 0.55000000000000004Initial program 99.8%
Taylor expanded in z around 0
lower-fma.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
lower-log.f6498.6
Applied rewrites98.6%
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 x around 0
associate-+r+N/A
+-commutativeN/A
distribute-lft-out--N/A
*-rgt-identityN/A
unsub-negN/A
mul-1-negN/A
+-commutativeN/A
associate-+r+N/A
+-commutativeN/A
associate-+r+N/A
+-commutativeN/A
associate-+l+N/A
associate-+r+N/A
+-commutativeN/A
mul-1-negN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
distribute-lft-inN/A
Applied rewrites100.0%
Taylor expanded in z around inf
Applied rewrites98.6%
(FPCore (x y z) :precision binary64 (fma (- (+ 1.0 (log z)) z) y (* x 0.5)))
double code(double x, double y, double z) {
return fma(((1.0 + log(z)) - z), y, (x * 0.5));
}
function code(x, y, z) return fma(Float64(Float64(1.0 + log(z)) - z), y, Float64(x * 0.5)) end
code[x_, y_, z_] := N[(N[(N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision] * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\left(1 + \log z\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 (fma (- (log z) z) y (fma 0.5 x y)))
double code(double x, double y, double z) {
return fma((log(z) - z), y, fma(0.5, x, y));
}
function code(x, y, z) return fma(Float64(log(z) - z), y, fma(0.5, x, y)) end
code[x_, y_, z_] := N[(N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] * y + N[(0.5 * x + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\log z - z, y, \mathsf{fma}\left(0.5, x, y\right)\right)
\end{array}
Initial program 99.9%
Taylor expanded in x around 0
sub-negN/A
associate-+r+N/A
mul-1-negN/A
distribute-lft-inN/A
*-rgt-identityN/A
associate-+r+N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f64N/A
lower-fma.f6499.9
Applied rewrites99.9%
(FPCore (x y z) :precision binary64 (fma x 0.5 (* (- z) y)))
double code(double x, double y, double z) {
return fma(x, 0.5, (-z * y));
}
function code(x, y, z) return fma(x, 0.5, Float64(Float64(-z) * y)) end
code[x_, y_, z_] := N[(x * 0.5 + N[((-z) * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, 0.5, \left(-z\right) \cdot y\right)
\end{array}
Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6475.1
Applied rewrites75.1%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6475.1
lift-*.f64N/A
*-commutativeN/A
lower-*.f6475.1
Applied rewrites75.1%
(FPCore (x y z) :precision binary64 (* 0.5 x))
double code(double x, double y, double z) {
return 0.5 * x;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = 0.5d0 * x
end function
public static double code(double x, double y, double z) {
return 0.5 * x;
}
def code(x, y, z): return 0.5 * x
function code(x, y, z) return Float64(0.5 * x) end
function tmp = code(x, y, z) tmp = 0.5 * x; end
code[x_, y_, z_] := N[(0.5 * x), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot x
\end{array}
Initial program 99.9%
Taylor expanded in z around 0
lower-fma.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
lower-log.f6464.0
Applied rewrites64.0%
Taylor expanded in x around inf
Applied rewrites58.1%
Taylor expanded in x around inf
Applied rewrites40.4%
(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 2024294
(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)))))