This section of the archives stores flipcode's complete Developer Toolbox collection,
featuring a variety of mini-articles and source code contributions from our readers.

SSE Ray/Box Intersection Test
Submitted by

A robust and branchless SSE ray/box intersection test.

In their excellent paper "A Cross-Platform Framework for Interactive Ray Tracing"[1] Markus Geimer
and Stefan Müller propose a branchless SIMD friendly variation of the slab test.
It's extremely fast but there's a catch, under some conditions (say (box_min-pos) == 0 while inv_dir = inf)
a NaN is produced and due to the way SSE min/max work, it ends up with a bogus result.
Here's an attempt to fix that corner case while keeping things tight.
It also works with flat voxels and take around 33 cycles on a good day (compared to the 17 cycles of the original version),
if you have a decent compiler (wink wink, nudge nudge).

Thierry Berger-Perrin.

PS: if you don't need the intersection points, just discard them.
PPS: scheduling is left as an exercise for the compiler.