Home | History | Annotate | Download | only in vpx_util
      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 VPX_UTIL_ENDIAN_INL_H_
     13 #define VPX_UTIL_ENDIAN_INL_H_
     14 
     15 #include <stdlib.h>
     16 #include "./vpx_config.h"
     17 #include "vpx/vpx_integer.h"
     18 
     19 #if defined(__GNUC__)
     20 #define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__)
     21 #define LOCAL_GCC_PREREQ(maj, min) (LOCAL_GCC_VERSION >= (((maj) << 8) | (min)))
     22 #else
     23 #define LOCAL_GCC_VERSION 0
     24 #define LOCAL_GCC_PREREQ(maj, min) 0
     25 #endif
     26 
     27 // handle clang compatibility
     28 #ifndef __has_builtin
     29 #define __has_builtin(x) 0
     30 #endif
     31 
     32 // some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
     33 #if !defined(WORDS_BIGENDIAN) &&                   \
     34     (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \
     35      (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
     36 #define WORDS_BIGENDIAN
     37 #endif
     38 
     39 #if defined(WORDS_BIGENDIAN)
     40 #define HToLE32 BSwap32
     41 #define HToLE16 BSwap16
     42 #define HToBE64(x) (x)
     43 #define HToBE32(x) (x)
     44 #else
     45 #define HToLE32(x) (x)
     46 #define HToLE16(x) (x)
     47 #define HToBE64(X) BSwap64(X)
     48 #define HToBE32(X) BSwap32(X)
     49 #endif
     50 
     51 #if LOCAL_GCC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16)
     52 #define HAVE_BUILTIN_BSWAP16
     53 #endif
     54 
     55 #if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap32)
     56 #define HAVE_BUILTIN_BSWAP32
     57 #endif
     58 
     59 #if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap64)
     60 #define HAVE_BUILTIN_BSWAP64
     61 #endif
     62 
     63 #if HAVE_MIPS32 && defined(__mips__) && !defined(__mips64) && \
     64     defined(__mips_isa_rev) && (__mips_isa_rev >= 2) && (__mips_isa_rev < 6)
     65 #define VPX_USE_MIPS32_R2
     66 #endif
     67 
     68 static INLINE uint16_t BSwap16(uint16_t x) {
     69 #if defined(HAVE_BUILTIN_BSWAP16)
     70   return __builtin_bswap16(x);
     71 #elif defined(_MSC_VER)
     72   return _byteswap_ushort(x);
     73 #else
     74   // gcc will recognize a 'rorw $8, ...' here:
     75   return (x >> 8) | ((x & 0xff) << 8);
     76 #endif  // HAVE_BUILTIN_BSWAP16
     77 }
     78 
     79 static INLINE uint32_t BSwap32(uint32_t x) {
     80 #if defined(VPX_USE_MIPS32_R2)
     81   uint32_t ret;
     82   __asm__ volatile(
     83       "wsbh   %[ret], %[x]          \n\t"
     84       "rotr   %[ret], %[ret],  16   \n\t"
     85       : [ret] "=r"(ret)
     86       : [x] "r"(x));
     87   return ret;
     88 #elif defined(HAVE_BUILTIN_BSWAP32)
     89   return __builtin_bswap32(x);
     90 #elif defined(__i386__) || defined(__x86_64__)
     91   uint32_t swapped_bytes;
     92   __asm__ volatile("bswap %0" : "=r"(swapped_bytes) : "0"(x));
     93   return swapped_bytes;
     94 #elif defined(_MSC_VER)
     95   return (uint32_t)_byteswap_ulong(x);
     96 #else
     97   return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24);
     98 #endif  // HAVE_BUILTIN_BSWAP32
     99 }
    100 
    101 static INLINE uint64_t BSwap64(uint64_t x) {
    102 #if defined(HAVE_BUILTIN_BSWAP64)
    103   return __builtin_bswap64(x);
    104 #elif defined(__x86_64__)
    105   uint64_t swapped_bytes;
    106   __asm__ volatile("bswapq %0" : "=r"(swapped_bytes) : "0"(x));
    107   return swapped_bytes;
    108 #elif defined(_MSC_VER)
    109   return (uint64_t)_byteswap_uint64(x);
    110 #else   // generic code for swapping 64-bit values (suggested by bdb@)
    111   x = ((x & 0xffffffff00000000ull) >> 32) | ((x & 0x00000000ffffffffull) << 32);
    112   x = ((x & 0xffff0000ffff0000ull) >> 16) | ((x & 0x0000ffff0000ffffull) << 16);
    113   x = ((x & 0xff00ff00ff00ff00ull) >> 8) | ((x & 0x00ff00ff00ff00ffull) << 8);
    114   return x;
    115 #endif  // HAVE_BUILTIN_BSWAP64
    116 }
    117 
    118 #endif  // VPX_UTIL_ENDIAN_INL_H_
    119