Debian Codegraph @

mklove/modules/configure.atomics

raw
1 #!/bin/bash
2 #
3 # Checks for atomic ops:
4 # compiler builtin (__sync_..) and portable libatomic's (__atomic_..)
5 # Will also provide abstraction by defining the prefix to use.
6 #
7 # Sets:
8 # HAVE_ATOMICS
9 # HAVE_ATOMICS_32
10 # HAVE_ATOMICS_64
11 # HAVE_ATOMICS_32_ATOMIC __atomic interface
12 # HAVE_ATOMICS_32_SYNC __sync interface
13 # HAVE_ATOMICS_64_ATOMIC __atomic interface
14 # HAVE_ATOMICS_64_SYNC __sync interface
15 # WITH_LIBATOMIC
16 # LIBS
17 #
18 # ATOMIC_OP(OP1,OP2,PTR,VAL)
19 # ATOMIC_OP32(OP1,OP2,PTR,VAL)
20 # ATOMIC_OP64(OP1,OP2,PTR,VAL)
21 # where op* is 'add,sub,fetch'
22 # e.g: ATOMIC_OP32(add, fetch, &i, 10)
23 # becomes __atomic_add_fetch(&i, 10, ..) or
24 # __sync_add_and_fetch(&i, 10)
25 #
26
27 function checks {
28
29
30 # We prefer the newer __atomic stuff, but 64-bit atomics might
31 # require linking with -latomic, so we need to perform these tests
32 # in the proper order:
33 # __atomic 32
34 # __atomic 32 -latomic
35 # __sync 32
36 #
37 # __atomic 64
38 # __atomic 64 -latomic
39 # __sync 64
40
41 local _libs=
42 local _a32="__atomic_ ## OP1 ## _ ## OP2(PTR, VAL, __ATOMIC_SEQ_CST)"
43 local _a64="__atomic_ ## OP1 ## _ ## OP2(PTR, VAL, __ATOMIC_SEQ_CST)"
44
45 # 32-bit:
46 # Try fully builtin __atomic
47 if ! mkl_compile_check __atomic_32 HAVE_ATOMICS_32 cont CC "" \
48 "
49 #include <inttypes.h>
50 int32_t foo (int32_t i) {
51 return __atomic_add_fetch(&i, 1, __ATOMIC_SEQ_CST);
52 }"
53 then
54 # Try __atomic with -latomic
55 if mkl_compile_check --ldflags="-latomic" __atomic_32_lib HAVE_ATOMICS_32 \
56 cont CC "" \
57 "
58 #include <inttypes.h>
59 int32_t foo (int32_t i) {
60 return __atomic_add_fetch(&i, 1, __ATOMIC_SEQ_CST);
61 }"
62 then
63 _libs="-latomic"
64 mkl_allvar_set "__atomic_32_lib" "HAVE_ATOMICS_32_ATOMIC" "y"
65 else
66 # Try __sync interface
67 if mkl_compile_check __sync_32 HAVE_ATOMICS_32 disable CC "" \
68 "
69 #include <inttypes.h>
70 int32_t foo (int32_t i) {
71 return __sync_add_and_fetch(&i, 1);
72 }"
73 then
74 _a32="__sync_ ## OP1 ## _and_ ## OP2(PTR, VAL)"
75 mkl_allvar_set "__sync_32" "HAVE_ATOMICS_32_SYNC" "y"
76 else
77 _a32=""
78 fi
79 fi
80 else
81 mkl_allvar_set "__atomic_32" "HAVE_ATOMICS_32_ATOMIC" "y"
82 fi
83
84
85 if [[ ! -z $_a32 ]]; then
86 mkl_define_set "atomic_32" "ATOMIC_OP32(OP1,OP2,PTR,VAL)" "code:$_a32"
87 fi
88
89
90
91 # 64-bit:
92 # Try fully builtin __atomic
93 if ! mkl_compile_check __atomic_64 HAVE_ATOMICS_64 cont CC "" \
94 "
95 #include <inttypes.h>
96 int64_t foo (int64_t i) {
97 return __atomic_add_fetch(&i, 1, __ATOMIC_SEQ_CST);
98 }"
99 then
100 # Try __atomic with -latomic
101 if mkl_compile_check --ldflags="-latomic" __atomic_64_lib HAVE_ATOMICS_64 \
102 cont CC "" \
103 "
104 #include <inttypes.h>
105 int64_t foo (int64_t i) {
106 return __atomic_add_fetch(&i, 1, __ATOMIC_SEQ_CST);
107 }"
108 then
109 _libs="-latomic"
110 mkl_allvar_set "__atomic_64_lib" "HAVE_ATOMICS_64_ATOMIC" "y"
111 else
112 # Try __sync interface
113 if mkl_compile_check __sync_64 HAVE_ATOMICS_64 disable CC "" \
114 "
115 #include <inttypes.h>
116 int64_t foo (int64_t i) {
117 return __sync_add_and_fetch(&i, 1);
118 }"
119 then
120 _a64="__sync_ ## OP1 ## _and_ ## OP2 (PTR, VAL)"
121 mkl_allvar_set "__sync_64" "HAVE_ATOMICS_64_SYNC" "y"
122 else
123 _a64=""
124 fi
125 fi
126 else
127 mkl_allvar_set "__atomic_64" "HAVE_ATOMICS_64_ATOMIC" "y"
128 fi
129
130
131 if [[ ! -z $_a64 ]]; then
132 mkl_define_set "atomic_64" "ATOMIC_OP64(OP1,OP2,PTR,VAL)" "code:$_a64"
133
134 # Define generic ATOMIC() macro identical to 64-bit atomics"
135 mkl_define_set "atomic_64" "ATOMIC_OP(OP1,OP2,PTR,VAL)" "code:$_a64"
136 fi
137
138
139 if [[ ! -z $_libs ]]; then
140 mkl_mkvar_append LDFLAGS LDFLAGS "-Wl,--as-needed"
141 mkl_mkvar_append LIBS LIBS "$_libs"
142 fi
143
144 }