Skip to content

Commit 8a4a304

Browse files
Zend: Add ZEND_BYTES_SWAP32/ZEND_BYTES_SWAP64 (#14910)
1 parent fe9fdd4 commit 8a4a304

File tree

3 files changed

+55
-79
lines changed

3 files changed

+55
-79
lines changed

Zend/zend_alloc.c

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -217,43 +217,11 @@ typedef zend_mm_bitset zend_mm_page_map[ZEND_MM_PAGE_MAP_LEN]; /* 64B */
217217

218218
#define ZEND_MM_BINS 30
219219

220-
#if defined(_MSC_VER)
221-
# if UINTPTR_MAX == UINT64_MAX
222-
# define BSWAPPTR(u) _byteswap_uint64(u)
223-
# else
224-
# define BSWAPPTR(u) _byteswap_ulong(u)
225-
# endif
220+
#if UINTPTR_MAX == UINT64_MAX
221+
# define BSWAPPTR(u) ZEND_BYTES_SWAP64(u)
226222
#else
227-
# if UINTPTR_MAX == UINT64_MAX
228-
# if __has_builtin(__builtin_bswap64)
229-
# define BSWAPPTR(u) __builtin_bswap64(u)
230-
# else
231-
static zend_always_inline uintptr_t BSWAPPTR(uintptr_t u)
232-
{
233-
return (((u & 0xff00000000000000ULL) >> 56)
234-
| ((u & 0x00ff000000000000ULL) >> 40)
235-
| ((u & 0x0000ff0000000000ULL) >> 24)
236-
| ((u & 0x000000ff00000000ULL) >> 8)
237-
| ((u & 0x00000000ff000000ULL) << 8)
238-
| ((u & 0x0000000000ff0000ULL) << 24)
239-
| ((u & 0x000000000000ff00ULL) << 40)
240-
| ((u & 0x00000000000000ffULL) << 56));
241-
}
242-
# endif /* __has_builtin(__builtin_bswap64) */
243-
# else /* UINTPTR_MAX == UINT64_MAX */
244-
# if __has_builtin(__builtin_bswap32)
245-
# define BSWAPPTR(u) __builtin_bswap32(u)
246-
# else
247-
static zend_always_inline uintptr_t BSWAPPTR(uintptr_t u)
248-
{
249-
return (((u & 0xff000000) >> 24)
250-
| ((u & 0x00ff0000) >> 8)
251-
| ((u & 0x0000ff00) << 8)
252-
| ((u & 0x000000ff) << 24));
253-
}
254-
# endif /* __has_builtin(__builtin_bswap32) */
255-
# endif /* UINTPTR_MAX == UINT64_MAX */
256-
#endif /* defined(_MSC_VER) */
223+
# define BSWAPPTR(u) ZEND_BYTES_SWAP32(u)
224+
#endif
257225

258226
typedef struct _zend_mm_page zend_mm_page;
259227
typedef struct _zend_mm_bin zend_mm_bin;

Zend/zend_portability.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,4 +811,52 @@ typedef union {
811811
} zend_max_align_t;
812812
#endif
813813

814+
/* Bytes swap */
815+
#ifdef _MSC_VER
816+
# include <stdlib.h>
817+
# define ZEND_BYTES_SWAP32(u) _byteswap_ulong(u)
818+
# define ZEND_BYTES_SWAP64(u) _byteswap_uint64(u)
819+
#elif defined(HAVE_BYTESWAP_H)
820+
# include <byteswap.h>
821+
# define ZEND_BYTES_SWAP32(u) bswap_32(u)
822+
# define ZEND_BYTES_SWAP64(u) bswap_64(u)
823+
#elif defined(HAVE_SYS_BSWAP_H)
824+
# include <sys/bswap.h>
825+
# define ZEND_BYTES_SWAP32(u) bswap32(u)
826+
# define ZEND_BYTES_SWAP64(u) bswap64(u)
827+
#elif defined(__GNUC__)
828+
# define ZEND_BYTES_SWAP32(u) __builtin_bswap32(u)
829+
# define ZEND_BYTES_SWAP64(u) __builtin_bswap64(u)
830+
#elif defined(__has_builtin)
831+
# if __has_builtin(__builtin_bswap32)
832+
# define ZEND_BYTES_SWAP32(u) __builtin_bswap32(u)
833+
# endif
834+
# if __has_builtin(__builtin_bswap64)
835+
# define ZEND_BYTES_SWAP64(u) __builtin_bswap64(u)
836+
# endif
837+
#endif
838+
839+
#ifndef ZEND_BYTES_SWAP32
840+
static zend_always_inline uint32_t ZEND_BYTES_SWAP32(uint32_t u)
841+
{
842+
return (((u & 0xff000000) >> 24)
843+
| ((u & 0x00ff0000) >> 8)
844+
| ((u & 0x0000ff00) << 8)
845+
| ((u & 0x000000ff) << 24));
846+
}
847+
#endif
848+
#ifndef ZEND_BYTES_SWAP64
849+
static zend_always_inline uint64_t ZEND_BYTES_SWAP64(uint64_t u)
850+
{
851+
return (((u & 0xff00000000000000ULL) >> 56)
852+
| ((u & 0x00ff000000000000ULL) >> 40)
853+
| ((u & 0x0000ff0000000000ULL) >> 24)
854+
| ((u & 0x000000ff00000000ULL) >> 8)
855+
| ((u & 0x00000000ff000000ULL) << 8)
856+
| ((u & 0x0000000000ff0000ULL) << 24)
857+
| ((u & 0x000000000000ff00ULL) << 40)
858+
| ((u & 0x00000000000000ffULL) << 56));
859+
}
860+
#endif
861+
814862
#endif /* ZEND_PORTABILITY_H */

ext/bcmath/libbcmath/src/private.h

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,62 +33,22 @@
3333

3434
#include <stdbool.h>
3535
#include <stddef.h>
36+
#include "zend_portability.h"
3637

3738
/* This will be 0x01010101 for 32-bit and 0x0101010101010101 for 64-bit */
3839
#define SWAR_ONES (~((size_t) 0) / 0xFF)
3940
/* This repeats a byte `x` into an entire 32/64-bit word.
4041
* Example: SWAR_REPEAT(0xAB) will be 0xABABABAB for 32-bit and 0xABABABABABABABAB for 64-bit. */
4142
#define SWAR_REPEAT(x) (SWAR_ONES * (x))
4243

43-
/* Bytes swap */
44-
#ifdef _MSC_VER
45-
# include <stdlib.h>
46-
# define BC_BSWAP32(u) _byteswap_ulong(u)
47-
# define BC_BSWAP64(u) _byteswap_uint64(u)
48-
#else
49-
# ifdef __GNUC__
50-
# define BC_BSWAP32(u) __builtin_bswap32(u)
51-
# define BC_BSWAP64(u) __builtin_bswap64(u)
52-
# elif defined(__has_builtin)
53-
# if __has_builtin(__builtin_bswap32)
54-
# define BC_BSWAP32(u) __builtin_bswap32(u)
55-
# endif // __has_builtin(__builtin_bswap32)
56-
# if __has_builtin(__builtin_bswap64)
57-
# define BC_BSWAP64(u) __builtin_bswap64(u)
58-
# endif // __has_builtin(__builtin_bswap64)
59-
# endif // __GNUC__
60-
#endif // _MSC_VER
61-
#ifndef BC_BSWAP32
62-
static inline uint32_t BC_BSWAP32(uint32_t u)
63-
{
64-
return (((u & 0xff000000) >> 24)
65-
| ((u & 0x00ff0000) >> 8)
66-
| ((u & 0x0000ff00) << 8)
67-
| ((u & 0x000000ff) << 24));
68-
}
69-
#endif
70-
#ifndef BC_BSWAP64
71-
static inline uint64_t BC_BSWAP64(uint64_t u)
72-
{
73-
return (((u & 0xff00000000000000ULL) >> 56)
74-
| ((u & 0x00ff000000000000ULL) >> 40)
75-
| ((u & 0x0000ff0000000000ULL) >> 24)
76-
| ((u & 0x000000ff00000000ULL) >> 8)
77-
| ((u & 0x00000000ff000000ULL) << 8)
78-
| ((u & 0x0000000000ff0000ULL) << 24)
79-
| ((u & 0x000000000000ff00ULL) << 40)
80-
| ((u & 0x00000000000000ffULL) << 56));
81-
}
82-
#endif
83-
8444
#if SIZEOF_SIZE_T >= 8
85-
# define BC_BSWAP(u) BC_BSWAP64(u)
45+
# define BC_BSWAP(u) ZEND_BYTES_SWAP64(u)
8646
typedef uint64_t BC_VECTOR;
8747
# define BC_VECTOR_SIZE 8
8848
/* The boundary number is computed from BASE ** BC_VECTOR_SIZE */
8949
# define BC_VECTOR_BOUNDARY_NUM (BC_VECTOR) 100000000
9050
#else
91-
# define BC_BSWAP(u) BC_BSWAP32(u)
51+
# define BC_BSWAP(u) ZEND_BYTES_SWAP32(u)
9252
typedef uint32_t BC_VECTOR;
9353
# define BC_VECTOR_SIZE 4
9454
/* The boundary number is computed from BASE ** BC_VECTOR_SIZE */

0 commit comments

Comments
 (0)