Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualHodgman

Posted 16 June 2013 - 12:14 AM

Though even more offtopic, do you have any suggestion on how to improve that, maybe some complete different branch-less approach for checking if a state was already applied before?

I assume that actually applying/submitting the states that make it past that continue statement pretty much has to involve branching, but sure, you could eliminate that if-continue branch like this:

const BaseState* toApply[MAX_STATES];
int toApplyCount = 0;
for(...)
{
	toApply[toApplyCount] = pState;
	u64 mask = (1ULL << type);
	int isValid = (bPreviousApplied & mask) ? 1 : 0; //should compile to a conditional move, not a branch
	toApplyCount += isValid;
}
for(int i=0; i!=toApplyCount; ++i)
{
	toApply[i]->Apply();
}

n.b. the pattern of if((bitfield & mask) == mask) is only required if the mask contains a pattern of more than one bit and you need to check that every bit in the mask are all set. If mask only has one bit set, you can just use if(bitfield & mask). But I guess that only matters if you're doing the bitfield logic by hand instead of using std::bitset ;)


#2Hodgman

Posted 15 June 2013 - 11:43 PM

Though even more offtopic, do you have any suggestion on how to improve that, maybe some complete different branch-less approach for checking if a state was already applied before?

I assume that actually applying/submitting the states that make it past that continue statement pretty much has to involve branching, but sure, you could eliminate that if-continue branch like this:

const BaseState* toApply[MAX_STATES];
int toApplyCount = 0;
for(...)
{
	toApply[toApplyCount] = pState;
	u64 mask = (1ULL << type);
	int isValid = (bPreviousApplied & mask) ? 1 : 0; //should compile to a conditional move, not a branch
	toApplyCount += isValid;
}
for(int i=0; i!=toApplyCount; ++i)
{
	toApply[i]->Apply();
}

n.b. the pattern of if((bitfield & mask) == mask) is only required if the mask contains a pattern of more than one bit. If mask only has one bit set, you can just use if(bitfield & mask). But I guess that only matters if you're doing the bitfield logic by hand instead of using std::bitset ;)


#1Hodgman

Posted 15 June 2013 - 11:38 PM

Though even more offtopic, do you have any suggestion on how to improve that, maybe some complete different branch-less approach for checking if a state was already applied before?

I assume that actually applying/submitting the states that make it past that continue statement pretty much has to involve branching, but sure, you could eliminate that if-continue branch like this:
const BaseState* toApply[MAX_STATES];
int toApplyCount = 0;
for(...)
{
	toApply[toApplyCount] = pState;
	u64 mask = (1ULL << type);
	int isValid = (bPreviousApplied & mask) ? 1 : 0;
	toApplyCount += isValid;
}
for(int i=0; i!=toApplyCount; ++i)
{
	toApply[i]->Apply();
}

PARTNERS