bug366 (missed optimization)

Percentage Accurate: 45.5% → 99.2%
Time: 8.5s
Alternatives: 2
Speedup: 32.0×

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))));
}
real(8) function code(x, y, z)
    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}

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 2 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: 45.5% 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))));
}
real(8) function code(x, y, z)
    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.2% accurate, 1.4× speedup?

\[\begin{array}{l} z_m = \left|z\right| \\ y_m = \left|y\right| \\ x_m = \left|x\right| \\ [x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\ \\ \mathsf{fma}\left(\frac{y\_m}{z\_m} \cdot y\_m, 0.5, z\_m\right) \end{array} \]
z_m = (fabs.f64 z)
y_m = (fabs.f64 y)
x_m = (fabs.f64 x)
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 (fma (* (/ y_m z_m) y_m) 0.5 z_m))
z_m = fabs(z);
y_m = fabs(y);
x_m = fabs(x);
assert(x_m < y_m && y_m < z_m);
double code(double x_m, double y_m, double z_m) {
	return fma(((y_m / z_m) * y_m), 0.5, z_m);
}
z_m = abs(z)
y_m = abs(y)
x_m = abs(x)
x_m, y_m, z_m = sort([x_m, y_m, z_m])
function code(x_m, y_m, z_m)
	return fma(Float64(Float64(y_m / z_m) * y_m), 0.5, z_m)
end
z_m = N[Abs[z], $MachinePrecision]
y_m = N[Abs[y], $MachinePrecision]
x_m = N[Abs[x], $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[(N[(y$95$m / z$95$m), $MachinePrecision] * y$95$m), $MachinePrecision] * 0.5 + z$95$m), $MachinePrecision]
\begin{array}{l}
z_m = \left|z\right|
\\
y_m = \left|y\right|
\\
x_m = \left|x\right|
\\
[x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\
\\
\mathsf{fma}\left(\frac{y\_m}{z\_m} \cdot y\_m, 0.5, z\_m\right)
\end{array}
Derivation
  1. Initial program 45.0%

    \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
  2. Add Preprocessing
  3. 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)} \]
  4. Step-by-step derivation
    1. distribute-rgt-inN/A

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \frac{\color{blue}{{x}^{2} + {y}^{2}}}{z} \cdot \frac{1}{2} + z \]
    13. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{{x}^{2} + {y}^{2}}{z}, \frac{1}{2}, z\right)} \]
  5. Applied rewrites16.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\mathsf{hypot}\left(y, x\right)}{z}, 0.5, z\right)} \]
  6. Taylor expanded in y around 0

    \[\leadsto \mathsf{fma}\left(\frac{{x}^{2}}{z}, \frac{1}{2}, z\right) \]
  7. Step-by-step derivation
    1. Applied rewrites16.3%

      \[\leadsto \mathsf{fma}\left(\frac{x \cdot x}{z}, 0.5, z\right) \]
    2. Taylor expanded in x around inf

      \[\leadsto \mathsf{fma}\left({x}^{2} \cdot \left(\frac{1}{z} + \frac{{y}^{2}}{{x}^{2} \cdot z}\right), \frac{1}{2}, z\right) \]
    3. Step-by-step derivation
      1. Applied rewrites9.3%

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\frac{y}{z}, \frac{y}{x \cdot x}, \frac{1}{z}\right) \cdot \left(x \cdot x\right), 0.5, z\right) \]
      2. Taylor expanded in y around inf

        \[\leadsto \mathsf{fma}\left(\frac{{y}^{2}}{z}, \frac{1}{2}, z\right) \]
      3. Step-by-step derivation
        1. Applied rewrites17.4%

          \[\leadsto \mathsf{fma}\left(y \cdot \frac{y}{z}, 0.5, z\right) \]
        2. Final simplification17.4%

          \[\leadsto \mathsf{fma}\left(\frac{y}{z} \cdot y, 0.5, z\right) \]
        3. Add Preprocessing

        Alternative 2: 98.4% accurate, 32.0× speedup?

        \[\begin{array}{l} z_m = \left|z\right| \\ y_m = \left|y\right| \\ x_m = \left|x\right| \\ [x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\ \\ z\_m \end{array} \]
        z_m = (fabs.f64 z)
        y_m = (fabs.f64 y)
        x_m = (fabs.f64 x)
        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 z_m)
        z_m = fabs(z);
        y_m = fabs(y);
        x_m = fabs(x);
        assert(x_m < y_m && y_m < z_m);
        double code(double x_m, double y_m, double z_m) {
        	return z_m;
        }
        
        z_m = abs(z)
        y_m = abs(y)
        x_m = abs(x)
        NOTE: x_m, y_m, and z_m should be sorted in increasing order before calling this function.
        real(8) function code(x_m, y_m, z_m)
            real(8), intent (in) :: x_m
            real(8), intent (in) :: y_m
            real(8), intent (in) :: z_m
            code = z_m
        end function
        
        z_m = Math.abs(z);
        y_m = Math.abs(y);
        x_m = Math.abs(x);
        assert x_m < y_m && y_m < z_m;
        public static double code(double x_m, double y_m, double z_m) {
        	return z_m;
        }
        
        z_m = math.fabs(z)
        y_m = math.fabs(y)
        x_m = math.fabs(x)
        [x_m, y_m, z_m] = sort([x_m, y_m, z_m])
        def code(x_m, y_m, z_m):
        	return z_m
        
        z_m = abs(z)
        y_m = abs(y)
        x_m = abs(x)
        x_m, y_m, z_m = sort([x_m, y_m, z_m])
        function code(x_m, y_m, z_m)
        	return z_m
        end
        
        z_m = abs(z);
        y_m = abs(y);
        x_m = abs(x);
        x_m, y_m, z_m = num2cell(sort([x_m, y_m, z_m])){:}
        function tmp = code(x_m, y_m, z_m)
        	tmp = z_m;
        end
        
        z_m = N[Abs[z], $MachinePrecision]
        y_m = N[Abs[y], $MachinePrecision]
        x_m = N[Abs[x], $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_] := z$95$m
        
        \begin{array}{l}
        z_m = \left|z\right|
        \\
        y_m = \left|y\right|
        \\
        x_m = \left|x\right|
        \\
        [x_m, y_m, z_m] = \mathsf{sort}([x_m, y_m, z_m])\\
        \\
        z\_m
        \end{array}
        
        Derivation
        1. Initial program 45.0%

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

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

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

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

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

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

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

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

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

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

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

            \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \color{blue}{\mathsf{hypot}\left(z, x\right)}\right)} \]
        4. Applied rewrites19.9%

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

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

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

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

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

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

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

            \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \frac{{\color{blue}{\left(z \cdot z\right)}}^{2} - \left(x \cdot x\right) \cdot \left(x \cdot x\right)}{z \cdot z - x \cdot x}\right)} \]
          8. pow-prod-downN/A

            \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \frac{\color{blue}{{z}^{2} \cdot {z}^{2}} - \left(x \cdot x\right) \cdot \left(x \cdot x\right)}{z \cdot z - x \cdot x}\right)} \]
          9. pow-prod-upN/A

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

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

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

            \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \frac{{z}^{4} - \color{blue}{{x}^{2}} \cdot \left(x \cdot x\right)}{z \cdot z - x \cdot x}\right)} \]
          13. pow2N/A

            \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \frac{{z}^{4} - {x}^{2} \cdot \color{blue}{{x}^{2}}}{z \cdot z - x \cdot x}\right)} \]
          14. pow-prod-upN/A

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

            \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \frac{{z}^{4} - \color{blue}{{x}^{\left(2 + 2\right)}}}{z \cdot z - x \cdot x}\right)} \]
          16. metadata-evalN/A

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

            \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \frac{{z}^{4} - {x}^{4}}{\color{blue}{z \cdot z} - x \cdot x}\right)} \]
          18. difference-of-squaresN/A

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

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

            \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \frac{{z}^{4} - {x}^{4}}{\color{blue}{\left(z + x\right)} \cdot \left(z - x\right)}\right)} \]
          21. lower--.f6426.5

            \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \frac{{z}^{4} - {x}^{4}}{\left(z + x\right) \cdot \color{blue}{\left(z - x\right)}}\right)} \]
        6. Applied rewrites26.5%

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

          \[\leadsto \color{blue}{z \cdot \left(1 + \frac{-1}{2} \cdot \frac{x + -1 \cdot x}{z}\right)} \]
        8. Applied rewrites17.2%

          \[\leadsto \color{blue}{z} \]
        9. Add Preprocessing

        Developer Target 1: 100.0% accurate, 0.2× speedup?

        \[\begin{array}{l} \\ \mathsf{hypot}\left(x, \mathsf{hypot}\left(y, z\right)\right) \end{array} \]
        (FPCore (x y z) :precision binary64 (hypot x (hypot y z)))
        double code(double x, double y, double z) {
        	return hypot(x, hypot(y, z));
        }
        
        public static double code(double x, double y, double z) {
        	return Math.hypot(x, Math.hypot(y, z));
        }
        
        def code(x, y, z):
        	return math.hypot(x, math.hypot(y, z))
        
        function code(x, y, z)
        	return hypot(x, hypot(y, z))
        end
        
        function tmp = code(x, y, z)
        	tmp = hypot(x, hypot(y, z));
        end
        
        code[x_, y_, z_] := N[Sqrt[x ^ 2 + N[Sqrt[y ^ 2 + z ^ 2], $MachinePrecision] ^ 2], $MachinePrecision]
        
        \begin{array}{l}
        
        \\
        \mathsf{hypot}\left(x, \mathsf{hypot}\left(y, z\right)\right)
        \end{array}
        

        Reproduce

        ?
        herbie shell --seed 2024249 
        (FPCore (x y z)
          :name "bug366 (missed optimization)"
          :precision binary64
        
          :alt
          (! :herbie-platform default (hypot x (hypot y z)))
        
          (sqrt (+ (* x x) (+ (* y y) (* z z)))))