Skip to content

Zend: Add ZEND_BYTES_SWAP32/ZEND_BYTES_SWAP64 #14910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 4 additions & 36 deletions Zend/zend_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
48 changes: 48 additions & 0 deletions Zend/zend_portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
46 changes: 3 additions & 43 deletions ext/bcmath/libbcmath/src/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,59 +33,19 @@

#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)
/* This repeats a byte `x` into an entire 32/64-bit word.
* 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;
#else
# define BC_BSWAP(u) BC_BSWAP32(u)
# define BC_BSWAP(u) ZEND_BYTES_SWAP32(u)
typedef uint32_t BC_VECTOR;
#endif

Expand Down
Loading