
(FPCore (x y) :precision binary64 (+ (+ (* x x) (* (* x 2.0) y)) (* y y)))
double code(double x, double y) {
return ((x * x) + ((x * 2.0) * y)) + (y * y);
}
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(x, y)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * x) + ((x * 2.0d0) * y)) + (y * y)
end function
public static double code(double x, double y) {
return ((x * x) + ((x * 2.0) * y)) + (y * y);
}
def code(x, y): return ((x * x) + ((x * 2.0) * y)) + (y * y)
function code(x, y) return Float64(Float64(Float64(x * x) + Float64(Float64(x * 2.0) * y)) + Float64(y * y)) end
function tmp = code(x, y) tmp = ((x * x) + ((x * 2.0) * y)) + (y * y); end
code[x_, y_] := N[(N[(N[(x * x), $MachinePrecision] + N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot x + \left(x \cdot 2\right) \cdot y\right) + y \cdot y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (+ (+ (* x x) (* (* x 2.0) y)) (* y y)))
double code(double x, double y) {
return ((x * x) + ((x * 2.0) * y)) + (y * y);
}
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(x, y)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * x) + ((x * 2.0d0) * y)) + (y * y)
end function
public static double code(double x, double y) {
return ((x * x) + ((x * 2.0) * y)) + (y * y);
}
def code(x, y): return ((x * x) + ((x * 2.0) * y)) + (y * y)
function code(x, y) return Float64(Float64(Float64(x * x) + Float64(Float64(x * 2.0) * y)) + Float64(y * y)) end
function tmp = code(x, y) tmp = ((x * x) + ((x * 2.0) * y)) + (y * y); end
code[x_, y_] := N[(N[(N[(x * x), $MachinePrecision] + N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot x + \left(x \cdot 2\right) \cdot y\right) + y \cdot y
\end{array}
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 4e+152) (fma y y (* (fma y 2.0 x) x)) (* y y)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 4e+152) {
tmp = fma(y, y, (fma(y, 2.0, x) * x));
} else {
tmp = y * y;
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 4e+152) tmp = fma(y, y, Float64(fma(y, 2.0, x) * x)); else tmp = Float64(y * y); end return tmp end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[y, 4e+152], N[(y * y + N[(N[(y * 2.0 + x), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 4 \cdot 10^{+152}:\\
\;\;\;\;\mathsf{fma}\left(y, y, \mathsf{fma}\left(y, 2, x\right) \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if y < 4.0000000000000002e152Initial program 95.0%
lift-+.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
pow2N/A
+-commutativeN/A
pow2N/A
lower-fma.f64N/A
pow2N/A
lift-*.f64N/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-*.f64N/A
associate-*l*N/A
pow2N/A
distribute-lft-inN/A
+-commutativeN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6497.3
Applied rewrites97.3%
if 4.0000000000000002e152 < y Initial program 85.7%
Taylor expanded in x around 0
pow2N/A
lift-*.f64100.0
Applied rewrites100.0%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 1.15e-55) (* (fma y 2.0 x) x) (* y y)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 1.15e-55) {
tmp = fma(y, 2.0, x) * x;
} else {
tmp = y * y;
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 1.15e-55) tmp = Float64(fma(y, 2.0, x) * x); else tmp = Float64(y * y); end return tmp end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[y, 1.15e-55], N[(N[(y * 2.0 + x), $MachinePrecision] * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.15 \cdot 10^{-55}:\\
\;\;\;\;\mathsf{fma}\left(y, 2, x\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if y < 1.15000000000000006e-55Initial program 95.1%
Taylor expanded in y around 0
+-commutativeN/A
pow2N/A
associate-*r*N/A
*-commutativeN/A
associate-*l*N/A
distribute-lft-inN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6470.2
Applied rewrites70.2%
if 1.15000000000000006e-55 < y Initial program 90.2%
Taylor expanded in x around 0
pow2N/A
lift-*.f6483.5
Applied rewrites83.5%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 1.15e-55) (* x x) (* y y)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 1.15e-55) {
tmp = x * x;
} else {
tmp = y * y;
}
return tmp;
}
NOTE: x and y 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(x, y)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 1.15d-55) then
tmp = x * x
else
tmp = y * y
end if
code = tmp
end function
assert x < y;
public static double code(double x, double y) {
double tmp;
if (y <= 1.15e-55) {
tmp = x * x;
} else {
tmp = y * y;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 1.15e-55: tmp = x * x else: tmp = y * y return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 1.15e-55) tmp = Float64(x * x); else tmp = Float64(y * y); end return tmp end
x, y = num2cell(sort([x, y])){:}
function tmp_2 = code(x, y)
tmp = 0.0;
if (y <= 1.15e-55)
tmp = x * x;
else
tmp = y * y;
end
tmp_2 = tmp;
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[y, 1.15e-55], N[(x * x), $MachinePrecision], N[(y * y), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.15 \cdot 10^{-55}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot y\\
\end{array}
\end{array}
if y < 1.15000000000000006e-55Initial program 95.1%
Taylor expanded in x around inf
pow2N/A
lift-*.f6470.6
Applied rewrites70.6%
if 1.15000000000000006e-55 < y Initial program 90.2%
Taylor expanded in x around 0
pow2N/A
lift-*.f6483.5
Applied rewrites83.5%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (* x x))
assert(x < y);
double code(double x, double y) {
return x * x;
}
NOTE: x and y 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(x, y)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * x
end function
assert x < y;
public static double code(double x, double y) {
return x * x;
}
[x, y] = sort([x, y]) def code(x, y): return x * x
x, y = sort([x, y]) function code(x, y) return Float64(x * x) end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
tmp = x * x;
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
x \cdot x
\end{array}
Initial program 93.7%
Taylor expanded in x around inf
pow2N/A
lift-*.f6458.7
Applied rewrites58.7%
(FPCore (x y) :precision binary64 (+ (* x x) (+ (* y y) (* (* x y) 2.0))))
double code(double x, double y) {
return (x * x) + ((y * y) + ((x * y) * 2.0));
}
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(x, y)
use fmin_fmax_functions
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) + ((y * y) + ((x * y) * 2.0d0))
end function
public static double code(double x, double y) {
return (x * x) + ((y * y) + ((x * y) * 2.0));
}
def code(x, y): return (x * x) + ((y * y) + ((x * y) * 2.0))
function code(x, y) return Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(Float64(x * y) * 2.0))) end
function tmp = code(x, y) tmp = (x * x) + ((y * y) + ((x * y) * 2.0)); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(N[(x * y), $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + \left(y \cdot y + \left(x \cdot y\right) \cdot 2\right)
\end{array}
herbie shell --seed 2025038
(FPCore (x y)
:name "Examples.Basics.ProofTests:f4 from sbv-4.4"
:precision binary64
:alt
(! :herbie-platform default (+ (* x x) (+ (* y y) (* (* x y) 2))))
(+ (+ (* x x) (* (* x 2.0) y)) (* y y)))