
(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 10 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 (fma y y (fma x x (* y (+ y y)))))
double code(double x, double y) {
return fma(y, y, fma(x, x, (y * (y + y))));
}
function code(x, y) return fma(y, y, fma(x, x, Float64(y * Float64(y + y)))) end
code[x_, y_] := N[(y * y + N[(x * x + N[(y * N[(y + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, y \cdot \left(y + y\right)\right)\right)
\end{array}
Initial program 99.9%
+-commutative99.9%
fma-def99.9%
associate-+l+100.0%
fma-def100.0%
distribute-lft-out100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (fma x x (* (* y y) 3.0)))
double code(double x, double y) {
return fma(x, x, ((y * y) * 3.0));
}
function code(x, y) return fma(x, x, Float64(Float64(y * y) * 3.0)) end
code[x_, y_] := N[(x * x + N[(N[(y * y), $MachinePrecision] * 3.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot 3\right)
\end{array}
Initial program 99.9%
associate-+l+99.9%
associate-+l+99.9%
fma-def99.9%
count-299.9%
distribute-rgt1-in99.9%
*-commutative99.9%
metadata-eval99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x y)
:precision binary64
(if (or (<= (* x x) 7.8e-169)
(and (not (<= (* x x) 2.35e-24)) (<= (* x x) 2.7e+23)))
(* y (* y 3.0))
(* x x)))
double code(double x, double y) {
double tmp;
if (((x * x) <= 7.8e-169) || (!((x * x) <= 2.35e-24) && ((x * x) <= 2.7e+23))) {
tmp = y * (y * 3.0);
} 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) <= 7.8d-169) .or. (.not. ((x * x) <= 2.35d-24)) .and. ((x * x) <= 2.7d+23)) then
tmp = y * (y * 3.0d0)
else
tmp = x * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (((x * x) <= 7.8e-169) || (!((x * x) <= 2.35e-24) && ((x * x) <= 2.7e+23))) {
tmp = y * (y * 3.0);
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): tmp = 0 if ((x * x) <= 7.8e-169) or (not ((x * x) <= 2.35e-24) and ((x * x) <= 2.7e+23)): tmp = y * (y * 3.0) else: tmp = x * x return tmp
function code(x, y) tmp = 0.0 if ((Float64(x * x) <= 7.8e-169) || (!(Float64(x * x) <= 2.35e-24) && (Float64(x * x) <= 2.7e+23))) tmp = Float64(y * Float64(y * 3.0)); else tmp = Float64(x * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (((x * x) <= 7.8e-169) || (~(((x * x) <= 2.35e-24)) && ((x * x) <= 2.7e+23))) tmp = y * (y * 3.0); else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[N[(x * x), $MachinePrecision], 7.8e-169], And[N[Not[LessEqual[N[(x * x), $MachinePrecision], 2.35e-24]], $MachinePrecision], LessEqual[N[(x * x), $MachinePrecision], 2.7e+23]]], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 7.8 \cdot 10^{-169} \lor \neg \left(x \cdot x \leq 2.35 \cdot 10^{-24}\right) \land x \cdot x \leq 2.7 \cdot 10^{+23}:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if (*.f64 x x) < 7.79999999999999954e-169 or 2.34999999999999996e-24 < (*.f64 x x) < 2.6999999999999999e23Initial program 99.8%
Taylor expanded in x around 0 87.7%
unpow287.7%
unpow287.7%
distribute-rgt1-in87.7%
metadata-eval87.7%
*-commutative87.7%
associate-*r*87.6%
Simplified87.6%
if 7.79999999999999954e-169 < (*.f64 x x) < 2.34999999999999996e-24 or 2.6999999999999999e23 < (*.f64 x x) Initial program 100.0%
Taylor expanded in x around inf 83.3%
unpow283.3%
Simplified83.3%
Final simplification85.1%
(FPCore (x y)
:precision binary64
(if (or (<= (* x x) 1.45e-169)
(and (not (<= (* x x) 2.35e-24)) (<= (* x x) 5.7e+23)))
(* (* y y) 3.0)
(* x x)))
double code(double x, double y) {
double tmp;
if (((x * x) <= 1.45e-169) || (!((x * x) <= 2.35e-24) && ((x * x) <= 5.7e+23))) {
tmp = (y * y) * 3.0;
} 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) <= 1.45d-169) .or. (.not. ((x * x) <= 2.35d-24)) .and. ((x * x) <= 5.7d+23)) then
tmp = (y * y) * 3.0d0
else
tmp = x * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (((x * x) <= 1.45e-169) || (!((x * x) <= 2.35e-24) && ((x * x) <= 5.7e+23))) {
tmp = (y * y) * 3.0;
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): tmp = 0 if ((x * x) <= 1.45e-169) or (not ((x * x) <= 2.35e-24) and ((x * x) <= 5.7e+23)): tmp = (y * y) * 3.0 else: tmp = x * x return tmp
function code(x, y) tmp = 0.0 if ((Float64(x * x) <= 1.45e-169) || (!(Float64(x * x) <= 2.35e-24) && (Float64(x * x) <= 5.7e+23))) tmp = Float64(Float64(y * y) * 3.0); else tmp = Float64(x * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (((x * x) <= 1.45e-169) || (~(((x * x) <= 2.35e-24)) && ((x * x) <= 5.7e+23))) tmp = (y * y) * 3.0; else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[N[(x * x), $MachinePrecision], 1.45e-169], And[N[Not[LessEqual[N[(x * x), $MachinePrecision], 2.35e-24]], $MachinePrecision], LessEqual[N[(x * x), $MachinePrecision], 5.7e+23]]], N[(N[(y * y), $MachinePrecision] * 3.0), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 1.45 \cdot 10^{-169} \lor \neg \left(x \cdot x \leq 2.35 \cdot 10^{-24}\right) \land x \cdot x \leq 5.7 \cdot 10^{+23}:\\
\;\;\;\;\left(y \cdot y\right) \cdot 3\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if (*.f64 x x) < 1.4500000000000001e-169 or 2.34999999999999996e-24 < (*.f64 x x) < 5.7e23Initial program 99.8%
Taylor expanded in x around 0 87.7%
unpow287.7%
Simplified87.7%
count-287.7%
metadata-eval87.7%
*-un-lft-identity87.7%
distribute-rgt-out87.7%
metadata-eval87.7%
metadata-eval87.7%
Applied egg-rr87.7%
if 1.4500000000000001e-169 < (*.f64 x x) < 2.34999999999999996e-24 or 5.7e23 < (*.f64 x x) Initial program 100.0%
Taylor expanded in x around inf 83.3%
unpow283.3%
Simplified83.3%
Final simplification85.1%
(FPCore (x y) :precision binary64 (+ (* y y) (+ (* y y) (+ (* y y) (* x x)))))
double code(double x, double y) {
return (y * y) + ((y * y) + ((y * y) + (x * x)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y * y) + ((y * y) + ((y * y) + (x * x)))
end function
public static double code(double x, double y) {
return (y * y) + ((y * y) + ((y * y) + (x * x)));
}
def code(x, y): return (y * y) + ((y * y) + ((y * y) + (x * x)))
function code(x, y) return Float64(Float64(y * y) + Float64(Float64(y * y) + Float64(Float64(y * y) + Float64(x * x)))) end
function tmp = code(x, y) tmp = (y * y) + ((y * y) + ((y * y) + (x * x))); end
code[x_, y_] := N[(N[(y * y), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y \cdot y + \left(y \cdot y + \left(y \cdot y + x \cdot x\right)\right)
\end{array}
Initial program 99.9%
Final simplification99.9%
(FPCore (x y) :precision binary64 (if (<= (* x x) 4.2e-171) (* (* y y) 3.0) (+ (* y y) (* x x))))
double code(double x, double y) {
double tmp;
if ((x * x) <= 4.2e-171) {
tmp = (y * y) * 3.0;
} else {
tmp = (y * y) + (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) <= 4.2d-171) then
tmp = (y * y) * 3.0d0
else
tmp = (y * y) + (x * x)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x * x) <= 4.2e-171) {
tmp = (y * y) * 3.0;
} else {
tmp = (y * y) + (x * x);
}
return tmp;
}
def code(x, y): tmp = 0 if (x * x) <= 4.2e-171: tmp = (y * y) * 3.0 else: tmp = (y * y) + (x * x) return tmp
function code(x, y) tmp = 0.0 if (Float64(x * x) <= 4.2e-171) tmp = Float64(Float64(y * y) * 3.0); else tmp = Float64(Float64(y * y) + Float64(x * x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x * x) <= 4.2e-171) tmp = (y * y) * 3.0; else tmp = (y * y) + (x * x); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 4.2e-171], N[(N[(y * y), $MachinePrecision] * 3.0), $MachinePrecision], N[(N[(y * y), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 4.2 \cdot 10^{-171}:\\
\;\;\;\;\left(y \cdot y\right) \cdot 3\\
\mathbf{else}:\\
\;\;\;\;y \cdot y + x \cdot x\\
\end{array}
\end{array}
if (*.f64 x x) < 4.2e-171Initial program 99.8%
Taylor expanded in x around 0 88.2%
unpow288.2%
Simplified88.2%
count-288.2%
metadata-eval88.2%
*-un-lft-identity88.2%
distribute-rgt-out88.2%
metadata-eval88.2%
metadata-eval88.2%
Applied egg-rr88.2%
if 4.2e-171 < (*.f64 x x) Initial program 99.9%
associate-+l+100.0%
associate-+l+100.0%
fma-def100.0%
count-2100.0%
distribute-rgt1-in100.0%
*-commutative100.0%
metadata-eval100.0%
Simplified100.0%
fma-udef100.0%
+-commutative100.0%
associate-*l*99.9%
Applied egg-rr99.9%
associate-*r*32.4%
metadata-eval32.4%
metadata-eval32.4%
distribute-rgt-out32.4%
metadata-eval32.4%
count-232.4%
*-un-lft-identity32.4%
flip-+0.0%
distribute-lft-out--0.0%
+-inverses0.0%
metadata-eval0.0%
distribute-rgt-out--0.0%
*-un-lft-identity0.0%
*-un-lft-identity0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified90.9%
Final simplification90.0%
(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.9%
associate-+l+99.9%
associate-+l+99.9%
fma-def99.9%
count-299.9%
distribute-rgt1-in99.9%
*-commutative99.9%
metadata-eval99.9%
Simplified99.9%
fma-udef99.9%
+-commutative99.9%
associate-*l*99.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x y) :precision binary64 (if (<= (* y y) 9.5e+305) (* x x) (* y y)))
double code(double x, double y) {
double tmp;
if ((y * y) <= 9.5e+305) {
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) <= 9.5d+305) 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) <= 9.5e+305) {
tmp = x * x;
} else {
tmp = y * y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 9.5e+305: tmp = x * x else: tmp = y * y return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 9.5e+305) 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) <= 9.5e+305) tmp = x * x; else tmp = y * y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 9.5e+305], N[(x * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 9.5 \cdot 10^{+305}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if (*.f64 y y) < 9.49999999999999962e305Initial program 99.9%
Taylor expanded in x around inf 68.8%
unpow268.8%
Simplified68.8%
if 9.49999999999999962e305 < (*.f64 y y) Initial program 100.0%
Taylor expanded in x around 0 100.0%
unpow2100.0%
unpow2100.0%
distribute-rgt1-in100.0%
metadata-eval100.0%
*-commutative100.0%
associate-*r*100.0%
Simplified100.0%
associate-*r*100.0%
metadata-eval100.0%
metadata-eval100.0%
distribute-rgt-out100.0%
metadata-eval100.0%
count-2100.0%
*-un-lft-identity100.0%
flip-+0.0%
distribute-lft-out--0.0%
+-inverses0.0%
metadata-eval0.0%
distribute-rgt-out--0.0%
*-un-lft-identity0.0%
*-un-lft-identity0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified100.0%
Final simplification75.0%
(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.9%
Taylor expanded in x around inf 61.1%
unpow261.1%
Simplified61.1%
Final simplification61.1%
(FPCore (x y) :precision binary64 -2.0)
double code(double x, double y) {
return -2.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = -2.0d0
end function
public static double code(double x, double y) {
return -2.0;
}
def code(x, y): return -2.0
function code(x, y) return -2.0 end
function tmp = code(x, y) tmp = -2.0; end
code[x_, y_] := -2.0
\begin{array}{l}
\\
-2
\end{array}
Initial program 99.9%
Taylor expanded in x around 0 52.4%
unpow252.4%
unpow252.4%
distribute-rgt1-in52.4%
metadata-eval52.4%
*-commutative52.4%
associate-*r*52.4%
Simplified52.4%
Taylor expanded in y around 0 52.4%
Simplified1.3%
Final simplification1.3%
(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 2023252
(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)))