Mercurial > hg
comparison mercurial/util.h @ 19744:06badf7d10dc
util.h: getbe32() and putbe32() use intrinsic function to improve performance
Refer to https://bugzilla.mozilla.org/show_bug.cgi?id=351557. It can improve
byte-swapping performance by intrinsic function.
author | Wei, Elson <elson.wei@gmail.com> |
---|---|
date | Sat, 14 Sep 2013 11:22:34 +0800 |
parents | 7999f4fa155a |
children | 2c9645c0a582 |
comparison
equal
deleted
inserted
replaced
19743:fdd41257def8 | 19744:06badf7d10dc |
---|---|
149 | 149 |
150 #ifdef __linux | 150 #ifdef __linux |
151 #define inline __inline | 151 #define inline __inline |
152 #endif | 152 #endif |
153 | 153 |
154 #if defined(_MSC_VER) && (_MSC_VER >= 1300) | |
155 static inline uint32_t getbe32(const char *c) | |
156 { | |
157 return _byteswap_ulong(*(uint32_t *)c); | |
158 } | |
159 #elif GCC_VERSION >= 403 | |
160 static inline uint32_t getbe32(const char *c) | |
161 { | |
162 return __builtin_bswap32(*(uint32_t *)c); | |
163 } | |
164 #else | |
154 static inline uint32_t getbe32(const char *c) | 165 static inline uint32_t getbe32(const char *c) |
155 { | 166 { |
156 const unsigned char *d = (const unsigned char *)c; | 167 const unsigned char *d = (const unsigned char *)c; |
157 | 168 |
158 return ((d[0] << 24) | | 169 return ((d[0] << 24) | |
159 (d[1] << 16) | | 170 (d[1] << 16) | |
160 (d[2] << 8) | | 171 (d[2] << 8) | |
161 (d[3])); | 172 (d[3])); |
162 } | 173 } |
174 #endif | |
163 | 175 |
176 #if defined(_MSC_VER) && (_MSC_VER >= 1300) | |
177 static inline void putbe32(uint32_t x, char *c) | |
178 { | |
179 x = _byteswap_ulong(x); | |
180 *(uint32_t *)c = x; | |
181 } | |
182 #elif GCC_VERSION >= 403 | |
183 static inline void putbe32(uint32_t x, char *c) | |
184 { | |
185 x = __builtin_bswap32(x); | |
186 *(uint32_t *)c = x; | |
187 } | |
188 #else | |
164 static inline void putbe32(uint32_t x, char *c) | 189 static inline void putbe32(uint32_t x, char *c) |
165 { | 190 { |
166 c[0] = (x >> 24) & 0xff; | 191 c[0] = (x >> 24) & 0xff; |
167 c[1] = (x >> 16) & 0xff; | 192 c[1] = (x >> 16) & 0xff; |
168 c[2] = (x >> 8) & 0xff; | 193 c[2] = (x >> 8) & 0xff; |
169 c[3] = (x) & 0xff; | 194 c[3] = (x) & 0xff; |
170 } | 195 } |
196 #endif | |
171 | 197 |
172 #endif /* _HG_UTIL_H_ */ | 198 #endif /* _HG_UTIL_H_ */ |