diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 5e2eec9e63850..a1a52205c4ef6 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -217,43 +217,11 @@ typedef zend_mm_bitset zend_mm_page_map[ZEND_MM_PAGE_MAP_LEN]; /* 64B */ #define ZEND_MM_BINS 30 -#if defined(_MSC_VER) -# if UINTPTR_MAX == UINT64_MAX -# define BSWAPPTR(u) _byteswap_uint64(u) -# else -# define BSWAPPTR(u) _byteswap_ulong(u) -# endif +#if UINTPTR_MAX == UINT64_MAX +# define BSWAPPTR(u) ZEND_BYTES_SWAP64(u) #else -# if UINTPTR_MAX == UINT64_MAX -# if __has_builtin(__builtin_bswap64) -# define BSWAPPTR(u) __builtin_bswap64(u) -# else -static zend_always_inline uintptr_t BSWAPPTR(uintptr_t u) -{ - return (((u & 0xff00000000000000ULL) >> 56) - | ((u & 0x00ff000000000000ULL) >> 40) - | ((u & 0x0000ff0000000000ULL) >> 24) - | ((u & 0x000000ff00000000ULL) >> 8) - | ((u & 0x00000000ff000000ULL) << 8) - | ((u & 0x0000000000ff0000ULL) << 24) - | ((u & 0x000000000000ff00ULL) << 40) - | ((u & 0x00000000000000ffULL) << 56)); -} -# endif /* __has_builtin(__builtin_bswap64) */ -# else /* UINTPTR_MAX == UINT64_MAX */ -# if __has_builtin(__builtin_bswap32) -# define BSWAPPTR(u) __builtin_bswap32(u) -# else -static zend_always_inline uintptr_t BSWAPPTR(uintptr_t u) -{ - return (((u & 0xff000000) >> 24) - | ((u & 0x00ff0000) >> 8) - | ((u & 0x0000ff00) << 8) - | ((u & 0x000000ff) << 24)); -} -# endif /* __has_builtin(__builtin_bswap32) */ -# endif /* UINTPTR_MAX == UINT64_MAX */ -#endif /* defined(_MSC_VER) */ +# define BSWAPPTR(u) ZEND_BYTES_SWAP32(u) +#endif typedef struct _zend_mm_page zend_mm_page; typedef struct _zend_mm_bin zend_mm_bin; diff --git a/Zend/zend_portability.h b/Zend/zend_portability.h index 4f785d1396312..cc5c0abb114b2 100644 --- a/Zend/zend_portability.h +++ b/Zend/zend_portability.h @@ -811,4 +811,52 @@ typedef union { } zend_max_align_t; #endif +/* Bytes swap */ +#ifdef _MSC_VER +# include +# define ZEND_BYTES_SWAP32(u) _byteswap_ulong(u) +# define ZEND_BYTES_SWAP64(u) _byteswap_uint64(u) +#elif defined(HAVE_BYTESWAP_H) +# include +# define ZEND_BYTES_SWAP32(u) bswap_32(u) +# define ZEND_BYTES_SWAP64(u) bswap_64(u) +#elif defined(HAVE_SYS_BSWAP_H) +# include +# define ZEND_BYTES_SWAP32(u) bswap32(u) +# define ZEND_BYTES_SWAP64(u) bswap64(u) +#elif defined(__GNUC__) +# define ZEND_BYTES_SWAP32(u) __builtin_bswap32(u) +# define ZEND_BYTES_SWAP64(u) __builtin_bswap64(u) +#elif defined(__has_builtin) +# if __has_builtin(__builtin_bswap32) +# define ZEND_BYTES_SWAP32(u) __builtin_bswap32(u) +# endif +# if __has_builtin(__builtin_bswap64) +# define ZEND_BYTES_SWAP64(u) __builtin_bswap64(u) +# endif +#endif + +#ifndef ZEND_BYTES_SWAP32 +static zend_always_inline uint32_t ZEND_BYTES_SWAP32(uint32_t u) +{ + return (((u & 0xff000000) >> 24) + | ((u & 0x00ff0000) >> 8) + | ((u & 0x0000ff00) << 8) + | ((u & 0x000000ff) << 24)); +} +#endif +#ifndef ZEND_BYTES_SWAP64 +static zend_always_inline uint64_t ZEND_BYTES_SWAP64(uint64_t u) +{ + return (((u & 0xff00000000000000ULL) >> 56) + | ((u & 0x00ff000000000000ULL) >> 40) + | ((u & 0x0000ff0000000000ULL) >> 24) + | ((u & 0x000000ff00000000ULL) >> 8) + | ((u & 0x00000000ff000000ULL) << 8) + | ((u & 0x0000000000ff0000ULL) << 24) + | ((u & 0x000000000000ff00ULL) << 40) + | ((u & 0x00000000000000ffULL) << 56)); +} +#endif + #endif /* ZEND_PORTABILITY_H */ diff --git a/ext/bcmath/libbcmath/src/private.h b/ext/bcmath/libbcmath/src/private.h index 4b72ce7040c85..0300ee524b093 100644 --- a/ext/bcmath/libbcmath/src/private.h +++ b/ext/bcmath/libbcmath/src/private.h @@ -33,6 +33,7 @@ #include #include +#include "zend_portability.h" /* This will be 0x01010101 for 32-bit and 0x0101010101010101 for 64-bit */ #define SWAR_ONES (~((size_t) 0) / 0xFF) @@ -40,52 +41,11 @@ * Example: SWAR_REPEAT(0xAB) will be 0xABABABAB for 32-bit and 0xABABABABABABABAB for 64-bit. */ #define SWAR_REPEAT(x) (SWAR_ONES * (x)) -/* Bytes swap */ -#ifdef _MSC_VER -# include -# define BC_BSWAP32(u) _byteswap_ulong(u) -# define BC_BSWAP64(u) _byteswap_uint64(u) -#else -# ifdef __GNUC__ -# define BC_BSWAP32(u) __builtin_bswap32(u) -# define BC_BSWAP64(u) __builtin_bswap64(u) -# elif defined(__has_builtin) -# if __has_builtin(__builtin_bswap32) -# define BC_BSWAP32(u) __builtin_bswap32(u) -# endif // __has_builtin(__builtin_bswap32) -# if __has_builtin(__builtin_bswap64) -# define BC_BSWAP64(u) __builtin_bswap64(u) -# endif // __has_builtin(__builtin_bswap64) -# endif // __GNUC__ -#endif // _MSC_VER -#ifndef BC_BSWAP32 -static inline uint32_t BC_BSWAP32(uint32_t u) -{ - return (((u & 0xff000000) >> 24) - | ((u & 0x00ff0000) >> 8) - | ((u & 0x0000ff00) << 8) - | ((u & 0x000000ff) << 24)); -} -#endif -#ifndef BC_BSWAP64 -static inline uint64_t BC_BSWAP64(uint64_t u) -{ - return (((u & 0xff00000000000000ULL) >> 56) - | ((u & 0x00ff000000000000ULL) >> 40) - | ((u & 0x0000ff0000000000ULL) >> 24) - | ((u & 0x000000ff00000000ULL) >> 8) - | ((u & 0x00000000ff000000ULL) << 8) - | ((u & 0x0000000000ff0000ULL) << 24) - | ((u & 0x000000000000ff00ULL) << 40) - | ((u & 0x00000000000000ffULL) << 56)); -} -#endif - #if SIZEOF_SIZE_T >= 8 -# define BC_BSWAP(u) BC_BSWAP64(u) +# define BC_BSWAP(u) ZEND_BYTES_SWAP64(u) typedef uint64_t BC_VECTOR; #else -# define BC_BSWAP(u) BC_BSWAP32(u) +# define BC_BSWAP(u) ZEND_BYTES_SWAP32(u) typedef uint32_t BC_VECTOR; #endif