00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #import "objfw-defs.h"
00013
00014 #include <stddef.h>
00015 #include <stdint.h>
00016
00017 #ifdef __GNUC__
00018 # define OF_INLINE inline __attribute__((always_inline))
00019 # define OF_LIKELY(cond) __builtin_expect(!!(cond), 1)
00020 # define OF_UNLIKELY(cond) __builtin_expect(!!(cond), 0)
00021 #else
00022 # define OF_INLINE inline
00023 # define OF_LIKELY(cond) cond
00024 # define OF_UNLIKELY(cond) cond
00025 #endif
00026
00027
00028 #if __BIG_ENDIAN__ || __LITTLE_ENDIAN__
00029 # if __BIG_ENDIAN__ && __LITTLE_ENDIAN__
00030 # error __BIG_ENDIAN__ and __LITTLE_ENDIAN__ defined!
00031 # endif
00032 # undef OF_BIG_ENDIAN
00033 # if __BIG_ENDIAN__
00034 # define OF_BIG_ENDIAN
00035 # endif
00036 #endif
00037
00038 #ifdef __GNUC__
00039 # if defined(__amd64__) || defined(__x86_64__)
00040 # define OF_AMD64_ASM
00041 # elif defined(__i386__)
00042 # define OF_X86_ASM
00043 # elif defined(__ppc__) || defined(__PPC__)
00044 # define OF_PPC_ASM
00045 # elif defined(__arm__) || defined(__ARM__)
00046 # define OF_ARM_ASM
00047 # endif
00048 #endif
00049
00050 #ifndef _WIN32
00051 # define OF_PATH_DELIM '/'
00052 #else
00053 # define OF_PATH_DELIM '\\'
00054 #endif
00055
00056 static OF_INLINE uint16_t
00057 OF_BSWAP16_CONST(uint16_t i)
00058 {
00059 return (i & UINT16_C(0xFF00)) >> 8 |
00060 (i & UINT16_C(0x00FF)) << 8;
00061 }
00062
00063 static OF_INLINE uint32_t
00064 OF_BSWAP32_CONST(uint32_t i)
00065 {
00066 return (i & UINT32_C(0xFF000000)) >> 24 |
00067 (i & UINT32_C(0x00FF0000)) >> 8 |
00068 (i & UINT32_C(0x0000FF00)) << 8 |
00069 (i & UINT32_C(0x000000FF)) << 24;
00070 }
00071
00072 static OF_INLINE uint64_t
00073 OF_BSWAP64_CONST(uint64_t i)
00074 {
00075 return (i & UINT64_C(0xFF00000000000000)) >> 56 |
00076 (i & UINT64_C(0x00FF000000000000)) >> 40 |
00077 (i & UINT64_C(0x0000FF0000000000)) >> 24 |
00078 (i & UINT64_C(0x000000FF00000000)) >> 8 |
00079 (i & UINT64_C(0x00000000FF000000)) << 8 |
00080 (i & UINT64_C(0x0000000000FF0000)) << 24 |
00081 (i & UINT64_C(0x000000000000FF00)) << 40 |
00082 (i & UINT64_C(0x00000000000000FF)) << 56;
00083 }
00084
00085 static OF_INLINE uint16_t
00086 OF_BSWAP16_NONCONST(uint16_t i)
00087 {
00088 #if defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
00089 __asm__ ("xchgb %h0, %b0" : "=Q"(i) : "0"(i));
00090 #elif defined(OF_PPC_ASM)
00091 __asm__ ("lhbrx %0, 0, %1" : "=r"(i) : "r"(&i), "m"(i));
00092 #elif defined(OF_ARM_ASM)
00093 __asm__ ("rev16 %0, %0" : "=r"(i) : "0"(i));
00094 #else
00095 i = (i & UINT16_C(0xFF00)) >> 8 |
00096 (i & UINT16_C(0x00FF)) << 8;
00097 #endif
00098 return i;
00099 }
00100
00101 static OF_INLINE uint32_t
00102 OF_BSWAP32_NONCONST(uint32_t i)
00103 {
00104 #if defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
00105 __asm__ ("bswap %0" : "=q"(i) : "0"(i));
00106 #elif defined(OF_PPC_ASM)
00107 __asm__ ("lwbrx %0, 0, %1" : "=r"(i) : "r"(&i), "m"(i));
00108 #elif defined(OF_ARM_ASM)
00109 __asm__ ("rev %0, %0" : "=r"(i) : "0"(i));
00110 #else
00111 i = (i & UINT32_C(0xFF000000)) >> 24 |
00112 (i & UINT32_C(0x00FF0000)) >> 8 |
00113 (i & UINT32_C(0x0000FF00)) << 8 |
00114 (i & UINT32_C(0x000000FF)) << 24;
00115 #endif
00116 return i;
00117 }
00118
00119 static OF_INLINE uint64_t
00120 OF_BSWAP64_NONCONST(uint64_t i)
00121 {
00122 #if defined(OF_AMD64_ASM)
00123 __asm__ ("bswap %0" : "=r"(i) : "0"(i));
00124 #elif defined(OF_X86_ASM)
00125 __asm__ ("bswap %%eax\n\t"
00126 "bswap %%edx\n\t"
00127 "xchgl %%eax, %%edx" : "=A"(i) : "0"(i));
00128 #else
00129 i = (uint64_t)OF_BSWAP32_NONCONST(i & 0xFFFFFFFF) << 32 |
00130 OF_BSWAP32_NONCONST(i >> 32);
00131 #endif
00132 return i;
00133 }
00134
00135 #ifdef __GNUC__
00136 # define OF_BSWAP16(i) \
00137 (__builtin_constant_p(i) ? OF_BSWAP16_CONST(i) : OF_BSWAP16_NONCONST(i))
00138 # define OF_BSWAP32(i) \
00139 (__builtin_constant_p(i) ? OF_BSWAP32_CONST(i) : OF_BSWAP32_NONCONST(i))
00140 # define OF_BSWAP64(i) \
00141 (__builtin_constant_p(i) ? OF_BSWAP64_CONST(i) : OF_BSWAP64_NONCONST(i))
00142 #else
00143 # define OF_BSWAP16(i) OF_BSWAP16_CONST(i)
00144 # define OF_BSWAP32(i) OF_BSWAP32_CONST(i)
00145 # define OF_BSWAP64(i) OF_BSWAP64_CONST(i)
00146 #endif
00147
00148 static OF_INLINE void
00149 OF_BSWAP32_V(uint32_t *buf, size_t len)
00150 {
00151 while (len--) {
00152 *buf = OF_BSWAP32(*buf);
00153 buf++;
00154 }
00155 }
00156
00157 #ifdef OF_BIG_ENDIAN
00158 # define OF_BSWAP16_IF_BE(i) OF_BSWAP16(i)
00159 # define OF_BSWAP32_IF_BE(i) OF_BSWAP32(i)
00160 # define OF_BSWAP64_IF_BE(i) OF_BSWAP64(i)
00161 # define OF_BSWAP16_IF_LE(i) i
00162 # define OF_BSWAP32_IF_LE(i) i
00163 # define OF_BSWAP64_IF_LE(i) i
00164 # define OF_BSWAP32_V_IF_BE(buf, len) OF_BSWAP32_V(buf, len)
00165 #else
00166 # define OF_BSWAP16_IF_BE(i) i
00167 # define OF_BSWAP32_IF_BE(i) i
00168 # define OF_BSWAP64_IF_BE(i) i
00169 # define OF_BSWAP16_IF_LE(i) OF_BSWAP16(i)
00170 # define OF_BSWAP32_IF_LE(i) OF_BSWAP32(i)
00171 # define OF_BSWAP64_IF_LE(i) OF_BSWAP64(i)
00172 # define OF_BSWAP32_V_IF_BE(buf, len)
00173 #endif
00174
00175 #define OF_ROL(val, bits) \
00176 (((val) << ((bits) % (sizeof(val) * 8))) | \
00177 (val) >> (sizeof(val) * 8 - ((bits) % (sizeof(val) * 8))))
00178
00179 #define OF_HASH_INIT(hash) hash = 0
00180 #define OF_HASH_ADD(hash, byte) \
00181 { \
00182 hash += byte; \
00183 hash += (hash << 10); \
00184 hash ^= (hash >> 6); \
00185 }
00186 #define OF_HASH_FINALIZE(hash) \
00187 { \
00188 hash += (hash << 3); \
00189 hash ^= (hash >> 11); \
00190 hash += (hash << 15); \
00191 }