News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

A Little Help With C++ Define Statements

Started by dedndave, December 23, 2013, 03:56:53 AM

Previous topic - Next topic

dedndave

how to go from "i get it" to "i am totally lost", in 3 easy steps   :P

this one, i get - you shift "n" left by 8 bits, then cast it to a word
#define DIDFT_MAKEINSTANCE(n)  ((WORD)(n) << 8)

i almost get this one - i use the MAKELONG macro, and cast the result to a dword
what i don't get is the "\" - i guess it's not an operator, but a puncuator
the only reference i can find is where the "\" character is used for escaped characters
that doesn't seem valid, here
#define DIMAKEUSAGEDWORD(UsagePage, Usage)  \ ((DWORD)MAKELONG(Usage, UsagePage))

this one - i wouldn't get it, even if you took the "\" character out   :redface:
#define DISEQUENCE_COMPARE(dwSequence1, cmp, dwSequence2)  \((int)((dwSequence1) - (dwSequence2)) cmp 0)

MichaelW

The backslash is apparently a line continuation character:

http://msdn.microsoft.com/en-US/library/teas0593(v=vs.80).aspx

You would think I could remember that, having coded this ~4 years ago.

#define CTR_BEGIN( n, loop_count, priority_class )            \
    _ctr_overhead_ = 2000000000;                              \
    ctr_cycles = 2000000000;                                  \
    _ctr_loopcount_ = loop_count;                             \
    SetPriorityClass( GetCurrentProcess(), priority_class );  \
    _ctr_warmup_();                                           \
    Sleep(0);                                                 \
    _ctr_loopcounter_ = 0;                                    \
    __asm{ align 16 }                                         \
  R##n:                                                       \
    _ctr_code1_();                                            \
    _ctr_code2_();                                            \
    if( _ctr_tsc2_ - _ctr_tsc1_ < _ctr_overhead_ )            \
        _ctr_overhead_ = _ctr_tsc2_ - _ctr_tsc1_;             \
    if( _ctr_loopcounter_++ < _ctr_loopcount_ )               \
        goto R##n;                                            \
    Sleep(0);                                                 \
    _ctr_loopcounter_ = 0;                                    \
    __asm{ align 16 }                                         \
  L##n:                                                       \
    _ctr_code1_();

#define CTR_END( n )                                          \
    _ctr_code2_();                                            \
    if( _ctr_tsc2_ - _ctr_tsc1_ < ctr_cycles )                \
        ctr_cycles = _ctr_tsc2_ - _ctr_tsc1_;                 \
    if( _ctr_loopcounter_++ < _ctr_loopcount_ )               \
        goto L##n;                                            \
    ctr_cycles -= _ctr_overhead_;                             \
    SetPriorityClass( GetCurrentProcess(), NORMAL_PRIORITY_CLASS );

Well Microsoft, here's another nice mess you've gotten us into.

dedndave

thanks, Michael   :t

that makes sense
on the msdn pages, it shows up on a single line, which was confusing me
but, when i look at dinput8.h, you are right - those 2 macros are split-line

i found some references to the last one
    if( DISEQUENCE_COMPARE( sequence1, >, sequence2 ) )
    if( DISEQUENCE_COMPARE( sequence1, <, sequence2 ) )


that helps a little - the "cmp" argument is a placeholder for a comparison operator

xanatose

Interesting.

As mentioned the \ is the line continuation operator. However I didn't know it could be used without problems on a single line. I guess the compiler just ignores the \


#define DIDFT_MAKEINSTANCE(n)  ((WORD)(n) << 8)

It first cast n to a word, then shift it by 8 bits.


#define DIMAKEUSAGEDWORD(UsagePage, Usage)  \ ((DWORD)MAKELONG(Usage, UsagePage))


Creates a long from UsagePage (high word) and Usage (low word) then cast it to an unsigned __int32 (DWORD)


#define DISEQUENCE_COMPARE(dwSequence1, cmp, dwSequence2)  \((int)((dwSequence1) - (dwSequence2)) cmp 0)


Substract dwSequence2 from dwSequence2 and then do a comparison with zero.

Something like

if(DISEQUENCE_COMPARE(a,>,b)) Greater();
if(DISEQUENCE_COMPARE(a,<,b)) Lower();
if(DISEQUENCE_COMPARE(a,==,b)) Equal();


Would result in

if(((a) - (b)) > 0) Greater();
if(((a) - (b)) < 0) Lower();
if(((a) - (b)) == 0) Equal();


But it begs the question of why they choose to use a macro instead of simply:

if(a > b) Greater();
if(a < b) Lower();
if(a == b) Equal();


My guess is that the macro is used in some form of automatic code generation.

dedndave

no - it's used for buffer pointers, i think
in ASM, we probably wouldn't use a macro   :P