Skip to content

Instantly share code, notes, and snippets.

@doug65536
Created October 31, 2016 15:26
Show Gist options
  • Save doug65536/e676b7eb344d3e5723dedf352119f21d to your computer and use it in GitHub Desktop.
Save doug65536/e676b7eb344d3e5723dedf352119f21d to your computer and use it in GitHub Desktop.
class BranchlessCompare
{
public:
using T = int;
BranchlessCompare()
{
reset();
}
void reset() noexcept
{
force_false = 0;
force_true = 0;
}
template<typename A>
void compare(A const& lhs, A const& rhs) noexcept
{
// Force T if we are not forcing F and we would return T
// Force F if we are not forcing T and we would return F
force_true |= (force_false^1) & T(lhs < rhs);
force_false |= (force_true^1) & T(rhs < lhs);
}
bool get() const noexcept
{
return (force_true & (force_false^1) & 1);
}
operator bool() const noexcept
{
return get();
}
private:
T force_true;
T force_false;
};
bool InstancingManager::Instance::operator <(Instance const& r) const
{
BranchlessCompare cmp;
cmp.compare(shaderState, r.shaderState);
cmp.compare(textureState, r.textureState);
cmp.compare(geometryState, r.geometryState);
cmp.compare(drawCall.mode, r.drawCall.mode);
cmp.compare(drawCall.count, r.drawCall.count);
cmp.compare(drawCall.count, r.drawCall.count);
cmp.compare(drawCall.indices, r.drawCall.indices);
cmp.compare(mvMatrix, r.mvMatrix);
return cmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment