Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

define a macro that converts values between byte order

I want to create a macro that converts unsigned value conv to the opposite byte order of the current CPU. When not in a macro, it works well but let’s say I want to do that as a macro. The compiler throws me an implict decleration when I try to use the macro. Take in mind that cpu_to_be32 and friends is a kernel-space functions, afaik at least.

#define be32_or_le32(conv)  do { #ifdef __LITTLE_ENDIAN \
            conv = cpu_to_be32(conv); \
        #elif __BIG_ENDIAN \
            conv = cpu_to_le32(conv); \
        #endif } while (0)

u32 x = 0x12345678;

/* Convert the CPU's default byteorder to the opposite one */
be32_or_le32(x); /* Implict decleration */

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You cannot have preprocessor conditionals inside the macro expansion text.

Switch the structure to:

#ifdef __LITTLE_ENDIAN
#define be32_or_le32(conv) do { conv = cpu_to_be32(conv); } while (0)
#elif __BIG_ENDIAN
#define be32_or_le32(conv) do { conv = cpu_to_le32(conv); } while (0)
#endif
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading