(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); }
module fmin_fmax_functions implicit none private public fmax public fmin interface fmax module procedure fmax88 module procedure fmax44 module procedure fmax84 module procedure fmax48 end interface interface fmin module procedure fmin88 module procedure fmin44 module procedure fmin84 module procedure fmin48 end interface contains real(8) function fmax88(x, y) result (res) real(8), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(x, max(x, y), y /= y), x /= x) end function real(4) function fmax44(x, y) result (res) real(4), intent (in) :: x real(4), intent (in) :: y res = merge(y, merge(x, max(x, y), y /= y), x /= x) end function real(8) function fmax84(x, y) result(res) real(8), intent (in) :: x real(4), intent (in) :: y res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x) end function real(8) function fmax48(x, y) result(res) real(4), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x) end function real(8) function fmin88(x, y) result (res) real(8), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(x, min(x, y), y /= y), x /= x) end function real(4) function fmin44(x, y) result (res) real(4), intent (in) :: x real(4), intent (in) :: y res = merge(y, merge(x, min(x, y), y /= y), x /= x) end function real(8) function fmin84(x, y) result(res) real(8), intent (in) :: x real(4), intent (in) :: y res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x) end function real(8) function fmin48(x, y) result(res) real(4), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x) end function end module real(8) function code(a, b, c, d) use fmin_fmax_functions 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); }
module fmin_fmax_functions implicit none private public fmax public fmin interface fmax module procedure fmax88 module procedure fmax44 module procedure fmax84 module procedure fmax48 end interface interface fmin module procedure fmin88 module procedure fmin44 module procedure fmin84 module procedure fmin48 end interface contains real(8) function fmax88(x, y) result (res) real(8), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(x, max(x, y), y /= y), x /= x) end function real(4) function fmax44(x, y) result (res) real(4), intent (in) :: x real(4), intent (in) :: y res = merge(y, merge(x, max(x, y), y /= y), x /= x) end function real(8) function fmax84(x, y) result(res) real(8), intent (in) :: x real(4), intent (in) :: y res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x) end function real(8) function fmax48(x, y) result(res) real(4), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x) end function real(8) function fmin88(x, y) result (res) real(8), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(x, min(x, y), y /= y), x /= x) end function real(4) function fmin44(x, y) result (res) real(4), intent (in) :: x real(4), intent (in) :: y res = merge(y, merge(x, min(x, y), y /= y), x /= x) end function real(8) function fmin84(x, y) result(res) real(8), intent (in) :: x real(4), intent (in) :: y res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x) end function real(8) function fmin48(x, y) result(res) real(4), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x) end function end module real(8) function code(a, b, c, d) use fmin_fmax_functions 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-*.f64
N/A
lift-+.f64
N/A
lift-+.f64
N/A
associate-+r+
N/A
+-commutative
N/A
associate-+r+
N/A
distribute-lft-out
N/A
*-commutative
N/A
lower-fma.f64
N/A
+-commutative
N/A
lower-+.f64
N/A
*-commutative
N/A
lower-*.f64
99.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. module fmin_fmax_functions implicit none private public fmax public fmin interface fmax module procedure fmax88 module procedure fmax44 module procedure fmax84 module procedure fmax48 end interface interface fmin module procedure fmin88 module procedure fmin44 module procedure fmin84 module procedure fmin48 end interface contains real(8) function fmax88(x, y) result (res) real(8), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(x, max(x, y), y /= y), x /= x) end function real(4) function fmax44(x, y) result (res) real(4), intent (in) :: x real(4), intent (in) :: y res = merge(y, merge(x, max(x, y), y /= y), x /= x) end function real(8) function fmax84(x, y) result(res) real(8), intent (in) :: x real(4), intent (in) :: y res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x) end function real(8) function fmax48(x, y) result(res) real(4), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x) end function real(8) function fmin88(x, y) result (res) real(8), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(x, min(x, y), y /= y), x /= x) end function real(4) function fmin44(x, y) result (res) real(4), intent (in) :: x real(4), intent (in) :: y res = merge(y, merge(x, min(x, y), y /= y), x /= x) end function real(8) function fmin84(x, y) result(res) real(8), intent (in) :: x real(4), intent (in) :: y res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x) end function real(8) function fmin48(x, y) result(res) real(4), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x) end function end module real(8) function code(a, b, c, d) use fmin_fmax_functions 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 (+ c d)))
assert(a < b && b < c && c < d); double code(double a, double b, double c, double d) { return a * (c + d); }
NOTE: a, b, c, and d should be sorted in increasing order before calling this function. module fmin_fmax_functions implicit none private public fmax public fmin interface fmax module procedure fmax88 module procedure fmax44 module procedure fmax84 module procedure fmax48 end interface interface fmin module procedure fmin88 module procedure fmin44 module procedure fmin84 module procedure fmin48 end interface contains real(8) function fmax88(x, y) result (res) real(8), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(x, max(x, y), y /= y), x /= x) end function real(4) function fmax44(x, y) result (res) real(4), intent (in) :: x real(4), intent (in) :: y res = merge(y, merge(x, max(x, y), y /= y), x /= x) end function real(8) function fmax84(x, y) result(res) real(8), intent (in) :: x real(4), intent (in) :: y res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x) end function real(8) function fmax48(x, y) result(res) real(4), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x) end function real(8) function fmin88(x, y) result (res) real(8), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(x, min(x, y), y /= y), x /= x) end function real(4) function fmin44(x, y) result (res) real(4), intent (in) :: x real(4), intent (in) :: y res = merge(y, merge(x, min(x, y), y /= y), x /= x) end function real(8) function fmin84(x, y) result(res) real(8), intent (in) :: x real(4), intent (in) :: y res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x) end function real(8) function fmin48(x, y) result(res) real(4), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x) end function end module real(8) function code(a, b, c, d) use fmin_fmax_functions real(8), intent (in) :: a real(8), intent (in) :: b real(8), intent (in) :: c real(8), intent (in) :: d code = a * (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 * (c + d); }
[a, b, c, d] = sort([a, b, c, d]) def code(a, b, c, d): return a * (c + d)
a, b, c, d = sort([a, b, c, d]) function code(a, b, c, d) return Float64(a * Float64(c + d)) end
a, b, c, d = num2cell(sort([a, b, c, d])){:} function tmp = code(a, b, c, d) tmp = a * (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[(c + d), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} [a, b, c, d] = \mathsf{sort}([a, b, c, d])\\ \\ a \cdot \left(c + d\right) \end{array}
Initial program 99.9%
Taylor expanded in b around 0
Applied rewrites69.8%
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))
assert(a < b && b < c && c < d); double code(double a, double b, double c, double d) { return a * d; }
NOTE: a, b, c, and d should be sorted in increasing order before calling this function. module fmin_fmax_functions implicit none private public fmax public fmin interface fmax module procedure fmax88 module procedure fmax44 module procedure fmax84 module procedure fmax48 end interface interface fmin module procedure fmin88 module procedure fmin44 module procedure fmin84 module procedure fmin48 end interface contains real(8) function fmax88(x, y) result (res) real(8), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(x, max(x, y), y /= y), x /= x) end function real(4) function fmax44(x, y) result (res) real(4), intent (in) :: x real(4), intent (in) :: y res = merge(y, merge(x, max(x, y), y /= y), x /= x) end function real(8) function fmax84(x, y) result(res) real(8), intent (in) :: x real(4), intent (in) :: y res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x) end function real(8) function fmax48(x, y) result(res) real(4), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x) end function real(8) function fmin88(x, y) result (res) real(8), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(x, min(x, y), y /= y), x /= x) end function real(4) function fmin44(x, y) result (res) real(4), intent (in) :: x real(4), intent (in) :: y res = merge(y, merge(x, min(x, y), y /= y), x /= x) end function real(8) function fmin84(x, y) result(res) real(8), intent (in) :: x real(4), intent (in) :: y res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x) end function real(8) function fmin48(x, y) result(res) real(4), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x) end function end module real(8) function code(a, b, c, d) use fmin_fmax_functions real(8), intent (in) :: a real(8), intent (in) :: b real(8), intent (in) :: c real(8), intent (in) :: d code = a * d end function
assert a < b && b < c && c < d; public static double code(double a, double b, double c, double d) { return a * d; }
[a, b, c, d] = sort([a, b, c, d]) def code(a, b, c, d): return a * d
a, b, c, d = sort([a, b, c, d]) function code(a, b, c, d) return Float64(a * d) end
a, b, c, d = num2cell(sort([a, b, c, d])){:} function tmp = code(a, b, c, d) tmp = a * 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 * d), $MachinePrecision]
\begin{array}{l} [a, b, c, d] = \mathsf{sort}([a, b, c, d])\\ \\ a \cdot d \end{array}
Initial program 99.9%
Taylor expanded in d around inf
Applied rewrites44.0%
(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)); }
module fmin_fmax_functions implicit none private public fmax public fmin interface fmax module procedure fmax88 module procedure fmax44 module procedure fmax84 module procedure fmax48 end interface interface fmin module procedure fmin88 module procedure fmin44 module procedure fmin84 module procedure fmin48 end interface contains real(8) function fmax88(x, y) result (res) real(8), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(x, max(x, y), y /= y), x /= x) end function real(4) function fmax44(x, y) result (res) real(4), intent (in) :: x real(4), intent (in) :: y res = merge(y, merge(x, max(x, y), y /= y), x /= x) end function real(8) function fmax84(x, y) result(res) real(8), intent (in) :: x real(4), intent (in) :: y res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x) end function real(8) function fmax48(x, y) result(res) real(4), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x) end function real(8) function fmin88(x, y) result (res) real(8), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(x, min(x, y), y /= y), x /= x) end function real(4) function fmin44(x, y) result (res) real(4), intent (in) :: x real(4), intent (in) :: y res = merge(y, merge(x, min(x, y), y /= y), x /= x) end function real(8) function fmin84(x, y) result(res) real(8), intent (in) :: x real(4), intent (in) :: y res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x) end function real(8) function fmin48(x, y) result(res) real(4), intent (in) :: x real(8), intent (in) :: y res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x) end function end module real(8) function code(a, b, c, d) use fmin_fmax_functions 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 2025056
(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)))