
(FPCore (x y) :precision binary64 (+ x (/ (fabs (- y x)) 2.0)))
double code(double x, double y) {
return x + (fabs((y - x)) / 2.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x + (abs((y - x)) / 2.0d0)
end function
public static double code(double x, double y) {
return x + (Math.abs((y - x)) / 2.0);
}
def code(x, y): return x + (math.fabs((y - x)) / 2.0)
function code(x, y) return Float64(x + Float64(abs(Float64(y - x)) / 2.0)) end
function tmp = code(x, y) tmp = x + (abs((y - x)) / 2.0); end
code[x_, y_] := N[(x + N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \frac{\left|y - x\right|}{2}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (+ x (/ (fabs (- y x)) 2.0)))
double code(double x, double y) {
return x + (fabs((y - x)) / 2.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x + (abs((y - x)) / 2.0d0)
end function
public static double code(double x, double y) {
return x + (Math.abs((y - x)) / 2.0);
}
def code(x, y): return x + (math.fabs((y - x)) / 2.0)
function code(x, y) return Float64(x + Float64(abs(Float64(y - x)) / 2.0)) end
function tmp = code(x, y) tmp = x + (abs((y - x)) / 2.0); end
code[x_, y_] := N[(x + N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \frac{\left|y - x\right|}{2}
\end{array}
(FPCore (x y) :precision binary64 (fma (fabs (- y x)) 0.5 x))
double code(double x, double y) {
return fma(fabs((y - x)), 0.5, x);
}
function code(x, y) return fma(abs(Float64(y - x)), 0.5, x) end
code[x_, y_] := N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] * 0.5 + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\left|y - x\right|, 0.5, x\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-lft-identityN/A
metadata-evalN/A
fabs-subN/A
fp-cancel-sign-sub-invN/A
lower-fabs.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64100.0
Applied rewrites100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= y -9.8e-112) (* -0.5 y) (if (<= y 3.5e-95) (* 1.5 x) (* (fabs (- y)) 0.5))))
double code(double x, double y) {
double tmp;
if (y <= -9.8e-112) {
tmp = -0.5 * y;
} else if (y <= 3.5e-95) {
tmp = 1.5 * x;
} else {
tmp = fabs(-y) * 0.5;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-9.8d-112)) then
tmp = (-0.5d0) * y
else if (y <= 3.5d-95) then
tmp = 1.5d0 * x
else
tmp = abs(-y) * 0.5d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -9.8e-112) {
tmp = -0.5 * y;
} else if (y <= 3.5e-95) {
tmp = 1.5 * x;
} else {
tmp = Math.abs(-y) * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -9.8e-112: tmp = -0.5 * y elif y <= 3.5e-95: tmp = 1.5 * x else: tmp = math.fabs(-y) * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= -9.8e-112) tmp = Float64(-0.5 * y); elseif (y <= 3.5e-95) tmp = Float64(1.5 * x); else tmp = Float64(abs(Float64(-y)) * 0.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -9.8e-112) tmp = -0.5 * y; elseif (y <= 3.5e-95) tmp = 1.5 * x; else tmp = abs(-y) * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -9.8e-112], N[(-0.5 * y), $MachinePrecision], If[LessEqual[y, 3.5e-95], N[(1.5 * x), $MachinePrecision], N[(N[Abs[(-y)], $MachinePrecision] * 0.5), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -9.8 \cdot 10^{-112}:\\
\;\;\;\;-0.5 \cdot y\\
\mathbf{elif}\;y \leq 3.5 \cdot 10^{-95}:\\
\;\;\;\;1.5 \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left|-y\right| \cdot 0.5\\
\end{array}
\end{array}
if y < -9.7999999999999996e-112Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
unpow1N/A
metadata-evalN/A
sqrt-pow1N/A
pow2N/A
sqrt-prodN/A
lower-fma.f64N/A
Applied rewrites74.0%
Taylor expanded in x around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
lower-*.f6465.7
Applied rewrites65.7%
if -9.7999999999999996e-112 < y < 3.4999999999999997e-95Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
unpow1N/A
metadata-evalN/A
sqrt-pow1N/A
pow2N/A
sqrt-prodN/A
lower-fma.f64N/A
Applied rewrites47.2%
Taylor expanded in x around inf
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
metadata-evalN/A
metadata-evalN/A
lower-*.f6450.6
Applied rewrites50.6%
if 3.4999999999999997e-95 < y Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
*-lft-identityN/A
metadata-evalN/A
fabs-subN/A
fp-cancel-sign-sub-invN/A
lower-fabs.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f6472.8
Applied rewrites72.8%
Taylor expanded in x around 0
Applied rewrites71.6%
(FPCore (x y) :precision binary64 (if (<= y 1.15e-117) (fma (- y x) -0.5 x) (fma (fabs (- y)) 0.5 x)))
double code(double x, double y) {
double tmp;
if (y <= 1.15e-117) {
tmp = fma((y - x), -0.5, x);
} else {
tmp = fma(fabs(-y), 0.5, x);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= 1.15e-117) tmp = fma(Float64(y - x), -0.5, x); else tmp = fma(abs(Float64(-y)), 0.5, x); end return tmp end
code[x_, y_] := If[LessEqual[y, 1.15e-117], N[(N[(y - x), $MachinePrecision] * -0.5 + x), $MachinePrecision], N[(N[Abs[(-y)], $MachinePrecision] * 0.5 + x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.15 \cdot 10^{-117}:\\
\;\;\;\;\mathsf{fma}\left(y - x, -0.5, x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\left|-y\right|, 0.5, x\right)\\
\end{array}
\end{array}
if y < 1.14999999999999997e-117Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
unpow1N/A
metadata-evalN/A
sqrt-pow1N/A
pow2N/A
sqrt-prodN/A
lower-fma.f64N/A
Applied rewrites62.7%
Taylor expanded in x around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
lower-*.f6441.0
Applied rewrites41.0%
Taylor expanded in x around 0
*-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
unpow2N/A
rem-square-sqrtN/A
metadata-evalN/A
metadata-evalN/A
distribute-rgt1-inN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
rem-square-sqrtN/A
unpow2N/A
*-commutativeN/A
fp-cancel-sub-signN/A
mul-1-negN/A
associate-*r*N/A
associate-+r+N/A
Applied rewrites69.3%
if 1.14999999999999997e-117 < y Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-lft-identityN/A
metadata-evalN/A
fabs-subN/A
fp-cancel-sign-sub-invN/A
lower-fabs.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites75.7%
(FPCore (x y) :precision binary64 (if (<= y 9.2e-105) (fma (- y x) -0.5 x) (* (fabs (- y x)) 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 9.2e-105) {
tmp = fma((y - x), -0.5, x);
} else {
tmp = fabs((y - x)) * 0.5;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= 9.2e-105) tmp = fma(Float64(y - x), -0.5, x); else tmp = Float64(abs(Float64(y - x)) * 0.5); end return tmp end
code[x_, y_] := If[LessEqual[y, 9.2e-105], N[(N[(y - x), $MachinePrecision] * -0.5 + x), $MachinePrecision], N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 9.2 \cdot 10^{-105}:\\
\;\;\;\;\mathsf{fma}\left(y - x, -0.5, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left|y - x\right| \cdot 0.5\\
\end{array}
\end{array}
if y < 9.2000000000000004e-105Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
unpow1N/A
metadata-evalN/A
sqrt-pow1N/A
pow2N/A
sqrt-prodN/A
lower-fma.f64N/A
Applied rewrites62.3%
Taylor expanded in x around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
lower-*.f6440.7
Applied rewrites40.7%
Taylor expanded in x around 0
*-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
unpow2N/A
rem-square-sqrtN/A
metadata-evalN/A
metadata-evalN/A
distribute-rgt1-inN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
rem-square-sqrtN/A
unpow2N/A
*-commutativeN/A
fp-cancel-sub-signN/A
mul-1-negN/A
associate-*r*N/A
associate-+r+N/A
Applied rewrites69.0%
if 9.2000000000000004e-105 < y Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
*-lft-identityN/A
metadata-evalN/A
fabs-subN/A
fp-cancel-sign-sub-invN/A
lower-fabs.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f6472.8
Applied rewrites72.8%
Final simplification70.2%
(FPCore (x y) :precision binary64 (if (<= y 3.6e-95) (fma (- y x) -0.5 x) (* (fabs (- y)) 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 3.6e-95) {
tmp = fma((y - x), -0.5, x);
} else {
tmp = fabs(-y) * 0.5;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= 3.6e-95) tmp = fma(Float64(y - x), -0.5, x); else tmp = Float64(abs(Float64(-y)) * 0.5); end return tmp end
code[x_, y_] := If[LessEqual[y, 3.6e-95], N[(N[(y - x), $MachinePrecision] * -0.5 + x), $MachinePrecision], N[(N[Abs[(-y)], $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 3.6 \cdot 10^{-95}:\\
\;\;\;\;\mathsf{fma}\left(y - x, -0.5, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left|-y\right| \cdot 0.5\\
\end{array}
\end{array}
if y < 3.6e-95Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
unpow1N/A
metadata-evalN/A
sqrt-pow1N/A
pow2N/A
sqrt-prodN/A
lower-fma.f64N/A
Applied rewrites62.3%
Taylor expanded in x around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
lower-*.f6440.7
Applied rewrites40.7%
Taylor expanded in x around 0
*-commutativeN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
unpow2N/A
rem-square-sqrtN/A
metadata-evalN/A
metadata-evalN/A
distribute-rgt1-inN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
rem-square-sqrtN/A
unpow2N/A
*-commutativeN/A
fp-cancel-sub-signN/A
mul-1-negN/A
associate-*r*N/A
associate-+r+N/A
Applied rewrites69.0%
if 3.6e-95 < y Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
*-lft-identityN/A
metadata-evalN/A
fabs-subN/A
fp-cancel-sign-sub-invN/A
lower-fabs.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f6472.8
Applied rewrites72.8%
Taylor expanded in x around 0
Applied rewrites71.6%
(FPCore (x y) :precision binary64 (if (<= y -9.8e-112) (* -0.5 y) (* 1.5 x)))
double code(double x, double y) {
double tmp;
if (y <= -9.8e-112) {
tmp = -0.5 * y;
} else {
tmp = 1.5 * x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-9.8d-112)) then
tmp = (-0.5d0) * y
else
tmp = 1.5d0 * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -9.8e-112) {
tmp = -0.5 * y;
} else {
tmp = 1.5 * x;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -9.8e-112: tmp = -0.5 * y else: tmp = 1.5 * x return tmp
function code(x, y) tmp = 0.0 if (y <= -9.8e-112) tmp = Float64(-0.5 * y); else tmp = Float64(1.5 * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -9.8e-112) tmp = -0.5 * y; else tmp = 1.5 * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -9.8e-112], N[(-0.5 * y), $MachinePrecision], N[(1.5 * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -9.8 \cdot 10^{-112}:\\
\;\;\;\;-0.5 \cdot y\\
\mathbf{else}:\\
\;\;\;\;1.5 \cdot x\\
\end{array}
\end{array}
if y < -9.7999999999999996e-112Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
unpow1N/A
metadata-evalN/A
sqrt-pow1N/A
pow2N/A
sqrt-prodN/A
lower-fma.f64N/A
Applied rewrites74.0%
Taylor expanded in x around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
lower-*.f6465.7
Applied rewrites65.7%
if -9.7999999999999996e-112 < y Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
unpow1N/A
metadata-evalN/A
sqrt-pow1N/A
pow2N/A
sqrt-prodN/A
lower-fma.f64N/A
Applied rewrites28.4%
Taylor expanded in x around inf
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
metadata-evalN/A
metadata-evalN/A
lower-*.f6432.6
Applied rewrites32.6%
(FPCore (x y) :precision binary64 (* -0.5 y))
double code(double x, double y) {
return -0.5 * y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (-0.5d0) * y
end function
public static double code(double x, double y) {
return -0.5 * y;
}
def code(x, y): return -0.5 * y
function code(x, y) return Float64(-0.5 * y) end
function tmp = code(x, y) tmp = -0.5 * y; end
code[x_, y_] := N[(-0.5 * y), $MachinePrecision]
\begin{array}{l}
\\
-0.5 \cdot y
\end{array}
Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
unpow1N/A
metadata-evalN/A
sqrt-pow1N/A
pow2N/A
sqrt-prodN/A
lower-fma.f64N/A
Applied rewrites45.8%
Taylor expanded in x around 0
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
lower-*.f6428.3
Applied rewrites28.3%
herbie shell --seed 2024326
(FPCore (x y)
:name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
:precision binary64
(+ x (/ (fabs (- y x)) 2.0)))