Migdal et al, Equation (64)

Percentage Accurate: 99.6% → 99.6%
Time: 11.0s
Alternatives: 10
Speedup: 1.9×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_1 := \frac{\cos th}{\sqrt{2}}\\ t\_1 \cdot \left(a1 \cdot a1\right) + t\_1 \cdot \left(a2 \cdot a2\right) \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (/ (cos th) (sqrt 2.0))))
   (+ (* t_1 (* a1 a1)) (* t_1 (* a2 a2)))))
double code(double a1, double a2, double th) {
	double t_1 = cos(th) / sqrt(2.0);
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
}
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: t_1
    t_1 = cos(th) / sqrt(2.0d0)
    code = (t_1 * (a1 * a1)) + (t_1 * (a2 * a2))
end function
public static double code(double a1, double a2, double th) {
	double t_1 = Math.cos(th) / Math.sqrt(2.0);
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
}
def code(a1, a2, th):
	t_1 = math.cos(th) / math.sqrt(2.0)
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2))
function code(a1, a2, th)
	t_1 = Float64(cos(th) / sqrt(2.0))
	return Float64(Float64(t_1 * Float64(a1 * a1)) + Float64(t_1 * Float64(a2 * a2)))
end
function tmp = code(a1, a2, th)
	t_1 = cos(th) / sqrt(2.0);
	tmp = (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
end
code[a1_, a2_, th_] := Block[{t$95$1 = N[(N[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$1 * N[(a1 * a1), $MachinePrecision]), $MachinePrecision] + N[(t$95$1 * N[(a2 * a2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \frac{\cos th}{\sqrt{2}}\\
t\_1 \cdot \left(a1 \cdot a1\right) + t\_1 \cdot \left(a2 \cdot a2\right)
\end{array}
\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 10 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: 99.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \frac{\cos th}{\sqrt{2}}\\ t\_1 \cdot \left(a1 \cdot a1\right) + t\_1 \cdot \left(a2 \cdot a2\right) \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (/ (cos th) (sqrt 2.0))))
   (+ (* t_1 (* a1 a1)) (* t_1 (* a2 a2)))))
double code(double a1, double a2, double th) {
	double t_1 = cos(th) / sqrt(2.0);
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
}
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: t_1
    t_1 = cos(th) / sqrt(2.0d0)
    code = (t_1 * (a1 * a1)) + (t_1 * (a2 * a2))
end function
public static double code(double a1, double a2, double th) {
	double t_1 = Math.cos(th) / Math.sqrt(2.0);
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
}
def code(a1, a2, th):
	t_1 = math.cos(th) / math.sqrt(2.0)
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2))
function code(a1, a2, th)
	t_1 = Float64(cos(th) / sqrt(2.0))
	return Float64(Float64(t_1 * Float64(a1 * a1)) + Float64(t_1 * Float64(a2 * a2)))
end
function tmp = code(a1, a2, th)
	t_1 = cos(th) / sqrt(2.0);
	tmp = (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
end
code[a1_, a2_, th_] := Block[{t$95$1 = N[(N[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$1 * N[(a1 * a1), $MachinePrecision]), $MachinePrecision] + N[(t$95$1 * N[(a2 * a2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \frac{\cos th}{\sqrt{2}}\\
t\_1 \cdot \left(a1 \cdot a1\right) + t\_1 \cdot \left(a2 \cdot a2\right)
\end{array}
\end{array}

Alternative 1: 99.6% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \frac{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\frac{\sqrt{2}}{\cos th}} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (/ (fma a1 a1 (* a2 a2)) (/ (sqrt 2.0) (cos th))))
double code(double a1, double a2, double th) {
	return fma(a1, a1, (a2 * a2)) / (sqrt(2.0) / cos(th));
}
function code(a1, a2, th)
	return Float64(fma(a1, a1, Float64(a2 * a2)) / Float64(sqrt(2.0) / cos(th)))
end
code[a1_, a2_, th_] := N[(N[(a1 * a1 + N[(a2 * a2), $MachinePrecision]), $MachinePrecision] / N[(N[Sqrt[2.0], $MachinePrecision] / N[Cos[th], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\frac{\sqrt{2}}{\cos th}}
\end{array}
Derivation
  1. Initial program 99.5%

    \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-+.f64N/A

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right)} \]
    2. lift-*.f64N/A

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right)} + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
    3. lift-*.f64N/A

      \[\leadsto \frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right)} \]
    4. distribute-lft-outN/A

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    5. *-commutativeN/A

      \[\leadsto \color{blue}{\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \frac{\cos th}{\sqrt{2}}} \]
    6. lift-/.f64N/A

      \[\leadsto \left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \color{blue}{\frac{\cos th}{\sqrt{2}}} \]
    7. clear-numN/A

      \[\leadsto \left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \color{blue}{\frac{1}{\frac{\sqrt{2}}{\cos th}}} \]
    8. un-div-invN/A

      \[\leadsto \color{blue}{\frac{a1 \cdot a1 + a2 \cdot a2}{\frac{\sqrt{2}}{\cos th}}} \]
    9. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{a1 \cdot a1 + a2 \cdot a2}{\frac{\sqrt{2}}{\cos th}}} \]
    10. lift-*.f64N/A

      \[\leadsto \frac{\color{blue}{a1 \cdot a1} + a2 \cdot a2}{\frac{\sqrt{2}}{\cos th}} \]
    11. lower-fma.f64N/A

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\frac{\sqrt{2}}{\cos th}} \]
    12. lower-/.f6499.6

      \[\leadsto \frac{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\color{blue}{\frac{\sqrt{2}}{\cos th}}} \]
  4. Applied rewrites99.6%

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\frac{\sqrt{2}}{\cos th}}} \]
  5. Add Preprocessing

Alternative 2: 76.0% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \frac{\cos th}{\sqrt{2}}\\ \mathbf{if}\;t\_1 \cdot \left(a1 \cdot a1\right) + \left(a2 \cdot a2\right) \cdot t\_1 \leq -5 \cdot 10^{-96}:\\ \;\;\;\;\frac{a2 \cdot \mathsf{fma}\left(a2, th \cdot \left(th \cdot -0.5\right), a2\right)}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a2 \cdot a2, \sqrt{2}, \sqrt{2} \cdot \left(a1 \cdot a1\right)\right)}{2}\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (/ (cos th) (sqrt 2.0))))
   (if (<= (+ (* t_1 (* a1 a1)) (* (* a2 a2) t_1)) -5e-96)
     (/ (* a2 (fma a2 (* th (* th -0.5)) a2)) (sqrt 2.0))
     (/ (fma (* a2 a2) (sqrt 2.0) (* (sqrt 2.0) (* a1 a1))) 2.0))))
double code(double a1, double a2, double th) {
	double t_1 = cos(th) / sqrt(2.0);
	double tmp;
	if (((t_1 * (a1 * a1)) + ((a2 * a2) * t_1)) <= -5e-96) {
		tmp = (a2 * fma(a2, (th * (th * -0.5)), a2)) / sqrt(2.0);
	} else {
		tmp = fma((a2 * a2), sqrt(2.0), (sqrt(2.0) * (a1 * a1))) / 2.0;
	}
	return tmp;
}
function code(a1, a2, th)
	t_1 = Float64(cos(th) / sqrt(2.0))
	tmp = 0.0
	if (Float64(Float64(t_1 * Float64(a1 * a1)) + Float64(Float64(a2 * a2) * t_1)) <= -5e-96)
		tmp = Float64(Float64(a2 * fma(a2, Float64(th * Float64(th * -0.5)), a2)) / sqrt(2.0));
	else
		tmp = Float64(fma(Float64(a2 * a2), sqrt(2.0), Float64(sqrt(2.0) * Float64(a1 * a1))) / 2.0);
	end
	return tmp
end
code[a1_, a2_, th_] := Block[{t$95$1 = N[(N[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(N[(t$95$1 * N[(a1 * a1), $MachinePrecision]), $MachinePrecision] + N[(N[(a2 * a2), $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision], -5e-96], N[(N[(a2 * N[(a2 * N[(th * N[(th * -0.5), $MachinePrecision]), $MachinePrecision] + a2), $MachinePrecision]), $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision], N[(N[(N[(a2 * a2), $MachinePrecision] * N[Sqrt[2.0], $MachinePrecision] + N[(N[Sqrt[2.0], $MachinePrecision] * N[(a1 * a1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \frac{\cos th}{\sqrt{2}}\\
\mathbf{if}\;t\_1 \cdot \left(a1 \cdot a1\right) + \left(a2 \cdot a2\right) \cdot t\_1 \leq -5 \cdot 10^{-96}:\\
\;\;\;\;\frac{a2 \cdot \mathsf{fma}\left(a2, th \cdot \left(th \cdot -0.5\right), a2\right)}{\sqrt{2}}\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a2 \cdot a2, \sqrt{2}, \sqrt{2} \cdot \left(a1 \cdot a1\right)\right)}{2}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (*.f64 (/.f64 (cos.f64 th) (sqrt.f64 #s(literal 2 binary64))) (*.f64 a1 a1)) (*.f64 (/.f64 (cos.f64 th) (sqrt.f64 #s(literal 2 binary64))) (*.f64 a2 a2))) < -4.99999999999999995e-96

    1. Initial program 99.6%

      \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
    2. Add Preprocessing
    3. Taylor expanded in a1 around inf

      \[\leadsto \color{blue}{{a1}^{2} \cdot \left(\frac{\cos th}{\sqrt{2}} + \frac{{a2}^{2} \cdot \cos th}{{a1}^{2} \cdot \sqrt{2}}\right)} \]
    4. Step-by-step derivation
      1. times-fracN/A

        \[\leadsto {a1}^{2} \cdot \left(\frac{\cos th}{\sqrt{2}} + \color{blue}{\frac{{a2}^{2}}{{a1}^{2}} \cdot \frac{\cos th}{\sqrt{2}}}\right) \]
      2. distribute-rgt1-inN/A

        \[\leadsto {a1}^{2} \cdot \color{blue}{\left(\left(\frac{{a2}^{2}}{{a1}^{2}} + 1\right) \cdot \frac{\cos th}{\sqrt{2}}\right)} \]
      3. associate-*r/N/A

        \[\leadsto {a1}^{2} \cdot \color{blue}{\frac{\left(\frac{{a2}^{2}}{{a1}^{2}} + 1\right) \cdot \cos th}{\sqrt{2}}} \]
      4. associate-*r/N/A

        \[\leadsto \color{blue}{\frac{{a1}^{2} \cdot \left(\left(\frac{{a2}^{2}}{{a1}^{2}} + 1\right) \cdot \cos th\right)}{\sqrt{2}}} \]
      5. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{{a1}^{2} \cdot \left(\left(\frac{{a2}^{2}}{{a1}^{2}} + 1\right) \cdot \cos th\right)}{\sqrt{2}}} \]
    5. Applied rewrites78.3%

      \[\leadsto \color{blue}{\frac{\left(a1 \cdot a1\right) \cdot \left(\mathsf{fma}\left(a2, \frac{a2}{a1 \cdot a1}, 1\right) \cdot \cos th\right)}{\sqrt{2}}} \]
    6. Taylor expanded in th around 0

      \[\leadsto \frac{\frac{-1}{2} \cdot \left({a1}^{2} \cdot \left({th}^{2} \cdot \left(1 + \frac{{a2}^{2}}{{a1}^{2}}\right)\right)\right) + {a1}^{2} \cdot \left(1 + \frac{{a2}^{2}}{{a1}^{2}}\right)}{\sqrt{\color{blue}{2}}} \]
    7. Step-by-step derivation
      1. Applied rewrites45.5%

        \[\leadsto \frac{\mathsf{fma}\left(a2, \frac{a2}{a1 \cdot a1}, 1\right) \cdot \mathsf{fma}\left(a1, a1, \left(\left(a1 \cdot a1\right) \cdot -0.5\right) \cdot \left(th \cdot th\right)\right)}{\sqrt{\color{blue}{2}}} \]
      2. Taylor expanded in a1 around 0

        \[\leadsto \frac{{a2}^{2} \cdot \left(1 + \frac{-1}{2} \cdot {th}^{2}\right)}{\sqrt{2}} \]
      3. Step-by-step derivation
        1. Applied rewrites45.4%

          \[\leadsto \frac{a2 \cdot \mathsf{fma}\left(a2, th \cdot \left(th \cdot -0.5\right), a2\right)}{\sqrt{2}} \]

        if -4.99999999999999995e-96 < (+.f64 (*.f64 (/.f64 (cos.f64 th) (sqrt.f64 #s(literal 2 binary64))) (*.f64 a1 a1)) (*.f64 (/.f64 (cos.f64 th) (sqrt.f64 #s(literal 2 binary64))) (*.f64 a2 a2)))

        1. Initial program 99.6%

          \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
        2. Add Preprocessing
        3. Taylor expanded in th around 0

          \[\leadsto \color{blue}{\frac{{a1}^{2}}{\sqrt{2}} + \frac{{a2}^{2}}{\sqrt{2}}} \]
        4. Step-by-step derivation
          1. unpow2N/A

            \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} + \frac{{a2}^{2}}{\sqrt{2}} \]
          2. associate-/l*N/A

            \[\leadsto \color{blue}{a1 \cdot \frac{a1}{\sqrt{2}}} + \frac{{a2}^{2}}{\sqrt{2}} \]
          3. lower-fma.f64N/A

            \[\leadsto \color{blue}{\mathsf{fma}\left(a1, \frac{a1}{\sqrt{2}}, \frac{{a2}^{2}}{\sqrt{2}}\right)} \]
          4. lower-/.f64N/A

            \[\leadsto \mathsf{fma}\left(a1, \color{blue}{\frac{a1}{\sqrt{2}}}, \frac{{a2}^{2}}{\sqrt{2}}\right) \]
          5. lower-sqrt.f64N/A

            \[\leadsto \mathsf{fma}\left(a1, \frac{a1}{\color{blue}{\sqrt{2}}}, \frac{{a2}^{2}}{\sqrt{2}}\right) \]
          6. lower-/.f64N/A

            \[\leadsto \mathsf{fma}\left(a1, \frac{a1}{\sqrt{2}}, \color{blue}{\frac{{a2}^{2}}{\sqrt{2}}}\right) \]
          7. unpow2N/A

            \[\leadsto \mathsf{fma}\left(a1, \frac{a1}{\sqrt{2}}, \frac{\color{blue}{a2 \cdot a2}}{\sqrt{2}}\right) \]
          8. lower-*.f64N/A

            \[\leadsto \mathsf{fma}\left(a1, \frac{a1}{\sqrt{2}}, \frac{\color{blue}{a2 \cdot a2}}{\sqrt{2}}\right) \]
          9. lower-sqrt.f6483.7

            \[\leadsto \mathsf{fma}\left(a1, \frac{a1}{\sqrt{2}}, \frac{a2 \cdot a2}{\color{blue}{\sqrt{2}}}\right) \]
        5. Applied rewrites83.7%

          \[\leadsto \color{blue}{\mathsf{fma}\left(a1, \frac{a1}{\sqrt{2}}, \frac{a2 \cdot a2}{\sqrt{2}}\right)} \]
        6. Step-by-step derivation
          1. Applied rewrites83.7%

            \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a2 \cdot a2, \sqrt{2}, \sqrt{2} \cdot \left(a1 \cdot a1\right)\right)}{2}} \]
        7. Recombined 2 regimes into one program.
        8. Final simplification76.0%

          \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \left(a2 \cdot a2\right) \cdot \frac{\cos th}{\sqrt{2}} \leq -5 \cdot 10^{-96}:\\ \;\;\;\;\frac{a2 \cdot \mathsf{fma}\left(a2, th \cdot \left(th \cdot -0.5\right), a2\right)}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a2 \cdot a2, \sqrt{2}, \sqrt{2} \cdot \left(a1 \cdot a1\right)\right)}{2}\\ \end{array} \]
        9. Add Preprocessing

        Reproduce

        ?
        herbie shell --seed 2024229 
        (FPCore (a1 a2 th)
          :name "Migdal et al, Equation (64)"
          :precision binary64
          (+ (* (/ (cos th) (sqrt 2.0)) (* a1 a1)) (* (/ (cos th) (sqrt 2.0)) (* a2 a2))))