Zend: Add ZEND_BYTES_SWAP32/ZEND_BYTES_SWAP64 (#14910)

This commit is contained in:
Saki Takamachi 2024-07-22 17:57:16 +09:00 committed by GitHub
parent fe9fdd4fc3
commit 8a4a30469a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 79 deletions

View File

@ -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;

View File

@ -811,4 +811,52 @@ typedef union {
} zend_max_align_t;
#endif
/* Bytes swap */
#ifdef _MSC_VER
# include <stdlib.h>
# define ZEND_BYTES_SWAP32(u) _byteswap_ulong(u)
# define ZEND_BYTES_SWAP64(u) _byteswap_uint64(u)
#elif defined(HAVE_BYTESWAP_H)
# include <byteswap.h>
# define ZEND_BYTES_SWAP32(u) bswap_32(u)
# define ZEND_BYTES_SWAP64(u) bswap_64(u)
#elif defined(HAVE_SYS_BSWAP_H)
# include <sys/bswap.h>
# 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 */

View File

@ -33,6 +33,7 @@
#include <stdbool.h>
#include <stddef.h>
#include "zend_portability.h"
/* This will be 0x01010101 for 32-bit and 0x0101010101010101 for 64-bit */
#define SWAR_ONES (~((size_t) 0) / 0xFF)
@ -40,55 +41,14 @@
* 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 <stdlib.h>
# 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;
# define BC_VECTOR_SIZE 8
/* The boundary number is computed from BASE ** BC_VECTOR_SIZE */
# define BC_VECTOR_BOUNDARY_NUM (BC_VECTOR) 100000000
#else
# define BC_BSWAP(u) BC_BSWAP32(u)
# define BC_BSWAP(u) ZEND_BYTES_SWAP32(u)
typedef uint32_t BC_VECTOR;
# define BC_VECTOR_SIZE 4
/* The boundary number is computed from BASE ** BC_VECTOR_SIZE */