
(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(x + y) - Float64(x * y)) end
function tmp = code(x, y) tmp = (x + y) - (x * y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x + y\right) - x \cdot y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 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(x + y) - Float64(x * y)) end
function tmp = code(x, y) tmp = (x + y) - (x * y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x + y\right) - x \cdot y
\end{array}
(FPCore (x y) :precision binary64 (fma y (- 1.0 x) x))
double code(double x, double y) {
return fma(y, (1.0 - x), x);
}
function code(x, y) return fma(y, Float64(1.0 - x), x) end
code[x_, y_] := N[(y * N[(1.0 - x), $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, 1 - x, x\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
distribute-rgt-out--N/A
*-lft-identityN/A
cancel-sign-sub-invN/A
+-commutativeN/A
associate-+r+N/A
*-rgt-identityN/A
distribute-lft-neg-outN/A
distribute-rgt-neg-outN/A
distribute-lft-inN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
(FPCore (x y) :precision binary64 (let* ((t_0 (- (+ x y) (* x y))) (t_1 (* x (- y)))) (if (<= t_0 -2e+292) t_1 (if (<= t_0 2e+301) (fma y 1.0 x) t_1))))
double code(double x, double y) {
double t_0 = (x + y) - (x * y);
double t_1 = x * -y;
double tmp;
if (t_0 <= -2e+292) {
tmp = t_1;
} else if (t_0 <= 2e+301) {
tmp = fma(y, 1.0, x);
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y) t_0 = Float64(Float64(x + y) - Float64(x * y)) t_1 = Float64(x * Float64(-y)) tmp = 0.0 if (t_0 <= -2e+292) tmp = t_1; elseif (t_0 <= 2e+301) tmp = fma(y, 1.0, x); else tmp = t_1; end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(N[(x + y), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(x * (-y)), $MachinePrecision]}, If[LessEqual[t$95$0, -2e+292], t$95$1, If[LessEqual[t$95$0, 2e+301], N[(y * 1.0 + x), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(x + y\right) - x \cdot y\\
t_1 := x \cdot \left(-y\right)\\
\mathbf{if}\;t\_0 \leq -2 \cdot 10^{+292}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 2 \cdot 10^{+301}:\\
\;\;\;\;\mathsf{fma}\left(y, 1, x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (-.f64 (+.f64 x y) (*.f64 x y)) < -2e292 or 2.00000000000000011e301 < (-.f64 (+.f64 x y) (*.f64 x y)) Initial program 100.0%
Taylor expanded in y around inf
distribute-rgt-out--N/A
*-lft-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f6495.1
Applied rewrites95.1%
Taylor expanded in x around inf
Applied rewrites95.1%
if -2e292 < (-.f64 (+.f64 x y) (*.f64 x y)) < 2.00000000000000011e301Initial program 100.0%
Taylor expanded in x around 0
distribute-rgt-out--N/A
*-lft-identityN/A
cancel-sign-sub-invN/A
+-commutativeN/A
associate-+r+N/A
*-rgt-identityN/A
distribute-lft-neg-outN/A
distribute-rgt-neg-outN/A
distribute-lft-inN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites90.4%
Final simplification91.1%
(FPCore (x y) :precision binary64 (if (<= (- (+ x y) (* x y)) -2e-228) (fma (- y) x x) (- y (* x y))))
double code(double x, double y) {
double tmp;
if (((x + y) - (x * y)) <= -2e-228) {
tmp = fma(-y, x, x);
} else {
tmp = y - (x * y);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (Float64(Float64(x + y) - Float64(x * y)) <= -2e-228) tmp = fma(Float64(-y), x, x); else tmp = Float64(y - Float64(x * y)); end return tmp end
code[x_, y_] := If[LessEqual[N[(N[(x + y), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision], -2e-228], N[((-y) * x + x), $MachinePrecision], N[(y - N[(x * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(x + y\right) - x \cdot y \leq -2 \cdot 10^{-228}:\\
\;\;\;\;\mathsf{fma}\left(-y, x, x\right)\\
\mathbf{else}:\\
\;\;\;\;y - x \cdot y\\
\end{array}
\end{array}
if (-.f64 (+.f64 x y) (*.f64 x y)) < -2.00000000000000007e-228Initial program 100.0%
Taylor expanded in x around inf
distribute-lft-out--N/A
*-rgt-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f6465.2
Applied rewrites65.2%
Applied rewrites65.2%
if -2.00000000000000007e-228 < (-.f64 (+.f64 x y) (*.f64 x y)) Initial program 100.0%
Taylor expanded in y around inf
distribute-rgt-out--N/A
*-lft-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f6466.1
Applied rewrites66.1%
Final simplification65.7%
(FPCore (x y) :precision binary64 (if (<= (- (+ x y) (* x y)) -2e-228) (- x (* x y)) (- y (* x y))))
double code(double x, double y) {
double tmp;
if (((x + y) - (x * y)) <= -2e-228) {
tmp = x - (x * y);
} else {
tmp = y - (x * y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (((x + y) - (x * y)) <= (-2d-228)) then
tmp = x - (x * y)
else
tmp = y - (x * y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (((x + y) - (x * y)) <= -2e-228) {
tmp = x - (x * y);
} else {
tmp = y - (x * y);
}
return tmp;
}
def code(x, y): tmp = 0 if ((x + y) - (x * y)) <= -2e-228: tmp = x - (x * y) else: tmp = y - (x * y) return tmp
function code(x, y) tmp = 0.0 if (Float64(Float64(x + y) - Float64(x * y)) <= -2e-228) tmp = Float64(x - Float64(x * y)); else tmp = Float64(y - Float64(x * y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (((x + y) - (x * y)) <= -2e-228) tmp = x - (x * y); else tmp = y - (x * y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(N[(x + y), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision], -2e-228], N[(x - N[(x * y), $MachinePrecision]), $MachinePrecision], N[(y - N[(x * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(x + y\right) - x \cdot y \leq -2 \cdot 10^{-228}:\\
\;\;\;\;x - x \cdot y\\
\mathbf{else}:\\
\;\;\;\;y - x \cdot y\\
\end{array}
\end{array}
if (-.f64 (+.f64 x y) (*.f64 x y)) < -2.00000000000000007e-228Initial program 100.0%
Taylor expanded in x around inf
distribute-lft-out--N/A
*-rgt-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f6465.2
Applied rewrites65.2%
if -2.00000000000000007e-228 < (-.f64 (+.f64 x y) (*.f64 x y)) Initial program 100.0%
Taylor expanded in y around inf
distribute-rgt-out--N/A
*-lft-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f6466.1
Applied rewrites66.1%
Final simplification65.6%
(FPCore (x y) :precision binary64 (if (<= x -5e+18) (- x (* x y)) (if (<= x 13500000000.0) (fma y 1.0 x) (* x (- y)))))
double code(double x, double y) {
double tmp;
if (x <= -5e+18) {
tmp = x - (x * y);
} else if (x <= 13500000000.0) {
tmp = fma(y, 1.0, x);
} else {
tmp = x * -y;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (x <= -5e+18) tmp = Float64(x - Float64(x * y)); elseif (x <= 13500000000.0) tmp = fma(y, 1.0, x); else tmp = Float64(x * Float64(-y)); end return tmp end
code[x_, y_] := If[LessEqual[x, -5e+18], N[(x - N[(x * y), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 13500000000.0], N[(y * 1.0 + x), $MachinePrecision], N[(x * (-y)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{+18}:\\
\;\;\;\;x - x \cdot y\\
\mathbf{elif}\;x \leq 13500000000:\\
\;\;\;\;\mathsf{fma}\left(y, 1, x\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(-y\right)\\
\end{array}
\end{array}
if x < -5e18Initial program 100.0%
Taylor expanded in x around inf
distribute-lft-out--N/A
*-rgt-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
if -5e18 < x < 1.35e10Initial program 100.0%
Taylor expanded in x around 0
distribute-rgt-out--N/A
*-lft-identityN/A
cancel-sign-sub-invN/A
+-commutativeN/A
associate-+r+N/A
*-rgt-identityN/A
distribute-lft-neg-outN/A
distribute-rgt-neg-outN/A
distribute-lft-inN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites96.2%
if 1.35e10 < x Initial program 99.9%
Taylor expanded in y around inf
distribute-rgt-out--N/A
*-lft-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f6447.6
Applied rewrites47.6%
Taylor expanded in x around inf
Applied rewrites47.5%
Final simplification85.4%
(FPCore (x y) :precision binary64 (fma y 1.0 x))
double code(double x, double y) {
return fma(y, 1.0, x);
}
function code(x, y) return fma(y, 1.0, x) end
code[x_, y_] := N[(y * 1.0 + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, 1, x\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
distribute-rgt-out--N/A
*-lft-identityN/A
cancel-sign-sub-invN/A
+-commutativeN/A
associate-+r+N/A
*-rgt-identityN/A
distribute-lft-neg-outN/A
distribute-rgt-neg-outN/A
distribute-lft-inN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites78.0%
herbie shell --seed 2024238
(FPCore (x y)
:name "Data.Colour.RGBSpace.HSL:hsl from colour-2.3.3, A"
:precision binary64
(- (+ x y) (* x y)))