1 // Copyright 2014 Google Inc. All Rights Reserved.
2 //
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 // -----------------------------------------------------------------------------
9 //
10 // Endian related functions.
11 
12 #ifndef WEBP_UTILS_ENDIAN_INL_UTILS_H_
13 #define WEBP_UTILS_ENDIAN_INL_UTILS_H_
14 
15 #ifdef HAVE_CONFIG_H
16 #include "src/webp/config.h"
17 #endif
18 
19 #include "src/dsp/dsp.h"
20 #include "src/webp/types.h"
21 
22 #if defined(WORDS_BIGENDIAN)
23 #define HToLE32 BSwap32
24 #define HToLE16 BSwap16
25 #else
26 #define HToLE32(x) (x)
27 #define HToLE16(x) (x)
28 #endif
29 
30 #if !defined(HAVE_CONFIG_H)
31 #if LOCAL_GCC_PREREQ(4,8) || __has_builtin(__builtin_bswap16)
32 #define HAVE_BUILTIN_BSWAP16
33 #endif
34 #if LOCAL_GCC_PREREQ(4,3) || __has_builtin(__builtin_bswap32)
35 #define HAVE_BUILTIN_BSWAP32
36 #endif
37 #if LOCAL_GCC_PREREQ(4,3) || __has_builtin(__builtin_bswap64)
38 #define HAVE_BUILTIN_BSWAP64
39 #endif
40 #endif  // !HAVE_CONFIG_H
41 
BSwap16(uint16_t x)42 static WEBP_INLINE uint16_t BSwap16(uint16_t x) {
43 #if defined(HAVE_BUILTIN_BSWAP16)
44   return __builtin_bswap16(x);
45 #elif defined(_MSC_VER)
46   return _byteswap_ushort(x);
47 #else
48   // gcc will recognize a 'rorw $8, ...' here:
49   return (x >> 8) | ((x & 0xff) << 8);
50 #endif  // HAVE_BUILTIN_BSWAP16
51 }
52 
BSwap32(uint32_t x)53 static WEBP_INLINE uint32_t BSwap32(uint32_t x) {
54 #if defined(WEBP_USE_MIPS32_R2)
55   uint32_t ret;
56   __asm__ volatile (
57     "wsbh   %[ret], %[x]          \n\t"
58     "rotr   %[ret], %[ret],  16   \n\t"
59     : [ret]"=r"(ret)
60     : [x]"r"(x)
61   );
62   return ret;
63 #elif defined(HAVE_BUILTIN_BSWAP32)
64   return __builtin_bswap32(x);
65 #elif defined(__i386__) || defined(__x86_64__)
66   uint32_t swapped_bytes;
67   __asm__ volatile("bswap %0" : "=r"(swapped_bytes) : "0"(x));
68   return swapped_bytes;
69 #elif defined(_MSC_VER)
70   return (uint32_t)_byteswap_ulong(x);
71 #else
72   return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24);
73 #endif  // HAVE_BUILTIN_BSWAP32
74 }
75 
BSwap64(uint64_t x)76 static WEBP_INLINE uint64_t BSwap64(uint64_t x) {
77 #if defined(HAVE_BUILTIN_BSWAP64)
78   return __builtin_bswap64(x);
79 #elif defined(__x86_64__)
80   uint64_t swapped_bytes;
81   __asm__ volatile("bswapq %0" : "=r"(swapped_bytes) : "0"(x));
82   return swapped_bytes;
83 #elif defined(_MSC_VER)
84   return (uint64_t)_byteswap_uint64(x);
85 #else  // generic code for swapping 64-bit values (suggested by bdb@)
86   x = ((x & 0xffffffff00000000ull) >> 32) | ((x & 0x00000000ffffffffull) << 32);
87   x = ((x & 0xffff0000ffff0000ull) >> 16) | ((x & 0x0000ffff0000ffffull) << 16);
88   x = ((x & 0xff00ff00ff00ff00ull) >>  8) | ((x & 0x00ff00ff00ff00ffull) <<  8);
89   return x;
90 #endif  // HAVE_BUILTIN_BSWAP64
91 }
92 
93 #endif  // WEBP_UTILS_ENDIAN_INL_UTILS_H_
94