tool/byteorder.h
author Florian Pose <fp@igh-essen.com>
Thu, 02 Oct 2008 10:44:43 +0000
changeset 1245 9f52fc9562ae
parent 1142 59be91dfcbe1
permissions -rw-r--r--
Minor change.
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 * $Id$
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 ****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
#include <sys/types.h>
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
/*****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
#define swap16(x) \
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
        ((uint16_t)( \
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
        (((uint16_t)(x) & 0x00ffU) << 8) | \
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
        (((uint16_t)(x) & 0xff00U) >> 8) ))
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
#define swap32(x) \
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
        ((uint32_t)( \
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
        (((uint32_t)(x) & 0x000000ffUL) << 24) | \
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
        (((uint32_t)(x) & 0x0000ff00UL) <<  8) | \
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
        (((uint32_t)(x) & 0x00ff0000UL) >>  8) | \
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
        (((uint32_t)(x) & 0xff000000UL) >> 24) ))
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
#if __BYTE_ORDER == __LITTLE_ENDIAN
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
#define le16tocpu(x) x
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
#define le32tocpu(x) x
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
#define cputole16(x) x
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
#define cputole32(x) x
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
#elif __BYTE_ORDER == __BIG_ENDIAN
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
#define le16tocpu(x) swap16(x)
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
#define le32tocpu(x) swap32(x)
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#define cputole16(x) swap16(x)
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#define cputole32(x) swap32(x)
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
#endif
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
/****************************************************************************/