Go to the documentation of this file.00001 #ifndef PLATFORM_HPP
00002 #define PLATFORM_HPP
00003
00004
00006
00007 #include "RakNetTypes.h"
00008
00009 #ifdef __APPLE__
00010 #include "TargetConditionals.h"
00011 #endif
00012
00013 #ifdef _WIN32_WCE //makslane
00014 # define _byteswap_ushort(s) ((s >> 8) | (s << 8))
00015 # define _byteswap_ulong(s) (byteswap_ushort(s&0xffff)<<16) | (byteswap_ushort(s>>16))
00016 # define byteswap_ushort _byteswap_ushort
00017 # define byteswap_ulong _byteswap_ulong
00018 #endif
00019
00020 #if !defined(LITTLE_ENDIAN) && !defined(__BIG_ENDIAN__)
00021 # if defined(__sparc) || defined(__sparc__) || defined(__powerpc__) || \
00022 defined(__ppc__) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || \
00023 defined(_M_PPC) || defined(_M_MPPC) || defined(_M_MRX000) || \
00024 defined(__POWERPC) || defined(m68k) || defined(powerpc) || \
00025 defined(sel) || defined(pyr) || defined(mc68000) || defined(is68k) || \
00026 defined(tahoe) || defined(ibm032) || defined(ibm370) || defined(MIPSEB) || \
00027 defined(__convex__) || defined(DGUX) || defined(hppa) || defined(apollo) || \
00028 defined(_CRAY) || defined(__hp9000) || defined(__hp9000s300) || defined(_AIX) || \
00029 defined(__AIX) || defined(__pyr__) || defined(hp9000s700) || defined(_IBMR2)
00030 # define __BIG_ENDIAN__
00031 # elif defined(__i386__) || defined(i386) || defined(intel) || defined(_M_IX86) || \
00032 defined(__alpha__) || defined(__alpha) || defined(__ia64) || defined(__ia64__) || \
00033 defined(_M_ALPHA) || defined(ns32000) || defined(__ns32000__) || defined(sequent) || \
00034 defined(MIPSEL) || defined(_MIPSEL) || defined(sun386) || defined(__sun386__) || \
00035 defined(x86_64) || defined(__x86_64) || (defined(TARGET_OS_IPHONE) && defined(TARGET_CPU_ARM))
00036 # define LITTLE_ENDIAN
00037 # else
00038 # error "Add your platform to the list"
00039 # endif
00040 #endif
00041
00042
00044
00045
00046 #if defined(__GNUC__)
00047 #ifndef PACKED
00048 # define PACKED __attribute__((__packed__))
00049 #endif
00050 # define ASSEMBLY_ATT_SYNTAX
00051 # define ASSEMBLY_BLOCK asm
00052 #elif defined(_WIN32)
00053 # define PACKED
00054 # define PRAGMA_PACK
00055 #ifndef DEBUG
00056 # define DEBUG _DEBUG
00057 #endif
00058 # if !defined(_M_X64)
00059 # define ASSEMBLY_INTEL_SYNTAX
00060 # define ASSEMBLY_BLOCK __asm
00061 # endif
00062 #elif defined(__BORLANDC__) // Borland
00063 # define ASSEMBLY_INTEL_SYNTAX
00064 # define ASSEMBLY_BLOCK _asm
00065 #else
00066 # error "Fix code for your compiler's packing syntax"
00067 #endif
00068
00069
00070 #define STRINGIZE(X) DO_STRINGIZE(X)
00071 #define DO_STRINGIZE(X) #X
00072
00073
00075
00076 struct u128 { uint32_t u[4]; };
00077
00078 union catFloat32 {
00079 float f;
00080 uint32_t i;
00081
00082 catFloat32(float n) { f = n; }
00083 catFloat32(uint32_t n) { i = n; }
00084 };
00085
00086
00088
00089
00090 enum QuadCoords
00091 {
00092 QUAD_UL,
00093 QUAD_LL,
00094 QUAD_LR,
00095 QUAD_UR,
00096 };
00097
00098
00100
00101 #ifndef ROL8
00102 #define ROL8(n, r) ( ((uint8_t)(n) << (r)) | ((uint8_t)(n) >> ( 8 - (r))) )
00103 #endif
00104 #ifndef ROR8
00105 #define ROR8(n, r) ( ((uint8_t)(n) >> (r)) | ((uint8_t)(n) << ( 8 - (r))) )
00106 #endif
00107 #ifndef ROL16
00108 #define ROL16(n, r) ( ((uint16_t)(n) << (r)) | ((uint16_t)(n) >> (16 - (r))) )
00109 #endif
00110 #ifndef ROR16
00111 #define ROR16(n, r) ( ((uint16_t)(n) >> (r)) | ((uint16_t)(n) << (16 - (r))) )
00112 #endif
00113 #ifndef ROL32
00114 #define ROL32(n, r) ( ((uint32_t)(n) << (r)) | ((uint32_t)(n) >> (32 - (r))) )
00115 #endif
00116 #ifndef ROR32
00117 #define ROR32(n, r) ( ((uint32_t)(n) >> (r)) | ((uint32_t)(n) << (32 - (r))) )
00118 #endif
00119 #ifndef ROL64
00120 #define ROL64(n, r) ( ((uint64_t)(n) << (r)) | ((uint64_t)(n) >> (64 - (r))) )
00121 #endif
00122 #ifndef ROR64
00123 #define ROR64(n, r) ( ((uint64_t)(n) >> (r)) | ((uint64_t)(n) << (64 - (r))) )
00124 #endif
00125
00127
00128 #ifndef BOSWAP16
00129 #define BOSWAP16(n) ROL16(n, 8)
00130 #endif
00131 #ifndef BOSWAP32
00132 #define BOSWAP32(n) ( (ROL32(n, 8) & 0x00ff00ff) | (ROL32(n, 24) & 0xff00ff00) )
00133 #endif
00134 #ifndef BOSWAP64
00135 #define BOSWAP64(n) ( ((uint64_t)BOSWAP32((uint32_t)n) << 32) | BOSWAP32((uint32_t)(n >> 32)) )
00136 #endif
00137
00139
00140 #define BITCLRHI8(reg, count) ((uint8_t)((uint8_t)(reg) << (count)) >> (count))
00141 #define BITCLRLO8(reg, count) ((uint8_t)((uint8_t)(reg) >> (count)) << (count))
00142 #define BITCLRHI16(reg, count) ((uint16_t)((uint16_t)(reg) << (count)) >> (count))
00143 #define BITCLRLO16(reg, count) ((uint16_t)((uint16_t)(reg) >> (count)) << (count))
00144 #define BITCLRHI32(reg, count) ((uint32_t)((uint32_t)(reg) << (count)) >> (count))
00145 #define BITCLRLO32(reg, count) ((uint32_t)((uint32_t)(reg) >> (count)) << (count))
00146
00147
00149
00150 #define AT_LEAST_2_BITS(n) ( (n) & ((n) - 1) )
00151 #define LEAST_SIGNIFICANT_BIT(n) ( (n) & -(n) )
00152 #define IS_POWER_OF_2(n) ( n && !AT_LEAST_2_BITS(n) )
00153
00154
00155
00156 #define CEIL_UNIT(n, width) ( ( (n) + (width) - 1 ) / (width) )
00157
00158 #define CEIL(n, width) ( CEIL_UNIT(n, width) * (width) )
00159
00160
00162
00163
00164
00165 #ifndef STRNCPY
00166 # define STRNCPY(dest, src, size) { strncpy(dest, src, size); (dest)[(size)-1] = '\0'; }
00167 #endif
00168
00169
00170 #define MEMCLR(dest, size) memset(dest, 0, size)
00171
00172
00173 #define OBJCLR(object) memset(&(object), 0, sizeof(object))
00174
00175
00177
00178 #if defined(_MSC_VER)
00179
00180 # include <cstdlib>
00181
00182 # pragma intrinsic(_lrotl)
00183 # pragma intrinsic(_lrotr)
00184 # pragma intrinsic(_byteswap_ushort)
00185 # pragma intrinsic(_byteswap_ulong)
00186
00187 # undef ROL32
00188 # undef ROR32
00189 # undef BOSWAP16
00190 # undef BOSWAP32
00191
00192 # define ROL32(n, r) _lrotl(n, r)
00193 # define ROR32(n, r) _lrotr(n, r)
00194 # define BOSWAP16(n) _byteswap_ushort(n)
00195 # define BOSWAP32(n) _byteswap_ulong(n)
00196
00197 #endif
00198
00199
00200
00201
00202
00203 #if defined(LITTLE_ENDIAN)
00204
00205 # define swapLE(n)
00206 # define getLE(n) (n)
00207 # define getLE16(n) (n)
00208 # define getLE32(n) (n)
00209 # define getLE64(n) (n)
00210
00211 inline void swapBE(uint16_t &n) { n = BOSWAP16(n); }
00212 inline void swapBE(uint32_t &n) { n = BOSWAP32(n); }
00213 inline void swapBE(uint64_t &n) { n = BOSWAP64(n); }
00214 inline uint16_t getBE(uint16_t n) { return BOSWAP16(n); }
00215 inline uint32_t getBE(uint32_t n) { return BOSWAP32(n); }
00216 inline uint64_t getBE(uint64_t n) { return BOSWAP64(n); }
00217 inline uint16_t getBE16(uint16_t n) { return BOSWAP16(n); }
00218 inline uint32_t getBE32(uint32_t n) { return BOSWAP32(n); }
00219 inline uint64_t getBE64(uint64_t n) { return BOSWAP64(n); }
00220 inline void swapBE(int16_t &n) { n = BOSWAP16((uint16_t)n); }
00221 inline void swapBE(int32_t &n) { n = BOSWAP32((uint32_t)n); }
00222 inline void swapBE(int64_t &n) { n = BOSWAP64((uint64_t)n); }
00223 inline int16_t getBE(int16_t n) { return BOSWAP16((uint16_t)n); }
00224 inline int32_t getBE(int32_t n) { return BOSWAP32((uint32_t)n); }
00225 inline int64_t getBE(int64_t n) { return BOSWAP64((uint64_t)n); }
00226
00227 inline float getBE(float n) {
00228 catFloat32 c = n;
00229 c.i = BOSWAP32(c.i);
00230 return c.f;
00231 }
00232
00233 #else
00234
00235 # define swapBE(n)
00236 # define getBE(n) (n)
00237 # define getBE16(n) (n)
00238 # define getBE32(n) (n)
00239 # define getBE64(n) (n)
00240
00241 inline void swapLE(uint16_t &n) { n = BOSWAP16(n); }
00242 inline void swapLE(uint32_t &n) { n = BOSWAP32(n); }
00243 inline void swapLE(uint64_t &n) { n = BOSWAP64(n); }
00244 inline uint16_t getLE(uint16_t n) { return BOSWAP16(n); }
00245 inline uint32_t getLE(uint32_t n) { return BOSWAP32(n); }
00246 inline uint64_t getLE(uint64_t n) { return BOSWAP64(n); }
00247 inline uint16_t getLE16(uint16_t n) { return BOSWAP16(n); }
00248 inline uint32_t getLE32(uint32_t n) { return BOSWAP32(n); }
00249 inline uint64_t getLE32(uint64_t n) { return BOSWAP64(n); }
00250 inline void swapLE(int16_t &n) { n = BOSWAP16((uint16_t)n); }
00251 inline void swapLE(int32_t &n) { n = BOSWAP32((uint32_t)n); }
00252 inline void swapLE(int64_t &n) { n = BOSWAP64((uint64_t)n); }
00253 inline int16_t getLE(int16_t n) { return BOSWAP16((uint16_t)n); }
00254 inline int32_t getLE(int32_t n) { return BOSWAP32((uint32_t)n); }
00255 inline int64_t getLE(int64_t n) { return BOSWAP64((uint64_t)n); }
00256
00257 inline float getLE(float n) {
00258 catFloat32 c = n;
00259 c.i = BOSWAP32(c.i);
00260 return c.f;
00261 }
00262
00263 #endif
00264
00265
00266 #endif // include guard