2000-11-14 21:22:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @(#)LESwaps.h 1.3 00/03/15
|
|
|
|
*
|
2003-06-03 20:58:22 +00:00
|
|
|
* (C) Copyright IBM Corp. 1998-2003 - All Rights Reserved
|
2000-11-14 21:22:14 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __LESWAPS_H
|
|
|
|
#define __LESWAPS_H
|
|
|
|
|
|
|
|
#include "LETypes.h"
|
|
|
|
|
2001-10-16 00:39:01 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2000-11-14 21:22:14 +00:00
|
|
|
/**
|
2002-12-16 23:12:45 +00:00
|
|
|
* A convenience macro which invokes the swapWord member function
|
2000-11-14 21:22:14 +00:00
|
|
|
* from a concise call.
|
2002-08-14 00:18:01 +00:00
|
|
|
*
|
2003-11-26 00:19:39 +00:00
|
|
|
* @stable ICU 2.8
|
2000-11-14 21:22:14 +00:00
|
|
|
*/
|
2003-12-05 21:21:30 +00:00
|
|
|
#if defined(U_IS_BIG_ENDIAN)
|
|
|
|
#if U_IS_BIG_ENDIAN
|
|
|
|
#define SWAPW(value) (value)
|
|
|
|
#else
|
|
|
|
#define SWAPW(value) LESwaps::swapWord(value)
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define SWAPW(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapWord(value))
|
|
|
|
#endif
|
2002-12-16 23:12:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A convenience macro which invokes the swapLong member function
|
|
|
|
* from a concise call.
|
|
|
|
*
|
2003-11-26 00:19:39 +00:00
|
|
|
* @stable ICU 2.8
|
2002-12-16 23:12:45 +00:00
|
|
|
*/
|
2003-12-05 21:21:30 +00:00
|
|
|
#if defined(U_IS_BIG_ENDIAN)
|
|
|
|
#if U_IS_BIG_ENDIAN
|
|
|
|
#define SWAPL(value) (value)
|
|
|
|
#else
|
|
|
|
#define SWAPL(value) LESwaps::swapLong(value)
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define SWAPL(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapLong(value))
|
|
|
|
#endif
|
2000-11-14 21:22:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is used to access data which stored in big endian order
|
|
|
|
* regardless of the conventions of the platform. It has been designed
|
|
|
|
* to automatically detect the endian-ness of the platform, so that a
|
|
|
|
* compilation flag is not needed.
|
|
|
|
*
|
|
|
|
* All methods are static and inline in an attempt to induce the compiler
|
|
|
|
* to do most of the calculations at compile time.
|
2002-08-14 00:18:01 +00:00
|
|
|
*
|
2003-11-26 00:19:39 +00:00
|
|
|
* @stable ICU 2.8
|
2000-11-14 21:22:14 +00:00
|
|
|
*/
|
2003-05-22 21:56:57 +00:00
|
|
|
class U_LAYOUT_API LESwaps /* not : public UObject because all methods are static */ {
|
2000-11-14 21:22:14 +00:00
|
|
|
public:
|
|
|
|
|
2003-12-05 21:21:30 +00:00
|
|
|
#if !defined(U_IS_BIG_ENDIAN)
|
2002-03-27 00:34:56 +00:00
|
|
|
/**
|
|
|
|
* This method detects the endian-ness of the platform by
|
|
|
|
* casting a pointer to a word to a pointer to a byte. On
|
|
|
|
* big endian platforms the FF will be in the byte with the
|
|
|
|
* lowest address. On little endian platforms, the FF will
|
|
|
|
* be in the byte with the highest address.
|
|
|
|
*
|
2003-12-08 22:43:41 +00:00
|
|
|
* @return TRUE if the platform is big endian
|
2002-08-14 00:18:01 +00:00
|
|
|
*
|
2003-11-26 00:19:39 +00:00
|
|
|
* @stable ICU 2.8
|
2002-03-27 00:34:56 +00:00
|
|
|
*/
|
2003-12-05 21:21:30 +00:00
|
|
|
static le_uint8 isBigEndian()
|
2000-11-14 21:22:14 +00:00
|
|
|
{
|
2003-05-22 21:56:57 +00:00
|
|
|
const le_uint16 word = 0xFF00;
|
2000-11-14 21:22:14 +00:00
|
|
|
|
2002-03-27 00:34:56 +00:00
|
|
|
return *((le_uint8 *) &word);
|
2000-11-14 21:22:14 +00:00
|
|
|
};
|
2003-12-05 21:21:30 +00:00
|
|
|
#endif
|
2000-11-14 21:22:14 +00:00
|
|
|
|
2002-03-27 00:34:56 +00:00
|
|
|
/**
|
2002-08-14 00:18:01 +00:00
|
|
|
* This method does the byte swap required on little endian platforms
|
|
|
|
* to correctly access a (16-bit) word.
|
2002-03-27 00:34:56 +00:00
|
|
|
*
|
|
|
|
* @param value - the word to be byte swapped
|
|
|
|
*
|
|
|
|
* @return the byte swapped word
|
2002-08-14 00:18:01 +00:00
|
|
|
*
|
2003-11-26 00:19:39 +00:00
|
|
|
* @stable ICU 2.8
|
2002-03-27 00:34:56 +00:00
|
|
|
*/
|
2000-11-14 21:22:14 +00:00
|
|
|
static le_uint16 swapWord(le_uint16 value)
|
|
|
|
{
|
|
|
|
return (((le_uint8) (value >> 8)) | (value << 8));
|
|
|
|
};
|
|
|
|
|
2002-03-27 00:34:56 +00:00
|
|
|
/**
|
|
|
|
* This method does the byte swapping required on little endian platforms
|
2002-08-14 00:18:01 +00:00
|
|
|
* to correctly access a (32-bit) long.
|
2002-03-27 00:34:56 +00:00
|
|
|
*
|
|
|
|
* @param value - the long to be byte swapped
|
|
|
|
*
|
|
|
|
* @return the byte swapped long
|
2002-08-14 00:18:01 +00:00
|
|
|
*
|
2003-11-26 00:19:39 +00:00
|
|
|
* @stable ICU 2.8
|
2002-03-27 00:34:56 +00:00
|
|
|
*/
|
2000-11-14 21:22:14 +00:00
|
|
|
static le_uint32 swapLong(le_uint32 value)
|
|
|
|
{
|
|
|
|
return swapWord((le_uint16) (value >> 16)) | (swapWord((le_uint16) value) << 16);
|
|
|
|
};
|
2002-06-27 01:19:20 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
LESwaps() {} // private - forbid instantiation
|
2000-11-14 21:22:14 +00:00
|
|
|
};
|
|
|
|
|
2001-10-16 00:39:01 +00:00
|
|
|
U_NAMESPACE_END
|
2000-11-14 21:22:14 +00:00
|
|
|
#endif
|