Re: macro help



<inftoconc@xxxxxxxxx> a écrit dans le message de news:
1191144396.861742.54850@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi all,

I wrote a macro to read 8 bytes from a byte stream. Am not
sure if it is working ok. Can anyone please point out if there looks
to be a problem!

#define READ64(b) ( (uint64_t(*b)) << 56) + ((uint64_t(*(b+1))) << 48)
+ ((uint64_t(*(b+2))) << 40) +((uint64_t(*(b+3))) << 32)+((uint64_t(*(b
+4))) << 24) + (( uint64_t(*(b+5))) << 16) + (( uint64_t(*(b+6))) <<
8) + ( *(b+7))

Here b is a pointer to an unsigned char.

The macro does not fit on a single line, you must use \ to ensure that the 4
lines get pasted together by the preprocessor.

Your casts to uint64_t are incorrect for C: use (uint64_t)x, not uint64_t(x)

You MUST use parentheses around b in the body of the macro in case b is not
a primary expression.

This macro evaluates its argument multiple times, it MUST not be used with
an argument that has side-effects.

You SHOULD use a function for this, possibly an inline function.

You should document that it fetches a 64 bit value from the octet stream in
big endian order.

fixed macro:

#define READ64(b) (((uint64_t)(b)[0] << 56) | \
((uint64_t)(b)[1] << 48) | \
((uint64_t)(b)[2] << 40) | \
((uint64_t)(b)[3] << 32) | \
((uint64_t)(b)[4] << 24) | \
((uint64_t)(b)[5] << 16) | \
((uint64_t)(b)[6] << 8) | \
((uint64_t)(b)[7] << 0))

Function:

static inline uint64_t read64(unsigned char const *b) {
return ((uint64_t)b[0] << 56) | ((uint64_t)b[1] << 48) |
((uint64_t)b[2] << 40) | ((uint64_t)b[3] << 32) |
((uint64_t)b[4] << 24) | ((uint64_t)b[5] << 16) |
((uint64_t)b[6] << 8) | ((uint64_t)b[7] << 0);
}

If your compiler is not too smart, this might be better:

static inline uint64_t read64(unsigned char const *b) {
return ((uint64_t)(((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) |
((uint32_t)b[2] << 8) | b[3]) << 32) |
(((uint32_t)b[4] << 24) | ((uint32_t)b[5] << 16) |
((uint32_t)b[6] << 8) | b[7]);
}

--
Chqrlie.


.



Relevant Pages

  • Re: lisp idiom for processing each line in a file?
    ... Add a:STREAM keyword arg for a variable name to bind ... Have the macro test for whether the:KEY parameter ... a good compiler will optimize (FUNCALL #'IDENTITY FOO) ... (remf options:stream)) ...
    (comp.lang.lisp)
  • Re: ANSI C Challenge on readint
    ... If you can't type out 'for', you'll never make it as a programmer. ... Don't define a macro to do a standard subroutine's job. ... > * be possible preserve the state of stream for the use of scanf, ... "Makes hackers smile" makes hackers smile. ...
    (comp.lang.c)
  • Re: Java, was Re: Be afraid of XML
    ... You're not passing the stream to BODY-FUNCTION. ... you have to define a protocol ... the macro allows you to abstract away the ...
    (comp.lang.lisp)
  • Re: Reading data by words from a file in Linux system
    ... That is the reason for the getc macro, which is special in that it ... The getc function is equivalent to fgetc, ... input stream pointed to by stream. ... indicator for the stream is set and getc returns EOF. ...
    (comp.lang.c)
  • Re: Using Workbook.XMLImport
    ... the macro author sign the macro. ... user reduce their security setting, simply because I do not have experience ... obtaining a certificate. ... I will have to brush up on my stream construction (I have always had ...
    (microsoft.public.vsnet.vstools.office)