
(FPCore (a b) :precision binary64 (- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (+ (* (* a a) (+ 1.0 a)) (* (* b b) (- 1.0 (* 3.0 a)))))) 1.0))
double code(double a, double b) {
return (pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.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(a, b)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
code = ((((a * a) + (b * b)) ** 2.0d0) + (4.0d0 * (((a * a) * (1.0d0 + a)) + ((b * b) * (1.0d0 - (3.0d0 * a)))))) - 1.0d0
end function
public static double code(double a, double b) {
return (Math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.0;
}
def code(a, b): return (math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.0
function code(a, b) return Float64(Float64((Float64(Float64(a * a) + Float64(b * b)) ^ 2.0) + Float64(4.0 * Float64(Float64(Float64(a * a) * Float64(1.0 + a)) + Float64(Float64(b * b) * Float64(1.0 - Float64(3.0 * a)))))) - 1.0) end
function tmp = code(a, b) tmp = ((((a * a) + (b * b)) ^ 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.0; end
code[a_, b_] := N[(N[(N[Power[N[(N[(a * a), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[(N[(N[(a * a), $MachinePrecision] * N[(1.0 + a), $MachinePrecision]), $MachinePrecision] + N[(N[(b * b), $MachinePrecision] * N[(1.0 - N[(3.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right)\right) - 1
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b) :precision binary64 (- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (+ (* (* a a) (+ 1.0 a)) (* (* b b) (- 1.0 (* 3.0 a)))))) 1.0))
double code(double a, double b) {
return (pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.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(a, b)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
code = ((((a * a) + (b * b)) ** 2.0d0) + (4.0d0 * (((a * a) * (1.0d0 + a)) + ((b * b) * (1.0d0 - (3.0d0 * a)))))) - 1.0d0
end function
public static double code(double a, double b) {
return (Math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.0;
}
def code(a, b): return (math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.0
function code(a, b) return Float64(Float64((Float64(Float64(a * a) + Float64(b * b)) ^ 2.0) + Float64(4.0 * Float64(Float64(Float64(a * a) * Float64(1.0 + a)) + Float64(Float64(b * b) * Float64(1.0 - Float64(3.0 * a)))))) - 1.0) end
function tmp = code(a, b) tmp = ((((a * a) + (b * b)) ^ 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.0; end
code[a_, b_] := N[(N[(N[Power[N[(N[(a * a), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[(N[(N[(a * a), $MachinePrecision] * N[(1.0 + a), $MachinePrecision]), $MachinePrecision] + N[(N[(b * b), $MachinePrecision] * N[(1.0 - N[(3.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right)\right) - 1
\end{array}
b_m = (fabs.f64 b)
(FPCore (a b_m)
:precision binary64
(if (<= b_m 2e+70)
(-
(+
(fma (* a a) (fma 2.0 (* b_m b_m) (* a a)) (pow b_m 4.0))
(* 4.0 (* b_m b_m)))
1.0)
(-
(fma
(* b_m b_m)
4.0
(fma (* a (+ 4.0 (* 2.0 (* b_m b_m)))) a (pow (* b_m b_m) 2.0)))
1.0)))b_m = fabs(b);
double code(double a, double b_m) {
double tmp;
if (b_m <= 2e+70) {
tmp = (fma((a * a), fma(2.0, (b_m * b_m), (a * a)), pow(b_m, 4.0)) + (4.0 * (b_m * b_m))) - 1.0;
} else {
tmp = fma((b_m * b_m), 4.0, fma((a * (4.0 + (2.0 * (b_m * b_m)))), a, pow((b_m * b_m), 2.0))) - 1.0;
}
return tmp;
}
b_m = abs(b) function code(a, b_m) tmp = 0.0 if (b_m <= 2e+70) tmp = Float64(Float64(fma(Float64(a * a), fma(2.0, Float64(b_m * b_m), Float64(a * a)), (b_m ^ 4.0)) + Float64(4.0 * Float64(b_m * b_m))) - 1.0); else tmp = Float64(fma(Float64(b_m * b_m), 4.0, fma(Float64(a * Float64(4.0 + Float64(2.0 * Float64(b_m * b_m)))), a, (Float64(b_m * b_m) ^ 2.0))) - 1.0); end return tmp end
b_m = N[Abs[b], $MachinePrecision] code[a_, b$95$m_] := If[LessEqual[b$95$m, 2e+70], N[(N[(N[(N[(a * a), $MachinePrecision] * N[(2.0 * N[(b$95$m * b$95$m), $MachinePrecision] + N[(a * a), $MachinePrecision]), $MachinePrecision] + N[Power[b$95$m, 4.0], $MachinePrecision]), $MachinePrecision] + N[(4.0 * N[(b$95$m * b$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision], N[(N[(N[(b$95$m * b$95$m), $MachinePrecision] * 4.0 + N[(N[(a * N[(4.0 + N[(2.0 * N[(b$95$m * b$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * a + N[Power[N[(b$95$m * b$95$m), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]]
\begin{array}{l}
b_m = \left|b\right|
\\
\begin{array}{l}
\mathbf{if}\;b\_m \leq 2 \cdot 10^{+70}:\\
\;\;\;\;\left(\mathsf{fma}\left(a \cdot a, \mathsf{fma}\left(2, b\_m \cdot b\_m, a \cdot a\right), {b\_m}^{4}\right) + 4 \cdot \left(b\_m \cdot b\_m\right)\right) - 1\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(b\_m \cdot b\_m, 4, \mathsf{fma}\left(a \cdot \left(4 + 2 \cdot \left(b\_m \cdot b\_m\right)\right), a, {\left(b\_m \cdot b\_m\right)}^{2}\right)\right) - 1\\
\end{array}
\end{array}
if b < 2.00000000000000015e70Initial program 70.4%
Taylor expanded in a around 0
pow2N/A
lift-*.f6499.5
Applied rewrites99.5%
Taylor expanded in a around 0
lower-fma.f64N/A
pow2N/A
lift-*.f64N/A
lower-fma.f64N/A
pow2N/A
lift-*.f64N/A
pow2N/A
lift-*.f64N/A
lower-pow.f6496.4
Applied rewrites96.4%
if 2.00000000000000015e70 < b Initial program 63.6%
Taylor expanded in a around 0
*-commutativeN/A
lower-fma.f64N/A
pow2N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites72.7%
Taylor expanded in a around inf
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
pow2N/A
lift-*.f64100.0
Applied rewrites100.0%
b_m = (fabs.f64 b) (FPCore (a b_m) :precision binary64 (- (+ (pow (+ (* a a) (* b_m b_m)) 2.0) (* 4.0 (* b_m b_m))) 1.0))
b_m = fabs(b);
double code(double a, double b_m) {
return (pow(((a * a) + (b_m * b_m)), 2.0) + (4.0 * (b_m * b_m))) - 1.0;
}
b_m = private
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_m)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b_m
code = ((((a * a) + (b_m * b_m)) ** 2.0d0) + (4.0d0 * (b_m * b_m))) - 1.0d0
end function
b_m = Math.abs(b);
public static double code(double a, double b_m) {
return (Math.pow(((a * a) + (b_m * b_m)), 2.0) + (4.0 * (b_m * b_m))) - 1.0;
}
b_m = math.fabs(b) def code(a, b_m): return (math.pow(((a * a) + (b_m * b_m)), 2.0) + (4.0 * (b_m * b_m))) - 1.0
b_m = abs(b) function code(a, b_m) return Float64(Float64((Float64(Float64(a * a) + Float64(b_m * b_m)) ^ 2.0) + Float64(4.0 * Float64(b_m * b_m))) - 1.0) end
b_m = abs(b); function tmp = code(a, b_m) tmp = ((((a * a) + (b_m * b_m)) ^ 2.0) + (4.0 * (b_m * b_m))) - 1.0; end
b_m = N[Abs[b], $MachinePrecision] code[a_, b$95$m_] := N[(N[(N[Power[N[(N[(a * a), $MachinePrecision] + N[(b$95$m * b$95$m), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[(b$95$m * b$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
b_m = \left|b\right|
\\
\left({\left(a \cdot a + b\_m \cdot b\_m\right)}^{2} + 4 \cdot \left(b\_m \cdot b\_m\right)\right) - 1
\end{array}
Initial program 68.7%
Taylor expanded in a around 0
pow2N/A
lift-*.f6499.6
Applied rewrites99.6%
b_m = (fabs.f64 b) (FPCore (a b_m) :precision binary64 (- (fma (* b_m b_m) 4.0 (fma (* a (+ 4.0 (* 2.0 (* b_m b_m)))) a (pow (* b_m b_m) 2.0))) 1.0))
b_m = fabs(b);
double code(double a, double b_m) {
return fma((b_m * b_m), 4.0, fma((a * (4.0 + (2.0 * (b_m * b_m)))), a, pow((b_m * b_m), 2.0))) - 1.0;
}
b_m = abs(b) function code(a, b_m) return Float64(fma(Float64(b_m * b_m), 4.0, fma(Float64(a * Float64(4.0 + Float64(2.0 * Float64(b_m * b_m)))), a, (Float64(b_m * b_m) ^ 2.0))) - 1.0) end
b_m = N[Abs[b], $MachinePrecision] code[a_, b$95$m_] := N[(N[(N[(b$95$m * b$95$m), $MachinePrecision] * 4.0 + N[(N[(a * N[(4.0 + N[(2.0 * N[(b$95$m * b$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * a + N[Power[N[(b$95$m * b$95$m), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
b_m = \left|b\right|
\\
\mathsf{fma}\left(b\_m \cdot b\_m, 4, \mathsf{fma}\left(a \cdot \left(4 + 2 \cdot \left(b\_m \cdot b\_m\right)\right), a, {\left(b\_m \cdot b\_m\right)}^{2}\right)\right) - 1
\end{array}
Initial program 68.7%
Taylor expanded in a around 0
*-commutativeN/A
lower-fma.f64N/A
pow2N/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites75.7%
Taylor expanded in a around inf
lower-*.f64N/A
lower-+.f64N/A
lower-*.f64N/A
pow2N/A
lift-*.f6489.3
Applied rewrites89.3%
herbie shell --seed 2025064
(FPCore (a b)
:name "Bouland and Aaronson, Equation (25)"
:precision binary64
(- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (+ (* (* a a) (+ 1.0 a)) (* (* b b) (- 1.0 (* 3.0 a)))))) 1.0))