
(FPCore (a b c d) :precision binary64 (* a (+ (+ b c) d)))
double code(double a, double b, double c, double d) {
return a * ((b + c) + d);
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = a * ((b + c) + d)
end function
public static double code(double a, double b, double c, double d) {
return a * ((b + c) + d);
}
def code(a, b, c, d): return a * ((b + c) + d)
function code(a, b, c, d) return Float64(a * Float64(Float64(b + c) + d)) end
function tmp = code(a, b, c, d) tmp = a * ((b + c) + d); end
code[a_, b_, c_, d_] := N[(a * N[(N[(b + c), $MachinePrecision] + d), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
a \cdot \left(\left(b + c\right) + d\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c d) :precision binary64 (* a (+ (+ b c) d)))
double code(double a, double b, double c, double d) {
return a * ((b + c) + d);
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = a * ((b + c) + d)
end function
public static double code(double a, double b, double c, double d) {
return a * ((b + c) + d);
}
def code(a, b, c, d): return a * ((b + c) + d)
function code(a, b, c, d) return Float64(a * Float64(Float64(b + c) + d)) end
function tmp = code(a, b, c, d) tmp = a * ((b + c) + d); end
code[a_, b_, c_, d_] := N[(a * N[(N[(b + c), $MachinePrecision] + d), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
a \cdot \left(\left(b + c\right) + d\right)
\end{array}
NOTE: a, b, c, and d should be sorted in increasing order before calling this function. (FPCore (a b c d) :precision binary64 (fma (+ d b) a (* c a)))
assert(a < b && b < c && c < d);
double code(double a, double b, double c, double d) {
return fma((d + b), a, (c * a));
}
a, b, c, d = sort([a, b, c, d]) function code(a, b, c, d) return fma(Float64(d + b), a, Float64(c * a)) end
NOTE: a, b, c, and d should be sorted in increasing order before calling this function. code[a_, b_, c_, d_] := N[(N[(d + b), $MachinePrecision] * a + N[(c * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b, c, d] = \mathsf{sort}([a, b, c, d])\\
\\
\mathsf{fma}\left(d + b, a, c \cdot a\right)
\end{array}
Initial program 99.9%
lift-*.f64N/A
lift-+.f64N/A
+-commutativeN/A
lift-+.f64N/A
associate-+r+N/A
distribute-rgt-inN/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-+.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
NOTE: a, b, c, and d should be sorted in increasing order before calling this function. (FPCore (a b c d) :precision binary64 (* a (+ (+ b c) d)))
assert(a < b && b < c && c < d);
double code(double a, double b, double c, double d) {
return a * ((b + c) + d);
}
NOTE: a, b, c, and d should be sorted in increasing order before calling this function.
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = a * ((b + c) + d)
end function
assert a < b && b < c && c < d;
public static double code(double a, double b, double c, double d) {
return a * ((b + c) + d);
}
[a, b, c, d] = sort([a, b, c, d]) def code(a, b, c, d): return a * ((b + c) + d)
a, b, c, d = sort([a, b, c, d]) function code(a, b, c, d) return Float64(a * Float64(Float64(b + c) + d)) end
a, b, c, d = num2cell(sort([a, b, c, d])){:}
function tmp = code(a, b, c, d)
tmp = a * ((b + c) + d);
end
NOTE: a, b, c, and d should be sorted in increasing order before calling this function. code[a_, b_, c_, d_] := N[(a * N[(N[(b + c), $MachinePrecision] + d), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b, c, d] = \mathsf{sort}([a, b, c, d])\\
\\
a \cdot \left(\left(b + c\right) + d\right)
\end{array}
Initial program 99.9%
NOTE: a, b, c, and d should be sorted in increasing order before calling this function. (FPCore (a b c d) :precision binary64 (* a (+ d c)))
assert(a < b && b < c && c < d);
double code(double a, double b, double c, double d) {
return a * (d + c);
}
NOTE: a, b, c, and d should be sorted in increasing order before calling this function.
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = a * (d + c)
end function
assert a < b && b < c && c < d;
public static double code(double a, double b, double c, double d) {
return a * (d + c);
}
[a, b, c, d] = sort([a, b, c, d]) def code(a, b, c, d): return a * (d + c)
a, b, c, d = sort([a, b, c, d]) function code(a, b, c, d) return Float64(a * Float64(d + c)) end
a, b, c, d = num2cell(sort([a, b, c, d])){:}
function tmp = code(a, b, c, d)
tmp = a * (d + c);
end
NOTE: a, b, c, and d should be sorted in increasing order before calling this function. code[a_, b_, c_, d_] := N[(a * N[(d + c), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b, c, d] = \mathsf{sort}([a, b, c, d])\\
\\
a \cdot \left(d + c\right)
\end{array}
Initial program 99.9%
Taylor expanded in b around 0
+-commutativeN/A
lower-+.f6466.7
Applied rewrites66.7%
NOTE: a, b, c, and d should be sorted in increasing order before calling this function. (FPCore (a b c d) :precision binary64 (* d a))
assert(a < b && b < c && c < d);
double code(double a, double b, double c, double d) {
return d * a;
}
NOTE: a, b, c, and d should be sorted in increasing order before calling this function.
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = d * a
end function
assert a < b && b < c && c < d;
public static double code(double a, double b, double c, double d) {
return d * a;
}
[a, b, c, d] = sort([a, b, c, d]) def code(a, b, c, d): return d * a
a, b, c, d = sort([a, b, c, d]) function code(a, b, c, d) return Float64(d * a) end
a, b, c, d = num2cell(sort([a, b, c, d])){:}
function tmp = code(a, b, c, d)
tmp = d * a;
end
NOTE: a, b, c, and d should be sorted in increasing order before calling this function. code[a_, b_, c_, d_] := N[(d * a), $MachinePrecision]
\begin{array}{l}
[a, b, c, d] = \mathsf{sort}([a, b, c, d])\\
\\
d \cdot a
\end{array}
Initial program 99.9%
Taylor expanded in d around inf
*-commutativeN/A
lower-*.f6440.4
Applied rewrites40.4%
(FPCore (a b c d) :precision binary64 (+ (* a b) (* a (+ c d))))
double code(double a, double b, double c, double d) {
return (a * b) + (a * (c + d));
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = (a * b) + (a * (c + d))
end function
public static double code(double a, double b, double c, double d) {
return (a * b) + (a * (c + d));
}
def code(a, b, c, d): return (a * b) + (a * (c + d))
function code(a, b, c, d) return Float64(Float64(a * b) + Float64(a * Float64(c + d))) end
function tmp = code(a, b, c, d) tmp = (a * b) + (a * (c + d)); end
code[a_, b_, c_, d_] := N[(N[(a * b), $MachinePrecision] + N[(a * N[(c + d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
a \cdot b + a \cdot \left(c + d\right)
\end{array}
herbie shell --seed 2024340
(FPCore (a b c d)
:name "Expression, p14"
:precision binary64
:pre (and (and (and (and (<= 56789.0 a) (<= a 98765.0)) (and (<= 0.0 b) (<= b 1.0))) (and (<= 0.0 c) (<= c 0.0016773))) (and (<= 0.0 d) (<= d 0.0016773)))
:alt
(! :herbie-platform default (+ (* a b) (* a (+ c d))))
(* a (+ (+ b c) d)))