
(FPCore (x y) :precision binary64 (* (+ x y) (+ x y)))
double code(double x, double y) {
return (x + y) * (x + y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) * (x + y)
end function
public static double code(double x, double y) {
return (x + y) * (x + y);
}
def code(x, y): return (x + y) * (x + y)
function code(x, y) return Float64(Float64(x + y) * Float64(x + y)) end
function tmp = code(x, y) tmp = (x + y) * (x + y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x + y\right) \cdot \left(x + y\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (* (+ x y) (+ x y)))
double code(double x, double y) {
return (x + y) * (x + y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) * (x + y)
end function
public static double code(double x, double y) {
return (x + y) * (x + y);
}
def code(x, y): return (x + y) * (x + y)
function code(x, y) return Float64(Float64(x + y) * Float64(x + y)) end
function tmp = code(x, y) tmp = (x + y) * (x + y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x + y\right) \cdot \left(x + y\right)
\end{array}
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (fma y (fma 2.0 x y) (pow x 2.0)))
assert(x < y);
double code(double x, double y) {
return fma(y, fma(2.0, x, y), pow(x, 2.0));
}
x, y = sort([x, y]) function code(x, y) return fma(y, fma(2.0, x, y), (x ^ 2.0)) end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(y * N[(2.0 * x + y), $MachinePrecision] + N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\mathsf{fma}\left(y, \mathsf{fma}\left(2, x, y\right), {x}^{2}\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around 0 95.3%
+-commutative95.3%
associate-+r+95.3%
associate-*r*95.3%
unpow295.3%
distribute-rgt-out97.6%
fma-define99.6%
fma-define99.6%
Simplified99.6%
Final simplification99.6%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (* x (+ x (* y 2.0))))
assert(x < y);
double code(double x, double y) {
return x * (x + (y * 2.0));
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * (x + (y * 2.0d0))
end function
assert x < y;
public static double code(double x, double y) {
return x * (x + (y * 2.0));
}
[x, y] = sort([x, y]) def code(x, y): return x * (x + (y * 2.0))
x, y = sort([x, y]) function code(x, y) return Float64(x * Float64(x + Float64(y * 2.0))) end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
tmp = x * (x + (y * 2.0));
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
x \cdot \left(x + y \cdot 2\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around inf 56.5%
*-commutative56.5%
associate-*l*56.5%
unpow256.5%
distribute-lft-out58.8%
Simplified58.8%
Final simplification58.8%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (* (+ y x) (+ y x)))
assert(x < y);
double code(double x, double y) {
return (y + x) * (y + x);
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y + x) * (y + x)
end function
assert x < y;
public static double code(double x, double y) {
return (y + x) * (y + x);
}
[x, y] = sort([x, y]) def code(x, y): return (y + x) * (y + x)
x, y = sort([x, y]) function code(x, y) return Float64(Float64(y + x) * Float64(y + x)) end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
tmp = (y + x) * (y + x);
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(N[(y + x), $MachinePrecision] * N[(y + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\left(y + x\right) \cdot \left(y + x\right)
\end{array}
Initial program 100.0%
Final simplification100.0%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (* 2.0 (* y x)))
assert(x < y);
double code(double x, double y) {
return 2.0 * (y * x);
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 2.0d0 * (y * x)
end function
assert x < y;
public static double code(double x, double y) {
return 2.0 * (y * x);
}
[x, y] = sort([x, y]) def code(x, y): return 2.0 * (y * x)
x, y = sort([x, y]) function code(x, y) return Float64(2.0 * Float64(y * x)) end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
tmp = 2.0 * (y * x);
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(2.0 * N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
2 \cdot \left(y \cdot x\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around inf 56.5%
*-commutative56.5%
associate-*l*56.5%
unpow256.5%
distribute-lft-out58.8%
Simplified58.8%
Taylor expanded in x around 0 17.5%
Final simplification17.5%
(FPCore (x y) :precision binary64 (+ (* x x) (+ (* y y) (* 2.0 (* y x)))))
double code(double x, double y) {
return (x * x) + ((y * y) + (2.0 * (y * x)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) + ((y * y) + (2.0d0 * (y * x)))
end function
public static double code(double x, double y) {
return (x * x) + ((y * y) + (2.0 * (y * x)));
}
def code(x, y): return (x * x) + ((y * y) + (2.0 * (y * x)))
function code(x, y) return Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(2.0 * Float64(y * x)))) end
function tmp = code(x, y) tmp = (x * x) + ((y * y) + (2.0 * (y * x))); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(2.0 * N[(y * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + \left(y \cdot y + 2 \cdot \left(y \cdot x\right)\right)
\end{array}
herbie shell --seed 2024039
(FPCore (x y)
:name "Examples.Basics.BasicTests:f3 from sbv-4.4"
:precision binary64
:herbie-target
(+ (* x x) (+ (* y y) (* 2.0 (* y x))))
(* (+ x y) (+ x y)))