bug366 (missed optimization)

Percentage Accurate: 44.9% → 99.0%
Time: 3.1s
Alternatives: 9
Speedup: 0.7×

Specification

?
\[\begin{array}{l} \\ \sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \end{array} \]
(FPCore (x y z) :precision binary64 (sqrt (+ (* x x) (+ (* y y) (* z z)))))
double code(double x, double y, double z) {
	return sqrt(((x * x) + ((y * y) + (z * z))));
}
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(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = sqrt(((x * x) + ((y * y) + (z * z))))
end function
public static double code(double x, double y, double z) {
	return Math.sqrt(((x * x) + ((y * y) + (z * z))));
}
def code(x, y, z):
	return math.sqrt(((x * x) + ((y * y) + (z * z))))
function code(x, y, z)
	return sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z))))
end
function tmp = code(x, y, z)
	tmp = sqrt(((x * x) + ((y * y) + (z * z))));
end
code[x_, y_, z_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\end{array}

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 9 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: 44.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \end{array} \]
(FPCore (x y z) :precision binary64 (sqrt (+ (* x x) (+ (* y y) (* z z)))))
double code(double x, double y, double z) {
	return sqrt(((x * x) + ((y * y) + (z * z))));
}
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(x, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = sqrt(((x * x) + ((y * y) + (z * z))))
end function
public static double code(double x, double y, double z) {
	return Math.sqrt(((x * x) + ((y * y) + (z * z))));
}
def code(x, y, z):
	return math.sqrt(((x * x) + ((y * y) + (z * z))))
function code(x, y, z)
	return sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z))))
end
function tmp = code(x, y, z)
	tmp = sqrt(((x * x) + ((y * y) + (z * z))));
end
code[x_, y_, z_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\end{array}

Alternative 1: 99.0% accurate, 0.5× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ y_m = \left|y\right| \\ z_m = \left|z\right| \\ [x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\ \\ \begin{array}{l} \mathbf{if}\;y\_m \leq 1.05 \cdot 10^{+120}:\\ \;\;\;\;z\_m \cdot \mathsf{fma}\left(\frac{0.5}{z\_m}, \frac{\mathsf{fma}\left(y\_m, y\_m, x\_m \cdot x\_m\right)}{z\_m}, 1\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\frac{z\_m}{y\_m}, \frac{z\_m}{y\_m}, \mathsf{fma}\left(x\_m, \frac{x\_m}{y\_m \cdot y\_m}, 1\right)\right)} \cdot \left|y\_m\right|\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
y_m = (fabs.f64 y)
z_m = (fabs.f64 z)
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
(FPCore (x_m y_m z_m)
 :precision binary64
 (if (<= y_m 1.05e+120)
   (* z_m (fma (/ 0.5 z_m) (/ (fma y_m y_m (* x_m x_m)) z_m) 1.0))
   (*
    (sqrt (fma (/ z_m y_m) (/ z_m y_m) (fma x_m (/ x_m (* y_m y_m)) 1.0)))
    (fabs y_m))))
x_m = fabs(x);
y_m = fabs(y);
z_m = fabs(z);
assert(x_m < y_m && y_m < z_m);
double code(double x_m, double y_m, double z_m) {
	double tmp;
	if (y_m <= 1.05e+120) {
		tmp = z_m * fma((0.5 / z_m), (fma(y_m, y_m, (x_m * x_m)) / z_m), 1.0);
	} else {
		tmp = sqrt(fma((z_m / y_m), (z_m / y_m), fma(x_m, (x_m / (y_m * y_m)), 1.0))) * fabs(y_m);
	}
	return tmp;
}
x_m = abs(x)
y_m = abs(y)
z_m = abs(z)
x_m, y_m, z_m = sort([x_m, y_m, z_m])
function code(x_m, y_m, z_m)
	tmp = 0.0
	if (y_m <= 1.05e+120)
		tmp = Float64(z_m * fma(Float64(0.5 / z_m), Float64(fma(y_m, y_m, Float64(x_m * x_m)) / z_m), 1.0));
	else
		tmp = Float64(sqrt(fma(Float64(z_m / y_m), Float64(z_m / y_m), fma(x_m, Float64(x_m / Float64(y_m * y_m)), 1.0))) * abs(y_m));
	end
	return tmp
end
x_m = N[Abs[x], $MachinePrecision]
y_m = N[Abs[y], $MachinePrecision]
z_m = N[Abs[z], $MachinePrecision]
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
code[x$95$m_, y$95$m_, z$95$m_] := If[LessEqual[y$95$m, 1.05e+120], N[(z$95$m * N[(N[(0.5 / z$95$m), $MachinePrecision] * N[(N[(y$95$m * y$95$m + N[(x$95$m * x$95$m), $MachinePrecision]), $MachinePrecision] / z$95$m), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[(N[Sqrt[N[(N[(z$95$m / y$95$m), $MachinePrecision] * N[(z$95$m / y$95$m), $MachinePrecision] + N[(x$95$m * N[(x$95$m / N[(y$95$m * y$95$m), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Abs[y$95$m], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
y_m = \left|y\right|
\\
z_m = \left|z\right|
\\
[x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\
\\
\begin{array}{l}
\mathbf{if}\;y\_m \leq 1.05 \cdot 10^{+120}:\\
\;\;\;\;z\_m \cdot \mathsf{fma}\left(\frac{0.5}{z\_m}, \frac{\mathsf{fma}\left(y\_m, y\_m, x\_m \cdot x\_m\right)}{z\_m}, 1\right)\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\frac{z\_m}{y\_m}, \frac{z\_m}{y\_m}, \mathsf{fma}\left(x\_m, \frac{x\_m}{y\_m \cdot y\_m}, 1\right)\right)} \cdot \left|y\_m\right|\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 1.05e120

    1. Initial program 44.9%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Taylor expanded in z around inf

      \[\leadsto \color{blue}{z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right)} \]
    3. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto z \cdot \color{blue}{\left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right)} \]
      2. lower-+.f64N/A

        \[\leadsto z \cdot \left(1 + \color{blue}{\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}}\right) \]
      3. lower-*.f64N/A

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

        \[\leadsto z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{\color{blue}{{z}^{2}}}\right) \]
      5. lower-+.f64N/A

        \[\leadsto z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{\color{blue}{z}}^{2}}\right) \]
      6. lower-pow.f64N/A

        \[\leadsto z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right) \]
      7. lower-pow.f64N/A

        \[\leadsto z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right) \]
      8. lower-pow.f6482.9

        \[\leadsto z \cdot \left(1 + 0.5 \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{\color{blue}{2}}}\right) \]
    4. Applied rewrites82.9%

      \[\leadsto \color{blue}{z \cdot \left(1 + 0.5 \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right)} \]
    5. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto z \cdot \left(1 + \color{blue}{\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}}\right) \]
      2. +-commutativeN/A

        \[\leadsto z \cdot \left(\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}} + \color{blue}{1}\right) \]
      3. lift-*.f64N/A

        \[\leadsto z \cdot \left(\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}} + 1\right) \]
      4. lift-pow.f64N/A

        \[\leadsto z \cdot \left(\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}} + 1\right) \]
      5. pow2N/A

        \[\leadsto z \cdot \left(\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{z \cdot z} + 1\right) \]
      6. lower-/.f64N/A

        \[\leadsto z \cdot \left(\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{z \cdot z} + 1\right) \]
      7. associate-*r/N/A

        \[\leadsto z \cdot \left(\frac{\frac{1}{2} \cdot \left({x}^{2} + {y}^{2}\right)}{z \cdot z} + 1\right) \]
      8. times-fracN/A

        \[\leadsto z \cdot \left(\frac{\frac{1}{2}}{z} \cdot \frac{{x}^{2} + {y}^{2}}{z} + 1\right) \]
      9. lower-fma.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \color{blue}{\frac{{x}^{2} + {y}^{2}}{z}}, 1\right) \]
      10. lower-/.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{\color{blue}{{x}^{2} + {y}^{2}}}{z}, 1\right) \]
      11. lower-/.f6485.3

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{0.5}{z}, \frac{{x}^{2} + {y}^{2}}{\color{blue}{z}}, 1\right) \]
      12. lift-+.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{{x}^{2} + {y}^{2}}{z}, 1\right) \]
      13. lift-pow.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{{x}^{2} + {y}^{2}}{z}, 1\right) \]
      14. pow2N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{x \cdot x + {y}^{2}}{z}, 1\right) \]
      15. lift-*.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{x \cdot x + {y}^{2}}{z}, 1\right) \]
      16. lift-pow.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{x \cdot x + {y}^{2}}{z}, 1\right) \]
      17. pow2N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{x \cdot x + y \cdot y}{z}, 1\right) \]
      18. lift-*.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{x \cdot x + y \cdot y}{z}, 1\right) \]
      19. +-commutativeN/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{y \cdot y + x \cdot x}{z}, 1\right) \]
      20. lift-*.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{y \cdot y + x \cdot x}{z}, 1\right) \]
      21. lower-fma.f6485.3

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{0.5}{z}, \frac{\mathsf{fma}\left(y, y, x \cdot x\right)}{z}, 1\right) \]
    6. Applied rewrites85.3%

      \[\leadsto z \cdot \mathsf{fma}\left(\frac{0.5}{z}, \color{blue}{\frac{\mathsf{fma}\left(y, y, x \cdot x\right)}{z}}, 1\right) \]

    if 1.05e120 < y

    1. Initial program 44.9%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \sqrt{\color{blue}{x \cdot x + \left(y \cdot y + z \cdot z\right)}} \]
      2. +-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{\left(y \cdot y + z \cdot z\right) + x \cdot x}} \]
      3. lift-+.f64N/A

        \[\leadsto \sqrt{\color{blue}{\left(y \cdot y + z \cdot z\right)} + x \cdot x} \]
      4. associate-+l+N/A

        \[\leadsto \sqrt{\color{blue}{y \cdot y + \left(z \cdot z + x \cdot x\right)}} \]
      5. lift-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{y \cdot y} + \left(z \cdot z + x \cdot x\right)} \]
      6. lower-fma.f64N/A

        \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(y, y, z \cdot z + x \cdot x\right)}} \]
      7. lift-*.f64N/A

        \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \color{blue}{z \cdot z} + x \cdot x\right)} \]
      8. lower-fma.f6444.9

        \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \color{blue}{\mathsf{fma}\left(z, z, x \cdot x\right)}\right)} \]
    3. Applied rewrites44.9%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(y, y, \mathsf{fma}\left(z, z, x \cdot x\right)\right)}} \]
    4. Step-by-step derivation
      1. lift-sqrt.f64N/A

        \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(z, z, x \cdot x\right)\right)}} \]
      2. lift-fma.f64N/A

        \[\leadsto \sqrt{\color{blue}{y \cdot y + \mathsf{fma}\left(z, z, x \cdot x\right)}} \]
      3. sum-to-multN/A

        \[\leadsto \sqrt{\color{blue}{\left(1 + \frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{y \cdot y}\right) \cdot \left(y \cdot y\right)}} \]
      4. sqrt-prodN/A

        \[\leadsto \color{blue}{\sqrt{1 + \frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{y \cdot y}} \cdot \sqrt{y \cdot y}} \]
      5. rem-sqrt-square-revN/A

        \[\leadsto \sqrt{1 + \frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{y \cdot y}} \cdot \color{blue}{\left|y\right|} \]
      6. lower-*.f64N/A

        \[\leadsto \color{blue}{\sqrt{1 + \frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{y \cdot y}} \cdot \left|y\right|} \]
      7. lower-sqrt.f64N/A

        \[\leadsto \color{blue}{\sqrt{1 + \frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{y \cdot y}}} \cdot \left|y\right| \]
      8. +-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{\frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{y \cdot y} + 1}} \cdot \left|y\right| \]
      9. lower-+.f64N/A

        \[\leadsto \sqrt{\color{blue}{\frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{y \cdot y} + 1}} \cdot \left|y\right| \]
      10. lower-/.f64N/A

        \[\leadsto \sqrt{\color{blue}{\frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{y \cdot y}} + 1} \cdot \left|y\right| \]
      11. lower-*.f64N/A

        \[\leadsto \sqrt{\frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{\color{blue}{y \cdot y}} + 1} \cdot \left|y\right| \]
      12. lower-fabs.f6429.1

        \[\leadsto \sqrt{\frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{y \cdot y} + 1} \cdot \color{blue}{\left|y\right|} \]
    5. Applied rewrites29.1%

      \[\leadsto \color{blue}{\sqrt{\frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{y \cdot y} + 1} \cdot \left|y\right|} \]
    6. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \sqrt{\color{blue}{\frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{y \cdot y} + 1}} \cdot \left|y\right| \]
      2. lift-*.f64N/A

        \[\leadsto \sqrt{\frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{\color{blue}{y \cdot y}} + 1} \cdot \left|y\right| \]
      3. lift-/.f64N/A

        \[\leadsto \sqrt{\color{blue}{\frac{\mathsf{fma}\left(z, z, x \cdot x\right)}{y \cdot y}} + 1} \cdot \left|y\right| \]
      4. lift-*.f64N/A

        \[\leadsto \sqrt{\frac{\mathsf{fma}\left(z, z, \color{blue}{x \cdot x}\right)}{y \cdot y} + 1} \cdot \left|y\right| \]
      5. lift-fma.f64N/A

        \[\leadsto \sqrt{\frac{\color{blue}{z \cdot z + x \cdot x}}{y \cdot y} + 1} \cdot \left|y\right| \]
      6. lift-*.f64N/A

        \[\leadsto \sqrt{\frac{\color{blue}{z \cdot z} + x \cdot x}{y \cdot y} + 1} \cdot \left|y\right| \]
      7. div-addN/A

        \[\leadsto \sqrt{\color{blue}{\left(\frac{z \cdot z}{y \cdot y} + \frac{x \cdot x}{y \cdot y}\right)} + 1} \cdot \left|y\right| \]
      8. associate-+l+N/A

        \[\leadsto \sqrt{\color{blue}{\frac{z \cdot z}{y \cdot y} + \left(\frac{x \cdot x}{y \cdot y} + 1\right)}} \cdot \left|y\right| \]
      9. lift-*.f64N/A

        \[\leadsto \sqrt{\frac{\color{blue}{z \cdot z}}{y \cdot y} + \left(\frac{x \cdot x}{y \cdot y} + 1\right)} \cdot \left|y\right| \]
      10. times-fracN/A

        \[\leadsto \sqrt{\color{blue}{\frac{z}{y} \cdot \frac{z}{y}} + \left(\frac{x \cdot x}{y \cdot y} + 1\right)} \cdot \left|y\right| \]
      11. lower-fma.f64N/A

        \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\frac{z}{y}, \frac{z}{y}, \frac{x \cdot x}{y \cdot y} + 1\right)}} \cdot \left|y\right| \]
      12. lower-/.f64N/A

        \[\leadsto \sqrt{\mathsf{fma}\left(\color{blue}{\frac{z}{y}}, \frac{z}{y}, \frac{x \cdot x}{y \cdot y} + 1\right)} \cdot \left|y\right| \]
      13. lower-/.f64N/A

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{z}{y}, \color{blue}{\frac{z}{y}}, \frac{x \cdot x}{y \cdot y} + 1\right)} \cdot \left|y\right| \]
      14. associate-/l*N/A

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{z}{y}, \frac{z}{y}, \color{blue}{x \cdot \frac{x}{y \cdot y}} + 1\right)} \cdot \left|y\right| \]
      15. lower-fma.f64N/A

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{z}{y}, \frac{z}{y}, \color{blue}{\mathsf{fma}\left(x, \frac{x}{y \cdot y}, 1\right)}\right)} \cdot \left|y\right| \]
      16. lower-/.f64N/A

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{z}{y}, \frac{z}{y}, \mathsf{fma}\left(x, \color{blue}{\frac{x}{y \cdot y}}, 1\right)\right)} \cdot \left|y\right| \]
      17. lift-*.f6457.0

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{z}{y}, \frac{z}{y}, \mathsf{fma}\left(x, \frac{x}{\color{blue}{y \cdot y}}, 1\right)\right)} \cdot \left|y\right| \]
    7. Applied rewrites57.0%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\frac{z}{y}, \frac{z}{y}, \mathsf{fma}\left(x, \frac{x}{y \cdot y}, 1\right)\right)}} \cdot \left|y\right| \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 97.2% accurate, 0.6× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ y_m = \left|y\right| \\ z_m = \left|z\right| \\ [x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\ \\ \begin{array}{l} \mathbf{if}\;y\_m \leq 1.32 \cdot 10^{+154}:\\ \;\;\;\;z\_m \cdot \mathsf{fma}\left(\frac{0.5}{z\_m}, \frac{\mathsf{fma}\left(y\_m, y\_m, x\_m \cdot x\_m\right)}{z\_m}, 1\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{e^{-\log z\_m}}\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
y_m = (fabs.f64 y)
z_m = (fabs.f64 z)
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
(FPCore (x_m y_m z_m)
 :precision binary64
 (if (<= y_m 1.32e+154)
   (* z_m (fma (/ 0.5 z_m) (/ (fma y_m y_m (* x_m x_m)) z_m) 1.0))
   (/ 1.0 (exp (- (log z_m))))))
x_m = fabs(x);
y_m = fabs(y);
z_m = fabs(z);
assert(x_m < y_m && y_m < z_m);
double code(double x_m, double y_m, double z_m) {
	double tmp;
	if (y_m <= 1.32e+154) {
		tmp = z_m * fma((0.5 / z_m), (fma(y_m, y_m, (x_m * x_m)) / z_m), 1.0);
	} else {
		tmp = 1.0 / exp(-log(z_m));
	}
	return tmp;
}
x_m = abs(x)
y_m = abs(y)
z_m = abs(z)
x_m, y_m, z_m = sort([x_m, y_m, z_m])
function code(x_m, y_m, z_m)
	tmp = 0.0
	if (y_m <= 1.32e+154)
		tmp = Float64(z_m * fma(Float64(0.5 / z_m), Float64(fma(y_m, y_m, Float64(x_m * x_m)) / z_m), 1.0));
	else
		tmp = Float64(1.0 / exp(Float64(-log(z_m))));
	end
	return tmp
end
x_m = N[Abs[x], $MachinePrecision]
y_m = N[Abs[y], $MachinePrecision]
z_m = N[Abs[z], $MachinePrecision]
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
code[x$95$m_, y$95$m_, z$95$m_] := If[LessEqual[y$95$m, 1.32e+154], N[(z$95$m * N[(N[(0.5 / z$95$m), $MachinePrecision] * N[(N[(y$95$m * y$95$m + N[(x$95$m * x$95$m), $MachinePrecision]), $MachinePrecision] / z$95$m), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[Exp[(-N[Log[z$95$m], $MachinePrecision])], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
y_m = \left|y\right|
\\
z_m = \left|z\right|
\\
[x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\
\\
\begin{array}{l}
\mathbf{if}\;y\_m \leq 1.32 \cdot 10^{+154}:\\
\;\;\;\;z\_m \cdot \mathsf{fma}\left(\frac{0.5}{z\_m}, \frac{\mathsf{fma}\left(y\_m, y\_m, x\_m \cdot x\_m\right)}{z\_m}, 1\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{-\log z\_m}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 1.31999999999999998e154

    1. Initial program 44.9%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Taylor expanded in z around inf

      \[\leadsto \color{blue}{z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right)} \]
    3. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto z \cdot \color{blue}{\left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right)} \]
      2. lower-+.f64N/A

        \[\leadsto z \cdot \left(1 + \color{blue}{\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}}\right) \]
      3. lower-*.f64N/A

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

        \[\leadsto z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{\color{blue}{{z}^{2}}}\right) \]
      5. lower-+.f64N/A

        \[\leadsto z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{\color{blue}{z}}^{2}}\right) \]
      6. lower-pow.f64N/A

        \[\leadsto z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right) \]
      7. lower-pow.f64N/A

        \[\leadsto z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right) \]
      8. lower-pow.f6482.9

        \[\leadsto z \cdot \left(1 + 0.5 \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{\color{blue}{2}}}\right) \]
    4. Applied rewrites82.9%

      \[\leadsto \color{blue}{z \cdot \left(1 + 0.5 \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right)} \]
    5. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto z \cdot \left(1 + \color{blue}{\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}}\right) \]
      2. +-commutativeN/A

        \[\leadsto z \cdot \left(\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}} + \color{blue}{1}\right) \]
      3. lift-*.f64N/A

        \[\leadsto z \cdot \left(\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}} + 1\right) \]
      4. lift-pow.f64N/A

        \[\leadsto z \cdot \left(\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}} + 1\right) \]
      5. pow2N/A

        \[\leadsto z \cdot \left(\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{z \cdot z} + 1\right) \]
      6. lower-/.f64N/A

        \[\leadsto z \cdot \left(\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{z \cdot z} + 1\right) \]
      7. associate-*r/N/A

        \[\leadsto z \cdot \left(\frac{\frac{1}{2} \cdot \left({x}^{2} + {y}^{2}\right)}{z \cdot z} + 1\right) \]
      8. times-fracN/A

        \[\leadsto z \cdot \left(\frac{\frac{1}{2}}{z} \cdot \frac{{x}^{2} + {y}^{2}}{z} + 1\right) \]
      9. lower-fma.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \color{blue}{\frac{{x}^{2} + {y}^{2}}{z}}, 1\right) \]
      10. lower-/.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{\color{blue}{{x}^{2} + {y}^{2}}}{z}, 1\right) \]
      11. lower-/.f6485.3

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{0.5}{z}, \frac{{x}^{2} + {y}^{2}}{\color{blue}{z}}, 1\right) \]
      12. lift-+.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{{x}^{2} + {y}^{2}}{z}, 1\right) \]
      13. lift-pow.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{{x}^{2} + {y}^{2}}{z}, 1\right) \]
      14. pow2N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{x \cdot x + {y}^{2}}{z}, 1\right) \]
      15. lift-*.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{x \cdot x + {y}^{2}}{z}, 1\right) \]
      16. lift-pow.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{x \cdot x + {y}^{2}}{z}, 1\right) \]
      17. pow2N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{x \cdot x + y \cdot y}{z}, 1\right) \]
      18. lift-*.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{x \cdot x + y \cdot y}{z}, 1\right) \]
      19. +-commutativeN/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{y \cdot y + x \cdot x}{z}, 1\right) \]
      20. lift-*.f64N/A

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{\frac{1}{2}}{z}, \frac{y \cdot y + x \cdot x}{z}, 1\right) \]
      21. lower-fma.f6485.3

        \[\leadsto z \cdot \mathsf{fma}\left(\frac{0.5}{z}, \frac{\mathsf{fma}\left(y, y, x \cdot x\right)}{z}, 1\right) \]
    6. Applied rewrites85.3%

      \[\leadsto z \cdot \mathsf{fma}\left(\frac{0.5}{z}, \color{blue}{\frac{\mathsf{fma}\left(y, y, x \cdot x\right)}{z}}, 1\right) \]

    if 1.31999999999999998e154 < y

    1. Initial program 44.9%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Step-by-step derivation
      1. lift-sqrt.f64N/A

        \[\leadsto \color{blue}{\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}} \]
      2. pow1/2N/A

        \[\leadsto \color{blue}{{\left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)}^{\frac{1}{2}}} \]
      3. pow-to-expN/A

        \[\leadsto \color{blue}{e^{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}}} \]
      4. lower-exp.f64N/A

        \[\leadsto \color{blue}{e^{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}}} \]
      5. lower-*.f64N/A

        \[\leadsto e^{\color{blue}{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}}} \]
      6. lower-log.f6441.8

        \[\leadsto e^{\color{blue}{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)} \cdot 0.5} \]
      7. lift-+.f64N/A

        \[\leadsto e^{\log \color{blue}{\left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)} \cdot \frac{1}{2}} \]
      8. lift-*.f64N/A

        \[\leadsto e^{\log \left(\color{blue}{x \cdot x} + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}} \]
      9. lower-fma.f6441.8

        \[\leadsto e^{\log \color{blue}{\left(\mathsf{fma}\left(x, x, y \cdot y + z \cdot z\right)\right)} \cdot 0.5} \]
      10. lift-+.f64N/A

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{y \cdot y + z \cdot z}\right)\right) \cdot \frac{1}{2}} \]
      11. +-commutativeN/A

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{z \cdot z + y \cdot y}\right)\right) \cdot \frac{1}{2}} \]
      12. lift-*.f64N/A

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{z \cdot z} + y \cdot y\right)\right) \cdot \frac{1}{2}} \]
      13. lower-fma.f6441.8

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{\mathsf{fma}\left(z, z, y \cdot y\right)}\right)\right) \cdot 0.5} \]
    3. Applied rewrites41.8%

      \[\leadsto \color{blue}{e^{\log \left(\mathsf{fma}\left(x, x, \mathsf{fma}\left(z, z, y \cdot y\right)\right)\right) \cdot 0.5}} \]
    4. Taylor expanded in z around inf

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(\frac{1}{z}\right)}} \]
    5. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto e^{-1 \cdot \color{blue}{\log \left(\frac{1}{z}\right)}} \]
      2. lower-log.f64N/A

        \[\leadsto e^{-1 \cdot \log \left(\frac{1}{z}\right)} \]
      3. lower-/.f6489.3

        \[\leadsto e^{-1 \cdot \log \left(\frac{1}{z}\right)} \]
    6. Applied rewrites89.3%

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(\frac{1}{z}\right)}} \]
    7. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto e^{-1 \cdot \color{blue}{\log \left(\frac{1}{z}\right)}} \]
      2. mul-1-negN/A

        \[\leadsto e^{\mathsf{neg}\left(\log \left(\frac{1}{z}\right)\right)} \]
      3. lift-log.f64N/A

        \[\leadsto e^{\mathsf{neg}\left(\log \left(\frac{1}{z}\right)\right)} \]
      4. lift-/.f64N/A

        \[\leadsto e^{\mathsf{neg}\left(\log \left(\frac{1}{z}\right)\right)} \]
      5. log-recN/A

        \[\leadsto e^{\mathsf{neg}\left(\left(\mathsf{neg}\left(\log z\right)\right)\right)} \]
      6. remove-double-negN/A

        \[\leadsto e^{\log z} \]
      7. lower-log.f6489.3

        \[\leadsto e^{\log z} \]
    8. Applied rewrites89.3%

      \[\leadsto e^{\log z} \]
    9. Step-by-step derivation
      1. lift-exp.f64N/A

        \[\leadsto \color{blue}{e^{\log z}} \]
      2. sinh-+-cosh-revN/A

        \[\leadsto \color{blue}{\cosh \log z + \sinh \log z} \]
      3. flip-+N/A

        \[\leadsto \color{blue}{\frac{\cosh \log z \cdot \cosh \log z - \sinh \log z \cdot \sinh \log z}{\cosh \log z - \sinh \log z}} \]
      4. sinh-coshN/A

        \[\leadsto \frac{\color{blue}{1}}{\cosh \log z - \sinh \log z} \]
      5. sinh---cosh-revN/A

        \[\leadsto \frac{1}{\color{blue}{e^{\mathsf{neg}\left(\log z\right)}}} \]
    10. Applied rewrites89.3%

      \[\leadsto \color{blue}{\frac{1}{e^{-\log z}}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 96.2% accurate, 0.6× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ y_m = \left|y\right| \\ z_m = \left|z\right| \\ [x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\ \\ \begin{array}{l} t_0 := \frac{1}{e^{-\log z\_m}}\\ \mathbf{if}\;y\_m \leq 1.4 \cdot 10^{-256}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y\_m \leq 1.32 \cdot 10^{+154}:\\ \;\;\;\;\mathsf{fma}\left(\frac{\mathsf{fma}\left(y\_m, y\_m, x\_m \cdot x\_m\right)}{z\_m \cdot z\_m} \cdot 0.5, z\_m, z\_m\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
y_m = (fabs.f64 y)
z_m = (fabs.f64 z)
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
(FPCore (x_m y_m z_m)
 :precision binary64
 (let* ((t_0 (/ 1.0 (exp (- (log z_m))))))
   (if (<= y_m 1.4e-256)
     t_0
     (if (<= y_m 1.32e+154)
       (fma (* (/ (fma y_m y_m (* x_m x_m)) (* z_m z_m)) 0.5) z_m z_m)
       t_0))))
x_m = fabs(x);
y_m = fabs(y);
z_m = fabs(z);
assert(x_m < y_m && y_m < z_m);
double code(double x_m, double y_m, double z_m) {
	double t_0 = 1.0 / exp(-log(z_m));
	double tmp;
	if (y_m <= 1.4e-256) {
		tmp = t_0;
	} else if (y_m <= 1.32e+154) {
		tmp = fma(((fma(y_m, y_m, (x_m * x_m)) / (z_m * z_m)) * 0.5), z_m, z_m);
	} else {
		tmp = t_0;
	}
	return tmp;
}
x_m = abs(x)
y_m = abs(y)
z_m = abs(z)
x_m, y_m, z_m = sort([x_m, y_m, z_m])
function code(x_m, y_m, z_m)
	t_0 = Float64(1.0 / exp(Float64(-log(z_m))))
	tmp = 0.0
	if (y_m <= 1.4e-256)
		tmp = t_0;
	elseif (y_m <= 1.32e+154)
		tmp = fma(Float64(Float64(fma(y_m, y_m, Float64(x_m * x_m)) / Float64(z_m * z_m)) * 0.5), z_m, z_m);
	else
		tmp = t_0;
	end
	return tmp
end
x_m = N[Abs[x], $MachinePrecision]
y_m = N[Abs[y], $MachinePrecision]
z_m = N[Abs[z], $MachinePrecision]
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
code[x$95$m_, y$95$m_, z$95$m_] := Block[{t$95$0 = N[(1.0 / N[Exp[(-N[Log[z$95$m], $MachinePrecision])], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y$95$m, 1.4e-256], t$95$0, If[LessEqual[y$95$m, 1.32e+154], N[(N[(N[(N[(y$95$m * y$95$m + N[(x$95$m * x$95$m), $MachinePrecision]), $MachinePrecision] / N[(z$95$m * z$95$m), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision] * z$95$m + z$95$m), $MachinePrecision], t$95$0]]]
\begin{array}{l}
x_m = \left|x\right|
\\
y_m = \left|y\right|
\\
z_m = \left|z\right|
\\
[x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\
\\
\begin{array}{l}
t_0 := \frac{1}{e^{-\log z\_m}}\\
\mathbf{if}\;y\_m \leq 1.4 \cdot 10^{-256}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y\_m \leq 1.32 \cdot 10^{+154}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\mathsf{fma}\left(y\_m, y\_m, x\_m \cdot x\_m\right)}{z\_m \cdot z\_m} \cdot 0.5, z\_m, z\_m\right)\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 1.40000000000000012e-256 or 1.31999999999999998e154 < y

    1. Initial program 44.9%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Step-by-step derivation
      1. lift-sqrt.f64N/A

        \[\leadsto \color{blue}{\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}} \]
      2. pow1/2N/A

        \[\leadsto \color{blue}{{\left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)}^{\frac{1}{2}}} \]
      3. pow-to-expN/A

        \[\leadsto \color{blue}{e^{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}}} \]
      4. lower-exp.f64N/A

        \[\leadsto \color{blue}{e^{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}}} \]
      5. lower-*.f64N/A

        \[\leadsto e^{\color{blue}{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}}} \]
      6. lower-log.f6441.8

        \[\leadsto e^{\color{blue}{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)} \cdot 0.5} \]
      7. lift-+.f64N/A

        \[\leadsto e^{\log \color{blue}{\left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)} \cdot \frac{1}{2}} \]
      8. lift-*.f64N/A

        \[\leadsto e^{\log \left(\color{blue}{x \cdot x} + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}} \]
      9. lower-fma.f6441.8

        \[\leadsto e^{\log \color{blue}{\left(\mathsf{fma}\left(x, x, y \cdot y + z \cdot z\right)\right)} \cdot 0.5} \]
      10. lift-+.f64N/A

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{y \cdot y + z \cdot z}\right)\right) \cdot \frac{1}{2}} \]
      11. +-commutativeN/A

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{z \cdot z + y \cdot y}\right)\right) \cdot \frac{1}{2}} \]
      12. lift-*.f64N/A

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{z \cdot z} + y \cdot y\right)\right) \cdot \frac{1}{2}} \]
      13. lower-fma.f6441.8

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{\mathsf{fma}\left(z, z, y \cdot y\right)}\right)\right) \cdot 0.5} \]
    3. Applied rewrites41.8%

      \[\leadsto \color{blue}{e^{\log \left(\mathsf{fma}\left(x, x, \mathsf{fma}\left(z, z, y \cdot y\right)\right)\right) \cdot 0.5}} \]
    4. Taylor expanded in z around inf

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(\frac{1}{z}\right)}} \]
    5. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto e^{-1 \cdot \color{blue}{\log \left(\frac{1}{z}\right)}} \]
      2. lower-log.f64N/A

        \[\leadsto e^{-1 \cdot \log \left(\frac{1}{z}\right)} \]
      3. lower-/.f6489.3

        \[\leadsto e^{-1 \cdot \log \left(\frac{1}{z}\right)} \]
    6. Applied rewrites89.3%

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(\frac{1}{z}\right)}} \]
    7. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto e^{-1 \cdot \color{blue}{\log \left(\frac{1}{z}\right)}} \]
      2. mul-1-negN/A

        \[\leadsto e^{\mathsf{neg}\left(\log \left(\frac{1}{z}\right)\right)} \]
      3. lift-log.f64N/A

        \[\leadsto e^{\mathsf{neg}\left(\log \left(\frac{1}{z}\right)\right)} \]
      4. lift-/.f64N/A

        \[\leadsto e^{\mathsf{neg}\left(\log \left(\frac{1}{z}\right)\right)} \]
      5. log-recN/A

        \[\leadsto e^{\mathsf{neg}\left(\left(\mathsf{neg}\left(\log z\right)\right)\right)} \]
      6. remove-double-negN/A

        \[\leadsto e^{\log z} \]
      7. lower-log.f6489.3

        \[\leadsto e^{\log z} \]
    8. Applied rewrites89.3%

      \[\leadsto e^{\log z} \]
    9. Step-by-step derivation
      1. lift-exp.f64N/A

        \[\leadsto \color{blue}{e^{\log z}} \]
      2. sinh-+-cosh-revN/A

        \[\leadsto \color{blue}{\cosh \log z + \sinh \log z} \]
      3. flip-+N/A

        \[\leadsto \color{blue}{\frac{\cosh \log z \cdot \cosh \log z - \sinh \log z \cdot \sinh \log z}{\cosh \log z - \sinh \log z}} \]
      4. sinh-coshN/A

        \[\leadsto \frac{\color{blue}{1}}{\cosh \log z - \sinh \log z} \]
      5. sinh---cosh-revN/A

        \[\leadsto \frac{1}{\color{blue}{e^{\mathsf{neg}\left(\log z\right)}}} \]
    10. Applied rewrites89.3%

      \[\leadsto \color{blue}{\frac{1}{e^{-\log z}}} \]

    if 1.40000000000000012e-256 < y < 1.31999999999999998e154

    1. Initial program 44.9%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Taylor expanded in z around inf

      \[\leadsto \color{blue}{z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right)} \]
    3. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto z \cdot \color{blue}{\left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right)} \]
      2. lower-+.f64N/A

        \[\leadsto z \cdot \left(1 + \color{blue}{\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}}\right) \]
      3. lower-*.f64N/A

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

        \[\leadsto z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{\color{blue}{{z}^{2}}}\right) \]
      5. lower-+.f64N/A

        \[\leadsto z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{\color{blue}{z}}^{2}}\right) \]
      6. lower-pow.f64N/A

        \[\leadsto z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right) \]
      7. lower-pow.f64N/A

        \[\leadsto z \cdot \left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right) \]
      8. lower-pow.f6482.9

        \[\leadsto z \cdot \left(1 + 0.5 \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{\color{blue}{2}}}\right) \]
    4. Applied rewrites82.9%

      \[\leadsto \color{blue}{z \cdot \left(1 + 0.5 \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right)} \]
    5. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto z \cdot \color{blue}{\left(1 + \frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right)} \]
      2. lift-+.f64N/A

        \[\leadsto z \cdot \left(1 + \color{blue}{\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}}\right) \]
      3. +-commutativeN/A

        \[\leadsto z \cdot \left(\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}} + \color{blue}{1}\right) \]
      4. distribute-rgt-inN/A

        \[\leadsto \left(\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right) \cdot z + \color{blue}{1 \cdot z} \]
      5. *-lft-identityN/A

        \[\leadsto \left(\frac{1}{2} \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}\right) \cdot z + z \]
      6. lower-fma.f6482.9

        \[\leadsto \mathsf{fma}\left(0.5 \cdot \frac{{x}^{2} + {y}^{2}}{{z}^{2}}, \color{blue}{z}, z\right) \]
    6. Applied rewrites82.9%

      \[\leadsto \mathsf{fma}\left(\frac{\mathsf{fma}\left(y, y, x \cdot x\right)}{z \cdot z} \cdot 0.5, \color{blue}{z}, z\right) \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 93.0% accurate, 0.6× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ y_m = \left|y\right| \\ z_m = \left|z\right| \\ [x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\ \\ \begin{array}{l} t_0 := \frac{1}{e^{-\log z\_m}}\\ \mathbf{if}\;z\_m \leq 8.5 \cdot 10^{-157}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z\_m \leq 1.12 \cdot 10^{+154}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y\_m, y\_m, \mathsf{fma}\left(z\_m, z\_m, x\_m \cdot x\_m\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
y_m = (fabs.f64 y)
z_m = (fabs.f64 z)
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
(FPCore (x_m y_m z_m)
 :precision binary64
 (let* ((t_0 (/ 1.0 (exp (- (log z_m))))))
   (if (<= z_m 8.5e-157)
     t_0
     (if (<= z_m 1.12e+154)
       (sqrt (fma y_m y_m (fma z_m z_m (* x_m x_m))))
       t_0))))
x_m = fabs(x);
y_m = fabs(y);
z_m = fabs(z);
assert(x_m < y_m && y_m < z_m);
double code(double x_m, double y_m, double z_m) {
	double t_0 = 1.0 / exp(-log(z_m));
	double tmp;
	if (z_m <= 8.5e-157) {
		tmp = t_0;
	} else if (z_m <= 1.12e+154) {
		tmp = sqrt(fma(y_m, y_m, fma(z_m, z_m, (x_m * x_m))));
	} else {
		tmp = t_0;
	}
	return tmp;
}
x_m = abs(x)
y_m = abs(y)
z_m = abs(z)
x_m, y_m, z_m = sort([x_m, y_m, z_m])
function code(x_m, y_m, z_m)
	t_0 = Float64(1.0 / exp(Float64(-log(z_m))))
	tmp = 0.0
	if (z_m <= 8.5e-157)
		tmp = t_0;
	elseif (z_m <= 1.12e+154)
		tmp = sqrt(fma(y_m, y_m, fma(z_m, z_m, Float64(x_m * x_m))));
	else
		tmp = t_0;
	end
	return tmp
end
x_m = N[Abs[x], $MachinePrecision]
y_m = N[Abs[y], $MachinePrecision]
z_m = N[Abs[z], $MachinePrecision]
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
code[x$95$m_, y$95$m_, z$95$m_] := Block[{t$95$0 = N[(1.0 / N[Exp[(-N[Log[z$95$m], $MachinePrecision])], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z$95$m, 8.5e-157], t$95$0, If[LessEqual[z$95$m, 1.12e+154], N[Sqrt[N[(y$95$m * y$95$m + N[(z$95$m * z$95$m + N[(x$95$m * x$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], t$95$0]]]
\begin{array}{l}
x_m = \left|x\right|
\\
y_m = \left|y\right|
\\
z_m = \left|z\right|
\\
[x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\
\\
\begin{array}{l}
t_0 := \frac{1}{e^{-\log z\_m}}\\
\mathbf{if}\;z\_m \leq 8.5 \cdot 10^{-157}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;z\_m \leq 1.12 \cdot 10^{+154}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(y\_m, y\_m, \mathsf{fma}\left(z\_m, z\_m, x\_m \cdot x\_m\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < 8.49999999999999976e-157 or 1.11999999999999994e154 < z

    1. Initial program 44.9%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Step-by-step derivation
      1. lift-sqrt.f64N/A

        \[\leadsto \color{blue}{\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}} \]
      2. pow1/2N/A

        \[\leadsto \color{blue}{{\left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)}^{\frac{1}{2}}} \]
      3. pow-to-expN/A

        \[\leadsto \color{blue}{e^{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}}} \]
      4. lower-exp.f64N/A

        \[\leadsto \color{blue}{e^{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}}} \]
      5. lower-*.f64N/A

        \[\leadsto e^{\color{blue}{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}}} \]
      6. lower-log.f6441.8

        \[\leadsto e^{\color{blue}{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)} \cdot 0.5} \]
      7. lift-+.f64N/A

        \[\leadsto e^{\log \color{blue}{\left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)} \cdot \frac{1}{2}} \]
      8. lift-*.f64N/A

        \[\leadsto e^{\log \left(\color{blue}{x \cdot x} + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}} \]
      9. lower-fma.f6441.8

        \[\leadsto e^{\log \color{blue}{\left(\mathsf{fma}\left(x, x, y \cdot y + z \cdot z\right)\right)} \cdot 0.5} \]
      10. lift-+.f64N/A

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{y \cdot y + z \cdot z}\right)\right) \cdot \frac{1}{2}} \]
      11. +-commutativeN/A

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{z \cdot z + y \cdot y}\right)\right) \cdot \frac{1}{2}} \]
      12. lift-*.f64N/A

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{z \cdot z} + y \cdot y\right)\right) \cdot \frac{1}{2}} \]
      13. lower-fma.f6441.8

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{\mathsf{fma}\left(z, z, y \cdot y\right)}\right)\right) \cdot 0.5} \]
    3. Applied rewrites41.8%

      \[\leadsto \color{blue}{e^{\log \left(\mathsf{fma}\left(x, x, \mathsf{fma}\left(z, z, y \cdot y\right)\right)\right) \cdot 0.5}} \]
    4. Taylor expanded in z around inf

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(\frac{1}{z}\right)}} \]
    5. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto e^{-1 \cdot \color{blue}{\log \left(\frac{1}{z}\right)}} \]
      2. lower-log.f64N/A

        \[\leadsto e^{-1 \cdot \log \left(\frac{1}{z}\right)} \]
      3. lower-/.f6489.3

        \[\leadsto e^{-1 \cdot \log \left(\frac{1}{z}\right)} \]
    6. Applied rewrites89.3%

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(\frac{1}{z}\right)}} \]
    7. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto e^{-1 \cdot \color{blue}{\log \left(\frac{1}{z}\right)}} \]
      2. mul-1-negN/A

        \[\leadsto e^{\mathsf{neg}\left(\log \left(\frac{1}{z}\right)\right)} \]
      3. lift-log.f64N/A

        \[\leadsto e^{\mathsf{neg}\left(\log \left(\frac{1}{z}\right)\right)} \]
      4. lift-/.f64N/A

        \[\leadsto e^{\mathsf{neg}\left(\log \left(\frac{1}{z}\right)\right)} \]
      5. log-recN/A

        \[\leadsto e^{\mathsf{neg}\left(\left(\mathsf{neg}\left(\log z\right)\right)\right)} \]
      6. remove-double-negN/A

        \[\leadsto e^{\log z} \]
      7. lower-log.f6489.3

        \[\leadsto e^{\log z} \]
    8. Applied rewrites89.3%

      \[\leadsto e^{\log z} \]
    9. Step-by-step derivation
      1. lift-exp.f64N/A

        \[\leadsto \color{blue}{e^{\log z}} \]
      2. sinh-+-cosh-revN/A

        \[\leadsto \color{blue}{\cosh \log z + \sinh \log z} \]
      3. flip-+N/A

        \[\leadsto \color{blue}{\frac{\cosh \log z \cdot \cosh \log z - \sinh \log z \cdot \sinh \log z}{\cosh \log z - \sinh \log z}} \]
      4. sinh-coshN/A

        \[\leadsto \frac{\color{blue}{1}}{\cosh \log z - \sinh \log z} \]
      5. sinh---cosh-revN/A

        \[\leadsto \frac{1}{\color{blue}{e^{\mathsf{neg}\left(\log z\right)}}} \]
    10. Applied rewrites89.3%

      \[\leadsto \color{blue}{\frac{1}{e^{-\log z}}} \]

    if 8.49999999999999976e-157 < z < 1.11999999999999994e154

    1. Initial program 44.9%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \sqrt{\color{blue}{x \cdot x + \left(y \cdot y + z \cdot z\right)}} \]
      2. +-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{\left(y \cdot y + z \cdot z\right) + x \cdot x}} \]
      3. lift-+.f64N/A

        \[\leadsto \sqrt{\color{blue}{\left(y \cdot y + z \cdot z\right)} + x \cdot x} \]
      4. associate-+l+N/A

        \[\leadsto \sqrt{\color{blue}{y \cdot y + \left(z \cdot z + x \cdot x\right)}} \]
      5. lift-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{y \cdot y} + \left(z \cdot z + x \cdot x\right)} \]
      6. lower-fma.f64N/A

        \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(y, y, z \cdot z + x \cdot x\right)}} \]
      7. lift-*.f64N/A

        \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \color{blue}{z \cdot z} + x \cdot x\right)} \]
      8. lower-fma.f6444.9

        \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \color{blue}{\mathsf{fma}\left(z, z, x \cdot x\right)}\right)} \]
    3. Applied rewrites44.9%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(y, y, \mathsf{fma}\left(z, z, x \cdot x\right)\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 93.0% accurate, 0.7× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ y_m = \left|y\right| \\ z_m = \left|z\right| \\ [x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\ \\ \begin{array}{l} t_0 := e^{\log z\_m}\\ \mathbf{if}\;z\_m \leq 8.5 \cdot 10^{-157}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z\_m \leq 1.12 \cdot 10^{+154}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y\_m, y\_m, \mathsf{fma}\left(z\_m, z\_m, x\_m \cdot x\_m\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
y_m = (fabs.f64 y)
z_m = (fabs.f64 z)
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
(FPCore (x_m y_m z_m)
 :precision binary64
 (let* ((t_0 (exp (log z_m))))
   (if (<= z_m 8.5e-157)
     t_0
     (if (<= z_m 1.12e+154)
       (sqrt (fma y_m y_m (fma z_m z_m (* x_m x_m))))
       t_0))))
x_m = fabs(x);
y_m = fabs(y);
z_m = fabs(z);
assert(x_m < y_m && y_m < z_m);
double code(double x_m, double y_m, double z_m) {
	double t_0 = exp(log(z_m));
	double tmp;
	if (z_m <= 8.5e-157) {
		tmp = t_0;
	} else if (z_m <= 1.12e+154) {
		tmp = sqrt(fma(y_m, y_m, fma(z_m, z_m, (x_m * x_m))));
	} else {
		tmp = t_0;
	}
	return tmp;
}
x_m = abs(x)
y_m = abs(y)
z_m = abs(z)
x_m, y_m, z_m = sort([x_m, y_m, z_m])
function code(x_m, y_m, z_m)
	t_0 = exp(log(z_m))
	tmp = 0.0
	if (z_m <= 8.5e-157)
		tmp = t_0;
	elseif (z_m <= 1.12e+154)
		tmp = sqrt(fma(y_m, y_m, fma(z_m, z_m, Float64(x_m * x_m))));
	else
		tmp = t_0;
	end
	return tmp
end
x_m = N[Abs[x], $MachinePrecision]
y_m = N[Abs[y], $MachinePrecision]
z_m = N[Abs[z], $MachinePrecision]
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
code[x$95$m_, y$95$m_, z$95$m_] := Block[{t$95$0 = N[Exp[N[Log[z$95$m], $MachinePrecision]], $MachinePrecision]}, If[LessEqual[z$95$m, 8.5e-157], t$95$0, If[LessEqual[z$95$m, 1.12e+154], N[Sqrt[N[(y$95$m * y$95$m + N[(z$95$m * z$95$m + N[(x$95$m * x$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], t$95$0]]]
\begin{array}{l}
x_m = \left|x\right|
\\
y_m = \left|y\right|
\\
z_m = \left|z\right|
\\
[x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\
\\
\begin{array}{l}
t_0 := e^{\log z\_m}\\
\mathbf{if}\;z\_m \leq 8.5 \cdot 10^{-157}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;z\_m \leq 1.12 \cdot 10^{+154}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(y\_m, y\_m, \mathsf{fma}\left(z\_m, z\_m, x\_m \cdot x\_m\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < 8.49999999999999976e-157 or 1.11999999999999994e154 < z

    1. Initial program 44.9%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Step-by-step derivation
      1. lift-sqrt.f64N/A

        \[\leadsto \color{blue}{\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}} \]
      2. pow1/2N/A

        \[\leadsto \color{blue}{{\left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)}^{\frac{1}{2}}} \]
      3. pow-to-expN/A

        \[\leadsto \color{blue}{e^{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}}} \]
      4. lower-exp.f64N/A

        \[\leadsto \color{blue}{e^{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}}} \]
      5. lower-*.f64N/A

        \[\leadsto e^{\color{blue}{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}}} \]
      6. lower-log.f6441.8

        \[\leadsto e^{\color{blue}{\log \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)} \cdot 0.5} \]
      7. lift-+.f64N/A

        \[\leadsto e^{\log \color{blue}{\left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)} \cdot \frac{1}{2}} \]
      8. lift-*.f64N/A

        \[\leadsto e^{\log \left(\color{blue}{x \cdot x} + \left(y \cdot y + z \cdot z\right)\right) \cdot \frac{1}{2}} \]
      9. lower-fma.f6441.8

        \[\leadsto e^{\log \color{blue}{\left(\mathsf{fma}\left(x, x, y \cdot y + z \cdot z\right)\right)} \cdot 0.5} \]
      10. lift-+.f64N/A

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{y \cdot y + z \cdot z}\right)\right) \cdot \frac{1}{2}} \]
      11. +-commutativeN/A

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{z \cdot z + y \cdot y}\right)\right) \cdot \frac{1}{2}} \]
      12. lift-*.f64N/A

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{z \cdot z} + y \cdot y\right)\right) \cdot \frac{1}{2}} \]
      13. lower-fma.f6441.8

        \[\leadsto e^{\log \left(\mathsf{fma}\left(x, x, \color{blue}{\mathsf{fma}\left(z, z, y \cdot y\right)}\right)\right) \cdot 0.5} \]
    3. Applied rewrites41.8%

      \[\leadsto \color{blue}{e^{\log \left(\mathsf{fma}\left(x, x, \mathsf{fma}\left(z, z, y \cdot y\right)\right)\right) \cdot 0.5}} \]
    4. Taylor expanded in z around inf

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(\frac{1}{z}\right)}} \]
    5. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto e^{-1 \cdot \color{blue}{\log \left(\frac{1}{z}\right)}} \]
      2. lower-log.f64N/A

        \[\leadsto e^{-1 \cdot \log \left(\frac{1}{z}\right)} \]
      3. lower-/.f6489.3

        \[\leadsto e^{-1 \cdot \log \left(\frac{1}{z}\right)} \]
    6. Applied rewrites89.3%

      \[\leadsto e^{\color{blue}{-1 \cdot \log \left(\frac{1}{z}\right)}} \]
    7. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto e^{-1 \cdot \color{blue}{\log \left(\frac{1}{z}\right)}} \]
      2. mul-1-negN/A

        \[\leadsto e^{\mathsf{neg}\left(\log \left(\frac{1}{z}\right)\right)} \]
      3. lift-log.f64N/A

        \[\leadsto e^{\mathsf{neg}\left(\log \left(\frac{1}{z}\right)\right)} \]
      4. lift-/.f64N/A

        \[\leadsto e^{\mathsf{neg}\left(\log \left(\frac{1}{z}\right)\right)} \]
      5. log-recN/A

        \[\leadsto e^{\mathsf{neg}\left(\left(\mathsf{neg}\left(\log z\right)\right)\right)} \]
      6. remove-double-negN/A

        \[\leadsto e^{\log z} \]
      7. lower-log.f6489.3

        \[\leadsto e^{\log z} \]
    8. Applied rewrites89.3%

      \[\leadsto e^{\log z} \]

    if 8.49999999999999976e-157 < z < 1.11999999999999994e154

    1. Initial program 44.9%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \sqrt{\color{blue}{x \cdot x + \left(y \cdot y + z \cdot z\right)}} \]
      2. +-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{\left(y \cdot y + z \cdot z\right) + x \cdot x}} \]
      3. lift-+.f64N/A

        \[\leadsto \sqrt{\color{blue}{\left(y \cdot y + z \cdot z\right)} + x \cdot x} \]
      4. associate-+l+N/A

        \[\leadsto \sqrt{\color{blue}{y \cdot y + \left(z \cdot z + x \cdot x\right)}} \]
      5. lift-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{y \cdot y} + \left(z \cdot z + x \cdot x\right)} \]
      6. lower-fma.f64N/A

        \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(y, y, z \cdot z + x \cdot x\right)}} \]
      7. lift-*.f64N/A

        \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \color{blue}{z \cdot z} + x \cdot x\right)} \]
      8. lower-fma.f6444.9

        \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \color{blue}{\mathsf{fma}\left(z, z, x \cdot x\right)}\right)} \]
    3. Applied rewrites44.9%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(y, y, \mathsf{fma}\left(z, z, x \cdot x\right)\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 44.9% accurate, 1.1× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ y_m = \left|y\right| \\ z_m = \left|z\right| \\ [x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\ \\ \sqrt{\mathsf{fma}\left(y\_m, y\_m, \mathsf{fma}\left(z\_m, z\_m, x\_m \cdot x\_m\right)\right)} \end{array} \]
x_m = (fabs.f64 x)
y_m = (fabs.f64 y)
z_m = (fabs.f64 z)
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
(FPCore (x_m y_m z_m)
 :precision binary64
 (sqrt (fma y_m y_m (fma z_m z_m (* x_m x_m)))))
x_m = fabs(x);
y_m = fabs(y);
z_m = fabs(z);
assert(x_m < y_m && y_m < z_m);
double code(double x_m, double y_m, double z_m) {
	return sqrt(fma(y_m, y_m, fma(z_m, z_m, (x_m * x_m))));
}
x_m = abs(x)
y_m = abs(y)
z_m = abs(z)
x_m, y_m, z_m = sort([x_m, y_m, z_m])
function code(x_m, y_m, z_m)
	return sqrt(fma(y_m, y_m, fma(z_m, z_m, Float64(x_m * x_m))))
end
x_m = N[Abs[x], $MachinePrecision]
y_m = N[Abs[y], $MachinePrecision]
z_m = N[Abs[z], $MachinePrecision]
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
code[x$95$m_, y$95$m_, z$95$m_] := N[Sqrt[N[(y$95$m * y$95$m + N[(z$95$m * z$95$m + N[(x$95$m * x$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
y_m = \left|y\right|
\\
z_m = \left|z\right|
\\
[x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\
\\
\sqrt{\mathsf{fma}\left(y\_m, y\_m, \mathsf{fma}\left(z\_m, z\_m, x\_m \cdot x\_m\right)\right)}
\end{array}
Derivation
  1. Initial program 44.9%

    \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
  2. Step-by-step derivation
    1. lift-+.f64N/A

      \[\leadsto \sqrt{\color{blue}{x \cdot x + \left(y \cdot y + z \cdot z\right)}} \]
    2. +-commutativeN/A

      \[\leadsto \sqrt{\color{blue}{\left(y \cdot y + z \cdot z\right) + x \cdot x}} \]
    3. lift-+.f64N/A

      \[\leadsto \sqrt{\color{blue}{\left(y \cdot y + z \cdot z\right)} + x \cdot x} \]
    4. associate-+l+N/A

      \[\leadsto \sqrt{\color{blue}{y \cdot y + \left(z \cdot z + x \cdot x\right)}} \]
    5. lift-*.f64N/A

      \[\leadsto \sqrt{\color{blue}{y \cdot y} + \left(z \cdot z + x \cdot x\right)} \]
    6. lower-fma.f64N/A

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(y, y, z \cdot z + x \cdot x\right)}} \]
    7. lift-*.f64N/A

      \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \color{blue}{z \cdot z} + x \cdot x\right)} \]
    8. lower-fma.f6444.9

      \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \color{blue}{\mathsf{fma}\left(z, z, x \cdot x\right)}\right)} \]
  3. Applied rewrites44.9%

    \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(y, y, \mathsf{fma}\left(z, z, x \cdot x\right)\right)}} \]
  4. Add Preprocessing

Alternative 7: 5.7% accurate, 2.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ y_m = \left|y\right| \\ z_m = \left|z\right| \\ [x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\ \\ \frac{y\_m}{x\_m} \cdot \left|x\_m\right| \end{array} \]
x_m = (fabs.f64 x)
y_m = (fabs.f64 y)
z_m = (fabs.f64 z)
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
(FPCore (x_m y_m z_m) :precision binary64 (* (/ y_m x_m) (fabs x_m)))
x_m = fabs(x);
y_m = fabs(y);
z_m = fabs(z);
assert(x_m < y_m && y_m < z_m);
double code(double x_m, double y_m, double z_m) {
	return (y_m / x_m) * fabs(x_m);
}
x_m =     private
y_m =     private
z_m =     private
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x_m, y_m, z_m)
use fmin_fmax_functions
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y_m
    real(8), intent (in) :: z_m
    code = (y_m / x_m) * abs(x_m)
end function
x_m = Math.abs(x);
y_m = Math.abs(y);
z_m = Math.abs(z);
assert x_m < y_m && y_m < z_m;
public static double code(double x_m, double y_m, double z_m) {
	return (y_m / x_m) * Math.abs(x_m);
}
x_m = math.fabs(x)
y_m = math.fabs(y)
z_m = math.fabs(z)
[x_m, y_m, z_m] = sort([x_m, y_m, z_m])
def code(x_m, y_m, z_m):
	return (y_m / x_m) * math.fabs(x_m)
x_m = abs(x)
y_m = abs(y)
z_m = abs(z)
x_m, y_m, z_m = sort([x_m, y_m, z_m])
function code(x_m, y_m, z_m)
	return Float64(Float64(y_m / x_m) * abs(x_m))
end
x_m = abs(x);
y_m = abs(y);
z_m = abs(z);
x_m, y_m, z_m = num2cell(sort([x_m, y_m, z_m])){:}
function tmp = code(x_m, y_m, z_m)
	tmp = (y_m / x_m) * abs(x_m);
end
x_m = N[Abs[x], $MachinePrecision]
y_m = N[Abs[y], $MachinePrecision]
z_m = N[Abs[z], $MachinePrecision]
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
code[x$95$m_, y$95$m_, z$95$m_] := N[(N[(y$95$m / x$95$m), $MachinePrecision] * N[Abs[x$95$m], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
y_m = \left|y\right|
\\
z_m = \left|z\right|
\\
[x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\
\\
\frac{y\_m}{x\_m} \cdot \left|x\_m\right|
\end{array}
Derivation
  1. Initial program 44.9%

    \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
  2. Step-by-step derivation
    1. lift-sqrt.f64N/A

      \[\leadsto \color{blue}{\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}} \]
    2. lift-+.f64N/A

      \[\leadsto \sqrt{\color{blue}{x \cdot x + \left(y \cdot y + z \cdot z\right)}} \]
    3. sum-to-multN/A

      \[\leadsto \sqrt{\color{blue}{\left(1 + \frac{y \cdot y + z \cdot z}{x \cdot x}\right) \cdot \left(x \cdot x\right)}} \]
    4. sqrt-prodN/A

      \[\leadsto \color{blue}{\sqrt{1 + \frac{y \cdot y + z \cdot z}{x \cdot x}} \cdot \sqrt{x \cdot x}} \]
    5. lift-*.f64N/A

      \[\leadsto \sqrt{1 + \frac{y \cdot y + z \cdot z}{x \cdot x}} \cdot \sqrt{\color{blue}{x \cdot x}} \]
    6. rem-sqrt-square-revN/A

      \[\leadsto \sqrt{1 + \frac{y \cdot y + z \cdot z}{x \cdot x}} \cdot \color{blue}{\left|x\right|} \]
    7. lower-*.f64N/A

      \[\leadsto \color{blue}{\sqrt{1 + \frac{y \cdot y + z \cdot z}{x \cdot x}} \cdot \left|x\right|} \]
    8. lower-sqrt.f64N/A

      \[\leadsto \color{blue}{\sqrt{1 + \frac{y \cdot y + z \cdot z}{x \cdot x}}} \cdot \left|x\right| \]
    9. +-commutativeN/A

      \[\leadsto \sqrt{\color{blue}{\frac{y \cdot y + z \cdot z}{x \cdot x} + 1}} \cdot \left|x\right| \]
    10. lower-+.f64N/A

      \[\leadsto \sqrt{\color{blue}{\frac{y \cdot y + z \cdot z}{x \cdot x} + 1}} \cdot \left|x\right| \]
    11. lower-/.f64N/A

      \[\leadsto \sqrt{\color{blue}{\frac{y \cdot y + z \cdot z}{x \cdot x}} + 1} \cdot \left|x\right| \]
    12. lift-+.f64N/A

      \[\leadsto \sqrt{\frac{\color{blue}{y \cdot y + z \cdot z}}{x \cdot x} + 1} \cdot \left|x\right| \]
    13. +-commutativeN/A

      \[\leadsto \sqrt{\frac{\color{blue}{z \cdot z + y \cdot y}}{x \cdot x} + 1} \cdot \left|x\right| \]
    14. lift-*.f64N/A

      \[\leadsto \sqrt{\frac{\color{blue}{z \cdot z} + y \cdot y}{x \cdot x} + 1} \cdot \left|x\right| \]
    15. lower-fma.f64N/A

      \[\leadsto \sqrt{\frac{\color{blue}{\mathsf{fma}\left(z, z, y \cdot y\right)}}{x \cdot x} + 1} \cdot \left|x\right| \]
    16. lower-fabs.f6411.9

      \[\leadsto \sqrt{\frac{\mathsf{fma}\left(z, z, y \cdot y\right)}{x \cdot x} + 1} \cdot \color{blue}{\left|x\right|} \]
  3. Applied rewrites11.9%

    \[\leadsto \color{blue}{\sqrt{\frac{\mathsf{fma}\left(z, z, y \cdot y\right)}{x \cdot x} + 1} \cdot \left|x\right|} \]
  4. Taylor expanded in y around inf

    \[\leadsto \color{blue}{\left(y \cdot \sqrt{\frac{1}{{x}^{2}}}\right)} \cdot \left|x\right| \]
  5. Step-by-step derivation
    1. lower-*.f64N/A

      \[\leadsto \left(y \cdot \color{blue}{\sqrt{\frac{1}{{x}^{2}}}}\right) \cdot \left|x\right| \]
    2. lower-sqrt.f64N/A

      \[\leadsto \left(y \cdot \sqrt{\frac{1}{{x}^{2}}}\right) \cdot \left|x\right| \]
    3. lower-/.f64N/A

      \[\leadsto \left(y \cdot \sqrt{\frac{1}{{x}^{2}}}\right) \cdot \left|x\right| \]
    4. lower-pow.f645.6

      \[\leadsto \left(y \cdot \sqrt{\frac{1}{{x}^{2}}}\right) \cdot \left|x\right| \]
  6. Applied rewrites5.6%

    \[\leadsto \color{blue}{\left(y \cdot \sqrt{\frac{1}{{x}^{2}}}\right)} \cdot \left|x\right| \]
  7. Taylor expanded in x around 0

    \[\leadsto \frac{y}{\color{blue}{x}} \cdot \left|x\right| \]
  8. Step-by-step derivation
    1. lower-/.f645.7

      \[\leadsto \frac{y}{x} \cdot \left|x\right| \]
  9. Applied rewrites5.7%

    \[\leadsto \frac{y}{\color{blue}{x}} \cdot \left|x\right| \]
  10. Add Preprocessing

Alternative 8: 3.4% accurate, 3.3× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ y_m = \left|y\right| \\ z_m = \left|z\right| \\ [x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\ \\ 1 \cdot \left|x\_m\right| \end{array} \]
x_m = (fabs.f64 x)
y_m = (fabs.f64 y)
z_m = (fabs.f64 z)
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
(FPCore (x_m y_m z_m) :precision binary64 (* 1.0 (fabs x_m)))
x_m = fabs(x);
y_m = fabs(y);
z_m = fabs(z);
assert(x_m < y_m && y_m < z_m);
double code(double x_m, double y_m, double z_m) {
	return 1.0 * fabs(x_m);
}
x_m =     private
y_m =     private
z_m =     private
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x_m, y_m, z_m)
use fmin_fmax_functions
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y_m
    real(8), intent (in) :: z_m
    code = 1.0d0 * abs(x_m)
end function
x_m = Math.abs(x);
y_m = Math.abs(y);
z_m = Math.abs(z);
assert x_m < y_m && y_m < z_m;
public static double code(double x_m, double y_m, double z_m) {
	return 1.0 * Math.abs(x_m);
}
x_m = math.fabs(x)
y_m = math.fabs(y)
z_m = math.fabs(z)
[x_m, y_m, z_m] = sort([x_m, y_m, z_m])
def code(x_m, y_m, z_m):
	return 1.0 * math.fabs(x_m)
x_m = abs(x)
y_m = abs(y)
z_m = abs(z)
x_m, y_m, z_m = sort([x_m, y_m, z_m])
function code(x_m, y_m, z_m)
	return Float64(1.0 * abs(x_m))
end
x_m = abs(x);
y_m = abs(y);
z_m = abs(z);
x_m, y_m, z_m = num2cell(sort([x_m, y_m, z_m])){:}
function tmp = code(x_m, y_m, z_m)
	tmp = 1.0 * abs(x_m);
end
x_m = N[Abs[x], $MachinePrecision]
y_m = N[Abs[y], $MachinePrecision]
z_m = N[Abs[z], $MachinePrecision]
NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
code[x$95$m_, y$95$m_, z$95$m_] := N[(1.0 * N[Abs[x$95$m], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
y_m = \left|y\right|
\\
z_m = \left|z\right|
\\
[x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\
\\
1 \cdot \left|x\_m\right|
\end{array}
Derivation
  1. Initial program 44.9%

    \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
  2. Step-by-step derivation
    1. lift-sqrt.f64N/A

      \[\leadsto \color{blue}{\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}} \]
    2. lift-+.f64N/A

      \[\leadsto \sqrt{\color{blue}{x \cdot x + \left(y \cdot y + z \cdot z\right)}} \]
    3. sum-to-multN/A

      \[\leadsto \sqrt{\color{blue}{\left(1 + \frac{y \cdot y + z \cdot z}{x \cdot x}\right) \cdot \left(x \cdot x\right)}} \]
    4. sqrt-prodN/A

      \[\leadsto \color{blue}{\sqrt{1 + \frac{y \cdot y + z \cdot z}{x \cdot x}} \cdot \sqrt{x \cdot x}} \]
    5. lift-*.f64N/A

      \[\leadsto \sqrt{1 + \frac{y \cdot y + z \cdot z}{x \cdot x}} \cdot \sqrt{\color{blue}{x \cdot x}} \]
    6. rem-sqrt-square-revN/A

      \[\leadsto \sqrt{1 + \frac{y \cdot y + z \cdot z}{x \cdot x}} \cdot \color{blue}{\left|x\right|} \]
    7. lower-*.f64N/A

      \[\leadsto \color{blue}{\sqrt{1 + \frac{y \cdot y + z \cdot z}{x \cdot x}} \cdot \left|x\right|} \]
    8. lower-sqrt.f64N/A

      \[\leadsto \color{blue}{\sqrt{1 + \frac{y \cdot y + z \cdot z}{x \cdot x}}} \cdot \left|x\right| \]
    9. +-commutativeN/A

      \[\leadsto \sqrt{\color{blue}{\frac{y \cdot y + z \cdot z}{x \cdot x} + 1}} \cdot \left|x\right| \]
    10. lower-+.f64N/A

      \[\leadsto \sqrt{\color{blue}{\frac{y \cdot y + z \cdot z}{x \cdot x} + 1}} \cdot \left|x\right| \]
    11. lower-/.f64N/A

      \[\leadsto \sqrt{\color{blue}{\frac{y \cdot y + z \cdot z}{x \cdot x}} + 1} \cdot \left|x\right| \]
    12. lift-+.f64N/A

      \[\leadsto \sqrt{\frac{\color{blue}{y \cdot y + z \cdot z}}{x \cdot x} + 1} \cdot \left|x\right| \]
    13. +-commutativeN/A

      \[\leadsto \sqrt{\frac{\color{blue}{z \cdot z + y \cdot y}}{x \cdot x} + 1} \cdot \left|x\right| \]
    14. lift-*.f64N/A

      \[\leadsto \sqrt{\frac{\color{blue}{z \cdot z} + y \cdot y}{x \cdot x} + 1} \cdot \left|x\right| \]
    15. lower-fma.f64N/A

      \[\leadsto \sqrt{\frac{\color{blue}{\mathsf{fma}\left(z, z, y \cdot y\right)}}{x \cdot x} + 1} \cdot \left|x\right| \]
    16. lower-fabs.f6411.9

      \[\leadsto \sqrt{\frac{\mathsf{fma}\left(z, z, y \cdot y\right)}{x \cdot x} + 1} \cdot \color{blue}{\left|x\right|} \]
  3. Applied rewrites11.9%

    \[\leadsto \color{blue}{\sqrt{\frac{\mathsf{fma}\left(z, z, y \cdot y\right)}{x \cdot x} + 1} \cdot \left|x\right|} \]
  4. Taylor expanded in x around inf

    \[\leadsto \color{blue}{1} \cdot \left|x\right| \]
  5. Step-by-step derivation
    1. Applied rewrites3.4%

      \[\leadsto \color{blue}{1} \cdot \left|x\right| \]
    2. Add Preprocessing

    Alternative 9: 1.7% accurate, 8.7× speedup?

    \[\begin{array}{l} x_m = \left|x\right| \\ y_m = \left|y\right| \\ z_m = \left|z\right| \\ [x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\ \\ -x\_m \end{array} \]
    x_m = (fabs.f64 x)
    y_m = (fabs.f64 y)
    z_m = (fabs.f64 z)
    NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
    (FPCore (x_m y_m z_m) :precision binary64 (- x_m))
    x_m = fabs(x);
    y_m = fabs(y);
    z_m = fabs(z);
    assert(x_m < y_m && y_m < z_m);
    double code(double x_m, double y_m, double z_m) {
    	return -x_m;
    }
    
    x_m =     private
    y_m =     private
    z_m =     private
    NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
    module fmin_fmax_functions
        implicit none
        private
        public fmax
        public fmin
    
        interface fmax
            module procedure fmax88
            module procedure fmax44
            module procedure fmax84
            module procedure fmax48
        end interface
        interface fmin
            module procedure fmin88
            module procedure fmin44
            module procedure fmin84
            module procedure fmin48
        end interface
    contains
        real(8) function fmax88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(4) function fmax44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(8) function fmax84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmax48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
        end function
        real(8) function fmin88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(4) function fmin44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(8) function fmin84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmin48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
        end function
    end module
    
    real(8) function code(x_m, y_m, z_m)
    use fmin_fmax_functions
        real(8), intent (in) :: x_m
        real(8), intent (in) :: y_m
        real(8), intent (in) :: z_m
        code = -x_m
    end function
    
    x_m = Math.abs(x);
    y_m = Math.abs(y);
    z_m = Math.abs(z);
    assert x_m < y_m && y_m < z_m;
    public static double code(double x_m, double y_m, double z_m) {
    	return -x_m;
    }
    
    x_m = math.fabs(x)
    y_m = math.fabs(y)
    z_m = math.fabs(z)
    [x_m, y_m, z_m] = sort([x_m, y_m, z_m])
    def code(x_m, y_m, z_m):
    	return -x_m
    
    x_m = abs(x)
    y_m = abs(y)
    z_m = abs(z)
    x_m, y_m, z_m = sort([x_m, y_m, z_m])
    function code(x_m, y_m, z_m)
    	return Float64(-x_m)
    end
    
    x_m = abs(x);
    y_m = abs(y);
    z_m = abs(z);
    x_m, y_m, z_m = num2cell(sort([x_m, y_m, z_m])){:}
    function tmp = code(x_m, y_m, z_m)
    	tmp = -x_m;
    end
    
    x_m = N[Abs[x], $MachinePrecision]
    y_m = N[Abs[y], $MachinePrecision]
    z_m = N[Abs[z], $MachinePrecision]
    NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
    code[x$95$m_, y$95$m_, z$95$m_] := (-x$95$m)
    
    \begin{array}{l}
    x_m = \left|x\right|
    \\
    y_m = \left|y\right|
    \\
    z_m = \left|z\right|
    \\
    [x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\
    \\
    -x\_m
    \end{array}
    
    Derivation
    1. Initial program 44.9%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Taylor expanded in x around -inf

      \[\leadsto \color{blue}{-1 \cdot x} \]
    3. Step-by-step derivation
      1. lower-*.f641.7

        \[\leadsto -1 \cdot \color{blue}{x} \]
    4. Applied rewrites1.7%

      \[\leadsto \color{blue}{-1 \cdot x} \]
    5. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto -1 \cdot \color{blue}{x} \]
      2. mul-1-negN/A

        \[\leadsto \mathsf{neg}\left(x\right) \]
      3. lower-neg.f641.7

        \[\leadsto -x \]
    6. Applied rewrites1.7%

      \[\leadsto -x \]
    7. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 2025142 
    (FPCore (x y z)
      :name "bug366 (missed optimization)"
      :precision binary64
      (sqrt (+ (* x x) (+ (* y y) (* z z)))))