
(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}
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}
re_m = (fabs.f64 re) (FPCore re_sqr (re_m im) :precision binary64 (if (<= re_m 8.5e+150) (- (* re_m re_m) (* im im)) (* re_m re_m)))
re_m = fabs(re);
double re_sqr(double re_m, double im) {
double tmp;
if (re_m <= 8.5e+150) {
tmp = (re_m * re_m) - (im * im);
} else {
tmp = re_m * re_m;
}
return tmp;
}
re_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 re_sqr(re_m, im)
use fmin_fmax_functions
real(8), intent (in) :: re_m
real(8), intent (in) :: im
real(8) :: tmp
if (re_m <= 8.5d+150) then
tmp = (re_m * re_m) - (im * im)
else
tmp = re_m * re_m
end if
re_sqr = tmp
end function
re_m = Math.abs(re);
public static double re_sqr(double re_m, double im) {
double tmp;
if (re_m <= 8.5e+150) {
tmp = (re_m * re_m) - (im * im);
} else {
tmp = re_m * re_m;
}
return tmp;
}
re_m = math.fabs(re) def re_sqr(re_m, im): tmp = 0 if re_m <= 8.5e+150: tmp = (re_m * re_m) - (im * im) else: tmp = re_m * re_m return tmp
re_m = abs(re) function re_sqr(re_m, im) tmp = 0.0 if (re_m <= 8.5e+150) tmp = Float64(Float64(re_m * re_m) - Float64(im * im)); else tmp = Float64(re_m * re_m); end return tmp end
re_m = abs(re); function tmp_2 = re_sqr(re_m, im) tmp = 0.0; if (re_m <= 8.5e+150) tmp = (re_m * re_m) - (im * im); else tmp = re_m * re_m; end tmp_2 = tmp; end
re_m = N[Abs[re], $MachinePrecision] re$95$sqr[re$95$m_, im_] := If[LessEqual[re$95$m, 8.5e+150], N[(N[(re$95$m * re$95$m), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision], N[(re$95$m * re$95$m), $MachinePrecision]]
\begin{array}{l}
re_m = \left|re\right|
\\
\begin{array}{l}
\mathbf{if}\;re\_m \leq 8.5 \cdot 10^{+150}:\\
\;\;\;\;re\_m \cdot re\_m - im \cdot im\\
\mathbf{else}:\\
\;\;\;\;re\_m \cdot re\_m\\
\end{array}
\end{array}
if re < 8.4999999999999999e150Initial program 100.0%
if 8.4999999999999999e150 < re Initial program 77.0%
Taylor expanded in re around inf
pow2N/A
lift-*.f6486.6
Applied rewrites86.6%
re_m = (fabs.f64 re) (FPCore re_sqr (re_m im) :precision binary64 (if (<= (- (* re_m re_m) (* im im)) -5e-260) (* (- im) im) (* re_m re_m)))
re_m = fabs(re);
double re_sqr(double re_m, double im) {
double tmp;
if (((re_m * re_m) - (im * im)) <= -5e-260) {
tmp = -im * im;
} else {
tmp = re_m * re_m;
}
return tmp;
}
re_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 re_sqr(re_m, im)
use fmin_fmax_functions
real(8), intent (in) :: re_m
real(8), intent (in) :: im
real(8) :: tmp
if (((re_m * re_m) - (im * im)) <= (-5d-260)) then
tmp = -im * im
else
tmp = re_m * re_m
end if
re_sqr = tmp
end function
re_m = Math.abs(re);
public static double re_sqr(double re_m, double im) {
double tmp;
if (((re_m * re_m) - (im * im)) <= -5e-260) {
tmp = -im * im;
} else {
tmp = re_m * re_m;
}
return tmp;
}
re_m = math.fabs(re) def re_sqr(re_m, im): tmp = 0 if ((re_m * re_m) - (im * im)) <= -5e-260: tmp = -im * im else: tmp = re_m * re_m return tmp
re_m = abs(re) function re_sqr(re_m, im) tmp = 0.0 if (Float64(Float64(re_m * re_m) - Float64(im * im)) <= -5e-260) tmp = Float64(Float64(-im) * im); else tmp = Float64(re_m * re_m); end return tmp end
re_m = abs(re); function tmp_2 = re_sqr(re_m, im) tmp = 0.0; if (((re_m * re_m) - (im * im)) <= -5e-260) tmp = -im * im; else tmp = re_m * re_m; end tmp_2 = tmp; end
re_m = N[Abs[re], $MachinePrecision] re$95$sqr[re$95$m_, im_] := If[LessEqual[N[(N[(re$95$m * re$95$m), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision], -5e-260], N[((-im) * im), $MachinePrecision], N[(re$95$m * re$95$m), $MachinePrecision]]
\begin{array}{l}
re_m = \left|re\right|
\\
\begin{array}{l}
\mathbf{if}\;re\_m \cdot re\_m - im \cdot im \leq -5 \cdot 10^{-260}:\\
\;\;\;\;\left(-im\right) \cdot im\\
\mathbf{else}:\\
\;\;\;\;re\_m \cdot re\_m\\
\end{array}
\end{array}
if (-.f64 (*.f64 re re) (*.f64 im im)) < -5.0000000000000003e-260Initial program 100.0%
Taylor expanded in re around 0
mul-1-negN/A
pow2N/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6499.5
Applied rewrites99.5%
if -5.0000000000000003e-260 < (-.f64 (*.f64 re re) (*.f64 im im)) Initial program 89.7%
Taylor expanded in re around inf
pow2N/A
lift-*.f6491.8
Applied rewrites91.8%
re_m = (fabs.f64 re) (FPCore re_sqr (re_m im) :precision binary64 (* re_m re_m))
re_m = fabs(re);
double re_sqr(double re_m, double im) {
return re_m * re_m;
}
re_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 re_sqr(re_m, im)
use fmin_fmax_functions
real(8), intent (in) :: re_m
real(8), intent (in) :: im
re_sqr = re_m * re_m
end function
re_m = Math.abs(re);
public static double re_sqr(double re_m, double im) {
return re_m * re_m;
}
re_m = math.fabs(re) def re_sqr(re_m, im): return re_m * re_m
re_m = abs(re) function re_sqr(re_m, im) return Float64(re_m * re_m) end
re_m = abs(re); function tmp = re_sqr(re_m, im) tmp = re_m * re_m; end
re_m = N[Abs[re], $MachinePrecision] re$95$sqr[re$95$m_, im_] := N[(re$95$m * re$95$m), $MachinePrecision]
\begin{array}{l}
re_m = \left|re\right|
\\
re\_m \cdot re\_m
\end{array}
Initial program 94.1%
Taylor expanded in re around inf
pow2N/A
lift-*.f6453.0
Applied rewrites53.0%
herbie shell --seed 2025112
(FPCore re_sqr (re im)
:name "math.square on complex, real part"
:precision binary64
(- (* re re) (* im im)))