
(FPCore (lo hi x) :precision binary64 (/ (- x lo) (- hi lo)))
double code(double lo, double hi, double x) {
return (x - lo) / (hi - lo);
}
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(lo, hi, x)
use fmin_fmax_functions
real(8), intent (in) :: lo
real(8), intent (in) :: hi
real(8), intent (in) :: x
code = (x - lo) / (hi - lo)
end function
public static double code(double lo, double hi, double x) {
return (x - lo) / (hi - lo);
}
def code(lo, hi, x): return (x - lo) / (hi - lo)
function code(lo, hi, x) return Float64(Float64(x - lo) / Float64(hi - lo)) end
function tmp = code(lo, hi, x) tmp = (x - lo) / (hi - lo); end
code[lo_, hi_, x_] := N[(N[(x - lo), $MachinePrecision] / N[(hi - lo), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - lo}{hi - lo}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (lo hi x) :precision binary64 (/ (- x lo) (- hi lo)))
double code(double lo, double hi, double x) {
return (x - lo) / (hi - lo);
}
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(lo, hi, x)
use fmin_fmax_functions
real(8), intent (in) :: lo
real(8), intent (in) :: hi
real(8), intent (in) :: x
code = (x - lo) / (hi - lo)
end function
public static double code(double lo, double hi, double x) {
return (x - lo) / (hi - lo);
}
def code(lo, hi, x): return (x - lo) / (hi - lo)
function code(lo, hi, x) return Float64(Float64(x - lo) / Float64(hi - lo)) end
function tmp = code(lo, hi, x) tmp = (x - lo) / (hi - lo); end
code[lo_, hi_, x_] := N[(N[(x - lo), $MachinePrecision] / N[(hi - lo), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - lo}{hi - lo}
\end{array}
(FPCore (lo hi x) :precision binary64 (* (- lo) (/ (* (/ (/ lo hi) hi) lo) hi)))
double code(double lo, double hi, double x) {
return -lo * ((((lo / hi) / hi) * lo) / hi);
}
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(lo, hi, x)
use fmin_fmax_functions
real(8), intent (in) :: lo
real(8), intent (in) :: hi
real(8), intent (in) :: x
code = -lo * ((((lo / hi) / hi) * lo) / hi)
end function
public static double code(double lo, double hi, double x) {
return -lo * ((((lo / hi) / hi) * lo) / hi);
}
def code(lo, hi, x): return -lo * ((((lo / hi) / hi) * lo) / hi)
function code(lo, hi, x) return Float64(Float64(-lo) * Float64(Float64(Float64(Float64(lo / hi) / hi) * lo) / hi)) end
function tmp = code(lo, hi, x) tmp = -lo * ((((lo / hi) / hi) * lo) / hi); end
code[lo_, hi_, x_] := N[((-lo) * N[(N[(N[(N[(lo / hi), $MachinePrecision] / hi), $MachinePrecision] * lo), $MachinePrecision] / hi), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(-lo\right) \cdot \frac{\frac{\frac{lo}{hi}}{hi} \cdot lo}{hi}
\end{array}
Initial program 3.1%
Taylor expanded in hi around inf
Applied rewrites15.2%
Applied rewrites18.9%
Taylor expanded in lo around inf
Applied rewrites19.5%
Applied rewrites19.5%
(FPCore (lo hi x) :precision binary64 (/ (- lo) hi))
double code(double lo, double hi, double x) {
return -lo / hi;
}
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(lo, hi, x)
use fmin_fmax_functions
real(8), intent (in) :: lo
real(8), intent (in) :: hi
real(8), intent (in) :: x
code = -lo / hi
end function
public static double code(double lo, double hi, double x) {
return -lo / hi;
}
def code(lo, hi, x): return -lo / hi
function code(lo, hi, x) return Float64(Float64(-lo) / hi) end
function tmp = code(lo, hi, x) tmp = -lo / hi; end
code[lo_, hi_, x_] := N[((-lo) / hi), $MachinePrecision]
\begin{array}{l}
\\
\frac{-lo}{hi}
\end{array}
Initial program 3.1%
Taylor expanded in lo around 0
Applied rewrites18.8%
Taylor expanded in lo around inf
Applied rewrites18.8%
(FPCore (lo hi x) :precision binary64 (/ lo lo))
double code(double lo, double hi, double x) {
return lo / lo;
}
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(lo, hi, x)
use fmin_fmax_functions
real(8), intent (in) :: lo
real(8), intent (in) :: hi
real(8), intent (in) :: x
code = lo / lo
end function
public static double code(double lo, double hi, double x) {
return lo / lo;
}
def code(lo, hi, x): return lo / lo
function code(lo, hi, x) return Float64(lo / lo) end
function tmp = code(lo, hi, x) tmp = lo / lo; end
code[lo_, hi_, x_] := N[(lo / lo), $MachinePrecision]
\begin{array}{l}
\\
\frac{lo}{lo}
\end{array}
Initial program 3.1%
Taylor expanded in hi around 0
Applied rewrites18.6%
Taylor expanded in lo around inf
Applied rewrites18.7%
(FPCore (lo hi x) :precision binary64 (/ x hi))
double code(double lo, double hi, double x) {
return x / hi;
}
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(lo, hi, x)
use fmin_fmax_functions
real(8), intent (in) :: lo
real(8), intent (in) :: hi
real(8), intent (in) :: x
code = x / hi
end function
public static double code(double lo, double hi, double x) {
return x / hi;
}
def code(lo, hi, x): return x / hi
function code(lo, hi, x) return Float64(x / hi) end
function tmp = code(lo, hi, x) tmp = x / hi; end
code[lo_, hi_, x_] := N[(x / hi), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{hi}
\end{array}
Initial program 3.1%
Taylor expanded in lo around 0
Applied rewrites3.5%
herbie shell --seed 2025021
(FPCore (lo hi x)
:name "xlohi (overflows)"
:precision binary64
:pre (and (< lo -1e+308) (> hi 1e+308))
(/ (- x lo) (- hi lo)))