
(FPCore (x y) :precision binary64 (+ (+ (* x y) x) y))
double code(double x, double y) {
return ((x * y) + x) + y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * y) + x) + y
end function
public static double code(double x, double y) {
return ((x * y) + x) + y;
}
def code(x, y): return ((x * y) + x) + y
function code(x, y) return Float64(Float64(Float64(x * y) + x) + y) end
function tmp = code(x, y) tmp = ((x * y) + x) + y; end
code[x_, y_] := N[(N[(N[(x * y), $MachinePrecision] + x), $MachinePrecision] + y), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot y + x\right) + y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (+ (+ (* x y) x) y))
double code(double x, double y) {
return ((x * y) + x) + y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * y) + x) + y
end function
public static double code(double x, double y) {
return ((x * y) + x) + y;
}
def code(x, y): return ((x * y) + x) + y
function code(x, y) return Float64(Float64(Float64(x * y) + x) + y) end
function tmp = code(x, y) tmp = ((x * y) + x) + y; end
code[x_, y_] := N[(N[(N[(x * y), $MachinePrecision] + x), $MachinePrecision] + y), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot y + x\right) + y
\end{array}
(FPCore (x y) :precision binary64 (fma (+ y 1.0) x y))
double code(double x, double y) {
return fma((y + 1.0), x, y);
}
function code(x, y) return fma(Float64(y + 1.0), x, y) end
code[x_, y_] := N[(N[(y + 1.0), $MachinePrecision] * x + y), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y + 1, x, y\right)
\end{array}
Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
fma-def100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(if (<= x -3e+230)
(* y x)
(if (<= x -9.5e+203)
x
(if (<= x -1.45e+179)
(* y x)
(if (<= x -4.8e+102)
x
(if (<= x -4.1e+27)
(* y x)
(if (<= x -4.9e-119) x (if (<= x 1.0) y (* y x)))))))))
double code(double x, double y) {
double tmp;
if (x <= -3e+230) {
tmp = y * x;
} else if (x <= -9.5e+203) {
tmp = x;
} else if (x <= -1.45e+179) {
tmp = y * x;
} else if (x <= -4.8e+102) {
tmp = x;
} else if (x <= -4.1e+27) {
tmp = y * x;
} else if (x <= -4.9e-119) {
tmp = x;
} else if (x <= 1.0) {
tmp = y;
} else {
tmp = 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 <= (-3d+230)) then
tmp = y * x
else if (x <= (-9.5d+203)) then
tmp = x
else if (x <= (-1.45d+179)) then
tmp = y * x
else if (x <= (-4.8d+102)) then
tmp = x
else if (x <= (-4.1d+27)) then
tmp = y * x
else if (x <= (-4.9d-119)) then
tmp = x
else if (x <= 1.0d0) then
tmp = y
else
tmp = y * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -3e+230) {
tmp = y * x;
} else if (x <= -9.5e+203) {
tmp = x;
} else if (x <= -1.45e+179) {
tmp = y * x;
} else if (x <= -4.8e+102) {
tmp = x;
} else if (x <= -4.1e+27) {
tmp = y * x;
} else if (x <= -4.9e-119) {
tmp = x;
} else if (x <= 1.0) {
tmp = y;
} else {
tmp = y * x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -3e+230: tmp = y * x elif x <= -9.5e+203: tmp = x elif x <= -1.45e+179: tmp = y * x elif x <= -4.8e+102: tmp = x elif x <= -4.1e+27: tmp = y * x elif x <= -4.9e-119: tmp = x elif x <= 1.0: tmp = y else: tmp = y * x return tmp
function code(x, y) tmp = 0.0 if (x <= -3e+230) tmp = Float64(y * x); elseif (x <= -9.5e+203) tmp = x; elseif (x <= -1.45e+179) tmp = Float64(y * x); elseif (x <= -4.8e+102) tmp = x; elseif (x <= -4.1e+27) tmp = Float64(y * x); elseif (x <= -4.9e-119) tmp = x; elseif (x <= 1.0) tmp = y; else tmp = Float64(y * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -3e+230) tmp = y * x; elseif (x <= -9.5e+203) tmp = x; elseif (x <= -1.45e+179) tmp = y * x; elseif (x <= -4.8e+102) tmp = x; elseif (x <= -4.1e+27) tmp = y * x; elseif (x <= -4.9e-119) tmp = x; elseif (x <= 1.0) tmp = y; else tmp = y * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -3e+230], N[(y * x), $MachinePrecision], If[LessEqual[x, -9.5e+203], x, If[LessEqual[x, -1.45e+179], N[(y * x), $MachinePrecision], If[LessEqual[x, -4.8e+102], x, If[LessEqual[x, -4.1e+27], N[(y * x), $MachinePrecision], If[LessEqual[x, -4.9e-119], x, If[LessEqual[x, 1.0], y, N[(y * x), $MachinePrecision]]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3 \cdot 10^{+230}:\\
\;\;\;\;y \cdot x\\
\mathbf{elif}\;x \leq -9.5 \cdot 10^{+203}:\\
\;\;\;\;x\\
\mathbf{elif}\;x \leq -1.45 \cdot 10^{+179}:\\
\;\;\;\;y \cdot x\\
\mathbf{elif}\;x \leq -4.8 \cdot 10^{+102}:\\
\;\;\;\;x\\
\mathbf{elif}\;x \leq -4.1 \cdot 10^{+27}:\\
\;\;\;\;y \cdot x\\
\mathbf{elif}\;x \leq -4.9 \cdot 10^{-119}:\\
\;\;\;\;x\\
\mathbf{elif}\;x \leq 1:\\
\;\;\;\;y\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if x < -3.00000000000000008e230 or -9.4999999999999995e203 < x < -1.45000000000000009e179 or -4.79999999999999989e102 < x < -4.1000000000000002e27 or 1 < x Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
+-commutative100.0%
distribute-lft1-in100.0%
*-commutative100.0%
associate-+r+100.0%
*-un-lft-identity100.0%
distribute-rgt-in100.0%
add-cube-cbrt99.4%
unpow299.4%
associate-*l*99.4%
fma-def99.4%
Applied egg-rr99.4%
Taylor expanded in x around inf 99.2%
pow-base-199.2%
*-lft-identity99.2%
Simplified99.2%
Taylor expanded in y around inf 58.0%
if -3.00000000000000008e230 < x < -9.4999999999999995e203 or -1.45000000000000009e179 < x < -4.79999999999999989e102 or -4.1000000000000002e27 < x < -4.9e-119Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
+-commutative100.0%
distribute-lft1-in100.0%
*-commutative100.0%
associate-+r+100.0%
*-un-lft-identity100.0%
distribute-rgt-in100.0%
add-cube-cbrt99.4%
unpow299.4%
associate-*l*99.3%
fma-def99.3%
Applied egg-rr99.3%
Taylor expanded in y around 0 61.1%
if -4.9e-119 < x < 1Initial program 100.0%
Taylor expanded in x around 0 73.4%
Final simplification64.8%
(FPCore (x y)
:precision binary64
(if (or (<= y -1500000.0)
(and (not (<= y 1.8e+121)) (or (<= y 5e+177) (not (<= y 1.05e+281)))))
(* y x)
(+ y x)))
double code(double x, double y) {
double tmp;
if ((y <= -1500000.0) || (!(y <= 1.8e+121) && ((y <= 5e+177) || !(y <= 1.05e+281)))) {
tmp = y * x;
} else {
tmp = y + x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y <= (-1500000.0d0)) .or. (.not. (y <= 1.8d+121)) .and. (y <= 5d+177) .or. (.not. (y <= 1.05d+281))) then
tmp = y * x
else
tmp = y + x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -1500000.0) || (!(y <= 1.8e+121) && ((y <= 5e+177) || !(y <= 1.05e+281)))) {
tmp = y * x;
} else {
tmp = y + x;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -1500000.0) or (not (y <= 1.8e+121) and ((y <= 5e+177) or not (y <= 1.05e+281))): tmp = y * x else: tmp = y + x return tmp
function code(x, y) tmp = 0.0 if ((y <= -1500000.0) || (!(y <= 1.8e+121) && ((y <= 5e+177) || !(y <= 1.05e+281)))) tmp = Float64(y * x); else tmp = Float64(y + x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -1500000.0) || (~((y <= 1.8e+121)) && ((y <= 5e+177) || ~((y <= 1.05e+281))))) tmp = y * x; else tmp = y + x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -1500000.0], And[N[Not[LessEqual[y, 1.8e+121]], $MachinePrecision], Or[LessEqual[y, 5e+177], N[Not[LessEqual[y, 1.05e+281]], $MachinePrecision]]]], N[(y * x), $MachinePrecision], N[(y + x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1500000 \lor \neg \left(y \leq 1.8 \cdot 10^{+121}\right) \land \left(y \leq 5 \cdot 10^{+177} \lor \neg \left(y \leq 1.05 \cdot 10^{+281}\right)\right):\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;y + x\\
\end{array}
\end{array}
if y < -1.5e6 or 1.79999999999999991e121 < y < 5.0000000000000003e177 or 1.05000000000000003e281 < y Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
+-commutative100.0%
distribute-lft1-in100.0%
*-commutative100.0%
associate-+r+100.0%
*-un-lft-identity100.0%
distribute-rgt-in100.0%
add-cube-cbrt98.7%
unpow298.7%
associate-*l*98.6%
fma-def98.6%
Applied egg-rr98.6%
Taylor expanded in x around inf 58.0%
pow-base-158.0%
*-lft-identity58.0%
Simplified58.0%
Taylor expanded in y around inf 57.6%
if -1.5e6 < y < 1.79999999999999991e121 or 5.0000000000000003e177 < y < 1.05000000000000003e281Initial program 100.0%
Taylor expanded in y around 0 88.1%
Final simplification77.8%
(FPCore (x y) :precision binary64 (if (<= y -2.6e-7) (* (+ y 1.0) x) (if (<= y 0.55) (+ y x) (* y (+ 1.0 x)))))
double code(double x, double y) {
double tmp;
if (y <= -2.6e-7) {
tmp = (y + 1.0) * x;
} else if (y <= 0.55) {
tmp = y + x;
} else {
tmp = y * (1.0 + x);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-2.6d-7)) then
tmp = (y + 1.0d0) * x
else if (y <= 0.55d0) then
tmp = y + x
else
tmp = y * (1.0d0 + x)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -2.6e-7) {
tmp = (y + 1.0) * x;
} else if (y <= 0.55) {
tmp = y + x;
} else {
tmp = y * (1.0 + x);
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -2.6e-7: tmp = (y + 1.0) * x elif y <= 0.55: tmp = y + x else: tmp = y * (1.0 + x) return tmp
function code(x, y) tmp = 0.0 if (y <= -2.6e-7) tmp = Float64(Float64(y + 1.0) * x); elseif (y <= 0.55) tmp = Float64(y + x); else tmp = Float64(y * Float64(1.0 + x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -2.6e-7) tmp = (y + 1.0) * x; elseif (y <= 0.55) tmp = y + x; else tmp = y * (1.0 + x); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -2.6e-7], N[(N[(y + 1.0), $MachinePrecision] * x), $MachinePrecision], If[LessEqual[y, 0.55], N[(y + x), $MachinePrecision], N[(y * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.6 \cdot 10^{-7}:\\
\;\;\;\;\left(y + 1\right) \cdot x\\
\mathbf{elif}\;y \leq 0.55:\\
\;\;\;\;y + x\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(1 + x\right)\\
\end{array}
\end{array}
if y < -2.59999999999999999e-7Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
+-commutative100.0%
distribute-lft1-in100.0%
*-commutative100.0%
associate-+r+100.0%
*-un-lft-identity100.0%
distribute-rgt-in100.0%
add-cube-cbrt98.7%
unpow298.7%
associate-*l*98.6%
fma-def98.6%
Applied egg-rr98.6%
Taylor expanded in x around inf 53.9%
pow-base-153.9%
*-lft-identity53.9%
Simplified53.9%
if -2.59999999999999999e-7 < y < 0.55000000000000004Initial program 100.0%
Taylor expanded in y around 0 99.9%
if 0.55000000000000004 < y Initial program 100.0%
Taylor expanded in y around inf 100.0%
Final simplification87.0%
(FPCore (x y) :precision binary64 (if (<= x -160000000.0) (* (+ y 1.0) x) (if (<= x 650000.0) (+ y x) (* y x))))
double code(double x, double y) {
double tmp;
if (x <= -160000000.0) {
tmp = (y + 1.0) * x;
} else if (x <= 650000.0) {
tmp = y + x;
} else {
tmp = 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 <= (-160000000.0d0)) then
tmp = (y + 1.0d0) * x
else if (x <= 650000.0d0) then
tmp = y + x
else
tmp = y * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -160000000.0) {
tmp = (y + 1.0) * x;
} else if (x <= 650000.0) {
tmp = y + x;
} else {
tmp = y * x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -160000000.0: tmp = (y + 1.0) * x elif x <= 650000.0: tmp = y + x else: tmp = y * x return tmp
function code(x, y) tmp = 0.0 if (x <= -160000000.0) tmp = Float64(Float64(y + 1.0) * x); elseif (x <= 650000.0) tmp = Float64(y + x); else tmp = Float64(y * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -160000000.0) tmp = (y + 1.0) * x; elseif (x <= 650000.0) tmp = y + x; else tmp = y * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -160000000.0], N[(N[(y + 1.0), $MachinePrecision] * x), $MachinePrecision], If[LessEqual[x, 650000.0], N[(y + x), $MachinePrecision], N[(y * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -160000000:\\
\;\;\;\;\left(y + 1\right) \cdot x\\
\mathbf{elif}\;x \leq 650000:\\
\;\;\;\;y + x\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if x < -1.6e8Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
+-commutative100.0%
distribute-lft1-in100.0%
*-commutative100.0%
associate-+r+100.0%
*-un-lft-identity100.0%
distribute-rgt-in100.0%
add-cube-cbrt99.4%
unpow299.4%
associate-*l*99.3%
fma-def99.3%
Applied egg-rr99.3%
Taylor expanded in x around inf 99.6%
pow-base-199.6%
*-lft-identity99.6%
Simplified99.6%
if -1.6e8 < x < 6.5e5Initial program 100.0%
Taylor expanded in y around 0 96.8%
if 6.5e5 < x Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
+-commutative100.0%
distribute-lft1-in100.0%
*-commutative100.0%
associate-+r+100.0%
*-un-lft-identity100.0%
distribute-rgt-in100.0%
add-cube-cbrt99.5%
unpow299.5%
associate-*l*99.6%
fma-def99.6%
Applied egg-rr99.6%
Taylor expanded in x around inf 98.7%
pow-base-198.7%
*-lft-identity98.7%
Simplified98.7%
Taylor expanded in y around inf 53.0%
Final simplification86.8%
(FPCore (x y) :precision binary64 (+ y (* (+ y 1.0) x)))
double code(double x, double y) {
return y + ((y + 1.0) * x);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y + ((y + 1.0d0) * x)
end function
public static double code(double x, double y) {
return y + ((y + 1.0) * x);
}
def code(x, y): return y + ((y + 1.0) * x)
function code(x, y) return Float64(y + Float64(Float64(y + 1.0) * x)) end
function tmp = code(x, y) tmp = y + ((y + 1.0) * x); end
code[x_, y_] := N[(y + N[(N[(y + 1.0), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y + \left(y + 1\right) \cdot x
\end{array}
Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= x -4.9e-119) x y))
double code(double x, double y) {
double tmp;
if (x <= -4.9e-119) {
tmp = x;
} else {
tmp = y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-4.9d-119)) then
tmp = x
else
tmp = y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -4.9e-119) {
tmp = x;
} else {
tmp = y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -4.9e-119: tmp = x else: tmp = y return tmp
function code(x, y) tmp = 0.0 if (x <= -4.9e-119) tmp = x; else tmp = y; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -4.9e-119) tmp = x; else tmp = y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -4.9e-119], x, y]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.9 \cdot 10^{-119}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y\\
\end{array}
\end{array}
if x < -4.9e-119Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
+-commutative100.0%
distribute-lft1-in100.0%
*-commutative100.0%
associate-+r+100.0%
*-un-lft-identity100.0%
distribute-rgt-in100.0%
add-cube-cbrt99.3%
unpow299.3%
associate-*l*99.3%
fma-def99.3%
Applied egg-rr99.3%
Taylor expanded in y around 0 49.4%
if -4.9e-119 < x Initial program 100.0%
Taylor expanded in x around 0 46.7%
Final simplification47.6%
(FPCore (x y) :precision binary64 x)
double code(double x, double y) {
return x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x
end function
public static double code(double x, double y) {
return x;
}
def code(x, y): return x
function code(x, y) return x end
function tmp = code(x, y) tmp = x; end
code[x_, y_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 100.0%
*-commutative100.0%
distribute-lft1-in100.0%
Applied egg-rr100.0%
+-commutative100.0%
distribute-lft1-in100.0%
*-commutative100.0%
associate-+r+100.0%
*-un-lft-identity100.0%
distribute-rgt-in100.0%
add-cube-cbrt99.0%
unpow299.0%
associate-*l*99.0%
fma-def99.0%
Applied egg-rr99.0%
Taylor expanded in y around 0 40.5%
Final simplification40.5%
herbie shell --seed 2023337
(FPCore (x y)
:name "Numeric.Log:$cexpm1 from log-domain-0.10.2.1, B"
:precision binary64
(+ (+ (* x y) x) y))