
(FPCore (x y) :precision binary64 (+ (+ (* x 2.0) (* x x)) (* y y)))
double code(double x, double y) {
return ((x * 2.0) + (x * x)) + (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * 2.0d0) + (x * x)) + (y * y)
end function
public static double code(double x, double y) {
return ((x * 2.0) + (x * x)) + (y * y);
}
def code(x, y): return ((x * 2.0) + (x * x)) + (y * y)
function code(x, y) return Float64(Float64(Float64(x * 2.0) + Float64(x * x)) + Float64(y * y)) end
function tmp = code(x, y) tmp = ((x * 2.0) + (x * x)) + (y * y); end
code[x_, y_] := N[(N[(N[(x * 2.0), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot 2 + x \cdot x\right) + y \cdot y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (+ (+ (* x 2.0) (* x x)) (* y y)))
double code(double x, double y) {
return ((x * 2.0) + (x * x)) + (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * 2.0d0) + (x * x)) + (y * y)
end function
public static double code(double x, double y) {
return ((x * 2.0) + (x * x)) + (y * y);
}
def code(x, y): return ((x * 2.0) + (x * x)) + (y * y)
function code(x, y) return Float64(Float64(Float64(x * 2.0) + Float64(x * x)) + Float64(y * y)) end
function tmp = code(x, y) tmp = ((x * 2.0) + (x * x)) + (y * y); end
code[x_, y_] := N[(N[(N[(x * 2.0), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot 2 + x \cdot x\right) + y \cdot y
\end{array}
(FPCore (x y) :precision binary64 (fma x x (fma y y (* 2.0 x))))
double code(double x, double y) {
return fma(x, x, fma(y, y, (2.0 * x)));
}
function code(x, y) return fma(x, x, fma(y, y, Float64(2.0 * x))) end
code[x_, y_] := N[(x * x + N[(y * y + N[(2.0 * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, 2 \cdot x\right)\right)
\end{array}
Initial program 100.0%
lift-+.f64N/A
lift-+.f64N/A
+-commutativeN/A
associate-+l+N/A
lift-*.f64N/A
lower-fma.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
(FPCore (x y)
:precision binary64
(let* ((t_0 (+ (* x x) (* 2.0 x))))
(if (<= t_0 -1e-44)
(* y y)
(if (<= t_0 -5e-201)
(* 2.0 x)
(if (<= t_0 2000000000000.0) (* y y) (* x x))))))
double code(double x, double y) {
double t_0 = (x * x) + (2.0 * x);
double tmp;
if (t_0 <= -1e-44) {
tmp = y * y;
} else if (t_0 <= -5e-201) {
tmp = 2.0 * x;
} else if (t_0 <= 2000000000000.0) {
tmp = y * y;
} else {
tmp = x * x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: tmp
t_0 = (x * x) + (2.0d0 * x)
if (t_0 <= (-1d-44)) then
tmp = y * y
else if (t_0 <= (-5d-201)) then
tmp = 2.0d0 * x
else if (t_0 <= 2000000000000.0d0) then
tmp = y * y
else
tmp = x * x
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = (x * x) + (2.0 * x);
double tmp;
if (t_0 <= -1e-44) {
tmp = y * y;
} else if (t_0 <= -5e-201) {
tmp = 2.0 * x;
} else if (t_0 <= 2000000000000.0) {
tmp = y * y;
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): t_0 = (x * x) + (2.0 * x) tmp = 0 if t_0 <= -1e-44: tmp = y * y elif t_0 <= -5e-201: tmp = 2.0 * x elif t_0 <= 2000000000000.0: tmp = y * y else: tmp = x * x return tmp
function code(x, y) t_0 = Float64(Float64(x * x) + Float64(2.0 * x)) tmp = 0.0 if (t_0 <= -1e-44) tmp = Float64(y * y); elseif (t_0 <= -5e-201) tmp = Float64(2.0 * x); elseif (t_0 <= 2000000000000.0) tmp = Float64(y * y); else tmp = Float64(x * x); end return tmp end
function tmp_2 = code(x, y) t_0 = (x * x) + (2.0 * x); tmp = 0.0; if (t_0 <= -1e-44) tmp = y * y; elseif (t_0 <= -5e-201) tmp = 2.0 * x; elseif (t_0 <= 2000000000000.0) tmp = y * y; else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(x * x), $MachinePrecision] + N[(2.0 * x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -1e-44], N[(y * y), $MachinePrecision], If[LessEqual[t$95$0, -5e-201], N[(2.0 * x), $MachinePrecision], If[LessEqual[t$95$0, 2000000000000.0], N[(y * y), $MachinePrecision], N[(x * x), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot x + 2 \cdot x\\
\mathbf{if}\;t\_0 \leq -1 \cdot 10^{-44}:\\
\;\;\;\;y \cdot y\\
\mathbf{elif}\;t\_0 \leq -5 \cdot 10^{-201}:\\
\;\;\;\;2 \cdot x\\
\mathbf{elif}\;t\_0 \leq 2000000000000:\\
\;\;\;\;y \cdot y\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if (+.f64 (*.f64 x #s(literal 2 binary64)) (*.f64 x x)) < -9.99999999999999953e-45 or -4.9999999999999999e-201 < (+.f64 (*.f64 x #s(literal 2 binary64)) (*.f64 x x)) < 2e12Initial program 100.0%
Taylor expanded in y around inf
unpow2N/A
lower-*.f6471.9
Applied rewrites71.9%
if -9.99999999999999953e-45 < (+.f64 (*.f64 x #s(literal 2 binary64)) (*.f64 x x)) < -4.9999999999999999e-201Initial program 100.0%
Taylor expanded in y around 0
unpow2N/A
distribute-rgt-inN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
metadata-evalN/A
metadata-evalN/A
sub-negN/A
lower--.f64N/A
metadata-eval68.8
Applied rewrites68.8%
Taylor expanded in x around 0
Applied rewrites68.8%
if 2e12 < (+.f64 (*.f64 x #s(literal 2 binary64)) (*.f64 x x)) Initial program 100.0%
Taylor expanded in x around inf
unpow2N/A
lower-*.f6487.0
Applied rewrites87.0%
Final simplification78.4%
(FPCore (x y) :precision binary64 (if (<= (+ (* x x) (* 2.0 x)) 2000000000000.0) (fma 2.0 x (* y y)) (* (- x -2.0) x)))
double code(double x, double y) {
double tmp;
if (((x * x) + (2.0 * x)) <= 2000000000000.0) {
tmp = fma(2.0, x, (y * y));
} else {
tmp = (x - -2.0) * x;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (Float64(Float64(x * x) + Float64(2.0 * x)) <= 2000000000000.0) tmp = fma(2.0, x, Float64(y * y)); else tmp = Float64(Float64(x - -2.0) * x); end return tmp end
code[x_, y_] := If[LessEqual[N[(N[(x * x), $MachinePrecision] + N[(2.0 * x), $MachinePrecision]), $MachinePrecision], 2000000000000.0], N[(2.0 * x + N[(y * y), $MachinePrecision]), $MachinePrecision], N[(N[(x - -2.0), $MachinePrecision] * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x + 2 \cdot x \leq 2000000000000:\\
\;\;\;\;\mathsf{fma}\left(2, x, y \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;\left(x - -2\right) \cdot x\\
\end{array}
\end{array}
if (+.f64 (*.f64 x #s(literal 2 binary64)) (*.f64 x x)) < 2e12Initial program 100.0%
Taylor expanded in x around 0
lower-fma.f64N/A
unpow2N/A
lower-*.f6498.7
Applied rewrites98.7%
if 2e12 < (+.f64 (*.f64 x #s(literal 2 binary64)) (*.f64 x x)) Initial program 100.0%
Taylor expanded in y around 0
unpow2N/A
distribute-rgt-inN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
metadata-evalN/A
metadata-evalN/A
sub-negN/A
lower--.f64N/A
metadata-eval87.2
Applied rewrites87.2%
Final simplification93.4%
(FPCore (x y) :precision binary64 (if (<= (+ (* x x) (* 2.0 x)) 2000000000000.0) (* y y) (* x x)))
double code(double x, double y) {
double tmp;
if (((x * x) + (2.0 * x)) <= 2000000000000.0) {
tmp = y * y;
} else {
tmp = x * x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (((x * x) + (2.0d0 * x)) <= 2000000000000.0d0) then
tmp = y * y
else
tmp = x * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (((x * x) + (2.0 * x)) <= 2000000000000.0) {
tmp = y * y;
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): tmp = 0 if ((x * x) + (2.0 * x)) <= 2000000000000.0: tmp = y * y else: tmp = x * x return tmp
function code(x, y) tmp = 0.0 if (Float64(Float64(x * x) + Float64(2.0 * x)) <= 2000000000000.0) tmp = Float64(y * y); else tmp = Float64(x * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (((x * x) + (2.0 * x)) <= 2000000000000.0) tmp = y * y; else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(N[(x * x), $MachinePrecision] + N[(2.0 * x), $MachinePrecision]), $MachinePrecision], 2000000000000.0], N[(y * y), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x + 2 \cdot x \leq 2000000000000:\\
\;\;\;\;y \cdot y\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if (+.f64 (*.f64 x #s(literal 2 binary64)) (*.f64 x x)) < 2e12Initial program 100.0%
Taylor expanded in y around inf
unpow2N/A
lower-*.f6463.2
Applied rewrites63.2%
if 2e12 < (+.f64 (*.f64 x #s(literal 2 binary64)) (*.f64 x x)) Initial program 100.0%
Taylor expanded in x around inf
unpow2N/A
lower-*.f6487.0
Applied rewrites87.0%
Final simplification74.0%
(FPCore (x y) :precision binary64 (if (<= (* y y) 4.5e+27) (* (- x -2.0) x) (* y y)))
double code(double x, double y) {
double tmp;
if ((y * y) <= 4.5e+27) {
tmp = (x - -2.0) * x;
} else {
tmp = y * y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y * y) <= 4.5d+27) then
tmp = (x - (-2.0d0)) * x
else
tmp = y * y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 4.5e+27) {
tmp = (x - -2.0) * x;
} else {
tmp = y * y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 4.5e+27: tmp = (x - -2.0) * x else: tmp = y * y return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 4.5e+27) tmp = Float64(Float64(x - -2.0) * x); else tmp = Float64(y * y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 4.5e+27) tmp = (x - -2.0) * x; else tmp = y * y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 4.5e+27], N[(N[(x - -2.0), $MachinePrecision] * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 4.5 \cdot 10^{+27}:\\
\;\;\;\;\left(x - -2\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if (*.f64 y y) < 4.4999999999999999e27Initial program 100.0%
Taylor expanded in y around 0
unpow2N/A
distribute-rgt-inN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
metadata-evalN/A
metadata-evalN/A
sub-negN/A
lower--.f64N/A
metadata-eval87.4
Applied rewrites87.4%
if 4.4999999999999999e27 < (*.f64 y y) Initial program 100.0%
Taylor expanded in y around inf
unpow2N/A
lower-*.f6486.6
Applied rewrites86.6%
(FPCore (x y) :precision binary64 (* x x))
double code(double x, double y) {
return x * x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * x
end function
public static double code(double x, double y) {
return x * x;
}
def code(x, y): return x * x
function code(x, y) return Float64(x * x) end
function tmp = code(x, y) tmp = x * x; end
code[x_, y_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x
\end{array}
Initial program 100.0%
Taylor expanded in x around inf
unpow2N/A
lower-*.f6442.0
Applied rewrites42.0%
(FPCore (x y) :precision binary64 (+ (* y y) (+ (* 2.0 x) (* x x))))
double code(double x, double y) {
return (y * y) + ((2.0 * x) + (x * x));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y * y) + ((2.0d0 * x) + (x * x))
end function
public static double code(double x, double y) {
return (y * y) + ((2.0 * x) + (x * x));
}
def code(x, y): return (y * y) + ((2.0 * x) + (x * x))
function code(x, y) return Float64(Float64(y * y) + Float64(Float64(2.0 * x) + Float64(x * x))) end
function tmp = code(x, y) tmp = (y * y) + ((2.0 * x) + (x * x)); end
code[x_, y_] := N[(N[(y * y), $MachinePrecision] + N[(N[(2.0 * x), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y \cdot y + \left(2 \cdot x + x \cdot x\right)
\end{array}
herbie shell --seed 2024276
(FPCore (x y)
:name "Numeric.Log:$clog1p from log-domain-0.10.2.1, A"
:precision binary64
:alt
(! :herbie-platform default (+ (* y y) (+ (* 2 x) (* x x))))
(+ (+ (* x 2.0) (* x x)) (* y y)))