
(FPCore (x y) :precision binary64 (+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))
double code(double x, double y) {
return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (((x * x) + (y * y)) + (y * y)) + (y * y)
end function
public static double code(double x, double y) {
return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
def code(x, y): return (((x * x) + (y * y)) + (y * y)) + (y * y)
function code(x, y) return Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(y * y)) + Float64(y * y)) end
function tmp = code(x, y) tmp = (((x * x) + (y * y)) + (y * y)) + (y * y); end
code[x_, y_] := N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))
double code(double x, double y) {
return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (((x * x) + (y * y)) + (y * y)) + (y * y)
end function
public static double code(double x, double y) {
return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
def code(x, y): return (((x * x) + (y * y)) + (y * y)) + (y * y)
function code(x, y) return Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(y * y)) + Float64(y * y)) end
function tmp = code(x, y) tmp = (((x * x) + (y * y)) + (y * y)) + (y * y); end
code[x_, y_] := N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y
\end{array}
(FPCore (x y) :precision binary64 (+ (* x x) (fma (* y 2.0) y (* y y))))
double code(double x, double y) {
return (x * x) + fma((y * 2.0), y, (y * y));
}
function code(x, y) return Float64(Float64(x * x) + fma(Float64(y * 2.0), y, Float64(y * y))) end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(N[(y * 2.0), $MachinePrecision] * y + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + \mathsf{fma}\left(y \cdot 2, y, y \cdot y\right)
\end{array}
Initial program 99.8%
Taylor expanded in x around 0 99.8%
Simplified99.9%
associate-*r*99.8%
metadata-eval99.8%
distribute-rgt-out99.8%
associate-*r*99.8%
*-un-lft-identity99.8%
fma-def99.9%
*-commutative99.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x y)
:precision binary64
(if (or (<= (* x x) 1.55e-193)
(and (not (<= (* x x) 3.2e-155)) (<= (* x x) 9e-57)))
(* y (* y 3.0))
(+ (* x x) (* y y))))
double code(double x, double y) {
double tmp;
if (((x * x) <= 1.55e-193) || (!((x * x) <= 3.2e-155) && ((x * x) <= 9e-57))) {
tmp = y * (y * 3.0);
} else {
tmp = (x * x) + (y * y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (((x * x) <= 1.55d-193) .or. (.not. ((x * x) <= 3.2d-155)) .and. ((x * x) <= 9d-57)) then
tmp = y * (y * 3.0d0)
else
tmp = (x * x) + (y * y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (((x * x) <= 1.55e-193) || (!((x * x) <= 3.2e-155) && ((x * x) <= 9e-57))) {
tmp = y * (y * 3.0);
} else {
tmp = (x * x) + (y * y);
}
return tmp;
}
def code(x, y): tmp = 0 if ((x * x) <= 1.55e-193) or (not ((x * x) <= 3.2e-155) and ((x * x) <= 9e-57)): tmp = y * (y * 3.0) else: tmp = (x * x) + (y * y) return tmp
function code(x, y) tmp = 0.0 if ((Float64(x * x) <= 1.55e-193) || (!(Float64(x * x) <= 3.2e-155) && (Float64(x * x) <= 9e-57))) tmp = Float64(y * Float64(y * 3.0)); else tmp = Float64(Float64(x * x) + Float64(y * y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (((x * x) <= 1.55e-193) || (~(((x * x) <= 3.2e-155)) && ((x * x) <= 9e-57))) tmp = y * (y * 3.0); else tmp = (x * x) + (y * y); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[N[(x * x), $MachinePrecision], 1.55e-193], And[N[Not[LessEqual[N[(x * x), $MachinePrecision], 3.2e-155]], $MachinePrecision], LessEqual[N[(x * x), $MachinePrecision], 9e-57]]], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision], N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 1.55 \cdot 10^{-193} \lor \neg \left(x \cdot x \leq 3.2 \cdot 10^{-155}\right) \land x \cdot x \leq 9 \cdot 10^{-57}:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot x + y \cdot y\\
\end{array}
\end{array}
if (*.f64 x x) < 1.5500000000000001e-193 or 3.20000000000000013e-155 < (*.f64 x x) < 8.99999999999999945e-57Initial program 99.7%
Taylor expanded in x around 0 93.6%
Simplified93.6%
if 1.5500000000000001e-193 < (*.f64 x x) < 3.20000000000000013e-155 or 8.99999999999999945e-57 < (*.f64 x x) Initial program 99.9%
add-sqr-sqrt99.9%
fma-def99.9%
+-commutative99.9%
add-sqr-sqrt99.9%
hypot-def100.0%
hypot-def100.0%
+-commutative100.0%
add-sqr-sqrt100.0%
hypot-def99.9%
hypot-def99.9%
Applied egg-rr99.9%
Simplified99.9%
Taylor expanded in y around 0 91.3%
Simplified91.3%
Final simplification92.3%
(FPCore (x y) :precision binary64 (if (or (<= (* y y) 2e-155) (and (not (<= (* y y) 2e-96)) (<= (* y y) 1e+58))) (* x x) (* y (* y 3.0))))
double code(double x, double y) {
double tmp;
if (((y * y) <= 2e-155) || (!((y * y) <= 2e-96) && ((y * y) <= 1e+58))) {
tmp = x * x;
} else {
tmp = y * (y * 3.0);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (((y * y) <= 2d-155) .or. (.not. ((y * y) <= 2d-96)) .and. ((y * y) <= 1d+58)) then
tmp = x * x
else
tmp = y * (y * 3.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (((y * y) <= 2e-155) || (!((y * y) <= 2e-96) && ((y * y) <= 1e+58))) {
tmp = x * x;
} else {
tmp = y * (y * 3.0);
}
return tmp;
}
def code(x, y): tmp = 0 if ((y * y) <= 2e-155) or (not ((y * y) <= 2e-96) and ((y * y) <= 1e+58)): tmp = x * x else: tmp = y * (y * 3.0) return tmp
function code(x, y) tmp = 0.0 if ((Float64(y * y) <= 2e-155) || (!(Float64(y * y) <= 2e-96) && (Float64(y * y) <= 1e+58))) tmp = Float64(x * x); else tmp = Float64(y * Float64(y * 3.0)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (((y * y) <= 2e-155) || (~(((y * y) <= 2e-96)) && ((y * y) <= 1e+58))) tmp = x * x; else tmp = y * (y * 3.0); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[N[(y * y), $MachinePrecision], 2e-155], And[N[Not[LessEqual[N[(y * y), $MachinePrecision], 2e-96]], $MachinePrecision], LessEqual[N[(y * y), $MachinePrecision], 1e+58]]], N[(x * x), $MachinePrecision], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 2 \cdot 10^{-155} \lor \neg \left(y \cdot y \leq 2 \cdot 10^{-96}\right) \land y \cdot y \leq 10^{+58}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 2.00000000000000003e-155 or 1.9999999999999998e-96 < (*.f64 y y) < 9.99999999999999944e57Initial program 99.9%
Taylor expanded in x around inf 83.7%
Simplified83.7%
if 2.00000000000000003e-155 < (*.f64 y y) < 1.9999999999999998e-96 or 9.99999999999999944e57 < (*.f64 y y) Initial program 99.8%
Taylor expanded in x around 0 89.2%
Simplified89.3%
Final simplification86.5%
(FPCore (x y)
:precision binary64
(if (<= (* y y) 2e-155)
(* x x)
(if (<= (* y y) 2e-96)
(* (* y y) 3.0)
(if (<= (* y y) 1e+58) (* x x) (* y (* y 3.0))))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 2e-155) {
tmp = x * x;
} else if ((y * y) <= 2e-96) {
tmp = (y * y) * 3.0;
} else if ((y * y) <= 1e+58) {
tmp = x * x;
} else {
tmp = y * (y * 3.0);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y * y) <= 2d-155) then
tmp = x * x
else if ((y * y) <= 2d-96) then
tmp = (y * y) * 3.0d0
else if ((y * y) <= 1d+58) then
tmp = x * x
else
tmp = y * (y * 3.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 2e-155) {
tmp = x * x;
} else if ((y * y) <= 2e-96) {
tmp = (y * y) * 3.0;
} else if ((y * y) <= 1e+58) {
tmp = x * x;
} else {
tmp = y * (y * 3.0);
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 2e-155: tmp = x * x elif (y * y) <= 2e-96: tmp = (y * y) * 3.0 elif (y * y) <= 1e+58: tmp = x * x else: tmp = y * (y * 3.0) return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 2e-155) tmp = Float64(x * x); elseif (Float64(y * y) <= 2e-96) tmp = Float64(Float64(y * y) * 3.0); elseif (Float64(y * y) <= 1e+58) tmp = Float64(x * x); else tmp = Float64(y * Float64(y * 3.0)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 2e-155) tmp = x * x; elseif ((y * y) <= 2e-96) tmp = (y * y) * 3.0; elseif ((y * y) <= 1e+58) tmp = x * x; else tmp = y * (y * 3.0); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 2e-155], N[(x * x), $MachinePrecision], If[LessEqual[N[(y * y), $MachinePrecision], 2e-96], N[(N[(y * y), $MachinePrecision] * 3.0), $MachinePrecision], If[LessEqual[N[(y * y), $MachinePrecision], 1e+58], N[(x * x), $MachinePrecision], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 2 \cdot 10^{-155}:\\
\;\;\;\;x \cdot x\\
\mathbf{elif}\;y \cdot y \leq 2 \cdot 10^{-96}:\\
\;\;\;\;\left(y \cdot y\right) \cdot 3\\
\mathbf{elif}\;y \cdot y \leq 10^{+58}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 2.00000000000000003e-155 or 1.9999999999999998e-96 < (*.f64 y y) < 9.99999999999999944e57Initial program 99.9%
Taylor expanded in x around inf 83.7%
Simplified83.7%
if 2.00000000000000003e-155 < (*.f64 y y) < 1.9999999999999998e-96Initial program 100.0%
Taylor expanded in x around 0 83.0%
Simplified83.0%
*-un-lft-identity83.0%
distribute-rgt-out83.0%
metadata-eval83.0%
Applied egg-rr83.0%
if 9.99999999999999944e57 < (*.f64 y y) Initial program 99.8%
Taylor expanded in x around 0 89.6%
Simplified89.7%
Final simplification86.5%
(FPCore (x y) :precision binary64 (+ (* x x) (* y (* y 3.0))))
double code(double x, double y) {
return (x * x) + (y * (y * 3.0));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) + (y * (y * 3.0d0))
end function
public static double code(double x, double y) {
return (x * x) + (y * (y * 3.0));
}
def code(x, y): return (x * x) + (y * (y * 3.0))
function code(x, y) return Float64(Float64(x * x) + Float64(y * Float64(y * 3.0))) end
function tmp = code(x, y) tmp = (x * x) + (y * (y * 3.0)); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + y \cdot \left(y \cdot 3\right)
\end{array}
Initial program 99.8%
Taylor expanded in x around 0 99.8%
Simplified99.9%
Final simplification99.9%
(FPCore (x y) :precision binary64 (if (<= (* y y) 1.8e+294) (* x x) (* y y)))
double code(double x, double y) {
double tmp;
if ((y * y) <= 1.8e+294) {
tmp = x * 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) <= 1.8d+294) then
tmp = x * x
else
tmp = y * y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 1.8e+294) {
tmp = x * x;
} else {
tmp = y * y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 1.8e+294: tmp = x * x else: tmp = y * y return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 1.8e+294) tmp = Float64(x * x); else tmp = Float64(y * y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 1.8e+294) tmp = x * x; else tmp = y * y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 1.8e+294], N[(x * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 1.8 \cdot 10^{+294}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if (*.f64 y y) < 1.79999999999999986e294Initial program 99.8%
Taylor expanded in x around inf 64.4%
Simplified64.4%
if 1.79999999999999986e294 < (*.f64 y y) Initial program 100.0%
add-sqr-sqrt100.0%
fma-def100.0%
+-commutative100.0%
add-sqr-sqrt100.0%
hypot-def100.0%
hypot-def100.0%
+-commutative100.0%
add-sqr-sqrt100.0%
hypot-def100.0%
hypot-def100.0%
Applied egg-rr100.0%
Simplified100.0%
Taylor expanded in y around 0 96.3%
Simplified96.3%
Taylor expanded in x around 0 96.3%
Simplified96.3%
Final simplification72.7%
(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 99.8%
Taylor expanded in x around inf 52.9%
Simplified52.9%
Final simplification52.9%
(FPCore (x y) :precision binary64 -3.0)
double code(double x, double y) {
return -3.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = -3.0d0
end function
public static double code(double x, double y) {
return -3.0;
}
def code(x, y): return -3.0
function code(x, y) return -3.0 end
function tmp = code(x, y) tmp = -3.0; end
code[x_, y_] := -3.0
\begin{array}{l}
\\
-3
\end{array}
Initial program 99.8%
Taylor expanded in x around 0 59.8%
Simplified59.8%
distribute-lft1-in59.8%
metadata-eval59.8%
associate-*r*59.8%
*-commutative59.8%
add-sqr-sqrt28.0%
associate-*r*28.0%
Applied egg-rr28.0%
Taylor expanded in y around -inf 0.0%
Simplified1.2%
Final simplification1.2%
(FPCore (x y) :precision binary64 (+ (* x x) (* y (+ y (+ y y)))))
double code(double x, double y) {
return (x * x) + (y * (y + (y + y)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) + (y * (y + (y + y)))
end function
public static double code(double x, double y) {
return (x * x) + (y * (y + (y + y)));
}
def code(x, y): return (x * x) + (y * (y + (y + y)))
function code(x, y) return Float64(Float64(x * x) + Float64(y * Float64(y + Float64(y + y)))) end
function tmp = code(x, y) tmp = (x * x) + (y * (y + (y + y))); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(y * N[(y + N[(y + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + y \cdot \left(y + \left(y + y\right)\right)
\end{array}
herbie shell --seed 2023240
(FPCore (x y)
:name "Linear.Quaternion:$c/ from linear-1.19.1.3, E"
:precision binary64
:herbie-target
(+ (* x x) (* y (+ y (+ y y))))
(+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))