Sign in to follow this  
Juliean

DWORD instead of std::bitset fails

Recommended Posts

Juliean    7068

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 :/

Share this post


Link to post
Share on other sites
Juliean    7068


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...

Share this post


Link to post
Share on other sites
Juliean    7068

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.

Share this post


Link to post
Share on other sites
Hodgman    51234
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 Edited by Hodgman

Share this post


Link to post
Share on other sites
Juliean    7068

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?

Edited by Juliean

Share this post


Link to post
Share on other sites
Hodgman    51234

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

Edited by Hodgman

Share this post


Link to post
Share on other sites
Juliean    7068


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:

 

Thanks, I'll try it out as soon as I'm done upgrading to DX11 (I'm much too hyped to backtrack in my repository right now :D). Howerver, supplying the states doesn't involve branching per se, its a long switch-statement, which is appearently optimized away (jump table?), because it doesn't take any noticable amount of performance (an if-else nesting takes litteraly 100 times as much performance, profiled). Doing the same thing for this if-condition didn't gain any performance, on the other hand, quess changing one if-condition into a switch-statement doesn't do much for the compiler. Lets hope that it works better with your suggestions, thats quite some performance that could be gained, other than that one check, there is no single line in the queue that takes up a noteworth amount of time...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this