Author Topic: A Little Help With C++ Define Statements  (Read 1818 times)

dedndave

  • Member
  • *****
  • Posts: 8752
  • Still using Abacus 2.0
    • DednDave
A Little Help With C++ Define Statements
« on: December 23, 2013, 03:56:53 AM »
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
Code: [Select]
#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
Code: [Select]
#define DIMAKEUSAGEDWORD(UsagePage, Usage)  \ ((DWORD)MAKELONG(Usage, UsagePage))
this one - i wouldn't get it, even if you took the "\" character out   :redface:
Code: [Select]
#define DISEQUENCE_COMPARE(dwSequence1, cmp, dwSequence2)  \((int)((dwSequence1) - (dwSequence2)) cmp 0)

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: A Little Help With C++ Define Statements
« Reply #1 on: December 23, 2013, 05:09:22 AM »
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.
Code: [Select]
#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

  • Member
  • *****
  • Posts: 8752
  • Still using Abacus 2.0
    • DednDave
Re: A Little Help With C++ Define Statements
« Reply #2 on: December 23, 2013, 05:43:35 AM »
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
Code: [Select]
    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

  • Member
  • ***
  • Posts: 370
Re: A Little Help With C++ Define Statements
« Reply #3 on: December 26, 2013, 04:36:02 PM »
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 \

Code: [Select]
#define DIDFT_MAKEINSTANCE(n)  ((WORD)(n) << 8)
It first cast n to a word, then shift it by 8 bits.

Code: [Select]
#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)

Code: [Select]
#define DISEQUENCE_COMPARE(dwSequence1, cmp, dwSequence2)  \((int)((dwSequence1) - (dwSequence2)) cmp 0)

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

Something like
Code: [Select]
if(DISEQUENCE_COMPARE(a,>,b)) Greater();
if(DISEQUENCE_COMPARE(a,<,b)) Lower();
if(DISEQUENCE_COMPARE(a,==,b)) Equal();

Would result in
Code: [Select]
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:
Code: [Select]
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

  • Member
  • *****
  • Posts: 8752
  • Still using Abacus 2.0
    • DednDave
Re: A Little Help With C++ Define Statements
« Reply #4 on: December 27, 2013, 02:50:41 AM »
no - it's used for buffer pointers, i think
in ASM, we probably wouldn't use a macro   :P