
(FPCore (x y z) :precision binary64 (* (+ x y) (+ z 1.0)))
double code(double x, double y, double z) {
return (x + y) * (z + 1.0);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x + y) * (z + 1.0d0)
end function
public static double code(double x, double y, double z) {
return (x + y) * (z + 1.0);
}
def code(x, y, z): return (x + y) * (z + 1.0)
function code(x, y, z) return Float64(Float64(x + y) * Float64(z + 1.0)) end
function tmp = code(x, y, z) tmp = (x + y) * (z + 1.0); end
code[x_, y_, z_] := N[(N[(x + y), $MachinePrecision] * N[(z + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x + y\right) \cdot \left(z + 1\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (* (+ x y) (+ z 1.0)))
double code(double x, double y, double z) {
return (x + y) * (z + 1.0);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x + y) * (z + 1.0d0)
end function
public static double code(double x, double y, double z) {
return (x + y) * (z + 1.0);
}
def code(x, y, z): return (x + y) * (z + 1.0)
function code(x, y, z) return Float64(Float64(x + y) * Float64(z + 1.0)) end
function tmp = code(x, y, z) tmp = (x + y) * (z + 1.0); end
code[x_, y_, z_] := N[(N[(x + y), $MachinePrecision] * N[(z + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x + y\right) \cdot \left(z + 1\right)
\end{array}
(FPCore (x y z) :precision binary64 (* (+ 1.0 z) (+ y x)))
double code(double x, double y, double z) {
return (1.0 + z) * (y + x);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (1.0d0 + z) * (y + x)
end function
public static double code(double x, double y, double z) {
return (1.0 + z) * (y + x);
}
def code(x, y, z): return (1.0 + z) * (y + x)
function code(x, y, z) return Float64(Float64(1.0 + z) * Float64(y + x)) end
function tmp = code(x, y, z) tmp = (1.0 + z) * (y + x); end
code[x_, y_, z_] := N[(N[(1.0 + z), $MachinePrecision] * N[(y + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(1 + z\right) \cdot \left(y + x\right)
\end{array}
Initial program 100.0%
Final simplification100.0%
(FPCore (x y z)
:precision binary64
(if (<= (+ y x) -5e-251)
(fma z x x)
(if (<= (+ y x) 1e+54)
(+ y x)
(if (<= (+ y x) 2e+122)
(* z y)
(if (<= (+ y x) 5e+258) (+ y x) (* z y))))))
double code(double x, double y, double z) {
double tmp;
if ((y + x) <= -5e-251) {
tmp = fma(z, x, x);
} else if ((y + x) <= 1e+54) {
tmp = y + x;
} else if ((y + x) <= 2e+122) {
tmp = z * y;
} else if ((y + x) <= 5e+258) {
tmp = y + x;
} else {
tmp = z * y;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(y + x) <= -5e-251) tmp = fma(z, x, x); elseif (Float64(y + x) <= 1e+54) tmp = Float64(y + x); elseif (Float64(y + x) <= 2e+122) tmp = Float64(z * y); elseif (Float64(y + x) <= 5e+258) tmp = Float64(y + x); else tmp = Float64(z * y); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(y + x), $MachinePrecision], -5e-251], N[(z * x + x), $MachinePrecision], If[LessEqual[N[(y + x), $MachinePrecision], 1e+54], N[(y + x), $MachinePrecision], If[LessEqual[N[(y + x), $MachinePrecision], 2e+122], N[(z * y), $MachinePrecision], If[LessEqual[N[(y + x), $MachinePrecision], 5e+258], N[(y + x), $MachinePrecision], N[(z * y), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y + x \leq -5 \cdot 10^{-251}:\\
\;\;\;\;\mathsf{fma}\left(z, x, x\right)\\
\mathbf{elif}\;y + x \leq 10^{+54}:\\
\;\;\;\;y + x\\
\mathbf{elif}\;y + x \leq 2 \cdot 10^{+122}:\\
\;\;\;\;z \cdot y\\
\mathbf{elif}\;y + x \leq 5 \cdot 10^{+258}:\\
\;\;\;\;y + x\\
\mathbf{else}:\\
\;\;\;\;z \cdot y\\
\end{array}
\end{array}
if (+.f64 x y) < -5.0000000000000003e-251Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f6451.9
Applied rewrites51.9%
if -5.0000000000000003e-251 < (+.f64 x y) < 1.0000000000000001e54 or 2.00000000000000003e122 < (+.f64 x y) < 5e258Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
lower-+.f6465.0
Applied rewrites65.0%
if 1.0000000000000001e54 < (+.f64 x y) < 2.00000000000000003e122 or 5e258 < (+.f64 x y) Initial program 100.0%
Taylor expanded in y around inf
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f6455.5
Applied rewrites55.5%
Taylor expanded in z around inf
Applied rewrites45.4%
Final simplification54.9%
(FPCore (x y z)
:precision binary64
(if (<= (+ 1.0 z) -1e+128)
(* z x)
(if (<= (+ 1.0 z) -500000000.0)
(* z y)
(if (<= (+ 1.0 z) 200000000.0) (+ y x) (* z x)))))
double code(double x, double y, double z) {
double tmp;
if ((1.0 + z) <= -1e+128) {
tmp = z * x;
} else if ((1.0 + z) <= -500000000.0) {
tmp = z * y;
} else if ((1.0 + z) <= 200000000.0) {
tmp = y + x;
} else {
tmp = z * x;
}
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 ((1.0d0 + z) <= (-1d+128)) then
tmp = z * x
else if ((1.0d0 + z) <= (-500000000.0d0)) then
tmp = z * y
else if ((1.0d0 + z) <= 200000000.0d0) then
tmp = y + x
else
tmp = z * x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((1.0 + z) <= -1e+128) {
tmp = z * x;
} else if ((1.0 + z) <= -500000000.0) {
tmp = z * y;
} else if ((1.0 + z) <= 200000000.0) {
tmp = y + x;
} else {
tmp = z * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (1.0 + z) <= -1e+128: tmp = z * x elif (1.0 + z) <= -500000000.0: tmp = z * y elif (1.0 + z) <= 200000000.0: tmp = y + x else: tmp = z * x return tmp
function code(x, y, z) tmp = 0.0 if (Float64(1.0 + z) <= -1e+128) tmp = Float64(z * x); elseif (Float64(1.0 + z) <= -500000000.0) tmp = Float64(z * y); elseif (Float64(1.0 + z) <= 200000000.0) tmp = Float64(y + x); else tmp = Float64(z * x); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((1.0 + z) <= -1e+128) tmp = z * x; elseif ((1.0 + z) <= -500000000.0) tmp = z * y; elseif ((1.0 + z) <= 200000000.0) tmp = y + x; else tmp = z * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(1.0 + z), $MachinePrecision], -1e+128], N[(z * x), $MachinePrecision], If[LessEqual[N[(1.0 + z), $MachinePrecision], -500000000.0], N[(z * y), $MachinePrecision], If[LessEqual[N[(1.0 + z), $MachinePrecision], 200000000.0], N[(y + x), $MachinePrecision], N[(z * x), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;1 + z \leq -1 \cdot 10^{+128}:\\
\;\;\;\;z \cdot x\\
\mathbf{elif}\;1 + z \leq -500000000:\\
\;\;\;\;z \cdot y\\
\mathbf{elif}\;1 + z \leq 200000000:\\
\;\;\;\;y + x\\
\mathbf{else}:\\
\;\;\;\;z \cdot x\\
\end{array}
\end{array}
if (+.f64 z #s(literal 1 binary64)) < -1.0000000000000001e128 or 2e8 < (+.f64 z #s(literal 1 binary64)) Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f6455.9
Applied rewrites55.9%
Taylor expanded in z around inf
Applied rewrites55.3%
if -1.0000000000000001e128 < (+.f64 z #s(literal 1 binary64)) < -5e8Initial program 100.0%
Taylor expanded in y around inf
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f6455.4
Applied rewrites55.4%
Taylor expanded in z around inf
Applied rewrites54.0%
if -5e8 < (+.f64 z #s(literal 1 binary64)) < 2e8Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
lower-+.f6497.4
Applied rewrites97.4%
Final simplification76.4%
(FPCore (x y z) :precision binary64 (if (<= (+ 1.0 z) -500000000.0) (* z x) (if (<= (+ 1.0 z) 200000000.0) (+ y x) (* z x))))
double code(double x, double y, double z) {
double tmp;
if ((1.0 + z) <= -500000000.0) {
tmp = z * x;
} else if ((1.0 + z) <= 200000000.0) {
tmp = y + x;
} else {
tmp = z * x;
}
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 ((1.0d0 + z) <= (-500000000.0d0)) then
tmp = z * x
else if ((1.0d0 + z) <= 200000000.0d0) then
tmp = y + x
else
tmp = z * x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((1.0 + z) <= -500000000.0) {
tmp = z * x;
} else if ((1.0 + z) <= 200000000.0) {
tmp = y + x;
} else {
tmp = z * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (1.0 + z) <= -500000000.0: tmp = z * x elif (1.0 + z) <= 200000000.0: tmp = y + x else: tmp = z * x return tmp
function code(x, y, z) tmp = 0.0 if (Float64(1.0 + z) <= -500000000.0) tmp = Float64(z * x); elseif (Float64(1.0 + z) <= 200000000.0) tmp = Float64(y + x); else tmp = Float64(z * x); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((1.0 + z) <= -500000000.0) tmp = z * x; elseif ((1.0 + z) <= 200000000.0) tmp = y + x; else tmp = z * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(1.0 + z), $MachinePrecision], -500000000.0], N[(z * x), $MachinePrecision], If[LessEqual[N[(1.0 + z), $MachinePrecision], 200000000.0], N[(y + x), $MachinePrecision], N[(z * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;1 + z \leq -500000000:\\
\;\;\;\;z \cdot x\\
\mathbf{elif}\;1 + z \leq 200000000:\\
\;\;\;\;y + x\\
\mathbf{else}:\\
\;\;\;\;z \cdot x\\
\end{array}
\end{array}
if (+.f64 z #s(literal 1 binary64)) < -5e8 or 2e8 < (+.f64 z #s(literal 1 binary64)) Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f6454.5
Applied rewrites54.5%
Taylor expanded in z around inf
Applied rewrites53.9%
if -5e8 < (+.f64 z #s(literal 1 binary64)) < 2e8Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
lower-+.f6497.4
Applied rewrites97.4%
Final simplification75.8%
(FPCore (x y z) :precision binary64 (if (<= (+ y x) -5e-251) (fma z x x) (fma z y y)))
double code(double x, double y, double z) {
double tmp;
if ((y + x) <= -5e-251) {
tmp = fma(z, x, x);
} else {
tmp = fma(z, y, y);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(y + x) <= -5e-251) tmp = fma(z, x, x); else tmp = fma(z, y, y); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(y + x), $MachinePrecision], -5e-251], N[(z * x + x), $MachinePrecision], N[(z * y + y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y + x \leq -5 \cdot 10^{-251}:\\
\;\;\;\;\mathsf{fma}\left(z, x, x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, y, y\right)\\
\end{array}
\end{array}
if (+.f64 x y) < -5.0000000000000003e-251Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f6451.9
Applied rewrites51.9%
if -5.0000000000000003e-251 < (+.f64 x y) Initial program 100.0%
Taylor expanded in y around inf
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f6449.9
Applied rewrites49.9%
Final simplification50.9%
(FPCore (x y z) :precision binary64 (+ y x))
double code(double x, double y, double z) {
return y + x;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = y + x
end function
public static double code(double x, double y, double z) {
return y + x;
}
def code(x, y, z): return y + x
function code(x, y, z) return Float64(y + x) end
function tmp = code(x, y, z) tmp = y + x; end
code[x_, y_, z_] := N[(y + x), $MachinePrecision]
\begin{array}{l}
\\
y + x
\end{array}
Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
lower-+.f6450.9
Applied rewrites50.9%
herbie shell --seed 2024244
(FPCore (x y z)
:name "Optimisation.CirclePacking:place from circle-packing-0.1.0.4, G"
:precision binary64
(* (+ x y) (+ z 1.0)))