
(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.3%
lift-+.f64N/A
lift-+.f64N/A
+-commutativeN/A
associate-+l+N/A
+-commutativeN/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 (or (<= d2 -30.0) (not (<= d2 30.0))) (* d2 d1) (* 30.0 d1)))
double code(double d1, double d2) {
double tmp;
if ((d2 <= -30.0) || !(d2 <= 30.0)) {
tmp = d2 * d1;
} else {
tmp = 30.0 * 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)) .or. (.not. (d2 <= 30.0d0))) then
tmp = d2 * d1
else
tmp = 30.0d0 * d1
end if
code = tmp
end function
public static double code(double d1, double d2) {
double tmp;
if ((d2 <= -30.0) || !(d2 <= 30.0)) {
tmp = d2 * d1;
} else {
tmp = 30.0 * d1;
}
return tmp;
}
def code(d1, d2): tmp = 0 if (d2 <= -30.0) or not (d2 <= 30.0): tmp = d2 * d1 else: tmp = 30.0 * d1 return tmp
function code(d1, d2) tmp = 0.0 if ((d2 <= -30.0) || !(d2 <= 30.0)) tmp = Float64(d2 * d1); else tmp = Float64(30.0 * d1); end return tmp end
function tmp_2 = code(d1, d2) tmp = 0.0; if ((d2 <= -30.0) || ~((d2 <= 30.0))) tmp = d2 * d1; else tmp = 30.0 * d1; end tmp_2 = tmp; end
code[d1_, d2_] := If[Or[LessEqual[d2, -30.0], N[Not[LessEqual[d2, 30.0]], $MachinePrecision]], N[(d2 * d1), $MachinePrecision], N[(30.0 * d1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d2 \leq -30 \lor \neg \left(d2 \leq 30\right):\\
\;\;\;\;d2 \cdot d1\\
\mathbf{else}:\\
\;\;\;\;30 \cdot d1\\
\end{array}
\end{array}
if d2 < -30 or 30 < d2 Initial program 99.1%
Taylor expanded in d2 around 0
distribute-rgt-outN/A
metadata-evalN/A
*-commutativeN/A
lower-*.f643.6
Applied rewrites3.6%
Applied rewrites12.6%
Taylor expanded in d2 around inf
*-commutativeN/A
lower-*.f6498.8
Applied rewrites98.8%
if -30 < d2 < 30Initial program 99.5%
Taylor expanded in d2 around 0
distribute-rgt-outN/A
metadata-evalN/A
*-commutativeN/A
lower-*.f6498.4
Applied rewrites98.4%
Final simplification98.6%
(FPCore (d1 d2) :precision binary64 (* (+ 30.0 d2) d1))
double code(double d1, double d2) {
return (30.0 + d2) * d1;
}
real(8) function code(d1, d2)
real(8), intent (in) :: d1
real(8), intent (in) :: d2
code = (30.0d0 + d2) * d1
end function
public static double code(double d1, double d2) {
return (30.0 + d2) * d1;
}
def code(d1, d2): return (30.0 + d2) * d1
function code(d1, d2) return Float64(Float64(30.0 + d2) * d1) end
function tmp = code(d1, d2) tmp = (30.0 + d2) * d1; end
code[d1_, d2_] := N[(N[(30.0 + d2), $MachinePrecision] * d1), $MachinePrecision]
\begin{array}{l}
\\
\left(30 + d2\right) \cdot d1
\end{array}
Initial program 99.3%
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%
(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.3%
Taylor expanded in d2 around 0
distribute-rgt-outN/A
metadata-evalN/A
*-commutativeN/A
lower-*.f6452.9
Applied rewrites52.9%
(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 2024339
(FPCore (d1 d2)
:name "FastMath test2"
:precision binary64
:alt
(! :herbie-platform default (* d1 (+ 30 d2)))
(+ (+ (* d1 10.0) (* d1 d2)) (* d1 20.0)))