Skip to contentSkip to conversation

User level Macros for Endianess

DISCUSSION Jesper Skov 4 participants 4 messages Jan 12, 2000 2 messages not in the archive

lore
Share & tools
lore
#2 lore raw
In reply to a message not in the archive
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

"iweiny" == iweiny <[email protected]> writes:

iweiny> What Macros should I be using to key off of for this? I have iweiny> looked through /usr/include/endian.h and
iweiny> /usr/include/bits/endian.h. BYTE_ORDER (without the leading iweiny> underscores) is only defined if __USE_BSD is defined. BOTH iweiny> __BIG_ENDIAN and __LITTLE_ENDIAN are defined as are BIG_ENDIAN iweiny> and LITTLE_ENDIAN. I'm so confused?

#include <asm/byteorder.h>

then use __le32_to_cpu and friends (see
/usr/include/linux/byteorder/generic.h)

I'm not sure if that's the Proper Way(TM) to do it from user land though.

Jesper

** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/

#3 lore raw
Quoted 7 lines · Jesper Skov

#include <asm/byteorder.h>

then use __le32_to_cpu and friends (see
/usr/include/linux/byteorder/generic.h)

I'm not sure if that's the Proper Way(TM) to do it from user land though.

No, it's very different between application and kernel code (unfortunately since this forced me to write 2 very different vme.h include files for VME systems, it's not only a simple cp). Under Linux, asm/byteorder.h is the right way to get the correct macros, and either LITTLE_ENDIAN or BIG_ENDIAN is defined.

Quoted 1 line

From userland both are defined and then BYTE_ORDER is set to one of these

(strange architecture might even have a byte order which is neither like the PDP) so the check is #if __BYTE_ORDER==__BIG_ENDIAN ...

Gabriel.

** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/

#4 lore raw
In reply to a message not in the archive
Quoted 5 lines · [email protected] wrote:

What Macros should I be using to key off of for this? I have looked through /usr/include/endian.h and /usr/include/bits/endian.h. BYTE_ORDER (without the leading underscores) is only defined if __USE_BSD is defined. BOTH __BIG_ENDIAN and __LITTLE_ENDIAN are defined as are BIG_ENDIAN and LITTLE_ENDIAN. I'm so confused?

You can take advantage of the fact that "network byte order" is big-endian, allowing you to use the hton?() and ntoh?() macros. I realize that this is specific to a big-endian device (such as the camera interface you have) but this is certainly easier than shoehorning an existing project into the GNU autoconf model.

That said, you wouldn't want to use this hack for a new project -- an autoconf-style test would be cleaner (IMHO). I would welcome any suggestions, however.

Signature · 5 lines
Charles Lepple
[email protected]


** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
#5 lore raw
In reply to a message not in the archive
Quoted 3 lines · On Wed, 12 Jan 2000 [email protected] wrote:

Unfortunately I don't have an x86 Linux machine and he has been reluctant to change his code because he feels this is not the "proper" way to detect endianess across Linux's, Solaris, and possibly other UNIX's.

Well, if you really want it to be completely cross-platform, you could do something like:

int a = 0x33323130;
char *b, d[5];
int c;

for (b=(char *)&a, c=0; c<4; c++,b++)

    {
	d[c]=*b;
    }

d[5]=0;

I believe that the Linux macros do this same basic thing, if memory serves.... BTW, if you printf("%s", d) you'll get 3210 on big endian, 0123 on little. You could also do it as short integers instead of characters by subtracting 48 in the d[c]=*b line.

Just a sick thought. :-)

Later,
David

** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/