xlohi (overflows)

Percentage Accurate: 3.1% → 19.3%
Time: 6.3s
Alternatives: 8
Speedup: 18.0×

Specification

?
\[lo < -1 \cdot 10^{+308} \land hi > 10^{+308}\]
\[\begin{array}{l} \\ \frac{x - lo}{hi - lo} \end{array} \]
(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:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 8 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 3.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{x - lo}{hi - lo} \end{array} \]
(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}

Alternative 1: 19.3% accurate, 0.1× speedup?

\[\begin{array}{l} \\ e^{\mathsf{fma}\left(-3, \log \left(\frac{-1}{lo}\right), -3 \cdot \log hi\right)} \end{array} \]
(FPCore (lo hi x)
 :precision binary64
 (exp (fma -3.0 (log (/ -1.0 lo)) (* -3.0 (log hi)))))
double code(double lo, double hi, double x) {
	return exp(fma(-3.0, log((-1.0 / lo)), (-3.0 * log(hi))));
}
function code(lo, hi, x)
	return exp(fma(-3.0, log(Float64(-1.0 / lo)), Float64(-3.0 * log(hi))))
end
code[lo_, hi_, x_] := N[Exp[N[(-3.0 * N[Log[N[(-1.0 / lo), $MachinePrecision]], $MachinePrecision] + N[(-3.0 * N[Log[hi], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
e^{\mathsf{fma}\left(-3, \log \left(\frac{-1}{lo}\right), -3 \cdot \log hi\right)}
\end{array}
Derivation
  1. Initial program 3.1%

    \[\frac{x - lo}{hi - lo} \]
  2. Add Preprocessing
  3. Taylor expanded in hi around inf

    \[\leadsto \color{blue}{\frac{\left(x + \frac{{lo}^{2} \cdot \left(x - lo\right)}{{hi}^{2}}\right) - \left(lo + -1 \cdot \frac{lo \cdot \left(x - lo\right)}{hi}\right)}{hi}} \]
  4. Applied rewrites15.2%

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{x - lo}{hi}, \mathsf{fma}\left(lo, \frac{lo}{hi}, lo\right), x - lo\right)}{hi}} \]
  5. Taylor expanded in lo around inf

    \[\leadsto -1 \cdot \color{blue}{\frac{{lo}^{3}}{{hi}^{3}}} \]
  6. Step-by-step derivation
    1. Applied rewrites19.3%

      \[\leadsto {\left(\frac{-lo}{hi}\right)}^{\color{blue}{3}} \]
    2. Step-by-step derivation
      1. Applied rewrites19.3%

        \[\leadsto e^{\log \left(\frac{-lo}{hi}\right) \cdot 3} \]
      2. Taylor expanded in lo around -inf

        \[\leadsto e^{3 \cdot \left(\log \left(\frac{1}{hi}\right) + -1 \cdot \log \left(\frac{-1}{lo}\right)\right)} \]
      3. Step-by-step derivation
        1. Applied rewrites19.3%

          \[\leadsto e^{\mathsf{fma}\left(-3, \log \left(\frac{-1}{lo}\right), -3 \cdot \log hi\right)} \]
        2. Add Preprocessing

        Alternative 2: 19.3% accurate, 0.2× speedup?

        \[\begin{array}{l} \\ {\left(\frac{-lo}{hi}\right)}^{3} \end{array} \]
        (FPCore (lo hi x) :precision binary64 (pow (/ (- lo) hi) 3.0))
        double code(double lo, double hi, double x) {
        	return pow((-lo / hi), 3.0);
        }
        
        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) ** 3.0d0
        end function
        
        public static double code(double lo, double hi, double x) {
        	return Math.pow((-lo / hi), 3.0);
        }
        
        def code(lo, hi, x):
        	return math.pow((-lo / hi), 3.0)
        
        function code(lo, hi, x)
        	return Float64(Float64(-lo) / hi) ^ 3.0
        end
        
        function tmp = code(lo, hi, x)
        	tmp = (-lo / hi) ^ 3.0;
        end
        
        code[lo_, hi_, x_] := N[Power[N[((-lo) / hi), $MachinePrecision], 3.0], $MachinePrecision]
        
        \begin{array}{l}
        
        \\
        {\left(\frac{-lo}{hi}\right)}^{3}
        \end{array}
        
        Derivation
        1. Initial program 3.1%

          \[\frac{x - lo}{hi - lo} \]
        2. Add Preprocessing
        3. Taylor expanded in hi around inf

          \[\leadsto \color{blue}{\frac{\left(x + \frac{{lo}^{2} \cdot \left(x - lo\right)}{{hi}^{2}}\right) - \left(lo + -1 \cdot \frac{lo \cdot \left(x - lo\right)}{hi}\right)}{hi}} \]
        4. Applied rewrites15.2%

          \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\frac{x - lo}{hi}, \mathsf{fma}\left(lo, \frac{lo}{hi}, lo\right), x - lo\right)}{hi}} \]
        5. Taylor expanded in lo around inf

          \[\leadsto -1 \cdot \color{blue}{\frac{{lo}^{3}}{{hi}^{3}}} \]
        6. Step-by-step derivation
          1. Applied rewrites19.3%

            \[\leadsto {\left(\frac{-lo}{hi}\right)}^{\color{blue}{3}} \]
          2. Add Preprocessing

          Alternative 3: 19.4% accurate, 0.4× speedup?

          \[\begin{array}{l} \\ \frac{\frac{1 - \frac{x}{lo}}{lo} \cdot hi}{lo} \cdot hi \end{array} \]
          (FPCore (lo hi x)
           :precision binary64
           (* (/ (* (/ (- 1.0 (/ x lo)) lo) hi) lo) hi))
          double code(double lo, double hi, double x) {
          	return ((((1.0 - (x / lo)) / lo) * 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 = ((((1.0d0 - (x / lo)) / lo) * hi) / lo) * hi
          end function
          
          public static double code(double lo, double hi, double x) {
          	return ((((1.0 - (x / lo)) / lo) * hi) / lo) * hi;
          }
          
          def code(lo, hi, x):
          	return ((((1.0 - (x / lo)) / lo) * hi) / lo) * hi
          
          function code(lo, hi, x)
          	return Float64(Float64(Float64(Float64(Float64(1.0 - Float64(x / lo)) / lo) * hi) / lo) * hi)
          end
          
          function tmp = code(lo, hi, x)
          	tmp = ((((1.0 - (x / lo)) / lo) * hi) / lo) * hi;
          end
          
          code[lo_, hi_, x_] := N[(N[(N[(N[(N[(1.0 - N[(x / lo), $MachinePrecision]), $MachinePrecision] / lo), $MachinePrecision] * hi), $MachinePrecision] / lo), $MachinePrecision] * hi), $MachinePrecision]
          
          \begin{array}{l}
          
          \\
          \frac{\frac{1 - \frac{x}{lo}}{lo} \cdot hi}{lo} \cdot hi
          \end{array}
          
          Derivation
          1. Initial program 3.1%

            \[\frac{x - lo}{hi - lo} \]
          2. Add Preprocessing
          3. Taylor expanded in hi around 0

            \[\leadsto \color{blue}{-1 \cdot \frac{x - lo}{lo} + hi \cdot \left(\left(\frac{1}{lo} + \frac{hi \cdot \left(\frac{1}{lo} - \frac{x}{{lo}^{2}}\right)}{lo}\right) - \frac{x}{{lo}^{2}}\right)} \]
          4. Applied rewrites18.8%

            \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\mathsf{fma}\left(\frac{1}{lo} - \frac{\frac{x}{lo}}{lo}, hi, 1\right)}{lo} - \frac{\frac{x}{lo}}{lo}, hi, 1 - \frac{x}{lo}\right)} \]
          5. Taylor expanded in hi around inf

            \[\leadsto {hi}^{2} \cdot \color{blue}{\left(\frac{1}{{lo}^{2}} - \frac{x}{{lo}^{3}}\right)} \]
          6. Step-by-step derivation
            1. Applied rewrites19.3%

              \[\leadsto \frac{\frac{1 - \frac{x}{lo}}{lo} \cdot hi}{lo} \cdot \color{blue}{hi} \]
            2. Add Preprocessing

            Alternative 4: 18.9% accurate, 0.4× speedup?

            \[\begin{array}{l} \\ \mathsf{fma}\left(\frac{\frac{hi}{lo} + 1}{lo}, hi, 1 - \frac{x}{lo}\right) \end{array} \]
            (FPCore (lo hi x)
             :precision binary64
             (fma (/ (+ (/ hi lo) 1.0) lo) hi (- 1.0 (/ x lo))))
            double code(double lo, double hi, double x) {
            	return fma((((hi / lo) + 1.0) / lo), hi, (1.0 - (x / lo)));
            }
            
            function code(lo, hi, x)
            	return fma(Float64(Float64(Float64(hi / lo) + 1.0) / lo), hi, Float64(1.0 - Float64(x / lo)))
            end
            
            code[lo_, hi_, x_] := N[(N[(N[(N[(hi / lo), $MachinePrecision] + 1.0), $MachinePrecision] / lo), $MachinePrecision] * hi + N[(1.0 - N[(x / lo), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
            
            \begin{array}{l}
            
            \\
            \mathsf{fma}\left(\frac{\frac{hi}{lo} + 1}{lo}, hi, 1 - \frac{x}{lo}\right)
            \end{array}
            
            Derivation
            1. Initial program 3.1%

              \[\frac{x - lo}{hi - lo} \]
            2. Add Preprocessing
            3. Taylor expanded in hi around 0

              \[\leadsto \color{blue}{-1 \cdot \frac{x - lo}{lo} + hi \cdot \left(\left(\frac{1}{lo} + \frac{hi \cdot \left(\frac{1}{lo} - \frac{x}{{lo}^{2}}\right)}{lo}\right) - \frac{x}{{lo}^{2}}\right)} \]
            4. Applied rewrites18.8%

              \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\mathsf{fma}\left(\frac{1}{lo} - \frac{\frac{x}{lo}}{lo}, hi, 1\right)}{lo} - \frac{\frac{x}{lo}}{lo}, hi, 1 - \frac{x}{lo}\right)} \]
            5. Taylor expanded in x around 0

              \[\leadsto \mathsf{fma}\left(\frac{1}{lo} + \frac{hi}{{lo}^{2}}, hi, 1 - \frac{x}{lo}\right) \]
            6. Step-by-step derivation
              1. Applied rewrites18.8%

                \[\leadsto \mathsf{fma}\left(\frac{\frac{hi}{lo} + 1}{lo}, hi, 1 - \frac{x}{lo}\right) \]
              2. Add Preprocessing

              Alternative 5: 18.9% accurate, 0.6× speedup?

              \[\begin{array}{l} \\ \mathsf{fma}\left(\frac{\frac{hi}{lo} + 1}{lo}, hi, 1\right) \end{array} \]
              (FPCore (lo hi x) :precision binary64 (fma (/ (+ (/ hi lo) 1.0) lo) hi 1.0))
              double code(double lo, double hi, double x) {
              	return fma((((hi / lo) + 1.0) / lo), hi, 1.0);
              }
              
              function code(lo, hi, x)
              	return fma(Float64(Float64(Float64(hi / lo) + 1.0) / lo), hi, 1.0)
              end
              
              code[lo_, hi_, x_] := N[(N[(N[(N[(hi / lo), $MachinePrecision] + 1.0), $MachinePrecision] / lo), $MachinePrecision] * hi + 1.0), $MachinePrecision]
              
              \begin{array}{l}
              
              \\
              \mathsf{fma}\left(\frac{\frac{hi}{lo} + 1}{lo}, hi, 1\right)
              \end{array}
              
              Derivation
              1. Initial program 3.1%

                \[\frac{x - lo}{hi - lo} \]
              2. Add Preprocessing
              3. Taylor expanded in hi around 0

                \[\leadsto \color{blue}{-1 \cdot \frac{x - lo}{lo} + hi \cdot \left(\left(\frac{1}{lo} + \frac{hi \cdot \left(\frac{1}{lo} - \frac{x}{{lo}^{2}}\right)}{lo}\right) - \frac{x}{{lo}^{2}}\right)} \]
              4. Applied rewrites18.8%

                \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\mathsf{fma}\left(\frac{1}{lo} - \frac{\frac{x}{lo}}{lo}, hi, 1\right)}{lo} - \frac{\frac{x}{lo}}{lo}, hi, 1 - \frac{x}{lo}\right)} \]
              5. Taylor expanded in x around 0

                \[\leadsto 1 + \color{blue}{hi \cdot \left(\frac{1}{lo} + \frac{hi}{{lo}^{2}}\right)} \]
              6. Step-by-step derivation
                1. Applied rewrites18.8%

                  \[\leadsto \mathsf{fma}\left(\frac{\frac{hi}{lo} + 1}{lo}, \color{blue}{hi}, 1\right) \]
                2. Add Preprocessing

                Alternative 6: 18.8% accurate, 1.2× speedup?

                \[\begin{array}{l} \\ \frac{x - lo}{hi} \end{array} \]
                (FPCore (lo hi x) :precision binary64 (/ (- x lo) hi))
                double code(double lo, double hi, double x) {
                	return (x - 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 = (x - lo) / hi
                end function
                
                public static double code(double lo, double hi, double x) {
                	return (x - lo) / hi;
                }
                
                def code(lo, hi, x):
                	return (x - lo) / hi
                
                function code(lo, hi, x)
                	return Float64(Float64(x - lo) / hi)
                end
                
                function tmp = code(lo, hi, x)
                	tmp = (x - lo) / hi;
                end
                
                code[lo_, hi_, x_] := N[(N[(x - lo), $MachinePrecision] / hi), $MachinePrecision]
                
                \begin{array}{l}
                
                \\
                \frac{x - lo}{hi}
                \end{array}
                
                Derivation
                1. Initial program 3.1%

                  \[\frac{x - lo}{hi - lo} \]
                2. Add Preprocessing
                3. Taylor expanded in lo around 0

                  \[\leadsto \frac{x - lo}{\color{blue}{hi}} \]
                4. Step-by-step derivation
                  1. Applied rewrites18.8%

                    \[\leadsto \frac{x - lo}{\color{blue}{hi}} \]
                  2. Add Preprocessing

                  Alternative 7: 18.8% accurate, 1.3× speedup?

                  \[\begin{array}{l} \\ \frac{-lo}{hi} \end{array} \]
                  (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}
                  
                  Derivation
                  1. Initial program 3.1%

                    \[\frac{x - lo}{hi - lo} \]
                  2. Add Preprocessing
                  3. Taylor expanded in lo around 0

                    \[\leadsto \frac{x - lo}{\color{blue}{hi}} \]
                  4. Step-by-step derivation
                    1. Applied rewrites18.8%

                      \[\leadsto \frac{x - lo}{\color{blue}{hi}} \]
                    2. Taylor expanded in lo around inf

                      \[\leadsto \frac{\color{blue}{-1 \cdot lo}}{hi} \]
                    3. Step-by-step derivation
                      1. Applied rewrites18.8%

                        \[\leadsto \frac{\color{blue}{-lo}}{hi} \]
                      2. Add Preprocessing

                      Alternative 8: 18.7% accurate, 18.0× speedup?

                      \[\begin{array}{l} \\ 1 \end{array} \]
                      (FPCore (lo hi x) :precision binary64 1.0)
                      double code(double lo, double hi, double x) {
                      	return 1.0;
                      }
                      
                      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 = 1.0d0
                      end function
                      
                      public static double code(double lo, double hi, double x) {
                      	return 1.0;
                      }
                      
                      def code(lo, hi, x):
                      	return 1.0
                      
                      function code(lo, hi, x)
                      	return 1.0
                      end
                      
                      function tmp = code(lo, hi, x)
                      	tmp = 1.0;
                      end
                      
                      code[lo_, hi_, x_] := 1.0
                      
                      \begin{array}{l}
                      
                      \\
                      1
                      \end{array}
                      
                      Derivation
                      1. Initial program 3.1%

                        \[\frac{x - lo}{hi - lo} \]
                      2. Add Preprocessing
                      3. Taylor expanded in lo around inf

                        \[\leadsto \color{blue}{1} \]
                      4. Step-by-step derivation
                        1. Applied rewrites18.7%

                          \[\leadsto \color{blue}{1} \]
                        2. Add Preprocessing

                        Reproduce

                        ?
                        herbie shell --seed 2025019 
                        (FPCore (lo hi x)
                          :name "xlohi (overflows)"
                          :precision binary64
                          :pre (and (< lo -1e+308) (> hi 1e+308))
                          (/ (- x lo) (- hi lo)))