
(FPCore (x y) :precision binary64 (/ (+ x y) (+ y 1.0)))
double code(double x, double y) {
return (x + y) / (y + 1.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) / (y + 1.0d0)
end function
public static double code(double x, double y) {
return (x + y) / (y + 1.0);
}
def code(x, y): return (x + y) / (y + 1.0)
function code(x, y) return Float64(Float64(x + y) / Float64(y + 1.0)) end
function tmp = code(x, y) tmp = (x + y) / (y + 1.0); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(y + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{y + 1}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (/ (+ x y) (+ y 1.0)))
double code(double x, double y) {
return (x + y) / (y + 1.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) / (y + 1.0d0)
end function
public static double code(double x, double y) {
return (x + y) / (y + 1.0);
}
def code(x, y): return (x + y) / (y + 1.0)
function code(x, y) return Float64(Float64(x + y) / Float64(y + 1.0)) end
function tmp = code(x, y) tmp = (x + y) / (y + 1.0); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(y + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{y + 1}
\end{array}
(FPCore (x y) :precision binary64 (/ (+ y x) (+ 1.0 y)))
double code(double x, double y) {
return (y + x) / (1.0 + y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y + x) / (1.0d0 + y)
end function
public static double code(double x, double y) {
return (y + x) / (1.0 + y);
}
def code(x, y): return (y + x) / (1.0 + y)
function code(x, y) return Float64(Float64(y + x) / Float64(1.0 + y)) end
function tmp = code(x, y) tmp = (y + x) / (1.0 + y); end
code[x_, y_] := N[(N[(y + x), $MachinePrecision] / N[(1.0 + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{y + x}{1 + y}
\end{array}
Initial program 100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(let* ((t_0 (/ (+ y x) (+ 1.0 y))) (t_1 (/ x (+ 1.0 y))))
(if (<= t_0 -4e+34)
t_1
(if (<= t_0 0.2)
(fma (- y) (- y 1.0) x)
(if (<= t_0 2.0) (/ y (+ 1.0 y)) t_1)))))
double code(double x, double y) {
double t_0 = (y + x) / (1.0 + y);
double t_1 = x / (1.0 + y);
double tmp;
if (t_0 <= -4e+34) {
tmp = t_1;
} else if (t_0 <= 0.2) {
tmp = fma(-y, (y - 1.0), x);
} else if (t_0 <= 2.0) {
tmp = y / (1.0 + y);
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y) t_0 = Float64(Float64(y + x) / Float64(1.0 + y)) t_1 = Float64(x / Float64(1.0 + y)) tmp = 0.0 if (t_0 <= -4e+34) tmp = t_1; elseif (t_0 <= 0.2) tmp = fma(Float64(-y), Float64(y - 1.0), x); elseif (t_0 <= 2.0) tmp = Float64(y / Float64(1.0 + y)); else tmp = t_1; end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(N[(y + x), $MachinePrecision] / N[(1.0 + y), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(x / N[(1.0 + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -4e+34], t$95$1, If[LessEqual[t$95$0, 0.2], N[((-y) * N[(y - 1.0), $MachinePrecision] + x), $MachinePrecision], If[LessEqual[t$95$0, 2.0], N[(y / N[(1.0 + y), $MachinePrecision]), $MachinePrecision], t$95$1]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{y + x}{1 + y}\\
t_1 := \frac{x}{1 + y}\\
\mathbf{if}\;t\_0 \leq -4 \cdot 10^{+34}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 0.2:\\
\;\;\;\;\mathsf{fma}\left(-y, y - 1, x\right)\\
\mathbf{elif}\;t\_0 \leq 2:\\
\;\;\;\;\frac{y}{1 + y}\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (/.f64 (+.f64 x y) (+.f64 y #s(literal 1 binary64))) < -3.99999999999999978e34 or 2 < (/.f64 (+.f64 x y) (+.f64 y #s(literal 1 binary64))) Initial program 100.0%
Taylor expanded in x around inf
lower-/.f64N/A
lower-+.f64100.0
Applied rewrites100.0%
if -3.99999999999999978e34 < (/.f64 (+.f64 x y) (+.f64 y #s(literal 1 binary64))) < 0.20000000000000001Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
sub-negN/A
+-commutativeN/A
mul-1-negN/A
associate-+l+N/A
distribute-lft-inN/A
+-commutativeN/A
distribute-rgt-inN/A
associate-*l*N/A
*-commutativeN/A
metadata-evalN/A
associate-*r*N/A
distribute-lft-inN/A
*-commutativeN/A
distribute-lft-inN/A
metadata-evalN/A
sub-negN/A
distribute-rgt-outN/A
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites100.0%
if 0.20000000000000001 < (/.f64 (+.f64 x y) (+.f64 y #s(literal 1 binary64))) < 2Initial program 100.0%
Taylor expanded in x around 0
lower-/.f64N/A
lower-+.f6499.1
Applied rewrites99.1%
Final simplification99.6%
(FPCore (x y) :precision binary64 (let* ((t_0 (/ x (+ 1.0 y)))) (if (<= x -5200000000.0) t_0 (if (<= x 3.3e-22) (fma 1.0 y x) t_0))))
double code(double x, double y) {
double t_0 = x / (1.0 + y);
double tmp;
if (x <= -5200000000.0) {
tmp = t_0;
} else if (x <= 3.3e-22) {
tmp = fma(1.0, y, x);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y) t_0 = Float64(x / Float64(1.0 + y)) tmp = 0.0 if (x <= -5200000000.0) tmp = t_0; elseif (x <= 3.3e-22) tmp = fma(1.0, y, x); else tmp = t_0; end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(x / N[(1.0 + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -5200000000.0], t$95$0, If[LessEqual[x, 3.3e-22], N[(1.0 * y + x), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x}{1 + y}\\
\mathbf{if}\;x \leq -5200000000:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \leq 3.3 \cdot 10^{-22}:\\
\;\;\;\;\mathsf{fma}\left(1, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if x < -5.2e9 or 3.3000000000000001e-22 < x Initial program 100.0%
Taylor expanded in x around inf
lower-/.f64N/A
lower-+.f6473.3
Applied rewrites73.3%
if -5.2e9 < x < 3.3000000000000001e-22Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f6447.1
Applied rewrites47.1%
Taylor expanded in x around 0
Applied rewrites47.1%
(FPCore (x y) :precision binary64 (if (<= y -1.0) (/ x y) (if (<= y 2.8) (fma (- 1.0 x) y x) (/ x y))))
double code(double x, double y) {
double tmp;
if (y <= -1.0) {
tmp = x / y;
} else if (y <= 2.8) {
tmp = fma((1.0 - x), y, x);
} else {
tmp = x / y;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= -1.0) tmp = Float64(x / y); elseif (y <= 2.8) tmp = fma(Float64(1.0 - x), y, x); else tmp = Float64(x / y); end return tmp end
code[x_, y_] := If[LessEqual[y, -1.0], N[(x / y), $MachinePrecision], If[LessEqual[y, 2.8], N[(N[(1.0 - x), $MachinePrecision] * y + x), $MachinePrecision], N[(x / y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;y \leq 2.8:\\
\;\;\;\;\mathsf{fma}\left(1 - x, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if y < -1 or 2.7999999999999998 < y Initial program 100.0%
Taylor expanded in x around inf
lower-/.f64N/A
lower-+.f6426.3
Applied rewrites26.3%
Taylor expanded in y around inf
Applied rewrites24.1%
if -1 < y < 2.7999999999999998Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f6499.8
Applied rewrites99.8%
(FPCore (x y) :precision binary64 (fma 1.0 y x))
double code(double x, double y) {
return fma(1.0, y, x);
}
function code(x, y) return fma(1.0, y, x) end
code[x_, y_] := N[(1.0 * y + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(1, y, x\right)
\end{array}
Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f6447.7
Applied rewrites47.7%
Taylor expanded in x around 0
Applied rewrites48.3%
(FPCore (x y) :precision binary64 (* 1.0 y))
double code(double x, double y) {
return 1.0 * y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0 * y
end function
public static double code(double x, double y) {
return 1.0 * y;
}
def code(x, y): return 1.0 * y
function code(x, y) return Float64(1.0 * y) end
function tmp = code(x, y) tmp = 1.0 * y; end
code[x_, y_] := N[(1.0 * y), $MachinePrecision]
\begin{array}{l}
\\
1 \cdot y
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
lower-/.f64N/A
lower-+.f6451.3
Applied rewrites51.3%
Taylor expanded in y around 0
Applied rewrites11.0%
Taylor expanded in y around inf
Applied rewrites2.4%
Taylor expanded in y around 0
Applied rewrites12.2%
herbie shell --seed 2024294
(FPCore (x y)
:name "Data.Colour.SRGB:invTransferFunction from colour-2.3.3"
:precision binary64
(/ (+ x y) (+ y 1.0)))