]>
git.saurik.com Git - apple/icu.git/blob - icuSources/layout/LESwaps.h
4 * (C) Copyright IBM Corp. 1998-2004 - All Rights Reserved
16 * A convenience macro which invokes the swapWord member function
17 * from a concise call.
21 #if defined(U_IS_BIG_ENDIAN)
23 #define SWAPW(value) (value)
25 #define SWAPW(value) LESwaps::swapWord(value)
28 #define SWAPW(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapWord(value))
32 * A convenience macro which invokes the swapLong member function
33 * from a concise call.
37 #if defined(U_IS_BIG_ENDIAN)
39 #define SWAPL(value) (value)
41 #define SWAPL(value) LESwaps::swapLong(value)
44 #define SWAPL(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapLong(value))
48 * This class is used to access data which stored in big endian order
49 * regardless of the conventions of the platform. It has been designed
50 * to automatically detect the endian-ness of the platform, so that a
51 * compilation flag is not needed.
53 * All methods are static and inline in an attempt to induce the compiler
54 * to do most of the calculations at compile time.
58 class U_LAYOUT_API LESwaps
/* not : public UObject because all methods are static */ {
61 #if !defined(U_IS_BIG_ENDIAN)
63 * This method detects the endian-ness of the platform by
64 * casting a pointer to a word to a pointer to a byte. On
65 * big endian platforms the FF will be in the byte with the
66 * lowest address. On little endian platforms, the FF will
67 * be in the byte with the highest address.
69 * @return TRUE if the platform is big endian
73 static le_uint8
isBigEndian()
75 const le_uint16 word
= 0xFF00;
77 return *((le_uint8
*) &word
);
82 * This method does the byte swap required on little endian platforms
83 * to correctly access a (16-bit) word.
85 * @param value - the word to be byte swapped
87 * @return the byte swapped word
91 static le_uint16
swapWord(le_uint16 value
)
93 return (((le_uint8
) (value
>> 8)) | (value
<< 8));
97 * This method does the byte swapping required on little endian platforms
98 * to correctly access a (32-bit) long.
100 * @param value - the long to be byte swapped
102 * @return the byte swapped long
106 static le_uint32
swapLong(le_uint32 value
)
108 return swapWord((le_uint16
) (value
>> 16)) | (swapWord((le_uint16
) value
) << 16);
112 LESwaps() {} // private - forbid instantiation