
(FPCore (d1 d2) :precision binary64 (+ (+ (* d1 10.0) (* d1 d2)) (* d1 20.0)))
double code(double d1, double d2) {
return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
real(8) function code(d1, d2)
real(8), intent (in) :: d1
real(8), intent (in) :: d2
code = ((d1 * 10.0d0) + (d1 * d2)) + (d1 * 20.0d0)
end function
public static double code(double d1, double d2) {
return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
def code(d1, d2): return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0)
function code(d1, d2) return Float64(Float64(Float64(d1 * 10.0) + Float64(d1 * d2)) + Float64(d1 * 20.0)) end
function tmp = code(d1, d2) tmp = ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0); end
code[d1_, d2_] := N[(N[(N[(d1 * 10.0), $MachinePrecision] + N[(d1 * d2), $MachinePrecision]), $MachinePrecision] + N[(d1 * 20.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (d1 d2) :precision binary64 (+ (+ (* d1 10.0) (* d1 d2)) (* d1 20.0)))
double code(double d1, double d2) {
return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
real(8) function code(d1, d2)
real(8), intent (in) :: d1
real(8), intent (in) :: d2
code = ((d1 * 10.0d0) + (d1 * d2)) + (d1 * 20.0d0)
end function
public static double code(double d1, double d2) {
return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0);
}
def code(d1, d2): return ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0)
function code(d1, d2) return Float64(Float64(Float64(d1 * 10.0) + Float64(d1 * d2)) + Float64(d1 * 20.0)) end
function tmp = code(d1, d2) tmp = ((d1 * 10.0) + (d1 * d2)) + (d1 * 20.0); end
code[d1_, d2_] := N[(N[(N[(d1 * 10.0), $MachinePrecision] + N[(d1 * d2), $MachinePrecision]), $MachinePrecision] + N[(d1 * 20.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(d1 \cdot 10 + d1 \cdot d2\right) + d1 \cdot 20
\end{array}
(FPCore (d1 d2) :precision binary64 (fma d1 30.0 (* d2 d1)))
double code(double d1, double d2) {
return fma(d1, 30.0, (d2 * d1));
}
function code(d1, d2) return fma(d1, 30.0, Float64(d2 * d1)) end
code[d1_, d2_] := N[(d1 * 30.0 + N[(d2 * d1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(d1, 30, d2 \cdot d1\right)
\end{array}
Initial program 99.8%
lift-+.f64N/A
lift-+.f64N/A
associate-+l+N/A
+-commutativeN/A
associate-+r+N/A
lift-*.f64N/A
lift-*.f64N/A
distribute-lft-outN/A
lower-fma.f64N/A
metadata-eval100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
(FPCore (d1 d2) :precision binary64 (if (<= d2 -30.0) (* d2 d1) (if (<= d2 30.0) (* 30.0 d1) (* d2 d1))))
double code(double d1, double d2) {
double tmp;
if (d2 <= -30.0) {
tmp = d2 * d1;
} else if (d2 <= 30.0) {
tmp = 30.0 * d1;
} else {
tmp = d2 * d1;
}
return tmp;
}
real(8) function code(d1, d2)
real(8), intent (in) :: d1
real(8), intent (in) :: d2
real(8) :: tmp
if (d2 <= (-30.0d0)) then
tmp = d2 * d1
else if (d2 <= 30.0d0) then
tmp = 30.0d0 * d1
else
tmp = d2 * d1
end if
code = tmp
end function
public static double code(double d1, double d2) {
double tmp;
if (d2 <= -30.0) {
tmp = d2 * d1;
} else if (d2 <= 30.0) {
tmp = 30.0 * d1;
} else {
tmp = d2 * d1;
}
return tmp;
}
def code(d1, d2): tmp = 0 if d2 <= -30.0: tmp = d2 * d1 elif d2 <= 30.0: tmp = 30.0 * d1 else: tmp = d2 * d1 return tmp
function code(d1, d2) tmp = 0.0 if (d2 <= -30.0) tmp = Float64(d2 * d1); elseif (d2 <= 30.0) tmp = Float64(30.0 * d1); else tmp = Float64(d2 * d1); end return tmp end
function tmp_2 = code(d1, d2) tmp = 0.0; if (d2 <= -30.0) tmp = d2 * d1; elseif (d2 <= 30.0) tmp = 30.0 * d1; else tmp = d2 * d1; end tmp_2 = tmp; end
code[d1_, d2_] := If[LessEqual[d2, -30.0], N[(d2 * d1), $MachinePrecision], If[LessEqual[d2, 30.0], N[(30.0 * d1), $MachinePrecision], N[(d2 * d1), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d2 \leq -30:\\
\;\;\;\;d2 \cdot d1\\
\mathbf{elif}\;d2 \leq 30:\\
\;\;\;\;30 \cdot d1\\
\mathbf{else}:\\
\;\;\;\;d2 \cdot d1\\
\end{array}
\end{array}
if d2 < -30 or 30 < d2 Initial program 100.0%
lift-+.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
distribute-lft-outN/A
lift-*.f64N/A
distribute-lft-outN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
associate-+r+N/A
metadata-evalN/A
metadata-evalN/A
lower-+.f64N/A
metadata-eval100.0
Applied rewrites100.0%
lift-*.f64N/A
lift-+.f64N/A
flip-+N/A
associate-*l/N/A
lower-/.f64N/A
lower-*.f64N/A
lift-*.f64N/A
lower--.f64N/A
metadata-evalN/A
lower--.f6465.4
Applied rewrites65.4%
Taylor expanded in d2 around inf
lower-*.f6494.9
Applied rewrites94.9%
if -30 < d2 < 30Initial program 99.6%
Taylor expanded in d2 around 0
distribute-rgt-outN/A
metadata-evalN/A
*-commutativeN/A
lower-*.f6497.9
Applied rewrites97.9%
Final simplification96.5%
(FPCore (d1 d2) :precision binary64 (* (+ d2 30.0) d1))
double code(double d1, double d2) {
return (d2 + 30.0) * d1;
}
real(8) function code(d1, d2)
real(8), intent (in) :: d1
real(8), intent (in) :: d2
code = (d2 + 30.0d0) * d1
end function
public static double code(double d1, double d2) {
return (d2 + 30.0) * d1;
}
def code(d1, d2): return (d2 + 30.0) * d1
function code(d1, d2) return Float64(Float64(d2 + 30.0) * d1) end
function tmp = code(d1, d2) tmp = (d2 + 30.0) * d1; end
code[d1_, d2_] := N[(N[(d2 + 30.0), $MachinePrecision] * d1), $MachinePrecision]
\begin{array}{l}
\\
\left(d2 + 30\right) \cdot d1
\end{array}
Initial program 99.8%
lift-+.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
distribute-lft-outN/A
lift-*.f64N/A
distribute-lft-outN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
associate-+r+N/A
metadata-evalN/A
metadata-evalN/A
lower-+.f64N/A
metadata-eval100.0
Applied rewrites100.0%
Final simplification100.0%
(FPCore (d1 d2) :precision binary64 (* 30.0 d1))
double code(double d1, double d2) {
return 30.0 * d1;
}
real(8) function code(d1, d2)
real(8), intent (in) :: d1
real(8), intent (in) :: d2
code = 30.0d0 * d1
end function
public static double code(double d1, double d2) {
return 30.0 * d1;
}
def code(d1, d2): return 30.0 * d1
function code(d1, d2) return Float64(30.0 * d1) end
function tmp = code(d1, d2) tmp = 30.0 * d1; end
code[d1_, d2_] := N[(30.0 * d1), $MachinePrecision]
\begin{array}{l}
\\
30 \cdot d1
\end{array}
Initial program 99.8%
Taylor expanded in d2 around 0
distribute-rgt-outN/A
metadata-evalN/A
*-commutativeN/A
lower-*.f6454.2
Applied rewrites54.2%
(FPCore (d1 d2) :precision binary64 (* d1 (+ 30.0 d2)))
double code(double d1, double d2) {
return d1 * (30.0 + d2);
}
real(8) function code(d1, d2)
real(8), intent (in) :: d1
real(8), intent (in) :: d2
code = d1 * (30.0d0 + d2)
end function
public static double code(double d1, double d2) {
return d1 * (30.0 + d2);
}
def code(d1, d2): return d1 * (30.0 + d2)
function code(d1, d2) return Float64(d1 * Float64(30.0 + d2)) end
function tmp = code(d1, d2) tmp = d1 * (30.0 + d2); end
code[d1_, d2_] := N[(d1 * N[(30.0 + d2), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
d1 \cdot \left(30 + d2\right)
\end{array}
herbie shell --seed 2024331
(FPCore (d1 d2)
:name "FastMath test2"
:precision binary64
:alt
(! :herbie-platform default (* d1 (+ 30 d2)))
(+ (+ (* d1 10.0) (* d1 d2)) (* d1 20.0)))