From fcafdfda28fd0e84e5f98fa2ae2dcf091e1ccc39 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Sat, 6 Feb 2016 09:47:10 -0600 Subject: [PATCH 1/1] Merge compiler-gcc.h into compiler.h Compiling with MSVC or another non-GCC-like compiler will be harder than previously thought. Since it's not currently a goal, simplify things by just merging compiler-gcc.h into compiler.h. --- Makefile.am | 1 - include/wimlib/compiler-gcc.h | 81 ---------------- include/wimlib/compiler.h | 168 +++++++++++++++++++++++----------- 3 files changed, 115 insertions(+), 135 deletions(-) delete mode 100644 include/wimlib/compiler-gcc.h diff --git a/Makefile.am b/Makefile.am index d80bb7a6..26bf6606 100644 --- a/Makefile.am +++ b/Makefile.am @@ -104,7 +104,6 @@ libwim_la_SOURCES = \ include/wimlib/capture.h \ include/wimlib/case.h \ include/wimlib/compiler.h \ - include/wimlib/compiler-gcc.h \ include/wimlib/compressor_ops.h \ include/wimlib/compress_common.h \ include/wimlib/chunk_compressor.h \ diff --git a/include/wimlib/compiler-gcc.h b/include/wimlib/compiler-gcc.h deleted file mode 100644 index 3a9d8e7e..00000000 --- a/include/wimlib/compiler-gcc.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * compiler-gcc.h - * - * The author dedicates this file to the public domain. - * You can do whatever you want with this file. - */ - -#ifndef _WIMLIB_COMPILER_GCC_H -#define _WIMLIB_COMPILER_GCC_H - -#ifdef __WIN32__ -# define WIMLIBAPI __declspec(dllexport) -#else -# define WIMLIBAPI __attribute__((visibility("default"))) -#endif - -#define _packed_attribute __attribute__((packed)) -#define _aligned_attribute(n) __attribute__((aligned(n))) -#define _may_alias_attribute __attribute__((may_alias)) -#define likely(expr) __builtin_expect(!!(expr), 1) -#define unlikely(expr) __builtin_expect(!!(expr), 0) -#define prefetchr(addr) __builtin_prefetch((addr), 0) -#define prefetchw(addr) __builtin_prefetch((addr), 1) -#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4) -# define _cold_attribute __attribute__((cold)) -#endif -#define _malloc_attribute __attribute__((malloc)) -#define inline inline __attribute__((always_inline)) -#define noinline __attribute__((noinline)) - -/* Newer gcc supports __BYTE_ORDER__. Older gcc doesn't. */ -#ifdef __BYTE_ORDER__ -# define CPU_IS_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) -#elif defined(HAVE_CONFIG_H) -# include "config.h" -# ifdef WORDS_BIGENDIAN -# define CPU_IS_BIG_ENDIAN 1 -# else -# define CPU_IS_BIG_ENDIAN 0 -# endif -#endif - -#if defined(__x86_64__) || defined(__i386__) -# define UNALIGNED_ACCESS_SPEED 3 -#elif defined(__ARM_FEATURE_UNALIGNED) && (__ARM_FEATURE_UNALIGNED == 1) -# define UNALIGNED_ACCESS_SPEED 2 -#else -# define UNALIGNED_ACCESS_SPEED 0 -#endif - -#define typeof __typeof__ - -#ifndef min -# define min(a, b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \ - (_a < _b) ? _a : _b; }) -#endif - -#ifndef max -# define max(a, b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \ - (_a > _b) ? _a : _b; }) -#endif - -#ifndef swap -# define swap(a, b) ({ typeof(a) _a = (a); (a) = (b); (b) = _a; }) -#endif - -#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) -# define compiler_bswap32 __builtin_bswap32 -# define compiler_bswap64 __builtin_bswap64 -#endif - -#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) -# define compiler_bswap16 __builtin_bswap16 -#endif - -#define compiler_fls32(n) (31 - __builtin_clz(n)) -#define compiler_fls64(n) (63 - __builtin_clzll(n)) -#define compiler_ffs32(n) __builtin_ctz(n) -#define compiler_ffs64(n) __builtin_ctzll(n) - -#endif /* _WIMLIB_COMPILER_GCC_H */ diff --git a/include/wimlib/compiler.h b/include/wimlib/compiler.h index 222f889b..53393039 100644 --- a/include/wimlib/compiler.h +++ b/include/wimlib/compiler.h @@ -1,6 +1,8 @@ /* * compiler.h * + * Compiler-specific definitions. Currently, only GCC and clang are supported. + * * The author dedicates this file to the public domain. * You can do whatever you want with this file. */ @@ -8,89 +10,149 @@ #ifndef _WIMLIB_COMPILER_H #define _WIMLIB_COMPILER_H -#ifdef __GNUC__ -# include "wimlib/compiler-gcc.h" -#else -# error "Unrecognized compiler. Please add a header file for your compiler." -#endif +/* Is the compiler GCC of the specified version or later? This always returns + * false for clang, since clang is "frozen" at GNUC 4.2. The __has_* + * feature-test macros should be used to detect clang functionality instead. */ +#define GCC_PREREQ(major, minor) \ + (!defined(__clang__) && !defined(__INTEL_COMPILER) && \ + (__GNUC__ > major || \ + (__GNUC__ == major && __GNUC_MINOR__ >= minor))) -#ifndef WIMLIBAPI -# define WIMLIBAPI +/* Feature-test macros defined by recent versions of clang. */ +#ifndef __has_attribute +# define __has_attribute(attribute) 0 #endif - -#ifndef _packed_attribute -# error "missing required definition of _packed_attribute" +#ifndef __has_feature +# define __has_feature(feature) 0 #endif - -#ifndef _aligned_attribute -# error "missing required definition of _aligned_attribute" +#ifndef __has_builtin +# define __has_builtin(builtin) 0 #endif -#ifndef _may_alias_attribute -# error "missing required definition of _may_alias_attribute" +/* Declare that the annotated function should be exported from the shared + * library (or DLL). */ +#ifdef __WIN32__ +# define WIMLIBAPI __declspec(dllexport) +#else +# define WIMLIBAPI __attribute__((visibility("default"))) #endif -#ifndef likely -# define likely(expr) (expr) -#endif +/* Declare that the annotated function should be inlined. Currently, we force + * the compiler to honor this because we use 'inline' in highly tuned code, e.g. + * compression codecs. */ +#define inline inline __attribute__((always_inline)) -#ifndef unlikely -# define unlikely(expr) (expr) -#endif +/* Declare that the annotated function should *not* be inlined. */ +#define noinline __attribute__((noinline)) -/* prefetchr() - prefetch into L1 cache for read */ -#ifndef prefetchr -# define prefetchr(addr) -#endif +/* Functionally the same as 'noinline', but documents that the reason for not + * inlining is to prevent the annotated function from being inlined into a + * recursive function, thereby increasing its stack usage. */ +#define noinline_for_stack noinline -/* prefetchw() - prefetch into L1 cache for write */ -#ifndef prefetchw -# define prefetchw(addr) -#endif +/* Hint that the expression is usually true. */ +#define likely(expr) __builtin_expect(!!(expr), 1) + +/* Hint that the expression is usually false. */ +#define unlikely(expr) __builtin_expect(!!(expr), 0) + +/* Prefetch into L1 cache for read. */ +#define prefetchr(addr) __builtin_prefetch((addr), 0) + +/* Prefetch into L1 cache for write. */ +#define prefetchw(addr) __builtin_prefetch((addr), 1) + +/* Declare that the members of the annotated struct are tightly packed, and the + * struct itself may be misaligned. */ +#define _packed_attribute __attribute__((packed)) -#ifndef _cold_attribute +/* Declare that the annotated variable, or variables of the annotated type, are + * to be aligned on n-byte boundaries. */ +#define _aligned_attribute(n) __attribute__((aligned(n))) + +/* Declare that pointers to the annotated type may alias other pointers. */ +#define _may_alias_attribute __attribute__((may_alias)) + +/* Hint that the annotated function is rarely called. */ +#if GCC_PREREQ(4, 4) || __has_attribute(cold) +# define _cold_attribute __attribute__((cold)) +#else # define _cold_attribute #endif -#ifndef _malloc_attribute -# define _malloc_attribute -#endif +/* Hint that the annotated function is malloc-like: any non-null pointer it + * returns will not alias any pointer previously in use by the program. */ +#define _malloc_attribute __attribute__((malloc)) -#ifndef _format_attribute -# define _format_attribute(type, format_str, format_start) +/* TODO: _format_attribute is currently ignored. */ +#define _format_attribute(type, format_str, format_start) + +/* Endianness definitions. Either CPU_IS_BIG_ENDIAN or CPU_IS_LITTLE_ENDIAN is + * set to 1. The other is set to 0. Note that newer gcc supports + * __BYTE_ORDER__ for easily determining the endianness; older gcc doesn't. In + * the latter case we fall back to a configure-time check. */ +#ifdef __BYTE_ORDER__ +# define CPU_IS_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#elif defined(HAVE_CONFIG_H) +# include "config.h" +# ifdef WORDS_BIGENDIAN +# define CPU_IS_BIG_ENDIAN 1 +# else +# define CPU_IS_BIG_ENDIAN 0 +# endif #endif +#define CPU_IS_LITTLE_ENDIAN (!CPU_IS_BIG_ENDIAN) -#ifndef noinline -# define noinline +#if defined(__x86_64__) || defined(__i386__) +# define UNALIGNED_ACCESS_SPEED 3 +#elif defined(__ARM_FEATURE_UNALIGNED) && (__ARM_FEATURE_UNALIGNED == 1) +# define UNALIGNED_ACCESS_SPEED 2 +#else +# define UNALIGNED_ACCESS_SPEED 0 #endif -/* Same as 'noinline', but 'noinline_for_stack' documents that 'noinline' is - * being used to prevent the annotated function from being inlined into a - * recursive function and increasing its stack usage. */ -#define noinline_for_stack noinline +/* Get the type of the specified expression. */ +#define typeof __typeof__ -#ifndef CPU_IS_BIG_ENDIAN -# error "missing required endianness definition" +/* Get the minimum of two variables, without multiple evaluation. */ +#ifndef min +# define min(a, b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \ + (_a < _b) ? _a : _b; }) #endif -#define CPU_IS_LITTLE_ENDIAN (!CPU_IS_BIG_ENDIAN) +/* Get the maximum of two variables, without multiple evaluation. */ +#ifndef max +# define max(a, b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \ + (_a > _b) ? _a : _b; }) +#endif -#ifndef UNALIGNED_ACCESS_SPEED -# define UNALIGNED_ACCESS_SPEED 0 +/* Swap the values of two variables, without multiple evaluation. */ +#ifndef swap +# define swap(a, b) ({ typeof(a) _a = (a); (a) = (b); (b) = _a; }) #endif -#define UNALIGNED_ACCESS_IS_ALLOWED (UNALIGNED_ACCESS_SPEED >= 1) -#define UNALIGNED_ACCESS_IS_FAST (UNALIGNED_ACCESS_SPEED >= 2) -#define UNALIGNED_ACCESS_IS_VERY_FAST (UNALIGNED_ACCESS_SPEED >= 3) +/* (Optional) Efficiently swap the bytes of a 16-bit integer. */ +#if GCC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16) +# define compiler_bswap16 __builtin_bswap16 +#endif -#ifndef typeof -# error "missing required definition of typeof" +/* (Optional) Efficiently swap the bytes of a 32-bit integer. */ +#if GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap32) +# define compiler_bswap32 __builtin_bswap32 #endif -#if !defined(min) || !defined(max) || !defined(swap) -# error "missing required definitions of min(), max(), and swap() macros" +/* (Optional) Efficiently swap the bytes of a 64-bit integer. */ +#if GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap64) +# define compiler_bswap64 __builtin_bswap64 #endif +/* (Optional) Find Last Set bit and Find First Set bit macros. */ +#define compiler_fls32(n) (31 - __builtin_clz(n)) +#define compiler_fls64(n) (63 - __builtin_clzll(n)) +#define compiler_ffs32(n) __builtin_ctz(n) +#define compiler_ffs64(n) __builtin_ctzll(n) + +/* Optional definitions for checking with 'sparse'. */ #ifdef __CHECKER__ # define _bitwise_attr __attribute__((bitwise)) # define _force_attr __attribute__((force)) -- 2.43.0