
(FPCore modulus (re im) :precision binary64 (sqrt (+ (* re re) (* im im))))
double modulus(double re, double im) {
return sqrt(((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(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
modulus = sqrt(((re * re) + (im * im)))
end function
public static double modulus(double re, double im) {
return Math.sqrt(((re * re) + (im * im)));
}
def modulus(re, im): return math.sqrt(((re * re) + (im * im)))
function modulus(re, im) return sqrt(Float64(Float64(re * re) + Float64(im * im))) end
function tmp = modulus(re, im) tmp = sqrt(((re * re) + (im * im))); end
modulus[re_, im_] := N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\sqrt{re \cdot re + im \cdot im}
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore modulus (re im) :precision binary64 (sqrt (+ (* re re) (* im im))))
double modulus(double re, double im) {
return sqrt(((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(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
modulus = sqrt(((re * re) + (im * im)))
end function
public static double modulus(double re, double im) {
return Math.sqrt(((re * re) + (im * im)));
}
def modulus(re, im): return math.sqrt(((re * re) + (im * im)))
function modulus(re, im) return sqrt(Float64(Float64(re * re) + Float64(im * im))) end
function tmp = modulus(re, im) tmp = sqrt(((re * re) + (im * im))); end
modulus[re_, im_] := N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\sqrt{re \cdot re + im \cdot im}
(FPCore modulus (re im) :precision binary64 (hypot re im))
double modulus(double re, double im) {
return hypot(re, im);
}
public static double modulus(double re, double im) {
return Math.hypot(re, im);
}
def modulus(re, im): return math.hypot(re, im)
function modulus(re, im) return hypot(re, im) end
function tmp = modulus(re, im) tmp = hypot(re, im); end
modulus[re_, im_] := N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]
\mathsf{hypot}\left(re, im\right)
Initial program 54.0%
lift-sqrt.f64N/A
sqrt-fabs-revN/A
lift-sqrt.f64N/A
rem-sqrt-square-revN/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrtN/A
lift-+.f64N/A
add-flipN/A
sub-flipN/A
remove-double-negN/A
lift-*.f64N/A
sqr-abs-revN/A
sqr-neg-revN/A
fp-cancel-sign-sub-invN/A
fp-cancel-sub-sign-invN/A
lift-*.f64N/A
distribute-lft-neg-inN/A
distribute-rgt-neg-outN/A
sqr-neg-revN/A
sqr-neg-revN/A
sqr-abs-revN/A
Applied rewrites100.0%
(FPCore modulus (re im) :precision binary64 (sqrt (fma re re (* im im))))
double modulus(double re, double im) {
return sqrt(fma(re, re, (im * im)));
}
function modulus(re, im) return sqrt(fma(re, re, Float64(im * im))) end
modulus[re_, im_] := N[Sqrt[N[(re * re + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\sqrt{\mathsf{fma}\left(re, re, im \cdot im\right)}
Initial program 54.0%
lift-+.f64N/A
add-flipN/A
sub-flipN/A
remove-double-negN/A
lift-*.f64N/A
sqr-abs-revN/A
sqr-neg-revN/A
fp-cancel-sign-sub-invN/A
fp-cancel-sub-sign-invN/A
lift-*.f64N/A
distribute-lft-neg-inN/A
distribute-lft-neg-inN/A
sqr-neg-revN/A
sqr-abs-revN/A
lift-*.f64N/A
remove-double-negN/A
lower-fma.f6454.0%
Applied rewrites54.0%
(FPCore modulus (re im) :precision binary64 (- (fmin re im)))
double modulus(double re, double im) {
return -fmin(re, 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(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
modulus = -fmin(re, im)
end function
public static double modulus(double re, double im) {
return -fmin(re, im);
}
def modulus(re, im): return -fmin(re, im)
function modulus(re, im) return Float64(-fmin(re, im)) end
function tmp = modulus(re, im) tmp = -min(re, im); end
modulus[re_, im_] := (-N[Min[re, im], $MachinePrecision])
-\mathsf{min}\left(re, im\right)
Initial program 54.0%
Taylor expanded in re around -inf
lower-*.f6426.3%
Applied rewrites26.3%
lift-*.f64N/A
mul-1-negN/A
lower-neg.f6426.3%
Applied rewrites26.3%
(FPCore modulus (re im) :precision binary64 (- im))
double modulus(double re, double im) {
return -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(re, im)
use fmin_fmax_functions
real(8), intent (in) :: re
real(8), intent (in) :: im
modulus = -im
end function
public static double modulus(double re, double im) {
return -im;
}
def modulus(re, im): return -im
function modulus(re, im) return Float64(-im) end
function tmp = modulus(re, im) tmp = -im; end
modulus[re_, im_] := (-im)
-im
Initial program 54.0%
Taylor expanded in im around -inf
lower-*.f6426.6%
Applied rewrites26.6%
lift-*.f64N/A
mul-1-negN/A
lower-neg.f6426.6%
Applied rewrites26.6%
herbie shell --seed 2025183
(FPCore modulus (re im)
:name "math.abs on complex"
:precision binary64
(sqrt (+ (* re re) (* im im))))