1 // Copyright 2014 Google Inc. All Rights Reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
10 // Endian related functions.
12 #ifndef WEBP_UTILS_ENDIAN_INL_H_
13 #define WEBP_UTILS_ENDIAN_INL_H_
16 #include "../webp/config.h"
19 #include "../webp/types.h"
21 // some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
22 #if !defined(WORDS_BIGENDIAN) && \
23 (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \
24 (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
25 #define WORDS_BIGENDIAN
28 #if defined(WORDS_BIGENDIAN)
29 #define HToLE32 BSwap32
30 #define HToLE16 BSwap16
32 #define HToLE32(x) (x)
33 #define HToLE16(x) (x)
36 #if !defined(HAVE_CONFIG_H)
38 # define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__)
40 # define LOCAL_GCC_VERSION 0
44 # define LOCAL_CLANG_VERSION ((__clang_major__ << 8) | __clang_minor__)
46 # define LOCAL_CLANG_VERSION 0
49 // clang-3.3 and gcc-4.3 have builtin functions for swap32/swap64
50 #if LOCAL_GCC_VERSION >= 0x403 || LOCAL_CLANG_VERSION >= 0x303
51 #define HAVE_BUILTIN_BSWAP32
52 #define HAVE_BUILTIN_BSWAP64
54 // clang-3.3 and gcc-4.8 have a builtin function for swap16
55 #if LOCAL_GCC_VERSION >= 0x408 || LOCAL_CLANG_VERSION >= 0x303
56 #define HAVE_BUILTIN_BSWAP16
58 #endif // !HAVE_CONFIG_H
60 static WEBP_INLINE
uint16_t BSwap16(uint16_t x
) {
61 #if defined(HAVE_BUILTIN_BSWAP16)
62 return __builtin_bswap16(x
);
63 #elif defined(_MSC_VER)
64 return _byteswap_ushort(x
);
66 // gcc will recognize a 'rorw $8, ...' here:
67 return (x
>> 8) | ((x
& 0xff) << 8);
68 #endif // HAVE_BUILTIN_BSWAP16
71 static WEBP_INLINE
uint32_t BSwap32(uint32_t x
) {
72 #if defined(HAVE_BUILTIN_BSWAP32)
73 return __builtin_bswap32(x
);
74 #elif defined(__i386__) || defined(__x86_64__)
75 uint32_t swapped_bytes
;
76 __asm__
volatile("bswap %0" : "=r"(swapped_bytes
) : "0"(x
));
78 #elif defined(_MSC_VER)
79 return (uint32_t)_byteswap_ulong(x
);
81 return (x
>> 24) | ((x
>> 8) & 0xff00) | ((x
<< 8) & 0xff0000) | (x
<< 24);
82 #endif // HAVE_BUILTIN_BSWAP32
85 static WEBP_INLINE
uint64_t BSwap64(uint64_t x
) {
86 #if defined(HAVE_BUILTIN_BSWAP64)
87 return __builtin_bswap64(x
);
88 #elif defined(__x86_64__)
89 uint64_t swapped_bytes
;
90 __asm__
volatile("bswapq %0" : "=r"(swapped_bytes
) : "0"(x
));
92 #elif defined(_MSC_VER)
93 return (uint64_t)_byteswap_uint64(x
);
94 #else // generic code for swapping 64-bit values (suggested by bdb@)
95 x
= ((x
& 0xffffffff00000000ull
) >> 32) | ((x
& 0x00000000ffffffffull
) << 32);
96 x
= ((x
& 0xffff0000ffff0000ull
) >> 16) | ((x
& 0x0000ffff0000ffffull
) << 16);
97 x
= ((x
& 0xff00ff00ff00ff00ull
) >> 8) | ((x
& 0x00ff00ff00ff00ffull
) << 8);
99 #endif // HAVE_BUILTIN_BSWAP64
102 #endif // WEBP_UTILS_ENDIAN_INL_H_