2014-07-03 10:41:37 -07:00
|
|
|
// Copyright 2014 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Endian related functions.
|
|
|
|
|
|
|
|
#ifndef WEBP_UTILS_ENDIAN_INL_H_
|
|
|
|
#define WEBP_UTILS_ENDIAN_INL_H_
|
|
|
|
|
2014-06-28 12:27:17 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "../webp/config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-07-03 11:21:24 -07:00
|
|
|
#include "../webp/types.h"
|
|
|
|
|
2014-07-03 10:41:37 -07:00
|
|
|
// some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
|
2014-06-28 12:27:17 -07:00
|
|
|
#if !defined(WORDS_BIGENDIAN) && \
|
2014-07-04 00:50:34 -07:00
|
|
|
(defined(__BIG_ENDIAN__) || defined(_M_PPC) || \
|
2014-06-28 12:27:17 -07:00
|
|
|
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
|
|
|
|
#define WORDS_BIGENDIAN
|
2014-07-03 10:41:37 -07:00
|
|
|
#endif
|
|
|
|
|
2014-07-04 00:50:34 -07:00
|
|
|
#if defined(WORDS_BIGENDIAN)
|
|
|
|
#define HToLE32 BSwap32
|
|
|
|
#define HToLE16 BSwap16
|
|
|
|
#else
|
|
|
|
#define HToLE32(x) (x)
|
|
|
|
#define HToLE16(x) (x)
|
2014-07-03 10:41:37 -07:00
|
|
|
#endif
|
|
|
|
|
2014-07-04 16:56:41 -07:00
|
|
|
#if !defined(HAVE_CONFIG_H)
|
|
|
|
#ifdef __GNUC__
|
|
|
|
# define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__)
|
|
|
|
#else
|
|
|
|
# define LOCAL_GCC_VERSION 0
|
|
|
|
#endif // __GNUC__
|
|
|
|
|
|
|
|
#ifdef __clang__
|
|
|
|
# define LOCAL_CLANG_VERSION ((__clang_major__ << 8) | __clang_minor__)
|
|
|
|
#else
|
|
|
|
# define LOCAL_CLANG_VERSION 0
|
|
|
|
#endif // __clang__
|
|
|
|
|
|
|
|
// clang-3.3 and gcc-4.3 have builtin functions for swap32/swap64
|
|
|
|
#if LOCAL_GCC_VERSION >= 0x403 || LOCAL_CLANG_VERSION >= 0x303
|
|
|
|
#define HAVE_BUILTIN_BSWAP32
|
|
|
|
#define HAVE_BUILTIN_BSWAP64
|
|
|
|
#endif
|
|
|
|
// clang-3.3 and gcc-4.8 have a builtin function for swap16
|
|
|
|
#if LOCAL_GCC_VERSION >= 0x408 || LOCAL_CLANG_VERSION >= 0x303
|
|
|
|
#define HAVE_BUILTIN_BSWAP16
|
2014-07-03 11:21:24 -07:00
|
|
|
#endif
|
2014-07-04 16:56:41 -07:00
|
|
|
#endif // !HAVE_CONFIG_H
|
2014-07-03 11:21:24 -07:00
|
|
|
|
2014-07-04 00:41:14 -07:00
|
|
|
static WEBP_INLINE uint16_t BSwap16(uint16_t x) {
|
2014-07-04 16:56:41 -07:00
|
|
|
#if defined(HAVE_BUILTIN_BSWAP16)
|
|
|
|
return __builtin_bswap16(x);
|
|
|
|
#elif defined(_MSC_VER)
|
2014-07-04 00:41:14 -07:00
|
|
|
return _byteswap_ushort(x);
|
|
|
|
#else
|
|
|
|
// gcc will recognize a 'rorw $8, ...' here:
|
|
|
|
return (x >> 8) | ((x & 0xff) << 8);
|
2014-07-04 16:56:41 -07:00
|
|
|
#endif // HAVE_BUILTIN_BSWAP16
|
2014-07-04 00:41:14 -07:00
|
|
|
}
|
|
|
|
|
2014-07-03 11:21:24 -07:00
|
|
|
static WEBP_INLINE uint32_t BSwap32(uint32_t x) {
|
2014-07-04 16:56:41 -07:00
|
|
|
#if defined(HAVE_BUILTIN_BSWAP32)
|
2014-07-03 11:21:24 -07:00
|
|
|
return __builtin_bswap32(x);
|
|
|
|
#elif defined(__i386__) || defined(__x86_64__)
|
|
|
|
uint32_t swapped_bytes;
|
|
|
|
__asm__ volatile("bswap %0" : "=r"(swapped_bytes) : "0"(x));
|
|
|
|
return swapped_bytes;
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
return (uint32_t)_byteswap_ulong(x);
|
|
|
|
#else
|
|
|
|
return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24);
|
2014-07-04 16:56:41 -07:00
|
|
|
#endif // HAVE_BUILTIN_BSWAP32
|
2014-07-03 11:21:24 -07:00
|
|
|
}
|
|
|
|
|
2014-07-03 11:21:24 -07:00
|
|
|
static WEBP_INLINE uint64_t BSwap64(uint64_t x) {
|
2014-07-04 16:56:41 -07:00
|
|
|
#if defined(HAVE_BUILTIN_BSWAP64)
|
2014-07-03 11:21:24 -07:00
|
|
|
return __builtin_bswap64(x);
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
uint64_t swapped_bytes;
|
|
|
|
__asm__ volatile("bswapq %0" : "=r"(swapped_bytes) : "0"(x));
|
|
|
|
return swapped_bytes;
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
return (uint64_t)_byteswap_uint64(x);
|
|
|
|
#else // generic code for swapping 64-bit values (suggested by bdb@)
|
|
|
|
x = ((x & 0xffffffff00000000ull) >> 32) | ((x & 0x00000000ffffffffull) << 32);
|
|
|
|
x = ((x & 0xffff0000ffff0000ull) >> 16) | ((x & 0x0000ffff0000ffffull) << 16);
|
|
|
|
x = ((x & 0xff00ff00ff00ff00ull) >> 8) | ((x & 0x00ff00ff00ff00ffull) << 8);
|
|
|
|
return x;
|
2014-07-04 16:56:41 -07:00
|
|
|
#endif // HAVE_BUILTIN_BSWAP64
|
2014-07-03 11:21:24 -07:00
|
|
|
}
|
|
|
|
|
2014-07-03 10:41:37 -07:00
|
|
|
#endif // WEBP_UTILS_ENDIAN_INL_H_
|