diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 942a8b8e1309..d5351113d7cb 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -175,12 +175,25 @@ static size_t _real_page_size = ZEND_MM_PAGE_SIZE; typedef uint32_t zend_mm_page_info; /* 4-byte integer */ typedef zend_ulong zend_mm_bitset; /* 4-byte or 8-byte integer */ -#define ZEND_MM_ALIGNED_OFFSET(size, alignment) \ - (((size_t)(size)) & ((alignment) - 1)) -#define ZEND_MM_ALIGNED_BASE(size, alignment) \ - (((size_t)(size)) & ~((alignment) - 1)) -#define ZEND_MM_SIZE_TO_NUM(size, alignment) \ +#ifdef PHP_HAVE_BUILTIN_ALIGN_DOWN +# define ZEND_MM_ALIGNED_BASE(ptr, alignment) \ + __builtin_align_down((ptr), (alignment)) +# define ZEND_MM_ALIGNED_OFFSET(ptr, alignment) \ + (((size_t)(ptr)) - (size_t)ZEND_MM_ALIGNED_BASE(ptr, alignment)) +#else +# define ZEND_MM_ALIGNED_BASE(ptr, alignment) \ + (((uintptr_t)(ptr)) & ~((alignment) - 1)) +# define ZEND_MM_ALIGNED_OFFSET(ptr, alignment) \ + (((size_t)(ptr)) & ((alignment) - 1)) +#endif + +#ifdef PHP_HAVE_BUILTIN_ALIGN_UP +# define ZEND_MM_SIZE_TO_NUM(size, alignment) \ + (__builtin_align_up((size), (alignment)) / (alignment)) +#else +# define ZEND_MM_SIZE_TO_NUM(size, alignment) \ (((size_t)(size) + ((alignment) - 1)) / (alignment)) +#endif #define ZEND_MM_BITSET_LEN (sizeof(zend_mm_bitset) * 8) /* 32 or 64 */ #define ZEND_MM_PAGE_MAP_LEN (ZEND_MM_PAGES / ZEND_MM_BITSET_LEN) /* 16 or 8 */ diff --git a/build/php.m4 b/build/php.m4 index 516e18e1abfb..e33e73c953ad 100644 --- a/build/php.m4 +++ b/build/php.m4 @@ -2380,6 +2380,8 @@ AC_CACHE_CHECK([for $1], [php_var], [__builtin_ssubll_overflow], [long long tmpvar; return $1(3, 7, &tmpvar);], [__builtin_unreachable], [$1();], [__builtin_usub_overflow], [unsigned int tmpvar; return $1(3, 7, &tmpvar);], + [__builtin_align_down], [return $1(1, 2) == 0 ? 1 : 0;], + [__builtin_align_up], [return $1(1, 2) == 2 ? 1 : 0;], [ m4_warn([syntax], [Unsupported builtin '$1', the test may fail.]) $1(); diff --git a/configure.ac b/configure.ac index 6c517ecc0a1e..4f69694ded7e 100644 --- a/configure.ac +++ b/configure.ac @@ -490,6 +490,8 @@ PHP_CHECK_BUILTIN([__builtin_ssubl_overflow]) PHP_CHECK_BUILTIN([__builtin_ssubll_overflow]) PHP_CHECK_BUILTIN([__builtin_unreachable]) PHP_CHECK_BUILTIN([__builtin_usub_overflow]) +PHP_CHECK_BUILTIN([__builtin_align_down]) +PHP_CHECK_BUILTIN([__builtin_align_up]) dnl Check AVX512 PHP_CHECK_AVX512_SUPPORTS diff --git a/win32/build/config.w32 b/win32/build/config.w32 index aefcfb5f8247..9426c566f133 100644 --- a/win32/build/config.w32 +++ b/win32/build/config.w32 @@ -370,6 +370,8 @@ if (VS_TOOLSET) { AC_DEFINE("PHP_HAVE_BUILTIN_SSUBLL_OVERFLOW", 1, "Define to 1 if the compiler supports '__builtin_ssubll_overflow'."); AC_DEFINE("PHP_HAVE_BUILTIN_SMULL_OVERFLOW", 1, "Define to 1 if the compiler supports '__builtin_smull_overflow '."); AC_DEFINE("PHP_HAVE_BUILTIN_SMULLL_OVERFLOW", 1, "Define to 1 if the compiler supports '__builtin_smulll_overflow'."); + AC_DEFINE("PHP_HAVE_BUILTIN_ALIGN_DOWN", 1, "Define to 1 if the compiler supports '__builtin_align_down'."); + AC_DEFINE("PHP_HAVE_BUILTIN_ALIGN_UP", 1, "Define to 1 if the compiler supports '__builtin_align_up'."); if (PHP_SANITIZER == "yes") { if (COMPILER_NUMERIC_VERSION < 500) {