
(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 8 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%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-fma.f64N/A
metadata-eval100.0
Applied rewrites100.0%
(FPCore (x y) :precision binary64 (if (<= y -2.6e-154) (fma x 1.5 (* y -0.5)) (if (<= y 5.4e-105) (fma (fabs (- x)) 0.5 x) (* (fabs (- y x)) 0.5))))
double code(double x, double y) {
double tmp;
if (y <= -2.6e-154) {
tmp = fma(x, 1.5, (y * -0.5));
} else if (y <= 5.4e-105) {
tmp = fma(fabs(-x), 0.5, x);
} else {
tmp = fabs((y - x)) * 0.5;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= -2.6e-154) tmp = fma(x, 1.5, Float64(y * -0.5)); elseif (y <= 5.4e-105) tmp = fma(abs(Float64(-x)), 0.5, x); else tmp = Float64(abs(Float64(y - x)) * 0.5); end return tmp end
code[x_, y_] := If[LessEqual[y, -2.6e-154], N[(x * 1.5 + N[(y * -0.5), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 5.4e-105], N[(N[Abs[(-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 -2.6 \cdot 10^{-154}:\\
\;\;\;\;\mathsf{fma}\left(x, 1.5, y \cdot -0.5\right)\\
\mathbf{elif}\;y \leq 5.4 \cdot 10^{-105}:\\
\;\;\;\;\mathsf{fma}\left(\left|-x\right|, 0.5, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left|y - x\right| \cdot 0.5\\
\end{array}
\end{array}
if y < -2.6e-154Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-fma.f64N/A
metadata-eval99.9
Applied rewrites99.9%
unpow1N/A
metadata-evalN/A
pow-prod-upN/A
lower-*.f64N/A
unpow1/2N/A
lower-sqrt.f64N/A
unpow1/2N/A
lower-sqrt.f6499.3
Applied rewrites99.3%
lift-*.f64N/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrt99.9
lift--.f64N/A
lift-fabs.f64N/A
fabs-subN/A
lift--.f64N/A
rem-sqrt-squareN/A
sqrt-prodN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f6483.0
Applied rewrites83.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6486.5
Applied rewrites86.5%
if -2.6e-154 < y < 5.39999999999999985e-105Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-fma.f64N/A
metadata-eval99.9
Applied rewrites99.9%
Taylor expanded in y around 0
mul-1-negN/A
lower-neg.f6490.3
Applied rewrites90.3%
if 5.39999999999999985e-105 < y Initial program 100.0%
Taylor expanded in x around 0
sub-negN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
remove-double-negN/A
mul-1-negN/A
distribute-neg-inN/A
+-commutativeN/A
lower-fabs.f64N/A
+-commutativeN/A
distribute-neg-inN/A
mul-1-negN/A
remove-double-negN/A
sub-negN/A
lower--.f6471.3
Applied rewrites71.3%
Final simplification81.9%
(FPCore (x y) :precision binary64 (if (<= y -2.6e-154) (fma (- x y) 0.5 x) (if (<= y 5.4e-105) (fma (fabs (- x)) 0.5 x) (* (fabs (- y x)) 0.5))))
double code(double x, double y) {
double tmp;
if (y <= -2.6e-154) {
tmp = fma((x - y), 0.5, x);
} else if (y <= 5.4e-105) {
tmp = fma(fabs(-x), 0.5, x);
} else {
tmp = fabs((y - x)) * 0.5;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= -2.6e-154) tmp = fma(Float64(x - y), 0.5, x); elseif (y <= 5.4e-105) tmp = fma(abs(Float64(-x)), 0.5, x); else tmp = Float64(abs(Float64(y - x)) * 0.5); end return tmp end
code[x_, y_] := If[LessEqual[y, -2.6e-154], N[(N[(x - y), $MachinePrecision] * 0.5 + x), $MachinePrecision], If[LessEqual[y, 5.4e-105], N[(N[Abs[(-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 -2.6 \cdot 10^{-154}:\\
\;\;\;\;\mathsf{fma}\left(x - y, 0.5, x\right)\\
\mathbf{elif}\;y \leq 5.4 \cdot 10^{-105}:\\
\;\;\;\;\mathsf{fma}\left(\left|-x\right|, 0.5, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left|y - x\right| \cdot 0.5\\
\end{array}
\end{array}
if y < -2.6e-154Initial program 99.9%
lift-/.f64N/A
clear-numN/A
lower-/.f64N/A
lower-/.f6499.8
Applied rewrites99.8%
lift-/.f64N/A
lift-/.f64N/A
clear-numN/A
lift--.f64N/A
lift-fabs.f64N/A
lower-+.f64N/A
+-commutativeN/A
Applied rewrites98.8%
lift-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-fabs.f64N/A
lift--.f64N/A
fabs-subN/A
lift--.f64N/A
rem-sqrt-squareN/A
sqrt-unprodN/A
rem-square-sqrtN/A
associate-*l*N/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrtN/A
lower-fma.f6486.4
Applied rewrites86.4%
if -2.6e-154 < y < 5.39999999999999985e-105Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-fma.f64N/A
metadata-eval99.9
Applied rewrites99.9%
Taylor expanded in y around 0
mul-1-negN/A
lower-neg.f6490.3
Applied rewrites90.3%
if 5.39999999999999985e-105 < y Initial program 100.0%
Taylor expanded in x around 0
sub-negN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
remove-double-negN/A
mul-1-negN/A
distribute-neg-inN/A
+-commutativeN/A
lower-fabs.f64N/A
+-commutativeN/A
distribute-neg-inN/A
mul-1-negN/A
remove-double-negN/A
sub-negN/A
lower--.f6471.3
Applied rewrites71.3%
Final simplification81.8%
(FPCore (x y) :precision binary64 (if (<= y 7.5e-112) (fma (- x y) 0.5 x) (* (fabs (- y x)) 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 7.5e-112) {
tmp = fma((x - y), 0.5, x);
} else {
tmp = fabs((y - x)) * 0.5;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= 7.5e-112) tmp = fma(Float64(x - y), 0.5, x); else tmp = Float64(abs(Float64(y - x)) * 0.5); end return tmp end
code[x_, y_] := If[LessEqual[y, 7.5e-112], N[(N[(x - y), $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 7.5 \cdot 10^{-112}:\\
\;\;\;\;\mathsf{fma}\left(x - y, 0.5, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left|y - x\right| \cdot 0.5\\
\end{array}
\end{array}
if y < 7.5000000000000002e-112Initial program 99.9%
lift-/.f64N/A
clear-numN/A
lower-/.f64N/A
lower-/.f6499.8
Applied rewrites99.8%
lift-/.f64N/A
lift-/.f64N/A
clear-numN/A
lift--.f64N/A
lift-fabs.f64N/A
lower-+.f64N/A
+-commutativeN/A
Applied rewrites98.9%
lift-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-fabs.f64N/A
lift--.f64N/A
fabs-subN/A
lift--.f64N/A
rem-sqrt-squareN/A
sqrt-unprodN/A
rem-square-sqrtN/A
associate-*l*N/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrtN/A
lower-fma.f6471.6
Applied rewrites71.6%
if 7.5000000000000002e-112 < y Initial program 100.0%
Taylor expanded in x around 0
sub-negN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
remove-double-negN/A
mul-1-negN/A
distribute-neg-inN/A
+-commutativeN/A
lower-fabs.f64N/A
+-commutativeN/A
distribute-neg-inN/A
mul-1-negN/A
remove-double-negN/A
sub-negN/A
lower--.f6471.3
Applied rewrites71.3%
Final simplification71.5%
(FPCore (x y) :precision binary64 (if (<= y -3.5e-163) (fma (- y) 0.5 x) (* x 1.5)))
double code(double x, double y) {
double tmp;
if (y <= -3.5e-163) {
tmp = fma(-y, 0.5, x);
} else {
tmp = x * 1.5;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= -3.5e-163) tmp = fma(Float64(-y), 0.5, x); else tmp = Float64(x * 1.5); end return tmp end
code[x_, y_] := If[LessEqual[y, -3.5e-163], N[((-y) * 0.5 + x), $MachinePrecision], N[(x * 1.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3.5 \cdot 10^{-163}:\\
\;\;\;\;\mathsf{fma}\left(-y, 0.5, x\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 1.5\\
\end{array}
\end{array}
if y < -3.50000000000000027e-163Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-fma.f64N/A
metadata-eval99.9
Applied rewrites99.9%
unpow1N/A
metadata-evalN/A
pow-prod-upN/A
lower-*.f64N/A
unpow1/2N/A
lower-sqrt.f64N/A
unpow1/2N/A
lower-sqrt.f6499.3
Applied rewrites99.3%
lift-*.f64N/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrt99.9
lift--.f64N/A
lift-fabs.f64N/A
fabs-subN/A
lift--.f64N/A
rem-sqrt-squareN/A
sqrt-prodN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f6483.0
Applied rewrites83.0%
Taylor expanded in x around 0
mul-1-negN/A
lower-neg.f6466.3
Applied rewrites66.3%
if -3.50000000000000027e-163 < y Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-fma.f64N/A
metadata-eval100.0
Applied rewrites100.0%
unpow1N/A
metadata-evalN/A
pow-prod-upN/A
lower-*.f64N/A
unpow1/2N/A
lower-sqrt.f64N/A
unpow1/2N/A
lower-sqrt.f6499.3
Applied rewrites99.3%
lift-*.f64N/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrt100.0
lift--.f64N/A
lift-fabs.f64N/A
fabs-subN/A
lift--.f64N/A
rem-sqrt-squareN/A
sqrt-prodN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f6427.6
Applied rewrites27.6%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6433.9
Applied rewrites33.9%
(FPCore (x y) :precision binary64 (if (<= y -2.15e-54) (* y -0.5) (* x 1.5)))
double code(double x, double y) {
double tmp;
if (y <= -2.15e-54) {
tmp = y * -0.5;
} else {
tmp = x * 1.5;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-2.15d-54)) then
tmp = y * (-0.5d0)
else
tmp = x * 1.5d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -2.15e-54) {
tmp = y * -0.5;
} else {
tmp = x * 1.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -2.15e-54: tmp = y * -0.5 else: tmp = x * 1.5 return tmp
function code(x, y) tmp = 0.0 if (y <= -2.15e-54) tmp = Float64(y * -0.5); else tmp = Float64(x * 1.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -2.15e-54) tmp = y * -0.5; else tmp = x * 1.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -2.15e-54], N[(y * -0.5), $MachinePrecision], N[(x * 1.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.15 \cdot 10^{-54}:\\
\;\;\;\;y \cdot -0.5\\
\mathbf{else}:\\
\;\;\;\;x \cdot 1.5\\
\end{array}
\end{array}
if y < -2.15e-54Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-fma.f64N/A
metadata-eval99.9
Applied rewrites99.9%
unpow1N/A
metadata-evalN/A
pow-prod-upN/A
lower-*.f64N/A
unpow1/2N/A
lower-sqrt.f64N/A
unpow1/2N/A
lower-sqrt.f6499.3
Applied rewrites99.3%
lift-*.f64N/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrt99.9
lift--.f64N/A
lift-fabs.f64N/A
fabs-subN/A
lift--.f64N/A
rem-sqrt-squareN/A
sqrt-prodN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f6486.1
Applied rewrites86.1%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6466.7
Applied rewrites66.7%
if -2.15e-54 < y Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-fma.f64N/A
metadata-eval100.0
Applied rewrites100.0%
unpow1N/A
metadata-evalN/A
pow-prod-upN/A
lower-*.f64N/A
unpow1/2N/A
lower-sqrt.f64N/A
unpow1/2N/A
lower-sqrt.f6499.3
Applied rewrites99.3%
lift-*.f64N/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrt100.0
lift--.f64N/A
lift-fabs.f64N/A
fabs-subN/A
lift--.f64N/A
rem-sqrt-squareN/A
sqrt-prodN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f6432.0
Applied rewrites32.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6434.8
Applied rewrites34.8%
(FPCore (x y) :precision binary64 (fma (- x y) 0.5 x))
double code(double x, double y) {
return fma((x - y), 0.5, x);
}
function code(x, y) return fma(Float64(x - y), 0.5, x) end
code[x_, y_] := N[(N[(x - y), $MachinePrecision] * 0.5 + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x - y, 0.5, x\right)
\end{array}
Initial program 100.0%
lift-/.f64N/A
clear-numN/A
lower-/.f64N/A
lower-/.f6499.8
Applied rewrites99.8%
lift-/.f64N/A
lift-/.f64N/A
clear-numN/A
lift--.f64N/A
lift-fabs.f64N/A
lower-+.f64N/A
+-commutativeN/A
Applied rewrites98.8%
lift-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-fabs.f64N/A
lift--.f64N/A
fabs-subN/A
lift--.f64N/A
rem-sqrt-squareN/A
sqrt-unprodN/A
rem-square-sqrtN/A
associate-*l*N/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrtN/A
lower-fma.f6449.7
Applied rewrites49.7%
(FPCore (x y) :precision binary64 (* x 1.5))
double code(double x, double y) {
return x * 1.5;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * 1.5d0
end function
public static double code(double x, double y) {
return x * 1.5;
}
def code(x, y): return x * 1.5
function code(x, y) return Float64(x * 1.5) end
function tmp = code(x, y) tmp = x * 1.5; end
code[x_, y_] := N[(x * 1.5), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 1.5
\end{array}
Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-fma.f64N/A
metadata-eval100.0
Applied rewrites100.0%
unpow1N/A
metadata-evalN/A
pow-prod-upN/A
lower-*.f64N/A
unpow1/2N/A
lower-sqrt.f64N/A
unpow1/2N/A
lower-sqrt.f6499.3
Applied rewrites99.3%
lift-*.f64N/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrt100.0
lift--.f64N/A
lift-fabs.f64N/A
fabs-subN/A
lift--.f64N/A
rem-sqrt-squareN/A
sqrt-prodN/A
lower-*.f64N/A
lower-sqrt.f64N/A
lower-sqrt.f6444.7
Applied rewrites44.7%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6431.6
Applied rewrites31.6%
herbie shell --seed 2024219
(FPCore (x y)
:name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
:precision binary64
(+ x (/ (fabs (- y x)) 2.0)))