Question: I'm writing a shader for rendering the sides of triangles with different colors. I have a value `mediump float back = dot(V, N)` which is positive if the normal faces away from the camera and negative if towards the camera.

To correctly select which diffuse color to shade a particular fragment, I need something like this:

```
lowp vec4 color = max(0.0, sign(-back)) * frontdiffuse + max(0.0, sign(back)) * back diffuse;

```

It's more or less an "unrolled conditional"...

Which code produces faster instructions?

* `x * 0.5 + 0.5`, or
* `max(0.0, x)` ?

The former may be better mathematically behaved than the latter, but maybe min/max/clamping is super efficient in hardware

Answer: 
As you say, the two formulas don't behave the same way mathematically. So if it makes a difference to the visuals, your first priority should be to pick the one that gives you the better-looking result.

Both formulas will be a single instruction on the vast majority of GPUs. `x * 0.5 + 0.5` can be done with a `mad` (multiply-add) instruction, and `max` is a single instruction as well. So I would expect no performance difference under ordinary circumstances.

However, the `sign` function isn't so efficient here, as it evaluates three cases (positive, negative, or zero) while you only need two cases (positive or negative). It should be faster to use an `if` statement or the `?` operator to explicitly check whether `back` is positive or negative and choose the correct color. The shader compiler *should* turn that into an efficient conditional move (as opposed to a more expensive dynamic branch).

All that being said, this is micro, micro-optimization, and quite possibly premature optimization. :) If your shaders aren't ALU-bound, this may produce no measurable difference in performance whatever. So it's probably not worth worrying about anyway.
`step` is what you are really looking for.
`step(0.0, back): = 1.0` if `(0 < back)`, or `0.0` otherwise.

So the lines become:

```
lowp float aux = step(0.0,back);
lowp vec4 color = (1.0-aux) * frontdiffuse + aux * back_diffuse;

```

For a more elegant (and maybe optimized) version with `mix`:

```
lowp vec4 color = mix(frontdiffuse, back_diffuse, step(0.0,back));

```