xlohi (overflows)

Percentage Accurate: 3.1% → 26.9%
Time: 17.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);
}
real(8) function code(lo, hi, x)
    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);
}
real(8) function code(lo, hi, x)
    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: 26.9% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{x}{lo} + -1\\ t_1 := 1 - \frac{x}{lo}\\ t_2 := \frac{t\_1}{lo}\\ t_3 := \left(\frac{hi}{lo} + 1\right) \cdot t\_2\\ \left(\mathsf{fma}\left(hi, t\_3, t\_1\right) \cdot \mathsf{fma}\left(hi, t\_3, t\_0\right)\right) \cdot \frac{1}{\mathsf{fma}\left(hi, t\_2 \cdot 1, t\_0\right)} \end{array} \end{array} \]
(FPCore (lo hi x)
 :precision binary64
 (let* ((t_0 (+ (/ x lo) -1.0))
        (t_1 (- 1.0 (/ x lo)))
        (t_2 (/ t_1 lo))
        (t_3 (* (+ (/ hi lo) 1.0) t_2)))
   (* (* (fma hi t_3 t_1) (fma hi t_3 t_0)) (/ 1.0 (fma hi (* t_2 1.0) t_0)))))
double code(double lo, double hi, double x) {
	double t_0 = (x / lo) + -1.0;
	double t_1 = 1.0 - (x / lo);
	double t_2 = t_1 / lo;
	double t_3 = ((hi / lo) + 1.0) * t_2;
	return (fma(hi, t_3, t_1) * fma(hi, t_3, t_0)) * (1.0 / fma(hi, (t_2 * 1.0), t_0));
}
function code(lo, hi, x)
	t_0 = Float64(Float64(x / lo) + -1.0)
	t_1 = Float64(1.0 - Float64(x / lo))
	t_2 = Float64(t_1 / lo)
	t_3 = Float64(Float64(Float64(hi / lo) + 1.0) * t_2)
	return Float64(Float64(fma(hi, t_3, t_1) * fma(hi, t_3, t_0)) * Float64(1.0 / fma(hi, Float64(t_2 * 1.0), t_0)))
end
code[lo_, hi_, x_] := Block[{t$95$0 = N[(N[(x / lo), $MachinePrecision] + -1.0), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 - N[(x / lo), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 / lo), $MachinePrecision]}, Block[{t$95$3 = N[(N[(N[(hi / lo), $MachinePrecision] + 1.0), $MachinePrecision] * t$95$2), $MachinePrecision]}, N[(N[(N[(hi * t$95$3 + t$95$1), $MachinePrecision] * N[(hi * t$95$3 + t$95$0), $MachinePrecision]), $MachinePrecision] * N[(1.0 / N[(hi * N[(t$95$2 * 1.0), $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{x}{lo} + -1\\
t_1 := 1 - \frac{x}{lo}\\
t_2 := \frac{t\_1}{lo}\\
t_3 := \left(\frac{hi}{lo} + 1\right) \cdot t\_2\\
\left(\mathsf{fma}\left(hi, t\_3, t\_1\right) \cdot \mathsf{fma}\left(hi, t\_3, t\_0\right)\right) \cdot \frac{1}{\mathsf{fma}\left(hi, t\_2 \cdot 1, t\_0\right)}
\end{array}
\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 + -1 \cdot \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
  4. Step-by-step derivation
    1. mul-1-negN/A

      \[\leadsto 1 + \color{blue}{\left(\mathsf{neg}\left(\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}\right)\right)} \]
    2. unsub-negN/A

      \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
    3. lower--.f64N/A

      \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
    4. lower-/.f64N/A

      \[\leadsto 1 - \color{blue}{\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
    5. +-commutativeN/A

      \[\leadsto 1 - \frac{\color{blue}{\left(\frac{hi \cdot \left(x - hi\right)}{lo} + x\right)} - hi}{lo} \]
    6. associate--l+N/A

      \[\leadsto 1 - \frac{\color{blue}{\frac{hi \cdot \left(x - hi\right)}{lo} + \left(x - hi\right)}}{lo} \]
    7. associate-/l*N/A

      \[\leadsto 1 - \frac{\color{blue}{hi \cdot \frac{x - hi}{lo}} + \left(x - hi\right)}{lo} \]
    8. lower-fma.f64N/A

      \[\leadsto 1 - \frac{\color{blue}{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, x - hi\right)}}{lo} \]
    9. lower-/.f64N/A

      \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \color{blue}{\frac{x - hi}{lo}}, x - hi\right)}{lo} \]
    10. lower--.f64N/A

      \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \frac{\color{blue}{x - hi}}{lo}, x - hi\right)}{lo} \]
    11. lower--.f6418.8

      \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, \color{blue}{x - hi}\right)}{lo} \]
  5. Applied rewrites18.8%

    \[\leadsto \color{blue}{1 - \frac{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, x - hi\right)}{lo}} \]
  6. 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)} \]
  7. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto \color{blue}{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) + -1 \cdot \frac{x - lo}{lo}} \]
    2. mul-1-negN/A

      \[\leadsto 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) + \color{blue}{\left(\mathsf{neg}\left(\frac{x - lo}{lo}\right)\right)} \]
    3. div-subN/A

      \[\leadsto 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) + \left(\mathsf{neg}\left(\color{blue}{\left(\frac{x}{lo} - \frac{lo}{lo}\right)}\right)\right) \]
    4. sub-negN/A

      \[\leadsto 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) + \left(\mathsf{neg}\left(\color{blue}{\left(\frac{x}{lo} + \left(\mathsf{neg}\left(\frac{lo}{lo}\right)\right)\right)}\right)\right) \]
    5. *-inversesN/A

      \[\leadsto 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) + \left(\mathsf{neg}\left(\left(\frac{x}{lo} + \left(\mathsf{neg}\left(\color{blue}{1}\right)\right)\right)\right)\right) \]
    6. metadata-evalN/A

      \[\leadsto 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) + \left(\mathsf{neg}\left(\left(\frac{x}{lo} + \color{blue}{-1}\right)\right)\right) \]
    7. distribute-neg-inN/A

      \[\leadsto 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) + \color{blue}{\left(\left(\mathsf{neg}\left(\frac{x}{lo}\right)\right) + \left(\mathsf{neg}\left(-1\right)\right)\right)} \]
    8. mul-1-negN/A

      \[\leadsto 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) + \left(\color{blue}{-1 \cdot \frac{x}{lo}} + \left(\mathsf{neg}\left(-1\right)\right)\right) \]
    9. metadata-evalN/A

      \[\leadsto 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) + \left(-1 \cdot \frac{x}{lo} + \color{blue}{1}\right) \]
    10. +-commutativeN/A

      \[\leadsto 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) + \color{blue}{\left(1 + -1 \cdot \frac{x}{lo}\right)} \]
    11. lower-fma.f64N/A

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

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

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

      \[\leadsto \left(\mathsf{fma}\left(hi, \left(\frac{hi}{lo} + 1\right) \cdot \frac{1 - \frac{x}{lo}}{lo}, 1 - \frac{x}{lo}\right) \cdot \mathsf{fma}\left(hi, \left(\frac{hi}{lo} + 1\right) \cdot \frac{1 - \frac{x}{lo}}{lo}, -1 + \frac{x}{lo}\right)\right) \cdot \frac{1}{\mathsf{fma}\left(hi, 1 \cdot \frac{\color{blue}{1 - \frac{x}{lo}}}{lo}, -1 + \frac{x}{lo}\right)} \]
    3. Step-by-step derivation
      1. Applied rewrites26.7%

        \[\leadsto \left(\mathsf{fma}\left(hi, \left(\frac{hi}{lo} + 1\right) \cdot \frac{1 - \frac{x}{lo}}{lo}, 1 - \frac{x}{lo}\right) \cdot \mathsf{fma}\left(hi, \left(\frac{hi}{lo} + 1\right) \cdot \frac{1 - \frac{x}{lo}}{lo}, -1 + \frac{x}{lo}\right)\right) \cdot \frac{1}{\mathsf{fma}\left(hi, 1 \cdot \frac{\color{blue}{1 - \frac{x}{lo}}}{lo}, -1 + \frac{x}{lo}\right)} \]
      2. Final simplification26.7%

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

      Alternative 2: 26.9% accurate, 0.1× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} t_0 := 1 - \frac{x}{lo}\\ t_1 := \left(\frac{hi}{lo} + 1\right) \cdot \frac{t\_0}{lo}\\ \left(\mathsf{fma}\left(hi, t\_1, t\_0\right) \cdot \mathsf{fma}\left(hi, t\_1, \frac{x}{lo} + -1\right)\right) \cdot \frac{1}{-1 + \frac{x + hi}{lo}} \end{array} \end{array} \]
      (FPCore (lo hi x)
       :precision binary64
       (let* ((t_0 (- 1.0 (/ x lo))) (t_1 (* (+ (/ hi lo) 1.0) (/ t_0 lo))))
         (*
          (* (fma hi t_1 t_0) (fma hi t_1 (+ (/ x lo) -1.0)))
          (/ 1.0 (+ -1.0 (/ (+ x hi) lo))))))
      double code(double lo, double hi, double x) {
      	double t_0 = 1.0 - (x / lo);
      	double t_1 = ((hi / lo) + 1.0) * (t_0 / lo);
      	return (fma(hi, t_1, t_0) * fma(hi, t_1, ((x / lo) + -1.0))) * (1.0 / (-1.0 + ((x + hi) / lo)));
      }
      
      function code(lo, hi, x)
      	t_0 = Float64(1.0 - Float64(x / lo))
      	t_1 = Float64(Float64(Float64(hi / lo) + 1.0) * Float64(t_0 / lo))
      	return Float64(Float64(fma(hi, t_1, t_0) * fma(hi, t_1, Float64(Float64(x / lo) + -1.0))) * Float64(1.0 / Float64(-1.0 + Float64(Float64(x + hi) / lo))))
      end
      
      code[lo_, hi_, x_] := Block[{t$95$0 = N[(1.0 - N[(x / lo), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(hi / lo), $MachinePrecision] + 1.0), $MachinePrecision] * N[(t$95$0 / lo), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[(hi * t$95$1 + t$95$0), $MachinePrecision] * N[(hi * t$95$1 + N[(N[(x / lo), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(1.0 / N[(-1.0 + N[(N[(x + hi), $MachinePrecision] / lo), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      t_0 := 1 - \frac{x}{lo}\\
      t_1 := \left(\frac{hi}{lo} + 1\right) \cdot \frac{t\_0}{lo}\\
      \left(\mathsf{fma}\left(hi, t\_1, t\_0\right) \cdot \mathsf{fma}\left(hi, t\_1, \frac{x}{lo} + -1\right)\right) \cdot \frac{1}{-1 + \frac{x + hi}{lo}}
      \end{array}
      \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 + -1 \cdot \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
      4. Step-by-step derivation
        1. mul-1-negN/A

          \[\leadsto 1 + \color{blue}{\left(\mathsf{neg}\left(\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}\right)\right)} \]
        2. unsub-negN/A

          \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
        3. lower--.f64N/A

          \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
        4. lower-/.f64N/A

          \[\leadsto 1 - \color{blue}{\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
        5. +-commutativeN/A

          \[\leadsto 1 - \frac{\color{blue}{\left(\frac{hi \cdot \left(x - hi\right)}{lo} + x\right)} - hi}{lo} \]
        6. associate--l+N/A

          \[\leadsto 1 - \frac{\color{blue}{\frac{hi \cdot \left(x - hi\right)}{lo} + \left(x - hi\right)}}{lo} \]
        7. associate-/l*N/A

          \[\leadsto 1 - \frac{\color{blue}{hi \cdot \frac{x - hi}{lo}} + \left(x - hi\right)}{lo} \]
        8. lower-fma.f64N/A

          \[\leadsto 1 - \frac{\color{blue}{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, x - hi\right)}}{lo} \]
        9. lower-/.f64N/A

          \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \color{blue}{\frac{x - hi}{lo}}, x - hi\right)}{lo} \]
        10. lower--.f64N/A

          \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \frac{\color{blue}{x - hi}}{lo}, x - hi\right)}{lo} \]
        11. lower--.f6418.8

          \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, \color{blue}{x - hi}\right)}{lo} \]
      5. Applied rewrites18.8%

        \[\leadsto \color{blue}{1 - \frac{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, x - hi\right)}{lo}} \]
      6. 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)} \]
      7. Step-by-step derivation
        1. +-commutativeN/A

          \[\leadsto \color{blue}{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) + -1 \cdot \frac{x - lo}{lo}} \]
        2. mul-1-negN/A

          \[\leadsto 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) + \color{blue}{\left(\mathsf{neg}\left(\frac{x - lo}{lo}\right)\right)} \]
        3. div-subN/A

          \[\leadsto 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) + \left(\mathsf{neg}\left(\color{blue}{\left(\frac{x}{lo} - \frac{lo}{lo}\right)}\right)\right) \]
        4. sub-negN/A

          \[\leadsto 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) + \left(\mathsf{neg}\left(\color{blue}{\left(\frac{x}{lo} + \left(\mathsf{neg}\left(\frac{lo}{lo}\right)\right)\right)}\right)\right) \]
        5. *-inversesN/A

          \[\leadsto 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) + \left(\mathsf{neg}\left(\left(\frac{x}{lo} + \left(\mathsf{neg}\left(\color{blue}{1}\right)\right)\right)\right)\right) \]
        6. metadata-evalN/A

          \[\leadsto 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) + \left(\mathsf{neg}\left(\left(\frac{x}{lo} + \color{blue}{-1}\right)\right)\right) \]
        7. distribute-neg-inN/A

          \[\leadsto 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) + \color{blue}{\left(\left(\mathsf{neg}\left(\frac{x}{lo}\right)\right) + \left(\mathsf{neg}\left(-1\right)\right)\right)} \]
        8. mul-1-negN/A

          \[\leadsto 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) + \left(\color{blue}{-1 \cdot \frac{x}{lo}} + \left(\mathsf{neg}\left(-1\right)\right)\right) \]
        9. metadata-evalN/A

          \[\leadsto 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) + \left(-1 \cdot \frac{x}{lo} + \color{blue}{1}\right) \]
        10. +-commutativeN/A

          \[\leadsto 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) + \color{blue}{\left(1 + -1 \cdot \frac{x}{lo}\right)} \]
        11. lower-fma.f64N/A

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

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

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

          \[\leadsto \left(\mathsf{fma}\left(hi, \left(\frac{hi}{lo} + 1\right) \cdot \frac{1 - \frac{x}{lo}}{lo}, 1 - \frac{x}{lo}\right) \cdot \mathsf{fma}\left(hi, \left(\frac{hi}{lo} + 1\right) \cdot \frac{1 - \frac{x}{lo}}{lo}, -1 + \frac{x}{lo}\right)\right) \cdot \frac{1}{-1 \cdot \frac{-1 \cdot hi + -1 \cdot x}{lo} - \color{blue}{1}} \]
        3. Step-by-step derivation
          1. Applied rewrites26.7%

            \[\leadsto \left(\mathsf{fma}\left(hi, \left(\frac{hi}{lo} + 1\right) \cdot \frac{1 - \frac{x}{lo}}{lo}, 1 - \frac{x}{lo}\right) \cdot \mathsf{fma}\left(hi, \left(\frac{hi}{lo} + 1\right) \cdot \frac{1 - \frac{x}{lo}}{lo}, -1 + \frac{x}{lo}\right)\right) \cdot \frac{1}{-1 + \color{blue}{\frac{x + hi}{lo}}} \]
          2. Final simplification26.7%

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

          Alternative 3: 19.0% accurate, 0.1× speedup?

          \[\begin{array}{l} \\ \begin{array}{l} t_0 := 1 - \frac{x}{lo}\\ t_1 := \left(\frac{hi}{lo} + 1\right) \cdot \frac{t\_0}{lo}\\ \left(\mathsf{fma}\left(hi, t\_1, t\_0\right) \cdot -1\right) \cdot \frac{1}{\mathsf{fma}\left(hi, t\_1, \frac{x}{lo} + -1\right)} \end{array} \end{array} \]
          (FPCore (lo hi x)
           :precision binary64
           (let* ((t_0 (- 1.0 (/ x lo))) (t_1 (* (+ (/ hi lo) 1.0) (/ t_0 lo))))
             (* (* (fma hi t_1 t_0) -1.0) (/ 1.0 (fma hi t_1 (+ (/ x lo) -1.0))))))
          double code(double lo, double hi, double x) {
          	double t_0 = 1.0 - (x / lo);
          	double t_1 = ((hi / lo) + 1.0) * (t_0 / lo);
          	return (fma(hi, t_1, t_0) * -1.0) * (1.0 / fma(hi, t_1, ((x / lo) + -1.0)));
          }
          
          function code(lo, hi, x)
          	t_0 = Float64(1.0 - Float64(x / lo))
          	t_1 = Float64(Float64(Float64(hi / lo) + 1.0) * Float64(t_0 / lo))
          	return Float64(Float64(fma(hi, t_1, t_0) * -1.0) * Float64(1.0 / fma(hi, t_1, Float64(Float64(x / lo) + -1.0))))
          end
          
          code[lo_, hi_, x_] := Block[{t$95$0 = N[(1.0 - N[(x / lo), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(hi / lo), $MachinePrecision] + 1.0), $MachinePrecision] * N[(t$95$0 / lo), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[(hi * t$95$1 + t$95$0), $MachinePrecision] * -1.0), $MachinePrecision] * N[(1.0 / N[(hi * t$95$1 + N[(N[(x / lo), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
          
          \begin{array}{l}
          
          \\
          \begin{array}{l}
          t_0 := 1 - \frac{x}{lo}\\
          t_1 := \left(\frac{hi}{lo} + 1\right) \cdot \frac{t\_0}{lo}\\
          \left(\mathsf{fma}\left(hi, t\_1, t\_0\right) \cdot -1\right) \cdot \frac{1}{\mathsf{fma}\left(hi, t\_1, \frac{x}{lo} + -1\right)}
          \end{array}
          \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 + -1 \cdot \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
          4. Step-by-step derivation
            1. mul-1-negN/A

              \[\leadsto 1 + \color{blue}{\left(\mathsf{neg}\left(\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}\right)\right)} \]
            2. unsub-negN/A

              \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
            3. lower--.f64N/A

              \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
            4. lower-/.f64N/A

              \[\leadsto 1 - \color{blue}{\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
            5. +-commutativeN/A

              \[\leadsto 1 - \frac{\color{blue}{\left(\frac{hi \cdot \left(x - hi\right)}{lo} + x\right)} - hi}{lo} \]
            6. associate--l+N/A

              \[\leadsto 1 - \frac{\color{blue}{\frac{hi \cdot \left(x - hi\right)}{lo} + \left(x - hi\right)}}{lo} \]
            7. associate-/l*N/A

              \[\leadsto 1 - \frac{\color{blue}{hi \cdot \frac{x - hi}{lo}} + \left(x - hi\right)}{lo} \]
            8. lower-fma.f64N/A

              \[\leadsto 1 - \frac{\color{blue}{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, x - hi\right)}}{lo} \]
            9. lower-/.f64N/A

              \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \color{blue}{\frac{x - hi}{lo}}, x - hi\right)}{lo} \]
            10. lower--.f64N/A

              \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \frac{\color{blue}{x - hi}}{lo}, x - hi\right)}{lo} \]
            11. lower--.f6418.8

              \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, \color{blue}{x - hi}\right)}{lo} \]
          5. Applied rewrites18.8%

            \[\leadsto \color{blue}{1 - \frac{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, x - hi\right)}{lo}} \]
          6. 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)} \]
          7. Step-by-step derivation
            1. +-commutativeN/A

              \[\leadsto \color{blue}{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) + -1 \cdot \frac{x - lo}{lo}} \]
            2. mul-1-negN/A

              \[\leadsto 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) + \color{blue}{\left(\mathsf{neg}\left(\frac{x - lo}{lo}\right)\right)} \]
            3. div-subN/A

              \[\leadsto 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) + \left(\mathsf{neg}\left(\color{blue}{\left(\frac{x}{lo} - \frac{lo}{lo}\right)}\right)\right) \]
            4. sub-negN/A

              \[\leadsto 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) + \left(\mathsf{neg}\left(\color{blue}{\left(\frac{x}{lo} + \left(\mathsf{neg}\left(\frac{lo}{lo}\right)\right)\right)}\right)\right) \]
            5. *-inversesN/A

              \[\leadsto 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) + \left(\mathsf{neg}\left(\left(\frac{x}{lo} + \left(\mathsf{neg}\left(\color{blue}{1}\right)\right)\right)\right)\right) \]
            6. metadata-evalN/A

              \[\leadsto 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) + \left(\mathsf{neg}\left(\left(\frac{x}{lo} + \color{blue}{-1}\right)\right)\right) \]
            7. distribute-neg-inN/A

              \[\leadsto 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) + \color{blue}{\left(\left(\mathsf{neg}\left(\frac{x}{lo}\right)\right) + \left(\mathsf{neg}\left(-1\right)\right)\right)} \]
            8. mul-1-negN/A

              \[\leadsto 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) + \left(\color{blue}{-1 \cdot \frac{x}{lo}} + \left(\mathsf{neg}\left(-1\right)\right)\right) \]
            9. metadata-evalN/A

              \[\leadsto 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) + \left(-1 \cdot \frac{x}{lo} + \color{blue}{1}\right) \]
            10. +-commutativeN/A

              \[\leadsto 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) + \color{blue}{\left(1 + -1 \cdot \frac{x}{lo}\right)} \]
            11. lower-fma.f64N/A

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

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

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

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

                \[\leadsto \left(\mathsf{fma}\left(hi, \left(\frac{hi}{lo} + 1\right) \cdot \frac{1 - \frac{x}{lo}}{lo}, 1 - \frac{x}{lo}\right) \cdot -1\right) \cdot \frac{1}{\mathsf{fma}\left(hi, \left(\frac{hi}{lo} + 1\right) \cdot \frac{1 - \frac{x}{lo}}{lo}, -1 + \frac{x}{lo}\right)} \]
              2. Final simplification18.9%

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

              Alternative 4: 18.9% accurate, 0.3× speedup?

              \[\begin{array}{l} \\ \begin{array}{l} t_0 := 1 - \frac{x}{lo}\\ \mathsf{fma}\left(\left(\frac{hi}{lo} + 1\right) \cdot \frac{t\_0}{lo}, hi, t\_0\right) \end{array} \end{array} \]
              (FPCore (lo hi x)
               :precision binary64
               (let* ((t_0 (- 1.0 (/ x lo)))) (fma (* (+ (/ hi lo) 1.0) (/ t_0 lo)) hi t_0)))
              double code(double lo, double hi, double x) {
              	double t_0 = 1.0 - (x / lo);
              	return fma((((hi / lo) + 1.0) * (t_0 / lo)), hi, t_0);
              }
              
              function code(lo, hi, x)
              	t_0 = Float64(1.0 - Float64(x / lo))
              	return fma(Float64(Float64(Float64(hi / lo) + 1.0) * Float64(t_0 / lo)), hi, t_0)
              end
              
              code[lo_, hi_, x_] := Block[{t$95$0 = N[(1.0 - N[(x / lo), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[(N[(hi / lo), $MachinePrecision] + 1.0), $MachinePrecision] * N[(t$95$0 / lo), $MachinePrecision]), $MachinePrecision] * hi + t$95$0), $MachinePrecision]]
              
              \begin{array}{l}
              
              \\
              \begin{array}{l}
              t_0 := 1 - \frac{x}{lo}\\
              \mathsf{fma}\left(\left(\frac{hi}{lo} + 1\right) \cdot \frac{t\_0}{lo}, hi, t\_0\right)
              \end{array}
              \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 + -1 \cdot \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
              4. Step-by-step derivation
                1. mul-1-negN/A

                  \[\leadsto 1 + \color{blue}{\left(\mathsf{neg}\left(\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}\right)\right)} \]
                2. unsub-negN/A

                  \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
                3. lower--.f64N/A

                  \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
                4. lower-/.f64N/A

                  \[\leadsto 1 - \color{blue}{\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
                5. +-commutativeN/A

                  \[\leadsto 1 - \frac{\color{blue}{\left(\frac{hi \cdot \left(x - hi\right)}{lo} + x\right)} - hi}{lo} \]
                6. associate--l+N/A

                  \[\leadsto 1 - \frac{\color{blue}{\frac{hi \cdot \left(x - hi\right)}{lo} + \left(x - hi\right)}}{lo} \]
                7. associate-/l*N/A

                  \[\leadsto 1 - \frac{\color{blue}{hi \cdot \frac{x - hi}{lo}} + \left(x - hi\right)}{lo} \]
                8. lower-fma.f64N/A

                  \[\leadsto 1 - \frac{\color{blue}{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, x - hi\right)}}{lo} \]
                9. lower-/.f64N/A

                  \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \color{blue}{\frac{x - hi}{lo}}, x - hi\right)}{lo} \]
                10. lower--.f64N/A

                  \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \frac{\color{blue}{x - hi}}{lo}, x - hi\right)}{lo} \]
                11. lower--.f6418.8

                  \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, \color{blue}{x - hi}\right)}{lo} \]
              5. Applied rewrites18.8%

                \[\leadsto \color{blue}{1 - \frac{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, x - hi\right)}{lo}} \]
              6. 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)} \]
              7. Step-by-step derivation
                1. +-commutativeN/A

                  \[\leadsto \color{blue}{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) + -1 \cdot \frac{x - lo}{lo}} \]
                2. mul-1-negN/A

                  \[\leadsto 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) + \color{blue}{\left(\mathsf{neg}\left(\frac{x - lo}{lo}\right)\right)} \]
                3. div-subN/A

                  \[\leadsto 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) + \left(\mathsf{neg}\left(\color{blue}{\left(\frac{x}{lo} - \frac{lo}{lo}\right)}\right)\right) \]
                4. sub-negN/A

                  \[\leadsto 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) + \left(\mathsf{neg}\left(\color{blue}{\left(\frac{x}{lo} + \left(\mathsf{neg}\left(\frac{lo}{lo}\right)\right)\right)}\right)\right) \]
                5. *-inversesN/A

                  \[\leadsto 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) + \left(\mathsf{neg}\left(\left(\frac{x}{lo} + \left(\mathsf{neg}\left(\color{blue}{1}\right)\right)\right)\right)\right) \]
                6. metadata-evalN/A

                  \[\leadsto 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) + \left(\mathsf{neg}\left(\left(\frac{x}{lo} + \color{blue}{-1}\right)\right)\right) \]
                7. distribute-neg-inN/A

                  \[\leadsto 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) + \color{blue}{\left(\left(\mathsf{neg}\left(\frac{x}{lo}\right)\right) + \left(\mathsf{neg}\left(-1\right)\right)\right)} \]
                8. mul-1-negN/A

                  \[\leadsto 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) + \left(\color{blue}{-1 \cdot \frac{x}{lo}} + \left(\mathsf{neg}\left(-1\right)\right)\right) \]
                9. metadata-evalN/A

                  \[\leadsto 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) + \left(-1 \cdot \frac{x}{lo} + \color{blue}{1}\right) \]
                10. +-commutativeN/A

                  \[\leadsto 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) + \color{blue}{\left(1 + -1 \cdot \frac{x}{lo}\right)} \]
                11. lower-fma.f64N/A

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

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

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

                Alternative 5: 18.9% accurate, 0.6× speedup?

                \[\begin{array}{l} \\ 1 + \frac{\mathsf{fma}\left(hi, \frac{hi}{lo}, hi\right)}{lo} \end{array} \]
                (FPCore (lo hi x) :precision binary64 (+ 1.0 (/ (fma hi (/ hi lo) hi) lo)))
                double code(double lo, double hi, double x) {
                	return 1.0 + (fma(hi, (hi / lo), hi) / lo);
                }
                
                function code(lo, hi, x)
                	return Float64(1.0 + Float64(fma(hi, Float64(hi / lo), hi) / lo))
                end
                
                code[lo_, hi_, x_] := N[(1.0 + N[(N[(hi * N[(hi / lo), $MachinePrecision] + hi), $MachinePrecision] / lo), $MachinePrecision]), $MachinePrecision]
                
                \begin{array}{l}
                
                \\
                1 + \frac{\mathsf{fma}\left(hi, \frac{hi}{lo}, hi\right)}{lo}
                \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 + -1 \cdot \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
                4. Step-by-step derivation
                  1. mul-1-negN/A

                    \[\leadsto 1 + \color{blue}{\left(\mathsf{neg}\left(\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}\right)\right)} \]
                  2. unsub-negN/A

                    \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
                  3. lower--.f64N/A

                    \[\leadsto \color{blue}{1 - \frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
                  4. lower-/.f64N/A

                    \[\leadsto 1 - \color{blue}{\frac{\left(x + \frac{hi \cdot \left(x - hi\right)}{lo}\right) - hi}{lo}} \]
                  5. +-commutativeN/A

                    \[\leadsto 1 - \frac{\color{blue}{\left(\frac{hi \cdot \left(x - hi\right)}{lo} + x\right)} - hi}{lo} \]
                  6. associate--l+N/A

                    \[\leadsto 1 - \frac{\color{blue}{\frac{hi \cdot \left(x - hi\right)}{lo} + \left(x - hi\right)}}{lo} \]
                  7. associate-/l*N/A

                    \[\leadsto 1 - \frac{\color{blue}{hi \cdot \frac{x - hi}{lo}} + \left(x - hi\right)}{lo} \]
                  8. lower-fma.f64N/A

                    \[\leadsto 1 - \frac{\color{blue}{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, x - hi\right)}}{lo} \]
                  9. lower-/.f64N/A

                    \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \color{blue}{\frac{x - hi}{lo}}, x - hi\right)}{lo} \]
                  10. lower--.f64N/A

                    \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \frac{\color{blue}{x - hi}}{lo}, x - hi\right)}{lo} \]
                  11. lower--.f6418.8

                    \[\leadsto 1 - \frac{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, \color{blue}{x - hi}\right)}{lo} \]
                5. Applied rewrites18.8%

                  \[\leadsto \color{blue}{1 - \frac{\mathsf{fma}\left(hi, \frac{x - hi}{lo}, x - hi\right)}{lo}} \]
                6. 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)} \]
                7. Step-by-step derivation
                  1. +-commutativeN/A

                    \[\leadsto \color{blue}{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) + -1 \cdot \frac{x - lo}{lo}} \]
                  2. mul-1-negN/A

                    \[\leadsto 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) + \color{blue}{\left(\mathsf{neg}\left(\frac{x - lo}{lo}\right)\right)} \]
                  3. div-subN/A

                    \[\leadsto 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) + \left(\mathsf{neg}\left(\color{blue}{\left(\frac{x}{lo} - \frac{lo}{lo}\right)}\right)\right) \]
                  4. sub-negN/A

                    \[\leadsto 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) + \left(\mathsf{neg}\left(\color{blue}{\left(\frac{x}{lo} + \left(\mathsf{neg}\left(\frac{lo}{lo}\right)\right)\right)}\right)\right) \]
                  5. *-inversesN/A

                    \[\leadsto 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) + \left(\mathsf{neg}\left(\left(\frac{x}{lo} + \left(\mathsf{neg}\left(\color{blue}{1}\right)\right)\right)\right)\right) \]
                  6. metadata-evalN/A

                    \[\leadsto 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) + \left(\mathsf{neg}\left(\left(\frac{x}{lo} + \color{blue}{-1}\right)\right)\right) \]
                  7. distribute-neg-inN/A

                    \[\leadsto 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) + \color{blue}{\left(\left(\mathsf{neg}\left(\frac{x}{lo}\right)\right) + \left(\mathsf{neg}\left(-1\right)\right)\right)} \]
                  8. mul-1-negN/A

                    \[\leadsto 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) + \left(\color{blue}{-1 \cdot \frac{x}{lo}} + \left(\mathsf{neg}\left(-1\right)\right)\right) \]
                  9. metadata-evalN/A

                    \[\leadsto 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) + \left(-1 \cdot \frac{x}{lo} + \color{blue}{1}\right) \]
                  10. +-commutativeN/A

                    \[\leadsto 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) + \color{blue}{\left(1 + -1 \cdot \frac{x}{lo}\right)} \]
                  11. lower-fma.f64N/A

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

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

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

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

                      \[\leadsto 1 + \color{blue}{\frac{\mathsf{fma}\left(hi, \frac{hi}{lo}, hi\right)}{lo}} \]
                    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;
                    }
                    
                    real(8) function code(lo, hi, x)
                        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 hi around inf

                      \[\leadsto \color{blue}{\frac{x - lo}{hi}} \]
                    4. Step-by-step derivation
                      1. lower-/.f64N/A

                        \[\leadsto \color{blue}{\frac{x - lo}{hi}} \]
                      2. lower--.f6418.8

                        \[\leadsto \frac{\color{blue}{x - lo}}{hi} \]
                    5. Applied rewrites18.8%

                      \[\leadsto \color{blue}{\frac{x - lo}{hi}} \]
                    6. 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;
                    }
                    
                    real(8) function code(lo, hi, x)
                        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 hi around inf

                      \[\leadsto \color{blue}{\frac{x - lo}{hi}} \]
                    4. Step-by-step derivation
                      1. lower-/.f64N/A

                        \[\leadsto \color{blue}{\frac{x - lo}{hi}} \]
                      2. lower--.f6418.8

                        \[\leadsto \frac{\color{blue}{x - lo}}{hi} \]
                    5. Applied rewrites18.8%

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

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

                        \[\leadsto \frac{-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;
                      }
                      
                      real(8) function code(lo, hi, x)
                          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 2024232 
                        (FPCore (lo hi x)
                          :name "xlohi (overflows)"
                          :precision binary64
                          :pre (and (< lo -1e+308) (> hi 1e+308))
                          (/ (- x lo) (- hi lo)))