
(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 (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.8%
+-commutative99.8%
fma-def99.9%
associate-+l+99.9%
fma-def99.9%
distribute-lft-out99.9%
Simplified99.9%
Final simplification99.9%
(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.8%
associate-+l+99.9%
associate-+l+99.9%
fma-def99.9%
cancel-sign-sub99.9%
count-299.9%
neg-mul-199.9%
associate-*l*99.9%
distribute-rgt-out--99.9%
metadata-eval99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x y)
:precision binary64
(if (or (<= (* x x) 1e-136)
(and (not (<= (* x x) 1e-50)) (<= (* x x) 100000000.0)))
(* y (* y 3.0))
(* x x)))
double code(double x, double y) {
double tmp;
if (((x * x) <= 1e-136) || (!((x * x) <= 1e-50) && ((x * x) <= 100000000.0))) {
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) <= 1d-136) .or. (.not. ((x * x) <= 1d-50)) .and. ((x * x) <= 100000000.0d0)) 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) <= 1e-136) || (!((x * x) <= 1e-50) && ((x * x) <= 100000000.0))) {
tmp = y * (y * 3.0);
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): tmp = 0 if ((x * x) <= 1e-136) or (not ((x * x) <= 1e-50) and ((x * x) <= 100000000.0)): tmp = y * (y * 3.0) else: tmp = x * x return tmp
function code(x, y) tmp = 0.0 if ((Float64(x * x) <= 1e-136) || (!(Float64(x * x) <= 1e-50) && (Float64(x * x) <= 100000000.0))) 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) <= 1e-136) || (~(((x * x) <= 1e-50)) && ((x * x) <= 100000000.0))) tmp = y * (y * 3.0); else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[N[(x * x), $MachinePrecision], 1e-136], And[N[Not[LessEqual[N[(x * x), $MachinePrecision], 1e-50]], $MachinePrecision], LessEqual[N[(x * x), $MachinePrecision], 100000000.0]]], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 10^{-136} \lor \neg \left(x \cdot x \leq 10^{-50}\right) \land x \cdot x \leq 100000000:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if (*.f64 x x) < 1e-136 or 1.00000000000000001e-50 < (*.f64 x x) < 1e8Initial program 99.7%
Taylor expanded in x around 0 88.3%
unpow288.3%
unpow288.3%
distribute-lft1-in88.3%
metadata-eval88.3%
*-commutative88.3%
associate-*l*88.4%
Simplified88.4%
if 1e-136 < (*.f64 x x) < 1.00000000000000001e-50 or 1e8 < (*.f64 x x) Initial program 100.0%
Taylor expanded in x around inf 85.8%
unpow285.8%
Simplified85.8%
Final simplification87.0%
(FPCore (x y)
:precision binary64
(if (<= (* x x) 1e-136)
(* y (* y 3.0))
(if (<= (* x x) 1e-50)
(* x x)
(if (<= (* x x) 100000000.0) (* (* y y) 3.0) (* x x)))))
double code(double x, double y) {
double tmp;
if ((x * x) <= 1e-136) {
tmp = y * (y * 3.0);
} else if ((x * x) <= 1e-50) {
tmp = x * x;
} else if ((x * x) <= 100000000.0) {
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) <= 1d-136) then
tmp = y * (y * 3.0d0)
else if ((x * x) <= 1d-50) then
tmp = x * x
else if ((x * x) <= 100000000.0d0) 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) <= 1e-136) {
tmp = y * (y * 3.0);
} else if ((x * x) <= 1e-50) {
tmp = x * x;
} else if ((x * x) <= 100000000.0) {
tmp = (y * y) * 3.0;
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): tmp = 0 if (x * x) <= 1e-136: tmp = y * (y * 3.0) elif (x * x) <= 1e-50: tmp = x * x elif (x * x) <= 100000000.0: tmp = (y * y) * 3.0 else: tmp = x * x return tmp
function code(x, y) tmp = 0.0 if (Float64(x * x) <= 1e-136) tmp = Float64(y * Float64(y * 3.0)); elseif (Float64(x * x) <= 1e-50) tmp = Float64(x * x); elseif (Float64(x * x) <= 100000000.0) 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) <= 1e-136) tmp = y * (y * 3.0); elseif ((x * x) <= 1e-50) tmp = x * x; elseif ((x * x) <= 100000000.0) tmp = (y * y) * 3.0; else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 1e-136], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * x), $MachinePrecision], 1e-50], N[(x * x), $MachinePrecision], If[LessEqual[N[(x * x), $MachinePrecision], 100000000.0], N[(N[(y * y), $MachinePrecision] * 3.0), $MachinePrecision], N[(x * x), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 10^{-136}:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\mathbf{elif}\;x \cdot x \leq 10^{-50}:\\
\;\;\;\;x \cdot x\\
\mathbf{elif}\;x \cdot x \leq 100000000:\\
\;\;\;\;\left(y \cdot y\right) \cdot 3\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if (*.f64 x x) < 1e-136Initial program 99.7%
Taylor expanded in x around 0 89.4%
unpow289.4%
unpow289.4%
distribute-lft1-in89.4%
metadata-eval89.4%
*-commutative89.4%
associate-*l*89.5%
Simplified89.5%
if 1e-136 < (*.f64 x x) < 1.00000000000000001e-50 or 1e8 < (*.f64 x x) Initial program 100.0%
Taylor expanded in x around inf 85.8%
unpow285.8%
Simplified85.8%
if 1.00000000000000001e-50 < (*.f64 x x) < 1e8Initial program 99.9%
add-sqr-sqrt_binary6499.7%
Applied rewrite-once99.7%
Taylor expanded in x around 0 80.5%
distribute-lft1-in80.5%
metadata-eval80.5%
*-commutative80.5%
unpow280.5%
Simplified80.5%
Final simplification87.0%
(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.8%
Final simplification99.8%
(FPCore (x y) :precision binary64 (if (<= x 1.35e-144) 0.0 x))
double code(double x, double y) {
double tmp;
if (x <= 1.35e-144) {
tmp = 0.0;
} else {
tmp = x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= 1.35d-144) then
tmp = 0.0d0
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 1.35e-144) {
tmp = 0.0;
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 1.35e-144: tmp = 0.0 else: tmp = x return tmp
function code(x, y) tmp = 0.0 if (x <= 1.35e-144) tmp = 0.0; else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 1.35e-144) tmp = 0.0; else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 1.35e-144], 0.0, x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.35 \cdot 10^{-144}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < 1.34999999999999988e-144Initial program 99.8%
Applied egg-rr8.0%
+-inverses8.0%
Simplified8.0%
if 1.34999999999999988e-144 < x Initial program 99.9%
Applied egg-rr6.4%
rem-exp-log6.4%
Simplified6.4%
Final simplification7.5%
(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 56.3%
unpow256.3%
Simplified56.3%
Final simplification56.3%
(FPCore (x y) :precision binary64 0.0)
double code(double x, double y) {
return 0.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 0.0d0
end function
public static double code(double x, double y) {
return 0.0;
}
def code(x, y): return 0.0
function code(x, y) return 0.0 end
function tmp = code(x, y) tmp = 0.0; end
code[x_, y_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 99.8%
Applied egg-rr6.2%
+-inverses6.2%
Simplified6.2%
Final simplification6.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 2023297
(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)))