
(FPCore modulus_sqr (re im) :precision binary64 (+ (* re re) (* im im)))
double modulus_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 modulus_sqr(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
modulus_sqr = (re * re) + (im * im)
end function
public static double modulus_sqr(double re, double im) {
return (re * re) + (im * im);
}
def modulus_sqr(re, im): return (re * re) + (im * im)
function modulus_sqr(re, im) return Float64(Float64(re * re) + Float64(im * im)) end
function tmp = modulus_sqr(re, im) tmp = (re * re) + (im * im); end
modulus$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 2 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore modulus_sqr (re im) :precision binary64 (+ (* re re) (* im im)))
double modulus_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 modulus_sqr(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
modulus_sqr = (re * re) + (im * im)
end function
public static double modulus_sqr(double re, double im) {
return (re * re) + (im * im);
}
def modulus_sqr(re, im): return (re * re) + (im * im)
function modulus_sqr(re, im) return Float64(Float64(re * re) + Float64(im * im)) end
function tmp = modulus_sqr(re, im) tmp = (re * re) + (im * im); end
modulus$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) NOTE: re_m and im should be sorted in increasing order before calling this function. (FPCore modulus_sqr (re_m im) :precision binary64 (fma im im (* re_m re_m)))
re_m = fabs(re);
assert(re_m < im);
double modulus_sqr(double re_m, double im) {
return fma(im, im, (re_m * re_m));
}
re_m = abs(re) re_m, im = sort([re_m, im]) function modulus_sqr(re_m, im) return fma(im, im, Float64(re_m * re_m)) end
re_m = N[Abs[re], $MachinePrecision] NOTE: re_m and im should be sorted in increasing order before calling this function. modulus$95$sqr[re$95$m_, im_] := N[(im * im + N[(re$95$m * re$95$m), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
re_m = \left|re\right|
\\
[re_m, im] = \mathsf{sort}([re_m, im])\\
\\
\mathsf{fma}\left(im, im, re\_m \cdot re\_m\right)
\end{array}
Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
Applied rewrites100.0%
re_m = (fabs.f64 re) NOTE: re_m and im should be sorted in increasing order before calling this function. (FPCore modulus_sqr (re_m im) :precision binary64 (* im im))
re_m = fabs(re);
assert(re_m < im);
double modulus_sqr(double re_m, double im) {
return im * im;
}
re_m = private
NOTE: re_m and im 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 modulus_sqr(re_m, im)
use fmin_fmax_functions
real(8), intent (in) :: re_m
real(8), intent (in) :: im
modulus_sqr = im * im
end function
re_m = Math.abs(re);
assert re_m < im;
public static double modulus_sqr(double re_m, double im) {
return im * im;
}
re_m = math.fabs(re) [re_m, im] = sort([re_m, im]) def modulus_sqr(re_m, im): return im * im
re_m = abs(re) re_m, im = sort([re_m, im]) function modulus_sqr(re_m, im) return Float64(im * im) end
re_m = abs(re);
re_m, im = num2cell(sort([re_m, im])){:}
function tmp = modulus_sqr(re_m, im)
tmp = im * im;
end
re_m = N[Abs[re], $MachinePrecision] NOTE: re_m and im should be sorted in increasing order before calling this function. modulus$95$sqr[re$95$m_, im_] := N[(im * im), $MachinePrecision]
\begin{array}{l}
re_m = \left|re\right|
\\
[re_m, im] = \mathsf{sort}([re_m, im])\\
\\
im \cdot im
\end{array}
Initial program 100.0%
Taylor expanded in re around 0
unpow2N/A
lower-*.f6453.7
Applied rewrites53.7%
herbie shell --seed 2025017
(FPCore modulus_sqr (re im)
:name "math.abs on complex (squared)"
:precision binary64
(+ (* re re) (* im im)))