(FPCore re_sqr (re im) :precision binary64 (- (* re re) (* im im)))
double re_sqr(double re, double im) { return (re * re) - (im * im); }
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 re_sqr(re, im) use fmin_fmax_functions real(8), intent (in) :: re real(8), intent (in) :: im re_sqr = (re * re) - (im * im) end function
public static double re_sqr(double re, double im) { return (re * re) - (im * im); }
def re_sqr(re, im): return (re * re) - (im * im)
function re_sqr(re, im) return Float64(Float64(re * re) - Float64(im * im)) end
function tmp = re_sqr(re, im) tmp = (re * re) - (im * im); end
re$95$sqr[re_, im_] := N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ re \cdot re - im \cdot im \end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
Alternative | Accuracy | Speedup |
---|
(FPCore re_sqr (re im) :precision binary64 (- (* re re) (* im im)))
double re_sqr(double re, double im) { return (re * re) - (im * im); }
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 re_sqr(re, im) use fmin_fmax_functions real(8), intent (in) :: re real(8), intent (in) :: im re_sqr = (re * re) - (im * im) end function
public static double re_sqr(double re, double im) { return (re * re) - (im * im); }
def re_sqr(re, im): return (re * re) - (im * im)
function re_sqr(re, im) return Float64(Float64(re * re) - Float64(im * im)) end
function tmp = re_sqr(re, im) tmp = (re * re) - (im * im); end
re$95$sqr[re_, im_] := N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ re \cdot re - im \cdot im \end{array}
(FPCore re_sqr (re im) :precision binary64 (* (- re im) (+ im re)))
double re_sqr(double re, double im) { return (re - im) * (im + re); }
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 re_sqr(re, im) use fmin_fmax_functions real(8), intent (in) :: re real(8), intent (in) :: im re_sqr = (re - im) * (im + re) end function
public static double re_sqr(double re, double im) { return (re - im) * (im + re); }
def re_sqr(re, im): return (re - im) * (im + re)
function re_sqr(re, im) return Float64(Float64(re - im) * Float64(im + re)) end
function tmp = re_sqr(re, im) tmp = (re - im) * (im + re); end
re$95$sqr[re_, im_] := N[(N[(re - im), $MachinePrecision] * N[(im + re), $MachinePrecision]), $MachinePrecision]
\begin{array}{l} \\ \left(re - im\right) \cdot \left(im + re\right) \end{array}
Initial program 93.7%
lift--.f64
N/A
lift-*.f64
N/A
lift-*.f64
N/A
difference-of-squares
N/A
*-commutative
N/A
lower-*.f64
N/A
lower--.f64
N/A
+-commutative
N/A
lower-+.f64
100.0
Applied rewrites100.0%
(FPCore re_sqr (re im) :precision binary64 (if (<= (- (* re re) (* im im)) -5e-315) (* (- im) im) (* re re)))
double re_sqr(double re, double im) { double tmp; if (((re * re) - (im * im)) <= -5e-315) { tmp = -im * im; } else { tmp = re * re; } return tmp; }
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 re_sqr(re, im) use fmin_fmax_functions real(8), intent (in) :: re real(8), intent (in) :: im real(8) :: tmp if (((re * re) - (im * im)) <= (-5d-315)) then tmp = -im * im else tmp = re * re end if re_sqr = tmp end function
public static double re_sqr(double re, double im) { double tmp; if (((re * re) - (im * im)) <= -5e-315) { tmp = -im * im; } else { tmp = re * re; } return tmp; }
def re_sqr(re, im): tmp = 0 if ((re * re) - (im * im)) <= -5e-315: tmp = -im * im else: tmp = re * re return tmp
function re_sqr(re, im) tmp = 0.0 if (Float64(Float64(re * re) - Float64(im * im)) <= -5e-315) tmp = Float64(Float64(-im) * im); else tmp = Float64(re * re); end return tmp end
function tmp_2 = re_sqr(re, im) tmp = 0.0; if (((re * re) - (im * im)) <= -5e-315) tmp = -im * im; else tmp = re * re; end tmp_2 = tmp; end
re$95$sqr[re_, im_] := If[LessEqual[N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision], -5e-315], N[((-im) * im), $MachinePrecision], N[(re * re), $MachinePrecision]]
\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \cdot re - im \cdot im \leq -5 \cdot 10^{-315}:\\ \;\;\;\;\left(-im\right) \cdot im\\ \mathbf{else}:\\ \;\;\;\;re \cdot re\\ \end{array} \end{array}
if (-.f64 (*.f64 re re) (*.f64 im im)) < -5.0000000023e-315
Initial program 100.0%
Taylor expanded in re around 0
mul-1-neg
N/A
pow2
N/A
distribute-lft-neg-in
N/A
lower-*.f64
N/A
lower-neg.f64
99.6
Applied rewrites99.6%
if -5.0000000023e-315 < (-.f64 (*.f64 re re) (*.f64 im im))
Initial program 88.4%
Taylor expanded in re around inf
pow2
N/A
lift-*.f64
95.0
Applied rewrites95.0%
(FPCore re_sqr (re im) :precision binary64 (* re re))
double re_sqr(double re, double im) { return re * re; }
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 re_sqr(re, im) use fmin_fmax_functions real(8), intent (in) :: re real(8), intent (in) :: im re_sqr = re * re end function
public static double re_sqr(double re, double im) { return re * re; }
def re_sqr(re, im): return re * re
function re_sqr(re, im) return Float64(re * re) end
function tmp = re_sqr(re, im) tmp = re * re; end
re$95$sqr[re_, im_] := N[(re * re), $MachinePrecision]
\begin{array}{l} \\ re \cdot re \end{array}
Initial program 93.7%
Taylor expanded in re around inf
pow2
N/A
lift-*.f64
52.4
Applied rewrites52.4%
herbie shell --seed 2025073
(FPCore re_sqr (re im)
:name "math.square on complex, real part"
:precision binary64
(- (* re re) (* im im)))