
(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 (- 1.0 x) y x))
double code(double x, double y) {
return fma((1.0 - x), y, x);
}
function code(x, y) return fma(Float64(1.0 - x), y, x) end
code[x_, y_] := N[(N[(1.0 - x), $MachinePrecision] * y + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(1 - x, y, 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
cancel-sign-sub-invN/A
*-rgt-identityN/A
distribute-lft-out--N/A
*-commutativeN/A
sub-negN/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 (- (+ y x) (* y x))) (t_1 (* (- x) y))) (if (<= t_0 -1e+247) t_1 (if (<= t_0 5e+307) (fma 1.0 y x) t_1))))
double code(double x, double y) {
double t_0 = (y + x) - (y * x);
double t_1 = -x * y;
double tmp;
if (t_0 <= -1e+247) {
tmp = t_1;
} else if (t_0 <= 5e+307) {
tmp = fma(1.0, y, x);
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y) t_0 = Float64(Float64(y + x) - Float64(y * x)) t_1 = Float64(Float64(-x) * y) tmp = 0.0 if (t_0 <= -1e+247) tmp = t_1; elseif (t_0 <= 5e+307) tmp = fma(1.0, y, x); else tmp = t_1; end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(N[(y + x), $MachinePrecision] - N[(y * x), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[((-x) * y), $MachinePrecision]}, If[LessEqual[t$95$0, -1e+247], t$95$1, If[LessEqual[t$95$0, 5e+307], N[(1.0 * y + x), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(y + x\right) - y \cdot x\\
t_1 := \left(-x\right) \cdot y\\
\mathbf{if}\;t\_0 \leq -1 \cdot 10^{+247}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 5 \cdot 10^{+307}:\\
\;\;\;\;\mathsf{fma}\left(1, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (-.f64 (+.f64 x y) (*.f64 x y)) < -9.99999999999999952e246 or 5e307 < (-.f64 (+.f64 x y) (*.f64 x y)) Initial program 100.0%
Taylor expanded in y around inf
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f6488.9
Applied rewrites88.9%
Taylor expanded in x around inf
Applied rewrites75.2%
if -9.99999999999999952e246 < (-.f64 (+.f64 x y) (*.f64 x y)) < 5e307Initial 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
cancel-sign-sub-invN/A
*-rgt-identityN/A
distribute-lft-out--N/A
*-commutativeN/A
sub-negN/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 rewrites86.7%
Final simplification84.4%
herbie shell --seed 2024230
(FPCore (x y)
:name "Data.Colour.RGBSpace.HSL:hsl from colour-2.3.3, A"
:precision binary64
(- (+ x y) (* x y)))