
(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 7 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}
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (fma (- 1.0 x) y x))
assert(x < y);
double code(double x, double y) {
return fma((1.0 - x), y, x);
}
x, y = sort([x, y]) function code(x, y) return fma(Float64(1.0 - x), y, x) end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(N[(1.0 - x), $MachinePrecision] * y + x), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\mathsf{fma}\left(1 - x, y, x\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-out--N/A
*-rgt-identityN/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
+-commutativeN/A
associate-+r+N/A
+-commutativeN/A
distribute-lft1-inN/A
+-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower-fma.f64N/A
lower--.f64100.0
Applied rewrites100.0%
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y)
:precision binary64
(let* ((t_0 (- (+ x y) (* x y))))
(if (or (<= t_0 (- INFINITY)) (not (<= t_0 2e+291)))
(* (- y) x)
(fma 1.0 y x))))assert(x < y);
double code(double x, double y) {
double t_0 = (x + y) - (x * y);
double tmp;
if ((t_0 <= -((double) INFINITY)) || !(t_0 <= 2e+291)) {
tmp = -y * x;
} else {
tmp = fma(1.0, y, x);
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) t_0 = Float64(Float64(x + y) - Float64(x * y)) tmp = 0.0 if ((t_0 <= Float64(-Inf)) || !(t_0 <= 2e+291)) tmp = Float64(Float64(-y) * x); else tmp = fma(1.0, y, x); end return tmp end
NOTE: x and y should be sorted in increasing order before calling this function.
code[x_, y_] := Block[{t$95$0 = N[(N[(x + y), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, (-Infinity)], N[Not[LessEqual[t$95$0, 2e+291]], $MachinePrecision]], N[((-y) * x), $MachinePrecision], N[(1.0 * y + x), $MachinePrecision]]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
t_0 := \left(x + y\right) - x \cdot y\\
\mathbf{if}\;t\_0 \leq -\infty \lor \neg \left(t\_0 \leq 2 \cdot 10^{+291}\right):\\
\;\;\;\;\left(-y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(1, y, x\right)\\
\end{array}
\end{array}
if (-.f64 (+.f64 x y) (*.f64 x y)) < -inf.0 or 1.9999999999999999e291 < (-.f64 (+.f64 x y) (*.f64 x y)) Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around inf
Applied rewrites97.1%
if -inf.0 < (-.f64 (+.f64 x y) (*.f64 x y)) < 1.9999999999999999e291Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-out--N/A
*-rgt-identityN/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
+-commutativeN/A
associate-+r+N/A
+-commutativeN/A
distribute-lft1-inN/A
+-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower-fma.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites86.3%
Final simplification87.7%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= x -500000.0) (fma (- x) y x) (if (<= x 1.02e-164) (fma 1.0 y x) (* (- 1.0 x) y))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (x <= -500000.0) {
tmp = fma(-x, y, x);
} else if (x <= 1.02e-164) {
tmp = fma(1.0, y, x);
} else {
tmp = (1.0 - x) * y;
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (x <= -500000.0) tmp = fma(Float64(-x), y, x); elseif (x <= 1.02e-164) tmp = fma(1.0, y, x); else tmp = Float64(Float64(1.0 - x) * y); end return tmp end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[x, -500000.0], N[((-x) * y + x), $MachinePrecision], If[LessEqual[x, 1.02e-164], N[(1.0 * y + x), $MachinePrecision], N[(N[(1.0 - x), $MachinePrecision] * y), $MachinePrecision]]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -500000:\\
\;\;\;\;\mathsf{fma}\left(-x, y, x\right)\\
\mathbf{elif}\;x \leq 1.02 \cdot 10^{-164}:\\
\;\;\;\;\mathsf{fma}\left(1, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 - x\right) \cdot y\\
\end{array}
\end{array}
if x < -5e5Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-out--N/A
*-rgt-identityN/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
+-commutativeN/A
associate-+r+N/A
+-commutativeN/A
distribute-lft1-inN/A
+-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower-fma.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in x around inf
Applied rewrites100.0%
if -5e5 < x < 1.02e-164Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-out--N/A
*-rgt-identityN/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
+-commutativeN/A
associate-+r+N/A
+-commutativeN/A
distribute-lft1-inN/A
+-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower-fma.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites98.9%
if 1.02e-164 < x Initial program 100.0%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f64N/A
lower--.f6453.5
Applied rewrites53.5%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= x -500000.0) (* (- 1.0 y) x) (if (<= x 1.02e-164) (fma 1.0 y x) (* (- 1.0 x) y))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (x <= -500000.0) {
tmp = (1.0 - y) * x;
} else if (x <= 1.02e-164) {
tmp = fma(1.0, y, x);
} else {
tmp = (1.0 - x) * y;
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (x <= -500000.0) tmp = Float64(Float64(1.0 - y) * x); elseif (x <= 1.02e-164) tmp = fma(1.0, y, x); else tmp = Float64(Float64(1.0 - x) * y); end return tmp end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[x, -500000.0], N[(N[(1.0 - y), $MachinePrecision] * x), $MachinePrecision], If[LessEqual[x, 1.02e-164], N[(1.0 * y + x), $MachinePrecision], N[(N[(1.0 - x), $MachinePrecision] * y), $MachinePrecision]]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -500000:\\
\;\;\;\;\left(1 - y\right) \cdot x\\
\mathbf{elif}\;x \leq 1.02 \cdot 10^{-164}:\\
\;\;\;\;\mathsf{fma}\left(1, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 - x\right) \cdot y\\
\end{array}
\end{array}
if x < -5e5Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
lower--.f64100.0
Applied rewrites100.0%
if -5e5 < x < 1.02e-164Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-out--N/A
*-rgt-identityN/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
+-commutativeN/A
associate-+r+N/A
+-commutativeN/A
distribute-lft1-inN/A
+-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower-fma.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites98.9%
if 1.02e-164 < x Initial program 100.0%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f64N/A
lower--.f6453.5
Applied rewrites53.5%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y -3400.0) (* (- y) x) (if (<= y 6e-10) (fma 1.0 y x) (* (- 1.0 x) y))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= -3400.0) {
tmp = -y * x;
} else if (y <= 6e-10) {
tmp = fma(1.0, y, x);
} else {
tmp = (1.0 - x) * y;
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= -3400.0) tmp = Float64(Float64(-y) * x); elseif (y <= 6e-10) tmp = fma(1.0, y, x); else tmp = Float64(Float64(1.0 - x) * y); end return tmp end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[y, -3400.0], N[((-y) * x), $MachinePrecision], If[LessEqual[y, 6e-10], N[(1.0 * y + x), $MachinePrecision], N[(N[(1.0 - x), $MachinePrecision] * y), $MachinePrecision]]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3400:\\
\;\;\;\;\left(-y\right) \cdot x\\
\mathbf{elif}\;y \leq 6 \cdot 10^{-10}:\\
\;\;\;\;\mathsf{fma}\left(1, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 - x\right) \cdot y\\
\end{array}
\end{array}
if y < -3400Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
lower--.f6449.8
Applied rewrites49.8%
Taylor expanded in y around inf
Applied rewrites48.3%
if -3400 < y < 6e-10Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-out--N/A
*-rgt-identityN/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
+-commutativeN/A
associate-+r+N/A
+-commutativeN/A
distribute-lft1-inN/A
+-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower-fma.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites99.6%
if 6e-10 < y Initial program 100.0%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f64N/A
lower--.f64100.0
Applied rewrites100.0%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (fma 1.0 y x))
assert(x < y);
double code(double x, double y) {
return fma(1.0, y, x);
}
x, y = sort([x, y]) function code(x, y) return fma(1.0, y, x) end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(1.0 * y + x), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\mathsf{fma}\left(1, y, x\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-out--N/A
*-rgt-identityN/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
+-commutativeN/A
associate-+r+N/A
+-commutativeN/A
distribute-lft1-inN/A
+-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower-fma.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites76.5%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (* 1.0 y))
assert(x < y);
double code(double x, double y) {
return 1.0 * y;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0 * y
end function
assert x < y;
public static double code(double x, double y) {
return 1.0 * y;
}
[x, y] = sort([x, y]) def code(x, y): return 1.0 * y
x, y = sort([x, y]) function code(x, y) return Float64(1.0 * y) end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
tmp = 1.0 * y;
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(1.0 * y), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
1 \cdot y
\end{array}
Initial program 100.0%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f64N/A
lower--.f6465.1
Applied rewrites65.1%
Taylor expanded in x around 0
Applied rewrites41.4%
herbie shell --seed 2024332
(FPCore (x y)
:name "Data.Colour.RGBSpace.HSL:hsl from colour-2.3.3, A"
:precision binary64
(- (+ x y) (* x y)))