]>
git.saurik.com Git - apple/icu.git/blob - icuSources/layout/LESwaps.h
3 * @(#)LESwaps.h 1.3 00/03/15
5 * (C) Copyright IBM Corp. 1998-2003 - All Rights Reserved
17 * A convenience macro which invokes the swapWord member function
18 * from a concise call.
22 #define SWAPW(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapWord(value))
26 * A convenience macro which invokes the swapLong member function
27 * from a concise call.
31 #define SWAPL(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapLong(value))
34 * This class is used to access data which stored in big endian order
35 * regardless of the conventions of the platform. It has been designed
36 * to automatically detect the endian-ness of the platform, so that a
37 * compilation flag is not needed.
39 * All methods are static and inline in an attempt to induce the compiler
40 * to do most of the calculations at compile time.
44 class U_LAYOUT_API LESwaps
/* not : public UObject because all methods are static */ {
48 * This method detects the endian-ness of the platform by
49 * casting a pointer to a word to a pointer to a byte. On
50 * big endian platforms the FF will be in the byte with the
51 * lowest address. On little endian platforms, the FF will
52 * be in the byte with the highest address.
54 * @return true if the platform is big endian
58 static le_bool
isBigEndian()
60 const le_uint16 word
= 0xFF00;
62 return *((le_uint8
*) &word
);
66 * This method does the byte swap required on little endian platforms
67 * to correctly access a (16-bit) word.
69 * @param value - the word to be byte swapped
71 * @return the byte swapped word
75 static le_uint16
swapWord(le_uint16 value
)
77 return (((le_uint8
) (value
>> 8)) | (value
<< 8));
81 * This method does the byte swapping required on little endian platforms
82 * to correctly access a (32-bit) long.
84 * @param value - the long to be byte swapped
86 * @return the byte swapped long
90 static le_uint32
swapLong(le_uint32 value
)
92 return swapWord((le_uint16
) (value
>> 16)) | (swapWord((le_uint16
) value
) << 16);
96 LESwaps() {} // private - forbid instantiation