Skip to content

Commit 129287b

Browse files
authored
Merge pull request #341 from taisei-project/assume-aligned-detection
more robust __builtin_assume_aligned detection
2 parents cb4a1b2 + 2724620 commit 129287b

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

include/cglm/types.h

+20-6
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,27 @@
3232
# define CGLM_ALIGN_MAT CGLM_ALIGN(16)
3333
#endif
3434

35-
#if defined(__has_builtin)
36-
# if __has_builtin(__builtin_assume_aligned)
37-
# define CGLM_ASSUME_ALIGNED(expr, alignment) \
38-
__builtin_assume_aligned((expr), (alignment))
39-
# else
40-
# define CGLM_ASSUME_ALIGNED(expr, alignment) (expr)
35+
#ifndef CGLM_HAVE_BUILTIN_ASSUME_ALIGNED
36+
37+
# if defined(__has_builtin)
38+
# if __has_builtin(__builtin_assume_aligned)
39+
# define CGLM_HAVE_BUILTIN_ASSUME_ALIGNED 1
40+
# endif
41+
# elif defined(__GNUC__) && defined(__GNUC_MINOR__)
42+
# if __GNUC__ >= 4 && __GNUC_MINOR__ >= 7
43+
# define CGLM_HAVE_BUILTIN_ASSUME_ALIGNED 1
44+
# endif
45+
# endif
46+
47+
# ifndef CGLM_HAVE_BUILTIN_ASSUME_ALIGNED
48+
# define CGLM_HAVE_BUILTIN_ASSUME_ALIGNED 0
4149
# endif
50+
51+
#endif
52+
53+
#if CGLM_HAVE_BUILTIN_ASSUME_ALIGNED
54+
# define CGLM_ASSUME_ALIGNED(expr, alignment) \
55+
__builtin_assume_aligned((expr), (alignment))
4256
#else
4357
# define CGLM_ASSUME_ALIGNED(expr, alignment) (expr)
4458
#endif

meson.build

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ cglm_args = []
1717
build_args = []
1818

1919
if get_option('default_library') == 'static'
20-
cglm_args = '-DCGLM_STATIC'
20+
cglm_args += '-DCGLM_STATIC'
21+
endif
22+
23+
if cc.compiles(
24+
'int *test(char *p) { return (int*)__builtin_assume_aligned(p, 4); }',
25+
name : '__builtin_assume_aligned test')
26+
cglm_args += '-DCGLM_HAVE_BUILTIN_ASSUME_ALIGNED=1'
27+
else
28+
cglm_args += '-DCGLM_HAVE_BUILTIN_ASSUME_ALIGNED=0'
2129
endif
2230

2331
if host_machine.system() == 'windows'
24-
build_args = '-DCGLM_EXPORTS'
32+
build_args += '-DCGLM_EXPORTS'
2533
endif
2634

2735
cglm_inc = include_directories('include')

0 commit comments

Comments
 (0)