
(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 7 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 (* 2.0 (* y y)))))
double code(double x, double y) {
return fma(y, y, fma(x, x, (2.0 * (y * y))));
}
function code(x, y) return fma(y, y, fma(x, x, Float64(2.0 * Float64(y * y)))) end
code[x_, y_] := N[(y * y + N[(x * x + N[(2.0 * N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, 2 \cdot \left(y \cdot y\right)\right)\right)
\end{array}
Initial program 99.8%
+-commutative99.8%
sqr-neg99.8%
+-commutative99.8%
sqr-neg99.8%
+-commutative99.8%
fma-define99.9%
sqr-neg99.9%
sqr-neg99.9%
associate-+l+99.9%
fma-define99.9%
count-299.9%
Simplified99.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(y * Float64(y * 3.0))) end
code[x_, y_] := N[(x * x + N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 3\right)\right)
\end{array}
Initial program 99.8%
associate-+l+99.8%
associate-+l+99.9%
fma-define99.9%
count-299.9%
distribute-rgt1-in99.9%
metadata-eval99.9%
*-commutative99.9%
associate-*l*99.8%
metadata-eval99.8%
metadata-eval99.8%
Simplified99.8%
(FPCore (x y) :precision binary64 (if (<= x 8.8e-93) (* y (* y 3.0)) (* (* x x) (+ 1.0 (* 3.0 (/ (* y (/ y x)) x))))))
double code(double x, double y) {
double tmp;
if (x <= 8.8e-93) {
tmp = y * (y * 3.0);
} else {
tmp = (x * x) * (1.0 + (3.0 * ((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 <= 8.8d-93) then
tmp = y * (y * 3.0d0)
else
tmp = (x * x) * (1.0d0 + (3.0d0 * ((y * (y / x)) / x)))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 8.8e-93) {
tmp = y * (y * 3.0);
} else {
tmp = (x * x) * (1.0 + (3.0 * ((y * (y / x)) / x)));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 8.8e-93: tmp = y * (y * 3.0) else: tmp = (x * x) * (1.0 + (3.0 * ((y * (y / x)) / x))) return tmp
function code(x, y) tmp = 0.0 if (x <= 8.8e-93) tmp = Float64(y * Float64(y * 3.0)); else tmp = Float64(Float64(x * x) * Float64(1.0 + Float64(3.0 * Float64(Float64(y * Float64(y / x)) / x)))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 8.8e-93) tmp = y * (y * 3.0); else tmp = (x * x) * (1.0 + (3.0 * ((y * (y / x)) / x))); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 8.8e-93], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision], N[(N[(x * x), $MachinePrecision] * N[(1.0 + N[(3.0 * N[(N[(y * N[(y / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 8.8 \cdot 10^{-93}:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\mathbf{else}:\\
\;\;\;\;\left(x \cdot x\right) \cdot \left(1 + 3 \cdot \frac{y \cdot \frac{y}{x}}{x}\right)\\
\end{array}
\end{array}
if x < 8.79999999999999983e-93Initial program 99.8%
associate-+l+99.8%
associate-+l+99.8%
fma-define99.8%
count-299.8%
distribute-rgt1-in99.8%
metadata-eval99.8%
*-commutative99.8%
associate-*l*99.8%
metadata-eval99.8%
metadata-eval99.8%
Simplified99.8%
add-sqr-sqrt99.6%
fma-undefine99.6%
add-sqr-sqrt99.6%
hypot-define99.6%
associate-*r*99.7%
sqrt-prod99.6%
sqrt-prod45.6%
add-sqr-sqrt99.7%
fma-undefine99.7%
add-sqr-sqrt99.7%
hypot-define99.7%
associate-*r*99.7%
sqrt-prod99.5%
sqrt-prod45.5%
add-sqr-sqrt99.5%
Applied egg-rr99.5%
Simplified99.5%
Taylor expanded in x around 0 65.8%
unpow-prod-down65.8%
pow265.8%
pow265.8%
rem-square-sqrt66.1%
associate-*l*66.1%
Applied egg-rr66.1%
if 8.79999999999999983e-93 < x Initial program 99.9%
Taylor expanded in x around inf 91.1%
Simplified91.1%
pow291.1%
pow291.1%
times-frac96.3%
Applied egg-rr96.3%
pow296.3%
Applied egg-rr96.3%
associate-*r/96.3%
Applied egg-rr96.3%
Final simplification75.2%
(FPCore (x y) :precision binary64 (if (<= x 1.15e-93) (* y (* y 3.0)) (* (* x x) (+ 1.0 (* 3.0 (* (/ y x) (/ y x)))))))
double code(double x, double y) {
double tmp;
if (x <= 1.15e-93) {
tmp = y * (y * 3.0);
} else {
tmp = (x * x) * (1.0 + (3.0 * ((y / x) * (y / 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.15d-93) then
tmp = y * (y * 3.0d0)
else
tmp = (x * x) * (1.0d0 + (3.0d0 * ((y / x) * (y / x))))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 1.15e-93) {
tmp = y * (y * 3.0);
} else {
tmp = (x * x) * (1.0 + (3.0 * ((y / x) * (y / x))));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 1.15e-93: tmp = y * (y * 3.0) else: tmp = (x * x) * (1.0 + (3.0 * ((y / x) * (y / x)))) return tmp
function code(x, y) tmp = 0.0 if (x <= 1.15e-93) tmp = Float64(y * Float64(y * 3.0)); else tmp = Float64(Float64(x * x) * Float64(1.0 + Float64(3.0 * Float64(Float64(y / x) * Float64(y / x))))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 1.15e-93) tmp = y * (y * 3.0); else tmp = (x * x) * (1.0 + (3.0 * ((y / x) * (y / x)))); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 1.15e-93], N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision], N[(N[(x * x), $MachinePrecision] * N[(1.0 + N[(3.0 * N[(N[(y / x), $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.15 \cdot 10^{-93}:\\
\;\;\;\;y \cdot \left(y \cdot 3\right)\\
\mathbf{else}:\\
\;\;\;\;\left(x \cdot x\right) \cdot \left(1 + 3 \cdot \left(\frac{y}{x} \cdot \frac{y}{x}\right)\right)\\
\end{array}
\end{array}
if x < 1.1499999999999999e-93Initial program 99.8%
associate-+l+99.8%
associate-+l+99.8%
fma-define99.8%
count-299.8%
distribute-rgt1-in99.8%
metadata-eval99.8%
*-commutative99.8%
associate-*l*99.8%
metadata-eval99.8%
metadata-eval99.8%
Simplified99.8%
add-sqr-sqrt99.6%
fma-undefine99.6%
add-sqr-sqrt99.6%
hypot-define99.6%
associate-*r*99.7%
sqrt-prod99.6%
sqrt-prod45.6%
add-sqr-sqrt99.7%
fma-undefine99.7%
add-sqr-sqrt99.7%
hypot-define99.7%
associate-*r*99.7%
sqrt-prod99.5%
sqrt-prod45.5%
add-sqr-sqrt99.5%
Applied egg-rr99.5%
Simplified99.5%
Taylor expanded in x around 0 65.8%
unpow-prod-down65.8%
pow265.8%
pow265.8%
rem-square-sqrt66.1%
associate-*l*66.1%
Applied egg-rr66.1%
if 1.1499999999999999e-93 < x Initial program 99.9%
Taylor expanded in x around inf 91.1%
Simplified91.1%
pow291.1%
pow291.1%
times-frac96.3%
Applied egg-rr96.3%
pow296.3%
Applied egg-rr96.3%
Final simplification75.2%
(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 (+ (* y y) (* y (+ y y))))
double code(double x, double y) {
return (y * y) + (y * (y + y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y * y) + (y * (y + y))
end function
public static double code(double x, double y) {
return (y * y) + (y * (y + y));
}
def code(x, y): return (y * y) + (y * (y + y))
function code(x, y) return Float64(Float64(y * y) + Float64(y * Float64(y + y))) end
function tmp = code(x, y) tmp = (y * y) + (y * (y + y)); end
code[x_, y_] := N[(N[(y * y), $MachinePrecision] + N[(y * N[(y + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y \cdot y + y \cdot \left(y + y\right)
\end{array}
Initial program 99.8%
add-sqr-sqrt99.7%
pow299.7%
+-commutative99.7%
add-sqr-sqrt99.7%
hypot-define99.8%
hypot-define99.7%
Applied egg-rr99.7%
Taylor expanded in x around 0 56.5%
unpow256.5%
hypot-undefine56.5%
hypot-undefine56.5%
add-sqr-sqrt56.6%
distribute-lft-out56.6%
Applied egg-rr56.6%
Final simplification56.6%
(FPCore (x y) :precision binary64 (* y (* y 3.0)))
double code(double x, double y) {
return y * (y * 3.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y * (y * 3.0d0)
end function
public static double code(double x, double y) {
return y * (y * 3.0);
}
def code(x, y): return y * (y * 3.0)
function code(x, y) return Float64(y * Float64(y * 3.0)) end
function tmp = code(x, y) tmp = y * (y * 3.0); end
code[x_, y_] := N[(y * N[(y * 3.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y \cdot \left(y \cdot 3\right)
\end{array}
Initial program 99.8%
associate-+l+99.8%
associate-+l+99.9%
fma-define99.9%
count-299.9%
distribute-rgt1-in99.9%
metadata-eval99.9%
*-commutative99.9%
associate-*l*99.8%
metadata-eval99.8%
metadata-eval99.8%
Simplified99.8%
add-sqr-sqrt99.7%
fma-undefine99.7%
add-sqr-sqrt99.7%
hypot-define99.7%
associate-*r*99.7%
sqrt-prod99.7%
sqrt-prod45.5%
add-sqr-sqrt99.7%
fma-undefine99.7%
add-sqr-sqrt99.7%
hypot-define99.7%
associate-*r*99.7%
sqrt-prod99.6%
sqrt-prod45.5%
add-sqr-sqrt99.6%
Applied egg-rr99.6%
Simplified99.6%
Taylor expanded in x around 0 56.4%
unpow-prod-down56.3%
pow256.3%
pow256.3%
rem-square-sqrt56.6%
associate-*l*56.6%
Applied egg-rr56.6%
(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 2024180
(FPCore (x y)
:name "Linear.Quaternion:$c/ from linear-1.19.1.3, E"
:precision binary64
:alt
(! :herbie-platform default (+ (* x x) (* y (+ y (+ y y)))))
(+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))