struct AABox
{
vec minmax[2]; // minimum [0] and maximum [1] corners in an array
// ...
void DistanceRayFrontAndBackface (float &ffd, float& bfd, const vec& rayOrigin, const vec& rayInvDirection) const
{
vec t0 = vec(minmax[0] - rayOrigin).MulPerElem (rayInvDirection); // MulPerElem does (x0*x1, y0*y1, z0*z1) in a single SIMD instruction
vec t1 = vec(minmax[1] - rayOrigin).MulPerElem (rayInvDirection);
vec tMin = t0.MinPerElem (t1); // gives new vector with minimum per dimension, also using just one SIMD instruction
vec tMax = t0.MaxPerElem (t1);
// MaxElem gives the largest dimension
ffd = tMin.MaxElem(); // front face distance (behind origin if inside box)
bfd = tMax.MinElem(); // back face distance
}
Ray box test is a fast operation if we reduce all the branching with SIMD.
But similar optimizations could be done for any other methods as well.