
(FPCore (x y) :precision binary64 (- (* (+ x 1.0) y) x))
double code(double x, double y) {
return ((x + 1.0) * y) - x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x + 1.0d0) * y) - x
end function
public static double code(double x, double y) {
return ((x + 1.0) * y) - x;
}
def code(x, y): return ((x + 1.0) * y) - x
function code(x, y) return Float64(Float64(Float64(x + 1.0) * y) - x) end
function tmp = code(x, y) tmp = ((x + 1.0) * y) - x; end
code[x_, y_] := N[(N[(N[(x + 1.0), $MachinePrecision] * y), $MachinePrecision] - x), $MachinePrecision]
\begin{array}{l}
\\
\left(x + 1\right) \cdot y - x
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (- (* (+ x 1.0) y) x))
double code(double x, double y) {
return ((x + 1.0) * y) - x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x + 1.0d0) * y) - x
end function
public static double code(double x, double y) {
return ((x + 1.0) * y) - x;
}
def code(x, y): return ((x + 1.0) * y) - x
function code(x, y) return Float64(Float64(Float64(x + 1.0) * y) - x) end
function tmp = code(x, y) tmp = ((x + 1.0) * y) - x; end
code[x_, y_] := N[(N[(N[(x + 1.0), $MachinePrecision] * y), $MachinePrecision] - x), $MachinePrecision]
\begin{array}{l}
\\
\left(x + 1\right) \cdot y - x
\end{array}
(FPCore (x y) :precision binary64 (fma y x (- y x)))
double code(double x, double y) {
return fma(y, x, (y - x));
}
function code(x, y) return fma(y, x, Float64(y - x)) end
code[x_, y_] := N[(y * x + N[(y - x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, x, y - x\right)
\end{array}
Initial program 100.0%
lift--.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
*-commutativeN/A
+-commutativeN/A
associate--l+N/A
lower-fma.f64N/A
lower--.f64100.0
Applied rewrites100.0%
(FPCore (x y) :precision binary64 (if (or (<= x -0.53) (not (<= x 3.6e-68))) (* (- y 1.0) x) (fma y x y)))
double code(double x, double y) {
double tmp;
if ((x <= -0.53) || !(x <= 3.6e-68)) {
tmp = (y - 1.0) * x;
} else {
tmp = fma(y, x, y);
}
return tmp;
}
function code(x, y) tmp = 0.0 if ((x <= -0.53) || !(x <= 3.6e-68)) tmp = Float64(Float64(y - 1.0) * x); else tmp = fma(y, x, y); end return tmp end
code[x_, y_] := If[Or[LessEqual[x, -0.53], N[Not[LessEqual[x, 3.6e-68]], $MachinePrecision]], N[(N[(y - 1.0), $MachinePrecision] * x), $MachinePrecision], N[(y * x + y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.53 \lor \neg \left(x \leq 3.6 \cdot 10^{-68}\right):\\
\;\;\;\;\left(y - 1\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, x, y\right)\\
\end{array}
\end{array}
if x < -0.53000000000000003 or 3.60000000000000007e-68 < x Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
lower--.f6496.8
Applied rewrites96.8%
if -0.53000000000000003 < x < 3.60000000000000007e-68Initial program 100.0%
lift--.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
*-commutativeN/A
+-commutativeN/A
associate--l+N/A
lower-fma.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around -inf
associate-*r*N/A
distribute-rgt-out--N/A
mul-1-negN/A
distribute-lft-neg-inN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
remove-double-negN/A
*-commutativeN/A
mul-1-negN/A
fp-cancel-sign-sub-invN/A
*-commutativeN/A
*-rgt-identityN/A
lower-fma.f6481.5
Applied rewrites81.5%
Final simplification90.2%
(FPCore (x y) :precision binary64 (if (<= x -54.0) (* (- y 1.0) x) (if (<= x 1.0) (- (* 1.0 y) x) (fma y x (- x)))))
double code(double x, double y) {
double tmp;
if (x <= -54.0) {
tmp = (y - 1.0) * x;
} else if (x <= 1.0) {
tmp = (1.0 * y) - x;
} else {
tmp = fma(y, x, -x);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (x <= -54.0) tmp = Float64(Float64(y - 1.0) * x); elseif (x <= 1.0) tmp = Float64(Float64(1.0 * y) - x); else tmp = fma(y, x, Float64(-x)); end return tmp end
code[x_, y_] := If[LessEqual[x, -54.0], N[(N[(y - 1.0), $MachinePrecision] * x), $MachinePrecision], If[LessEqual[x, 1.0], N[(N[(1.0 * y), $MachinePrecision] - x), $MachinePrecision], N[(y * x + (-x)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -54:\\
\;\;\;\;\left(y - 1\right) \cdot x\\
\mathbf{elif}\;x \leq 1:\\
\;\;\;\;1 \cdot y - x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, x, -x\right)\\
\end{array}
\end{array}
if x < -54Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
lower--.f6499.7
Applied rewrites99.7%
if -54 < x < 1Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites98.4%
if 1 < x Initial program 99.9%
Taylor expanded in y around 0
mul-1-negN/A
lower-neg.f6449.7
Applied rewrites49.7%
Taylor expanded in x around -inf
mul-1-negN/A
distribute-rgt-neg-inN/A
+-commutativeN/A
distribute-neg-inN/A
mul-1-negN/A
remove-double-negN/A
metadata-evalN/A
distribute-rgt-outN/A
lower-fma.f64N/A
mul-1-negN/A
lower-neg.f6498.9
Applied rewrites98.9%
Final simplification98.8%
(FPCore (x y) :precision binary64 (if (<= x -0.53) (* (- y 1.0) x) (if (<= x 3.6e-68) (fma y x y) (fma y x (- x)))))
double code(double x, double y) {
double tmp;
if (x <= -0.53) {
tmp = (y - 1.0) * x;
} else if (x <= 3.6e-68) {
tmp = fma(y, x, y);
} else {
tmp = fma(y, x, -x);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (x <= -0.53) tmp = Float64(Float64(y - 1.0) * x); elseif (x <= 3.6e-68) tmp = fma(y, x, y); else tmp = fma(y, x, Float64(-x)); end return tmp end
code[x_, y_] := If[LessEqual[x, -0.53], N[(N[(y - 1.0), $MachinePrecision] * x), $MachinePrecision], If[LessEqual[x, 3.6e-68], N[(y * x + y), $MachinePrecision], N[(y * x + (-x)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.53:\\
\;\;\;\;\left(y - 1\right) \cdot x\\
\mathbf{elif}\;x \leq 3.6 \cdot 10^{-68}:\\
\;\;\;\;\mathsf{fma}\left(y, x, y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, x, -x\right)\\
\end{array}
\end{array}
if x < -0.53000000000000003Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
lower--.f6499.7
Applied rewrites99.7%
if -0.53000000000000003 < x < 3.60000000000000007e-68Initial program 100.0%
lift--.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
*-commutativeN/A
+-commutativeN/A
associate--l+N/A
lower-fma.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around -inf
associate-*r*N/A
distribute-rgt-out--N/A
mul-1-negN/A
distribute-lft-neg-inN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
remove-double-negN/A
*-commutativeN/A
mul-1-negN/A
fp-cancel-sign-sub-invN/A
*-commutativeN/A
*-rgt-identityN/A
lower-fma.f6481.5
Applied rewrites81.5%
if 3.60000000000000007e-68 < x Initial program 99.9%
Taylor expanded in y around 0
mul-1-negN/A
lower-neg.f6451.0
Applied rewrites51.0%
Taylor expanded in x around -inf
mul-1-negN/A
distribute-rgt-neg-inN/A
+-commutativeN/A
distribute-neg-inN/A
mul-1-negN/A
remove-double-negN/A
metadata-evalN/A
distribute-rgt-outN/A
lower-fma.f64N/A
mul-1-negN/A
lower-neg.f6494.7
Applied rewrites94.7%
Final simplification90.2%
(FPCore (x y) :precision binary64 (if (or (<= y -1.48e-114) (not (<= y 2.8e-17))) (fma y x y) (- x)))
double code(double x, double y) {
double tmp;
if ((y <= -1.48e-114) || !(y <= 2.8e-17)) {
tmp = fma(y, x, y);
} else {
tmp = -x;
}
return tmp;
}
function code(x, y) tmp = 0.0 if ((y <= -1.48e-114) || !(y <= 2.8e-17)) tmp = fma(y, x, y); else tmp = Float64(-x); end return tmp end
code[x_, y_] := If[Or[LessEqual[y, -1.48e-114], N[Not[LessEqual[y, 2.8e-17]], $MachinePrecision]], N[(y * x + y), $MachinePrecision], (-x)]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.48 \cdot 10^{-114} \lor \neg \left(y \leq 2.8 \cdot 10^{-17}\right):\\
\;\;\;\;\mathsf{fma}\left(y, x, y\right)\\
\mathbf{else}:\\
\;\;\;\;-x\\
\end{array}
\end{array}
if y < -1.48e-114 or 2.7999999999999999e-17 < y Initial program 100.0%
lift--.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
*-commutativeN/A
+-commutativeN/A
associate--l+N/A
lower-fma.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around -inf
associate-*r*N/A
distribute-rgt-out--N/A
mul-1-negN/A
distribute-lft-neg-inN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
remove-double-negN/A
*-commutativeN/A
mul-1-negN/A
fp-cancel-sign-sub-invN/A
*-commutativeN/A
*-rgt-identityN/A
lower-fma.f6493.8
Applied rewrites93.8%
if -1.48e-114 < y < 2.7999999999999999e-17Initial program 100.0%
Taylor expanded in y around 0
mul-1-negN/A
lower-neg.f6483.3
Applied rewrites83.3%
Final simplification89.6%
(FPCore (x y) :precision binary64 (if (or (<= y -2.1e+18) (not (<= y 1.0))) (* x y) (- x)))
double code(double x, double y) {
double tmp;
if ((y <= -2.1e+18) || !(y <= 1.0)) {
tmp = x * y;
} else {
tmp = -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.1d+18)) .or. (.not. (y <= 1.0d0))) then
tmp = x * y
else
tmp = -x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -2.1e+18) || !(y <= 1.0)) {
tmp = x * y;
} else {
tmp = -x;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -2.1e+18) or not (y <= 1.0): tmp = x * y else: tmp = -x return tmp
function code(x, y) tmp = 0.0 if ((y <= -2.1e+18) || !(y <= 1.0)) tmp = Float64(x * y); else tmp = Float64(-x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -2.1e+18) || ~((y <= 1.0))) tmp = x * y; else tmp = -x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -2.1e+18], N[Not[LessEqual[y, 1.0]], $MachinePrecision]], N[(x * y), $MachinePrecision], (-x)]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.1 \cdot 10^{+18} \lor \neg \left(y \leq 1\right):\\
\;\;\;\;x \cdot y\\
\mathbf{else}:\\
\;\;\;\;-x\\
\end{array}
\end{array}
if y < -2.1e18 or 1 < y Initial program 100.0%
lift--.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
*-commutativeN/A
+-commutativeN/A
associate--l+N/A
lower-fma.f64N/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around -inf
associate-*r*N/A
distribute-rgt-out--N/A
mul-1-negN/A
distribute-lft-neg-inN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
remove-double-negN/A
*-commutativeN/A
mul-1-negN/A
fp-cancel-sign-sub-invN/A
*-commutativeN/A
*-rgt-identityN/A
lower-fma.f6498.3
Applied rewrites98.3%
Taylor expanded in x around inf
Applied rewrites53.0%
if -2.1e18 < y < 1Initial program 100.0%
Taylor expanded in y around 0
mul-1-negN/A
lower-neg.f6473.2
Applied rewrites73.2%
Final simplification62.9%
(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 Float64(-x) end
function tmp = code(x, y) tmp = -x; end
code[x_, y_] := (-x)
\begin{array}{l}
\\
-x
\end{array}
Initial program 100.0%
Taylor expanded in y around 0
mul-1-negN/A
lower-neg.f6437.4
Applied rewrites37.4%
Final simplification37.4%
herbie shell --seed 2024320
(FPCore (x y)
:name "Data.Colour.SRGB:transferFunction from colour-2.3.3"
:precision binary64
(- (* (+ x 1.0) y) x))