more robust __builtin_assume_aligned detection

__builtin_assume_aligned is available since GCC 4.7, but __has_builtin
was added much later. Check for the GCC version if __has_builtin is not
available.

Users can also define CGLM_HAVE_BUILTIN_ASSUME_ALIGNED to either 1 or 0
to explicitly enable/disable the use of __builtin_assume_aligned. Meson
will do it automatically (by performing a configure-time test).
This commit is contained in:
Andrei Alexeyev
2023-07-31 22:33:51 +02:00
parent cb4a1b2677
commit 2724620d83
2 changed files with 30 additions and 8 deletions

View File

@@ -32,13 +32,27 @@
# define CGLM_ALIGN_MAT CGLM_ALIGN(16)
#endif
#if defined(__has_builtin)
# if __has_builtin(__builtin_assume_aligned)
# define CGLM_ASSUME_ALIGNED(expr, alignment) \
__builtin_assume_aligned((expr), (alignment))
# else
# define CGLM_ASSUME_ALIGNED(expr, alignment) (expr)
#ifndef CGLM_HAVE_BUILTIN_ASSUME_ALIGNED
# if defined(__has_builtin)
# if __has_builtin(__builtin_assume_aligned)
# define CGLM_HAVE_BUILTIN_ASSUME_ALIGNED 1
# endif
# elif defined(__GNUC__) && defined(__GNUC_MINOR__)
# if __GNUC__ >= 4 && __GNUC_MINOR__ >= 7
# define CGLM_HAVE_BUILTIN_ASSUME_ALIGNED 1
# endif
# endif
# ifndef CGLM_HAVE_BUILTIN_ASSUME_ALIGNED
# define CGLM_HAVE_BUILTIN_ASSUME_ALIGNED 0
# endif
#endif
#if CGLM_HAVE_BUILTIN_ASSUME_ALIGNED
# define CGLM_ASSUME_ALIGNED(expr, alignment) \
__builtin_assume_aligned((expr), (alignment))
#else
# define CGLM_ASSUME_ALIGNED(expr, alignment) (expr)
#endif