From d5104b1ff6a597cdc822a8b3cf6d9329fc910ce3 Mon Sep 17 00:00:00 2001 From: James Zern Date: Thu, 3 Jul 2014 10:41:37 -0700 Subject: [PATCH] utils: add endian_inl.h moves the following to this header: - htole*() definitions from bit_writer.c - __BIG_ENDIAN__ fallback define from bit_reader_inl.h Change-Id: I7fff59543f08a70bf8f9ddac849b72ed290471b1 --- makefile.unix | 11 ++++++--- src/utils/Makefile.am | 1 + src/utils/bit_reader_inl.h | 7 +----- src/utils/bit_writer.c | 30 +---------------------- src/utils/endian_inl.h | 50 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 39 deletions(-) create mode 100644 src/utils/endian_inl.h diff --git a/makefile.unix b/makefile.unix index 7a2321e2..6d416413 100644 --- a/makefile.unix +++ b/makefile.unix @@ -246,10 +246,13 @@ all: ex $(EXTRA_EXAMPLES) $(EX_FORMAT_DEC_OBJS): %.o: %.h -# special dependencies for tree.c/vp8.c/bit_reader.c <-> bit_reader_inl.h -src/dec/tree.o: src/utils/bit_reader_inl.h -src/dec/vp8.o: src/utils/bit_reader_inl.h -src/utils/bit_reader.o: src/utils/bit_reader_inl.h +# special dependencies: +# tree.c/vp8.c/bit_reader.c <-> bit_reader_inl.h, endian_inl.h +# bit_writer.c <-> endian_inl.h +src/dec/tree.o: src/utils/bit_reader_inl.h src/utils/endian_inl.h +src/dec/vp8.o: src/utils/bit_reader_inl.h src/utils/endian_inl.h +src/utils/bit_reader.o: src/utils/bit_reader_inl.h src/utils/endian_inl.h +src/utils/bit_writer.o: src/utils/endian_inl.h %.o: %.c $(HDRS) $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ diff --git a/src/utils/Makefile.am b/src/utils/Makefile.am index a46a6dda..534cc836 100644 --- a/src/utils/Makefile.am +++ b/src/utils/Makefile.am @@ -13,6 +13,7 @@ COMMON_SOURCES += bit_reader.h COMMON_SOURCES += bit_reader_inl.h COMMON_SOURCES += color_cache.c COMMON_SOURCES += color_cache.h +COMMON_SOURCES += endian_inl.h COMMON_SOURCES += filters.c COMMON_SOURCES += filters.h COMMON_SOURCES += huffman.c diff --git a/src/utils/bit_reader_inl.h b/src/utils/bit_reader_inl.h index 5127ef41..56d33d80 100644 --- a/src/utils/bit_reader_inl.h +++ b/src/utils/bit_reader_inl.h @@ -17,6 +17,7 @@ #define WEBP_UTILS_BIT_READER_INL_H_ #include "./bit_reader.h" +#include "./endian_inl.h" #ifdef __cplusplus extern "C" { @@ -35,12 +36,6 @@ typedef uint16_t lbit_t; typedef uint8_t lbit_t; #endif -// some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__) -#if !defined(__BIG_ENDIAN__) && defined(__BYTE_ORDER__) && \ - (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) -#define __BIG_ENDIAN__ -#endif - // gcc 4.3 has builtin functions for swap32/swap64 #if defined(__GNUC__) && \ (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) diff --git a/src/utils/bit_writer.c b/src/utils/bit_writer.c index 0138b54d..23031f60 100644 --- a/src/utils/bit_writer.c +++ b/src/utils/bit_writer.c @@ -17,6 +17,7 @@ #include #include "./bit_writer.h" +#include "./endian_inl.h" #include "./utils.h" //------------------------------------------------------------------------------ @@ -204,35 +205,6 @@ void VP8BitWriterWipeOut(VP8BitWriter* const bw) { #define VP8L_WRITER_BITS (VP8L_WRITER_BYTES * 8) #define VP8L_WRITER_MAX_BITS (8 * (int)sizeof(vp8l_atype_t)) -// endian-specific htoleXX() definition -// TODO(skal): move this to config.h, and collect all the endian-related code -// in a proper .h file -#if defined(_WIN32) -#if !defined(_M_PPC) -#define htole32(x) (x) -#define htole16(x) (x) -#else // PPC is BIG_ENDIAN -#include -#define htole32(x) (_byteswap_ulong((unsigned long)(x))) -#define htole16(x) (_byteswap_ushort((unsigned short)(x))) -#endif // _M_PPC -#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || \ - defined(__DragonFly__) -#include -#elif defined(__APPLE__) -#include -#define htole32 OSSwapHostToLittleInt32 -#define htole16 OSSwapHostToLittleInt16 -#elif defined(__native_client__) && !defined(__GLIBC__) -// NaCl without glibc is assumed to be little-endian -#define htole32(x) (x) -#define htole16(x) (x) -#elif defined(__QNX__) -#include -#else // pretty much all linux and/or glibc -#include -#endif - // Returns 1 on success. static int VP8LBitWriterResize(VP8LBitWriter* const bw, size_t extra_size) { uint8_t* allocated_buf; diff --git a/src/utils/endian_inl.h b/src/utils/endian_inl.h new file mode 100644 index 00000000..ef68fb1d --- /dev/null +++ b/src/utils/endian_inl.h @@ -0,0 +1,50 @@ +// 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_ + +// some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__) +#if !defined(__BIG_ENDIAN__) && defined(__BYTE_ORDER__) && \ + (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#define __BIG_ENDIAN__ +#endif + +// endian-specific htoleXX() definition +// TODO(skal): add a test for htoleXX() in endian.h and others in autoconf or +// remove it and replace with a generic implementation. +#if defined(_WIN32) +#if !defined(_M_PPC) +#define htole32(x) (x) +#define htole16(x) (x) +#else // PPC is BIG_ENDIAN +#include +#define htole32(x) (_byteswap_ulong((unsigned long)(x))) +#define htole16(x) (_byteswap_ushort((unsigned short)(x))) +#endif // _M_PPC +#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || \ + defined(__DragonFly__) +#include +#elif defined(__APPLE__) +#include +#define htole32 OSSwapHostToLittleInt32 +#define htole16 OSSwapHostToLittleInt16 +#elif defined(__native_client__) && !defined(__GLIBC__) +// NaCl without glibc is assumed to be little-endian +#define htole32(x) (x) +#define htole16(x) (x) +#elif defined(__QNX__) +#include +#else // pretty much all linux and/or glibc +#include +#endif + +#endif // WEBP_UTILS_ENDIAN_INL_H_