I have a 3-vector, let’s say
v = vec3(-4, 2, 3)
I would like to do a max on the absolute values of the components, so the equivalent of:
max(abs(v[0]), max(abs(v[1]), abs(v[2]))) == 4
However, I have a requirement that I need to preserve the sign. So for example:
magic_max(v[0], magic_max(v[1], v[2])) == -4.
It’s a trivial problem if I use conditional branching, but I’m trying to do this in as few operations as possible, and avoid branching. Any ideas on where to look? Maybe there’s some bit-shifting magic that can be done?
I would determ the max AND the min of all values, and then decide what is abs larger
If you want to get the sign, replace the last line with an if
However, what should happen on (0, 0, 0)? no sign?