
(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: b, c, and d should be sorted in increasing order before calling this function. (FPCore (a b c d) :precision binary64 (fma d a (* a (+ b c))))
assert(b < c && c < d);
double code(double a, double b, double c, double d) {
return fma(d, a, (a * (b + c)));
}
b, c, d = sort([b, c, d]) function code(a, b, c, d) return fma(d, a, Float64(a * Float64(b + c))) end
NOTE: b, c, and d should be sorted in increasing order before calling this function. code[a_, b_, c_, d_] := N[(d * a + N[(a * N[(b + c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[b, c, d] = \mathsf{sort}([b, c, d])\\
\\
\mathsf{fma}\left(d, a, a \cdot \left(b + c\right)\right)
\end{array}
Initial program 100.0%
+-commutative100.0%
distribute-rgt-in100.0%
fma-def100.0%
*-commutative100.0%
Applied egg-rr100.0%
Final simplification100.0%
NOTE: b, c, and d should be sorted in increasing order before calling this function. (FPCore (a b c d) :precision binary64 (* a (+ d (+ b c))))
assert(b < c && c < d);
double code(double a, double b, double c, double d) {
return a * (d + (b + c));
}
NOTE: 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 + (b + c))
end function
assert b < c && c < d;
public static double code(double a, double b, double c, double d) {
return a * (d + (b + c));
}
[b, c, d] = sort([b, c, d]) def code(a, b, c, d): return a * (d + (b + c))
b, c, d = sort([b, c, d]) function code(a, b, c, d) return Float64(a * Float64(d + Float64(b + c))) end
b, c, d = num2cell(sort([b, c, d])){:}
function tmp = code(a, b, c, d)
tmp = a * (d + (b + c));
end
NOTE: b, c, and d should be sorted in increasing order before calling this function. code[a_, b_, c_, d_] := N[(a * N[(d + N[(b + c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[b, c, d] = \mathsf{sort}([b, c, d])\\
\\
a \cdot \left(d + \left(b + c\right)\right)
\end{array}
Initial program 100.0%
Final simplification100.0%
NOTE: 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(b < c && c < d);
double code(double a, double b, double c, double d) {
return a * (d + c);
}
NOTE: 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 b < c && c < d;
public static double code(double a, double b, double c, double d) {
return a * (d + c);
}
[b, c, d] = sort([b, c, d]) def code(a, b, c, d): return a * (d + c)
b, c, d = sort([b, c, d]) function code(a, b, c, d) return Float64(a * Float64(d + c)) end
b, c, d = num2cell(sort([b, c, d])){:}
function tmp = code(a, b, c, d)
tmp = a * (d + c);
end
NOTE: 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}
[b, c, d] = \mathsf{sort}([b, c, d])\\
\\
a \cdot \left(d + c\right)
\end{array}
Initial program 100.0%
Taylor expanded in b around 0 62.6%
Final simplification62.6%
NOTE: b, c, and d should be sorted in increasing order before calling this function. (FPCore (a b c d) :precision binary64 (* d a))
assert(b < c && c < d);
double code(double a, double b, double c, double d) {
return d * a;
}
NOTE: 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 b < c && c < d;
public static double code(double a, double b, double c, double d) {
return d * a;
}
[b, c, d] = sort([b, c, d]) def code(a, b, c, d): return d * a
b, c, d = sort([b, c, d]) function code(a, b, c, d) return Float64(d * a) end
b, c, d = num2cell(sort([b, c, d])){:}
function tmp = code(a, b, c, d)
tmp = d * a;
end
NOTE: 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}
[b, c, d] = \mathsf{sort}([b, c, d])\\
\\
d \cdot a
\end{array}
Initial program 100.0%
Taylor expanded in d around inf 30.5%
*-commutative30.5%
Simplified30.5%
Final simplification30.5%
(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 2023178
(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)))
:herbie-target
(+ (* a b) (* a (+ c d)))
(* a (+ (+ b c) d)))