• FEATURED

View more

View more

View more

\$99

### Image of the Day Submit

IOTD | Top Screenshots

## DWORD instead of std::bitset fails

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1Juliean  GDNet+

Posted 13 June 2013 - 05:55 PM

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

### #2Brother Bob  Moderators

Posted 13 June 2013 - 06:24 PM

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

### #3Juliean  GDNet+

Posted 14 June 2013 - 06:12 AM

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

Posted 14 June 2013 - 06:18 AM

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

### #5Hodgman  Moderators

Posted 14 June 2013 - 06:28 AM

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.

### #6Carandiru  Members

Posted 14 June 2013 - 02:19 PM

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

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

### #7Juliean  GDNet+

Posted 15 June 2013 - 08:16 AM

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.

### #8Hodgman  Moderators

Posted 15 June 2013 - 09:33 AM

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: output=lhs;for(int i=0; i!=rhs; ++i) output <<= 1;
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.
for(i=0; ...; ++i) { mask = 1<<i; ... }
Becomes:

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

Edited by Hodgman, 15 June 2013 - 09:56 AM.

### #9Juliean  GDNet+

Posted 15 June 2013 - 10:08 AM

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, 15 June 2013 - 10:08 AM.

### #10Hodgman  Moderators

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; //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, 16 June 2013 - 12:14 AM.

### #11Juliean  GDNet+

Posted 17 June 2013 - 12:35 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.