Skip to content

Commit

Permalink
RSBinToGroups() use __builtin_clz() instead of manual bsr if available
Browse files Browse the repository at this point in the history
should be faster.
  • Loading branch information
DanielGibson committed May 9, 2016
1 parent 46353ff commit c58328d
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions Sources/Engine/Graphics/DrawPort_RenderScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,18 +499,27 @@ static void RSBinToGroups( ScenePolygon *pspoFirst)
);

#else
// emulate x86's bsr opcode...not fast. :/
register DWORD val = _ctGroupsCount;
register INDEX bsr = 31;
if (val != 0)
{
while (bsr > 0)
{
if (val & (1l << bsr))
break;
bsr--;
}
}
// emulate x86's bsr opcode...

// GCC and clang have an architecture-independent intrinsic for this
// (it counts leading zeros starting at MSB and is undefined for 0)
#ifdef __GNUC__
INDEX bsr = 31;
if(_ctGroupsCount != 0) bsr -= __builtin_clz(_ctGroupsCount);
else bsr = 0;
#else // another compiler - doing it manually.. not fast. :/
register DWORD val = _ctGroupsCount;
register INDEX bsr = 31;
if (val != 0)
{
while (bsr > 0)
{
if (val & (1l << bsr))
break;
bsr--;
}
}
#endif

_ctGroupsCount = 2 << bsr;
#endif
Expand Down

0 comments on commit c58328d

Please sign in to comment.