DWORD instead of std::bitset fails

Started by
9 comments, last by Juliean 10 years, 10 months ago

Hello,

without talking too much about the modalities, why does this code work:


	        //iteratre through render instances
	        for(auto pInstance : vSorted)
	        {
		        //store pointer to instance and state groups
		        const StateGroup** pStateGroups = pInstance->GetStateGroups();

		        std::bitset<64> bPreviousApplied;

		        //iterate through instance state groups
		        for(unsigned int i = 0; i < pInstance->GetStateCount(); i++)
		        {
			        //store pointer to state group and state pointer
			        const StateGroup* pGroup(*pStateGroups);
			        const BaseState** pvCmds = pGroup->GetCommands();

			        //iterate through command group
			        for(unsigned int j = 0; j < pGroup->GetCommandCount(); j++)
			        {
				        //get pointer to state
				        const BaseState* pState(*pvCmds);
				        //get state type
				        const BaseState::Type type(pState->GetType());

				        //ignore if same command was applied earlier on stack
				        if(bPreviousApplied[type])
                        {
                            pvCmds++;
					        continue;
                        }

				        //set applied status to skip any further commands of this type
				        bPreviousApplied[type] = true;

and this one doesn't?


	        //iteratre through render instances
	        for(auto pInstance : vSorted)
	        {
		        //store pointer to instance and state groups
		        const StateGroup** pStateGroups = pInstance->GetStateGroups();

		        DWORD bPreviousApplied(0);

		        //iterate through instance state groups
		        for(unsigned int i = 0; i < pInstance->GetStateCount(); i++)
		        {
			        //store pointer to state group and state pointer
			        const StateGroup* pGroup(*pStateGroups);
			        const BaseState** pvCmds = pGroup->GetCommands();

			        //iterate through command group
			        for(unsigned int j = 0; j < pGroup->GetCommandCount(); j++)
			        {
				        //get pointer to state
				        const BaseState* pState(*pvCmds);
				        //get state type
				        const BaseState::Type type(pState->GetType());

				        //ignore if same command was applied earlier on stack
				        if((bPreviousApplied & (1 << type)) == (1 << type))
                        {
                            pvCmds++;
					        continue;
                        }

				        //set applied status to skip any further commands of this type
				        bPreviousApplied |=  (1 << type);

in the second code, where all I did was change the std::bitset to a standard dword. However, in this code he will almost never jump into the if condition, resulting in strange visual anomalies. Well, does somebody see anything I screwed up? I though I got the bit-part of c++ figured out, but seems I didn't. I'd be glad for any help, its already getting late and I start feeling kinda numb :/

Advertisement

Can type be larger than 32? You have a 64-bit bitfield but a 32-bit DWORD.


Can type be larger than 32? You have a 64-bit bitfield but a 32-bit DWORD.

It can definately go beyond 32, yes. I already tried it with size_t before, didn't do anything eigther... however, now trying it with "ULONGLONG", while it still makes no difference in execution, at least I get this warning:


warning C4334: '<<' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)

Anything I need to do to make a shift to 64 bit (already tried converting "type" to ULONGLONG...

Try using

1ULL << type

to force the literal 1 to be a ULONGLONG.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Yes, "1<<x" is dangerous, because "1" is an "int". If "int" is 32 bits, then if x is 32 or higher, you're gonna have a bad time.
Make sure that "1" is cast to the appropriate type before shifting, as above.

I would recommend using bitmagic http://bmagic.sourceforge.net/

A way better version of the std::bitset for game programming purposes.

Thanks, 1ULL actually did the job! Unfortunately it was not worth trying anyway, I've been shown in my profiling that the if(bitset[x]) - line was 5% of my overall CPU time in my stress-tests, so I thought about changing it to a literal type just to try it out. With that bug, I got about 5ms (from 24 to 28 FPS), but now I'm back to old values. Well, nevermind, I've at least learned something new about bitshifts.


I would recommend using bitmagic http://bmagic.sourceforge.net/

Thanks for the suggestion, however in my current project I don't want to use any external libary whatsoever, for the sake of learning things from the very ground. If I ever feel for working more productively, I'll keep it in mind though.

Getting off topic now, but FWIW, on many CPUs, "literal << variable" is far, far slower than "variable << literal".
The former (lhs<<rhs where rhs isn't known at compile time) is often microcoded and/or implemented as the loop: [font='courier new']output=lhs;for(int i=0; i!=rhs; ++i) output <<= 1;[/font]
i.e. shifting by a variable amount is very slow, but shifting by a constant amount is fast.
Often it's very easy to convert your loops (though not this case, hence off topic), e.g.
[font='courier new']for(i=0; ...; ++i) { mask = 1<<i; ... }[/font]
Becomes:
[font='courier new']for(i=0, mask=1; ...; ++i, mask<<=1) { ... }[/font]

However, the OP code might benefit from not repeating this expensive operation (or [font='courier new']operator[][/font]) 3 times in a row, and instead caching the result once, depending on how much faith you have in the compiler to spot and clean up the redundancy wink.png

Thanks, noted it, in case I might come across a case where I can optimize this.


However, the OP code might benefit from not repeating this expensive operation (or operator[]) 3 times in a row, and instead caching the result once, depending on how much faith you have in the compiler to spot and clean up the redundancy wink.png

I already tried that, it didn't show any significant difference neigther in FPS nor when profiling, in release mode. Even though in my tests, the operation was likely executed about 300.000+ times, it seems most of the time spend here comes from the if()-condition itself. 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?

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 ;)

This topic is closed to new replies.

Advertisement