Falkner and Boettcher, Appendix B, 2

Percentage Accurate: 100.0% → 100.0%
Time: 5.2s
Alternatives: 5
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \left(\frac{\sqrt{2}}{4} \cdot \sqrt{1 - 3 \cdot \left(v \cdot v\right)}\right) \cdot \left(1 - v \cdot v\right) \end{array} \]
(FPCore (v)
 :precision binary64
 (* (* (/ (sqrt 2.0) 4.0) (sqrt (- 1.0 (* 3.0 (* v v))))) (- 1.0 (* v v))))
double code(double v) {
	return ((sqrt(2.0) / 4.0) * sqrt((1.0 - (3.0 * (v * v))))) * (1.0 - (v * v));
}
real(8) function code(v)
    real(8), intent (in) :: v
    code = ((sqrt(2.0d0) / 4.0d0) * sqrt((1.0d0 - (3.0d0 * (v * v))))) * (1.0d0 - (v * v))
end function
public static double code(double v) {
	return ((Math.sqrt(2.0) / 4.0) * Math.sqrt((1.0 - (3.0 * (v * v))))) * (1.0 - (v * v));
}
def code(v):
	return ((math.sqrt(2.0) / 4.0) * math.sqrt((1.0 - (3.0 * (v * v))))) * (1.0 - (v * v))
function code(v)
	return Float64(Float64(Float64(sqrt(2.0) / 4.0) * sqrt(Float64(1.0 - Float64(3.0 * Float64(v * v))))) * Float64(1.0 - Float64(v * v)))
end
function tmp = code(v)
	tmp = ((sqrt(2.0) / 4.0) * sqrt((1.0 - (3.0 * (v * v))))) * (1.0 - (v * v));
end
code[v_] := N[(N[(N[(N[Sqrt[2.0], $MachinePrecision] / 4.0), $MachinePrecision] * N[Sqrt[N[(1.0 - N[(3.0 * N[(v * v), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[(1.0 - N[(v * v), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\frac{\sqrt{2}}{4} \cdot \sqrt{1 - 3 \cdot \left(v \cdot v\right)}\right) \cdot \left(1 - v \cdot v\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 5 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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\frac{\sqrt{2}}{4} \cdot \sqrt{1 - 3 \cdot \left(v \cdot v\right)}\right) \cdot \left(1 - v \cdot v\right) \end{array} \]
(FPCore (v)
 :precision binary64
 (* (* (/ (sqrt 2.0) 4.0) (sqrt (- 1.0 (* 3.0 (* v v))))) (- 1.0 (* v v))))
double code(double v) {
	return ((sqrt(2.0) / 4.0) * sqrt((1.0 - (3.0 * (v * v))))) * (1.0 - (v * v));
}
real(8) function code(v)
    real(8), intent (in) :: v
    code = ((sqrt(2.0d0) / 4.0d0) * sqrt((1.0d0 - (3.0d0 * (v * v))))) * (1.0d0 - (v * v))
end function
public static double code(double v) {
	return ((Math.sqrt(2.0) / 4.0) * Math.sqrt((1.0 - (3.0 * (v * v))))) * (1.0 - (v * v));
}
def code(v):
	return ((math.sqrt(2.0) / 4.0) * math.sqrt((1.0 - (3.0 * (v * v))))) * (1.0 - (v * v))
function code(v)
	return Float64(Float64(Float64(sqrt(2.0) / 4.0) * sqrt(Float64(1.0 - Float64(3.0 * Float64(v * v))))) * Float64(1.0 - Float64(v * v)))
end
function tmp = code(v)
	tmp = ((sqrt(2.0) / 4.0) * sqrt((1.0 - (3.0 * (v * v))))) * (1.0 - (v * v));
end
code[v_] := N[(N[(N[(N[Sqrt[2.0], $MachinePrecision] / 4.0), $MachinePrecision] * N[Sqrt[N[(1.0 - N[(3.0 * N[(v * v), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[(1.0 - N[(v * v), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\frac{\sqrt{2}}{4} \cdot \sqrt{1 - 3 \cdot \left(v \cdot v\right)}\right) \cdot \left(1 - v \cdot v\right)
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(1 - v \cdot v\right) \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)} \end{array} \]
(FPCore (v)
 :precision binary64
 (* (- 1.0 (* v v)) (sqrt (* 0.125 (fma (* v v) -3.0 1.0)))))
double code(double v) {
	return (1.0 - (v * v)) * sqrt((0.125 * fma((v * v), -3.0, 1.0)));
}
function code(v)
	return Float64(Float64(1.0 - Float64(v * v)) * sqrt(Float64(0.125 * fma(Float64(v * v), -3.0, 1.0))))
end
code[v_] := N[(N[(1.0 - N[(v * v), $MachinePrecision]), $MachinePrecision] * N[Sqrt[N[(0.125 * N[(N[(v * v), $MachinePrecision] * -3.0 + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(1 - v \cdot v\right) \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(\frac{\sqrt{2}}{4} \cdot \sqrt{1 - 3 \cdot \left(v \cdot v\right)}\right) \cdot \left(1 - v \cdot v\right) \]
  2. Step-by-step derivation
    1. associate-*l*100.0%

      \[\leadsto \color{blue}{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)} \]
  4. Step-by-step derivation
    1. add-sqr-sqrt98.4%

      \[\leadsto \color{blue}{\sqrt{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)} \cdot \sqrt{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)}} \]
    2. sqrt-unprod100.0%

      \[\leadsto \color{blue}{\sqrt{\left(\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right) \cdot \left(\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)}} \]
    3. swap-sqr100.0%

      \[\leadsto \sqrt{\color{blue}{\left(\frac{\sqrt{2}}{4} \cdot \frac{\sqrt{2}}{4}\right) \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)}} \]
    4. frac-times100.0%

      \[\leadsto \sqrt{\color{blue}{\frac{\sqrt{2} \cdot \sqrt{2}}{4 \cdot 4}} \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)} \]
    5. add-sqr-sqrt100.0%

      \[\leadsto \sqrt{\frac{\color{blue}{2}}{4 \cdot 4} \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)} \]
    6. metadata-eval100.0%

      \[\leadsto \sqrt{\frac{2}{\color{blue}{16}} \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)} \]
    7. metadata-eval100.0%

      \[\leadsto \sqrt{\color{blue}{0.125} \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)} \]
    8. swap-sqr100.0%

      \[\leadsto \sqrt{0.125 \cdot \color{blue}{\left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \sqrt{1 - 3 \cdot \left(v \cdot v\right)}\right) \cdot \left(\left(1 - v \cdot v\right) \cdot \left(1 - v \cdot v\right)\right)\right)}} \]
  5. Applied egg-rr100.0%

    \[\leadsto \color{blue}{\sqrt{0.125 \cdot \left(\mathsf{fma}\left(v \cdot v, -3, 1\right) \cdot {\left(1 - v \cdot v\right)}^{2}\right)}} \]
  6. Step-by-step derivation
    1. associate-*r*100.0%

      \[\leadsto \sqrt{\color{blue}{\left(0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)\right) \cdot {\left(1 - v \cdot v\right)}^{2}}} \]
    2. *-commutative100.0%

      \[\leadsto \sqrt{\color{blue}{{\left(1 - v \cdot v\right)}^{2} \cdot \left(0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)\right)}} \]
  7. Simplified100.0%

    \[\leadsto \color{blue}{\sqrt{{\left(1 - v \cdot v\right)}^{2} \cdot \left(0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)\right)}} \]
  8. Step-by-step derivation
    1. expm1-log1p-u100.0%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{{\left(1 - v \cdot v\right)}^{2} \cdot \left(0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)\right)}\right)\right)} \]
    2. expm1-udef98.4%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{{\left(1 - v \cdot v\right)}^{2} \cdot \left(0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)\right)}\right)} - 1} \]
    3. sqrt-prod98.4%

      \[\leadsto e^{\mathsf{log1p}\left(\color{blue}{\sqrt{{\left(1 - v \cdot v\right)}^{2}} \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}}\right)} - 1 \]
    4. unpow298.4%

      \[\leadsto e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\left(1 - v \cdot v\right) \cdot \left(1 - v \cdot v\right)}} \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}\right)} - 1 \]
    5. sqrt-prod98.4%

      \[\leadsto e^{\mathsf{log1p}\left(\color{blue}{\left(\sqrt{1 - v \cdot v} \cdot \sqrt{1 - v \cdot v}\right)} \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}\right)} - 1 \]
    6. add-sqr-sqrt98.4%

      \[\leadsto e^{\mathsf{log1p}\left(\color{blue}{\left(1 - v \cdot v\right)} \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}\right)} - 1 \]
  9. Applied egg-rr98.4%

    \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\left(1 - v \cdot v\right) \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}\right)} - 1} \]
  10. Step-by-step derivation
    1. expm1-def100.0%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\left(1 - v \cdot v\right) \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}\right)\right)} \]
    2. expm1-log1p100.0%

      \[\leadsto \color{blue}{\left(1 - v \cdot v\right) \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}} \]
  11. Simplified100.0%

    \[\leadsto \color{blue}{\left(1 - v \cdot v\right) \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}} \]
  12. Final simplification100.0%

    \[\leadsto \left(1 - v \cdot v\right) \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)} \]

Alternative 2: 99.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\mathsf{fma}\left(v \cdot v, -2.5, 1\right)}{\frac{1}{\sqrt{0.125}}} \end{array} \]
(FPCore (v)
 :precision binary64
 (/ (fma (* v v) -2.5 1.0) (/ 1.0 (sqrt 0.125))))
double code(double v) {
	return fma((v * v), -2.5, 1.0) / (1.0 / sqrt(0.125));
}
function code(v)
	return Float64(fma(Float64(v * v), -2.5, 1.0) / Float64(1.0 / sqrt(0.125)))
end
code[v_] := N[(N[(N[(v * v), $MachinePrecision] * -2.5 + 1.0), $MachinePrecision] / N[(1.0 / N[Sqrt[0.125], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\mathsf{fma}\left(v \cdot v, -2.5, 1\right)}{\frac{1}{\sqrt{0.125}}}
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(\frac{\sqrt{2}}{4} \cdot \sqrt{1 - 3 \cdot \left(v \cdot v\right)}\right) \cdot \left(1 - v \cdot v\right) \]
  2. Step-by-step derivation
    1. associate-*l*100.0%

      \[\leadsto \color{blue}{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)} \]
  4. Taylor expanded in v around 0 99.8%

    \[\leadsto \frac{\sqrt{2}}{4} \cdot \color{blue}{\left(1 + -2.5 \cdot {v}^{2}\right)} \]
  5. Step-by-step derivation
    1. unpow299.8%

      \[\leadsto \frac{\sqrt{2}}{4} \cdot \left(1 + -2.5 \cdot \color{blue}{\left(v \cdot v\right)}\right) \]
  6. Simplified99.8%

    \[\leadsto \frac{\sqrt{2}}{4} \cdot \color{blue}{\left(1 + -2.5 \cdot \left(v \cdot v\right)\right)} \]
  7. Step-by-step derivation
    1. *-commutative99.8%

      \[\leadsto \color{blue}{\left(1 + -2.5 \cdot \left(v \cdot v\right)\right) \cdot \frac{\sqrt{2}}{4}} \]
    2. clear-num99.8%

      \[\leadsto \left(1 + -2.5 \cdot \left(v \cdot v\right)\right) \cdot \color{blue}{\frac{1}{\frac{4}{\sqrt{2}}}} \]
    3. un-div-inv99.8%

      \[\leadsto \color{blue}{\frac{1 + -2.5 \cdot \left(v \cdot v\right)}{\frac{4}{\sqrt{2}}}} \]
    4. +-commutative99.8%

      \[\leadsto \frac{\color{blue}{-2.5 \cdot \left(v \cdot v\right) + 1}}{\frac{4}{\sqrt{2}}} \]
    5. *-commutative99.8%

      \[\leadsto \frac{\color{blue}{\left(v \cdot v\right) \cdot -2.5} + 1}{\frac{4}{\sqrt{2}}} \]
    6. fma-def99.8%

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(v \cdot v, -2.5, 1\right)}}{\frac{4}{\sqrt{2}}} \]
    7. clear-num99.8%

      \[\leadsto \frac{\mathsf{fma}\left(v \cdot v, -2.5, 1\right)}{\color{blue}{\frac{1}{\frac{\sqrt{2}}{4}}}} \]
    8. add-sqr-sqrt98.2%

      \[\leadsto \frac{\mathsf{fma}\left(v \cdot v, -2.5, 1\right)}{\frac{1}{\color{blue}{\sqrt{\frac{\sqrt{2}}{4}} \cdot \sqrt{\frac{\sqrt{2}}{4}}}}} \]
    9. sqrt-unprod99.8%

      \[\leadsto \frac{\mathsf{fma}\left(v \cdot v, -2.5, 1\right)}{\frac{1}{\color{blue}{\sqrt{\frac{\sqrt{2}}{4} \cdot \frac{\sqrt{2}}{4}}}}} \]
    10. frac-times99.8%

      \[\leadsto \frac{\mathsf{fma}\left(v \cdot v, -2.5, 1\right)}{\frac{1}{\sqrt{\color{blue}{\frac{\sqrt{2} \cdot \sqrt{2}}{4 \cdot 4}}}}} \]
    11. add-sqr-sqrt99.8%

      \[\leadsto \frac{\mathsf{fma}\left(v \cdot v, -2.5, 1\right)}{\frac{1}{\sqrt{\frac{\color{blue}{2}}{4 \cdot 4}}}} \]
    12. metadata-eval99.8%

      \[\leadsto \frac{\mathsf{fma}\left(v \cdot v, -2.5, 1\right)}{\frac{1}{\sqrt{\frac{2}{\color{blue}{16}}}}} \]
    13. metadata-eval99.8%

      \[\leadsto \frac{\mathsf{fma}\left(v \cdot v, -2.5, 1\right)}{\frac{1}{\sqrt{\color{blue}{0.125}}}} \]
  8. Applied egg-rr99.8%

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(v \cdot v, -2.5, 1\right)}{\frac{1}{\sqrt{0.125}}}} \]
  9. Final simplification99.8%

    \[\leadsto \frac{\mathsf{fma}\left(v \cdot v, -2.5, 1\right)}{\frac{1}{\sqrt{0.125}}} \]

Alternative 3: 99.5% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \frac{\sqrt{2}}{4} \cdot \left(1 + \left(v \cdot v\right) \cdot -2.5\right) \end{array} \]
(FPCore (v)
 :precision binary64
 (* (/ (sqrt 2.0) 4.0) (+ 1.0 (* (* v v) -2.5))))
double code(double v) {
	return (sqrt(2.0) / 4.0) * (1.0 + ((v * v) * -2.5));
}
real(8) function code(v)
    real(8), intent (in) :: v
    code = (sqrt(2.0d0) / 4.0d0) * (1.0d0 + ((v * v) * (-2.5d0)))
end function
public static double code(double v) {
	return (Math.sqrt(2.0) / 4.0) * (1.0 + ((v * v) * -2.5));
}
def code(v):
	return (math.sqrt(2.0) / 4.0) * (1.0 + ((v * v) * -2.5))
function code(v)
	return Float64(Float64(sqrt(2.0) / 4.0) * Float64(1.0 + Float64(Float64(v * v) * -2.5)))
end
function tmp = code(v)
	tmp = (sqrt(2.0) / 4.0) * (1.0 + ((v * v) * -2.5));
end
code[v_] := N[(N[(N[Sqrt[2.0], $MachinePrecision] / 4.0), $MachinePrecision] * N[(1.0 + N[(N[(v * v), $MachinePrecision] * -2.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\sqrt{2}}{4} \cdot \left(1 + \left(v \cdot v\right) \cdot -2.5\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(\frac{\sqrt{2}}{4} \cdot \sqrt{1 - 3 \cdot \left(v \cdot v\right)}\right) \cdot \left(1 - v \cdot v\right) \]
  2. Step-by-step derivation
    1. associate-*l*100.0%

      \[\leadsto \color{blue}{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)} \]
  4. Taylor expanded in v around 0 99.8%

    \[\leadsto \frac{\sqrt{2}}{4} \cdot \color{blue}{\left(1 + -2.5 \cdot {v}^{2}\right)} \]
  5. Step-by-step derivation
    1. unpow299.8%

      \[\leadsto \frac{\sqrt{2}}{4} \cdot \left(1 + -2.5 \cdot \color{blue}{\left(v \cdot v\right)}\right) \]
  6. Simplified99.8%

    \[\leadsto \frac{\sqrt{2}}{4} \cdot \color{blue}{\left(1 + -2.5 \cdot \left(v \cdot v\right)\right)} \]
  7. Final simplification99.8%

    \[\leadsto \frac{\sqrt{2}}{4} \cdot \left(1 + \left(v \cdot v\right) \cdot -2.5\right) \]

Alternative 4: 98.9% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \left(1 - v \cdot v\right) \cdot \sqrt{0.125} \end{array} \]
(FPCore (v) :precision binary64 (* (- 1.0 (* v v)) (sqrt 0.125)))
double code(double v) {
	return (1.0 - (v * v)) * sqrt(0.125);
}
real(8) function code(v)
    real(8), intent (in) :: v
    code = (1.0d0 - (v * v)) * sqrt(0.125d0)
end function
public static double code(double v) {
	return (1.0 - (v * v)) * Math.sqrt(0.125);
}
def code(v):
	return (1.0 - (v * v)) * math.sqrt(0.125)
function code(v)
	return Float64(Float64(1.0 - Float64(v * v)) * sqrt(0.125))
end
function tmp = code(v)
	tmp = (1.0 - (v * v)) * sqrt(0.125);
end
code[v_] := N[(N[(1.0 - N[(v * v), $MachinePrecision]), $MachinePrecision] * N[Sqrt[0.125], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(1 - v \cdot v\right) \cdot \sqrt{0.125}
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(\frac{\sqrt{2}}{4} \cdot \sqrt{1 - 3 \cdot \left(v \cdot v\right)}\right) \cdot \left(1 - v \cdot v\right) \]
  2. Step-by-step derivation
    1. associate-*l*100.0%

      \[\leadsto \color{blue}{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)} \]
  4. Step-by-step derivation
    1. add-sqr-sqrt98.4%

      \[\leadsto \color{blue}{\sqrt{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)} \cdot \sqrt{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)}} \]
    2. sqrt-unprod100.0%

      \[\leadsto \color{blue}{\sqrt{\left(\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right) \cdot \left(\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)}} \]
    3. swap-sqr100.0%

      \[\leadsto \sqrt{\color{blue}{\left(\frac{\sqrt{2}}{4} \cdot \frac{\sqrt{2}}{4}\right) \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)}} \]
    4. frac-times100.0%

      \[\leadsto \sqrt{\color{blue}{\frac{\sqrt{2} \cdot \sqrt{2}}{4 \cdot 4}} \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)} \]
    5. add-sqr-sqrt100.0%

      \[\leadsto \sqrt{\frac{\color{blue}{2}}{4 \cdot 4} \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)} \]
    6. metadata-eval100.0%

      \[\leadsto \sqrt{\frac{2}{\color{blue}{16}} \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)} \]
    7. metadata-eval100.0%

      \[\leadsto \sqrt{\color{blue}{0.125} \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)} \]
    8. swap-sqr100.0%

      \[\leadsto \sqrt{0.125 \cdot \color{blue}{\left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \sqrt{1 - 3 \cdot \left(v \cdot v\right)}\right) \cdot \left(\left(1 - v \cdot v\right) \cdot \left(1 - v \cdot v\right)\right)\right)}} \]
  5. Applied egg-rr100.0%

    \[\leadsto \color{blue}{\sqrt{0.125 \cdot \left(\mathsf{fma}\left(v \cdot v, -3, 1\right) \cdot {\left(1 - v \cdot v\right)}^{2}\right)}} \]
  6. Step-by-step derivation
    1. associate-*r*100.0%

      \[\leadsto \sqrt{\color{blue}{\left(0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)\right) \cdot {\left(1 - v \cdot v\right)}^{2}}} \]
    2. *-commutative100.0%

      \[\leadsto \sqrt{\color{blue}{{\left(1 - v \cdot v\right)}^{2} \cdot \left(0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)\right)}} \]
  7. Simplified100.0%

    \[\leadsto \color{blue}{\sqrt{{\left(1 - v \cdot v\right)}^{2} \cdot \left(0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)\right)}} \]
  8. Step-by-step derivation
    1. expm1-log1p-u100.0%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{{\left(1 - v \cdot v\right)}^{2} \cdot \left(0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)\right)}\right)\right)} \]
    2. expm1-udef98.4%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{{\left(1 - v \cdot v\right)}^{2} \cdot \left(0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)\right)}\right)} - 1} \]
    3. sqrt-prod98.4%

      \[\leadsto e^{\mathsf{log1p}\left(\color{blue}{\sqrt{{\left(1 - v \cdot v\right)}^{2}} \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}}\right)} - 1 \]
    4. unpow298.4%

      \[\leadsto e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\left(1 - v \cdot v\right) \cdot \left(1 - v \cdot v\right)}} \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}\right)} - 1 \]
    5. sqrt-prod98.4%

      \[\leadsto e^{\mathsf{log1p}\left(\color{blue}{\left(\sqrt{1 - v \cdot v} \cdot \sqrt{1 - v \cdot v}\right)} \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}\right)} - 1 \]
    6. add-sqr-sqrt98.4%

      \[\leadsto e^{\mathsf{log1p}\left(\color{blue}{\left(1 - v \cdot v\right)} \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}\right)} - 1 \]
  9. Applied egg-rr98.4%

    \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\left(1 - v \cdot v\right) \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}\right)} - 1} \]
  10. Step-by-step derivation
    1. expm1-def100.0%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\left(1 - v \cdot v\right) \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}\right)\right)} \]
    2. expm1-log1p100.0%

      \[\leadsto \color{blue}{\left(1 - v \cdot v\right) \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}} \]
  11. Simplified100.0%

    \[\leadsto \color{blue}{\left(1 - v \cdot v\right) \cdot \sqrt{0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)}} \]
  12. Taylor expanded in v around 0 99.7%

    \[\leadsto \left(1 - v \cdot v\right) \cdot \color{blue}{\left(-1.5 \cdot \left({v}^{2} \cdot \sqrt{0.125}\right) + \sqrt{0.125}\right)} \]
  13. Step-by-step derivation
    1. associate-*r*99.7%

      \[\leadsto \left(1 - v \cdot v\right) \cdot \left(\color{blue}{\left(-1.5 \cdot {v}^{2}\right) \cdot \sqrt{0.125}} + \sqrt{0.125}\right) \]
    2. distribute-lft1-in99.7%

      \[\leadsto \left(1 - v \cdot v\right) \cdot \color{blue}{\left(\left(-1.5 \cdot {v}^{2} + 1\right) \cdot \sqrt{0.125}\right)} \]
    3. *-commutative99.7%

      \[\leadsto \left(1 - v \cdot v\right) \cdot \left(\left(\color{blue}{{v}^{2} \cdot -1.5} + 1\right) \cdot \sqrt{0.125}\right) \]
    4. unpow299.7%

      \[\leadsto \left(1 - v \cdot v\right) \cdot \left(\left(\color{blue}{\left(v \cdot v\right)} \cdot -1.5 + 1\right) \cdot \sqrt{0.125}\right) \]
  14. Simplified99.7%

    \[\leadsto \left(1 - v \cdot v\right) \cdot \color{blue}{\left(\left(\left(v \cdot v\right) \cdot -1.5 + 1\right) \cdot \sqrt{0.125}\right)} \]
  15. Taylor expanded in v around 0 98.9%

    \[\leadsto \left(1 - v \cdot v\right) \cdot \color{blue}{\sqrt{0.125}} \]
  16. Final simplification98.9%

    \[\leadsto \left(1 - v \cdot v\right) \cdot \sqrt{0.125} \]

Alternative 5: 98.9% accurate, 2.1× speedup?

\[\begin{array}{l} \\ \sqrt{0.125} \end{array} \]
(FPCore (v) :precision binary64 (sqrt 0.125))
double code(double v) {
	return sqrt(0.125);
}
real(8) function code(v)
    real(8), intent (in) :: v
    code = sqrt(0.125d0)
end function
public static double code(double v) {
	return Math.sqrt(0.125);
}
def code(v):
	return math.sqrt(0.125)
function code(v)
	return sqrt(0.125)
end
function tmp = code(v)
	tmp = sqrt(0.125);
end
code[v_] := N[Sqrt[0.125], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{0.125}
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(\frac{\sqrt{2}}{4} \cdot \sqrt{1 - 3 \cdot \left(v \cdot v\right)}\right) \cdot \left(1 - v \cdot v\right) \]
  2. Step-by-step derivation
    1. associate-*l*100.0%

      \[\leadsto \color{blue}{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)} \]
  4. Step-by-step derivation
    1. add-sqr-sqrt98.4%

      \[\leadsto \color{blue}{\sqrt{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)} \cdot \sqrt{\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)}} \]
    2. sqrt-unprod100.0%

      \[\leadsto \color{blue}{\sqrt{\left(\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right) \cdot \left(\frac{\sqrt{2}}{4} \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)}} \]
    3. swap-sqr100.0%

      \[\leadsto \sqrt{\color{blue}{\left(\frac{\sqrt{2}}{4} \cdot \frac{\sqrt{2}}{4}\right) \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)}} \]
    4. frac-times100.0%

      \[\leadsto \sqrt{\color{blue}{\frac{\sqrt{2} \cdot \sqrt{2}}{4 \cdot 4}} \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)} \]
    5. add-sqr-sqrt100.0%

      \[\leadsto \sqrt{\frac{\color{blue}{2}}{4 \cdot 4} \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)} \]
    6. metadata-eval100.0%

      \[\leadsto \sqrt{\frac{2}{\color{blue}{16}} \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)} \]
    7. metadata-eval100.0%

      \[\leadsto \sqrt{\color{blue}{0.125} \cdot \left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right) \cdot \left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \left(1 - v \cdot v\right)\right)\right)} \]
    8. swap-sqr100.0%

      \[\leadsto \sqrt{0.125 \cdot \color{blue}{\left(\left(\sqrt{1 - 3 \cdot \left(v \cdot v\right)} \cdot \sqrt{1 - 3 \cdot \left(v \cdot v\right)}\right) \cdot \left(\left(1 - v \cdot v\right) \cdot \left(1 - v \cdot v\right)\right)\right)}} \]
  5. Applied egg-rr100.0%

    \[\leadsto \color{blue}{\sqrt{0.125 \cdot \left(\mathsf{fma}\left(v \cdot v, -3, 1\right) \cdot {\left(1 - v \cdot v\right)}^{2}\right)}} \]
  6. Step-by-step derivation
    1. associate-*r*100.0%

      \[\leadsto \sqrt{\color{blue}{\left(0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)\right) \cdot {\left(1 - v \cdot v\right)}^{2}}} \]
    2. *-commutative100.0%

      \[\leadsto \sqrt{\color{blue}{{\left(1 - v \cdot v\right)}^{2} \cdot \left(0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)\right)}} \]
  7. Simplified100.0%

    \[\leadsto \color{blue}{\sqrt{{\left(1 - v \cdot v\right)}^{2} \cdot \left(0.125 \cdot \mathsf{fma}\left(v \cdot v, -3, 1\right)\right)}} \]
  8. Taylor expanded in v around 0 98.8%

    \[\leadsto \sqrt{\color{blue}{0.125}} \]
  9. Final simplification98.8%

    \[\leadsto \sqrt{0.125} \]

Reproduce

?
herbie shell --seed 2023203 
(FPCore (v)
  :name "Falkner and Boettcher, Appendix B, 2"
  :precision binary64
  (* (* (/ (sqrt 2.0) 4.0) (sqrt (- 1.0 (* 3.0 (* v v))))) (- 1.0 (* v v))))