# Reciprocal, RSqrt, and Sqrt

Preferred way to use reciprocal and sqrt functions

On the PowerVR Rogue architecture, the reciprocal operation is directly supported by an instruction (`RCP`):

```fragColor.x = 1.0 / t.x; // one cycle
{frcp}```

The same is true with the `inversesqrt()` function (`RSQ`):

```fragColor.x = inversesqrt(t.x); // one cycle
{frsq}```

However, `sqrt()` is implemented as: `1 / (1/sqrt(x))`.

This results in a two cycle cost:

```fragColor.x = sqrt(t.x); // two cycles
{frsq}
{frcp}```

A commonly used alternative: `x * 1/sqrt(x)` gives the same two cycle results.

```fragColor.x = t.x * inversesqrt(t.x); // two cycles
{frsq}
{sop, sop}```

The only case when it is better to use the above alternative is if the result is tested. In this case, the test instructions can fit into the second instruction.

```fragColor.x = sqrt(t.x) > 0.5 ? 0.5 : 1.0; // three cycles
{frsq}
{frcp}
{mov, mov, pck, tstg, mov}
-->
fragColor.x = (t.x * inversesqrt(t.x)) > 0.5 ? 0.5 : 1.0; // two cycles
{frsq}
{fmul, pck, tstg, mov}```