]>
git.proxmox.com Git - libgit2.git/blob - src/bswap.h
2 * Copyright (C) 2009-2011 the libgit2 contributors
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
11 * Default version that the compiler ought to optimize properly with
14 GIT_INLINE(uint32_t) default_swab32(uint32_t val
)
16 return (((val
& 0xff000000) >> 24) |
17 ((val
& 0x00ff0000) >> 8) |
18 ((val
& 0x0000ff00) << 8) |
19 ((val
& 0x000000ff) << 24));
24 GIT_INLINE(uint16_t) default_swab16(uint16_t val
)
26 return (((val
& 0xff00) >> 8) |
27 ((val
& 0x00ff) << 8));
32 #if defined(__GNUC__) && defined(__i386__)
34 #define bswap32(x) ({ \
36 if (__builtin_constant_p(x)) { \
37 __res = default_swab32(x); \
39 __asm__("bswap %0" : "=r" (__res) : "0" ((uint32_t)(x))); \
43 #define bswap16(x) ({ \
45 if (__builtin_constant_p(x)) { \
46 __res = default_swab16(x); \
48 __asm__("xchgb %b0,%h0" : "=q" (__res) : "0" ((uint16_t)(x))); \
52 #elif defined(__GNUC__) && defined(__x86_64__)
54 #define bswap32(x) ({ \
56 if (__builtin_constant_p(x)) { \
57 __res = default_swab32(x); \
59 __asm__("bswapl %0" : "=r" (__res) : "0" ((uint32_t)(x))); \
63 #define bswap16(x) ({ \
65 if (__builtin_constant_p(x)) { \
66 __res = default_swab16(x); \
68 __asm__("xchgb %b0,%h0" : "=Q" (__res) : "0" ((uint16_t)(x))); \
72 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
76 #define bswap32(x) _byteswap_ulong(x)
77 #define bswap16(x) _byteswap_ushort(x)
85 #define ntohl(x) bswap32(x)
86 #define htonl(x) bswap32(x)
94 #define ntohs(x) bswap16(x)
95 #define htons(x) bswap16(x)