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