Skip to content

Commit 2649e1a

Browse files
committed
Added ZEND_BYTES_SWAP32/ZEND_BYTES_SWAP64
1 parent df6d85a commit 2649e1a

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Zend/zend_portability.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,4 +811,45 @@ 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+
#else
820+
# ifdef __GNUC__
821+
# define ZEND_BYTES_SWAP32(u) __builtin_bswap32(u)
822+
# define ZEND_BYTES_SWAP64(u) __builtin_bswap64(u)
823+
# elif defined(__has_builtin)
824+
# if __has_builtin(__builtin_bswap32)
825+
# define ZEND_BYTES_SWAP32(u) __builtin_bswap32(u)
826+
# endif // __has_builtin(__builtin_bswap32)
827+
# if __has_builtin(__builtin_bswap64)
828+
# define ZEND_BYTES_SWAP64(u) __builtin_bswap64(u)
829+
# endif // __has_builtin(__builtin_bswap64)
830+
# endif // __GNUC__
831+
#endif // _MSC_VER
832+
#ifndef ZEND_BYTES_SWAP32
833+
static zend_always_inline uint32_t ZEND_BYTES_SWAP32(uint32_t u)
834+
{
835+
return (((u & 0xff000000) >> 24)
836+
| ((u & 0x00ff0000) >> 8)
837+
| ((u & 0x0000ff00) << 8)
838+
| ((u & 0x000000ff) << 24));
839+
}
840+
#endif
841+
#ifndef ZEND_BYTES_SWAP64
842+
static zend_always_inline uint64_t ZEND_BYTES_SWAP64(uint64_t u)
843+
{
844+
return (((u & 0xff00000000000000ULL) >> 56)
845+
| ((u & 0x00ff000000000000ULL) >> 40)
846+
| ((u & 0x0000ff0000000000ULL) >> 24)
847+
| ((u & 0x000000ff00000000ULL) >> 8)
848+
| ((u & 0x00000000ff000000ULL) << 8)
849+
| ((u & 0x0000000000ff0000ULL) << 24)
850+
| ((u & 0x000000000000ff00ULL) << 40)
851+
| ((u & 0x00000000000000ffULL) << 56));
852+
}
853+
#endif
854+
814855
#endif /* ZEND_PORTABILITY_H */

0 commit comments

Comments
 (0)