Best way min/max for burst compiler

What is the best way to write this function for burst compile? (for best performance)

private bool Check(float3 t1, float3 t2)
{
  float3 tMin = min(t1, t2);
  float3 tMax = max(t1, t2);

  return max(tMin.x, max(tMin.y, tMin.z)) <= min(tMax.x, min(tMax.y, tMax.z));
}

I can manually inline tMin and tMax, replace min/max with ternary operator but will i have any profit with it?

There are math.cmin and math.cmax which would simplify your return statement down to return math.cmax(tMin) <= math.cmin(tMax);

I recommend playing around with Burst Inspector to determine which implementation has shorter assembly. It will likely be the fastest one.

Just make function static and add [BurstCompile] attribute to its class and to function declaration.

1 Like

@R2-RT is correct - check the assembly.
Inlining or doing tricks on C# side is unlikely to help, but you could always give it a try. :slight_smile:

Thanks! I will try

I find it strange how you knew about inlining and not the math class, check out burst manual for optimization guidelines.