]> granicus.if.org Git - postgresql/blob - config/c-compiler.m4
Speed up CRC calculation using slicing-by-8 algorithm.
[postgresql] / config / c-compiler.m4
1 # Macros to detect C compiler features
2 # config/c-compiler.m4
3
4
5 # PGAC_C_SIGNED
6 # -------------
7 # Check if the C compiler understands signed types.
8 AC_DEFUN([PGAC_C_SIGNED],
9 [AC_CACHE_CHECK(for signed types, pgac_cv_c_signed,
10 [AC_TRY_COMPILE([],
11 [signed char c; signed short s; signed int i;],
12 [pgac_cv_c_signed=yes],
13 [pgac_cv_c_signed=no])])
14 if test x"$pgac_cv_c_signed" = xno ; then
15   AC_DEFINE(signed,, [Define to empty if the C compiler does not understand signed types.])
16 fi])# PGAC_C_SIGNED
17
18
19
20 # PGAC_C_INLINE
21 # -------------
22 # Check if the C compiler understands inline functions without being
23 # noisy about unused static inline functions. Some older compilers
24 # understand inline functions (as tested by AC_C_INLINE) but warn about
25 # them if they aren't used in a translation unit.
26 #
27 # This test used to just define an inline function, but some compilers
28 # (notably clang) got too smart and now warn about unused static
29 # inline functions when defined inside a .c file, but not when defined
30 # in an included header. Since the latter is what we want to use, test
31 # to see if the warning appears when the function is in a header file.
32 # Not pretty, but it works.
33 #
34 # Defines: inline, PG_USE_INLINE
35 AC_DEFUN([PGAC_C_INLINE],
36 [AC_C_INLINE
37 AC_CACHE_CHECK([for quiet inline (no complaint if unreferenced)], pgac_cv_c_inline_quietly,
38   [pgac_cv_c_inline_quietly=no
39   if test "$ac_cv_c_inline" != no; then
40     pgac_c_inline_save_werror=$ac_c_werror_flag
41     ac_c_werror_flag=yes
42     AC_LINK_IFELSE([AC_LANG_PROGRAM([#include "$srcdir/config/test_quiet_include.h"],[])],
43                    [pgac_cv_c_inline_quietly=yes])
44     ac_c_werror_flag=$pgac_c_inline_save_werror
45   fi])
46 if test "$pgac_cv_c_inline_quietly" != no; then
47   AC_DEFINE_UNQUOTED([PG_USE_INLINE], 1,
48     [Define to 1 if "static inline" works without unwanted warnings from ]
49     [compilations where static inline functions are defined but not called.])
50 fi
51 ])# PGAC_C_INLINE
52
53
54 # PGAC_C_PRINTF_ARCHETYPE
55 # -----------------------
56 # Set the format archetype used by gcc to check printf type functions.  We
57 # prefer "gnu_printf", which includes what glibc uses, such as %m for error
58 # strings and %lld for 64 bit long longs.  GCC 4.4 introduced it.  It makes a
59 # dramatic difference on Windows.
60 AC_DEFUN([PGAC_PRINTF_ARCHETYPE],
61 [AC_CACHE_CHECK([for printf format archetype], pgac_cv_printf_archetype,
62 [ac_save_c_werror_flag=$ac_c_werror_flag
63 ac_c_werror_flag=yes
64 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
65 [extern int
66 pgac_write(int ignore, const char *fmt,...)
67 __attribute__((format(gnu_printf, 2, 3)));], [])],
68                   [pgac_cv_printf_archetype=gnu_printf],
69                   [pgac_cv_printf_archetype=printf])
70 ac_c_werror_flag=$ac_save_c_werror_flag])
71 AC_DEFINE_UNQUOTED([PG_PRINTF_ATTRIBUTE], [$pgac_cv_printf_archetype],
72                    [Define to gnu_printf if compiler supports it, else printf.])
73 ])# PGAC_PRINTF_ARCHETYPE
74
75
76 # PGAC_TYPE_64BIT_INT(TYPE)
77 # -------------------------
78 # Check if TYPE is a working 64 bit integer type. Set HAVE_TYPE_64 to
79 # yes or no respectively, and define HAVE_TYPE_64 if yes.
80 AC_DEFUN([PGAC_TYPE_64BIT_INT],
81 [define([Ac_define], [translit([have_$1_64], [a-z *], [A-Z_P])])dnl
82 define([Ac_cachevar], [translit([pgac_cv_type_$1_64], [ *], [_p])])dnl
83 AC_CACHE_CHECK([whether $1 is 64 bits], [Ac_cachevar],
84 [AC_TRY_RUN(
85 [typedef $1 ac_int64;
86
87 /*
88  * These are globals to discourage the compiler from folding all the
89  * arithmetic tests down to compile-time constants.
90  */
91 ac_int64 a = 20000001;
92 ac_int64 b = 40000005;
93
94 int does_int64_work()
95 {
96   ac_int64 c,d;
97
98   if (sizeof(ac_int64) != 8)
99     return 0;                   /* definitely not the right size */
100
101   /* Do perfunctory checks to see if 64-bit arithmetic seems to work */
102   c = a * b;
103   d = (c + b) / b;
104   if (d != a+1)
105     return 0;
106   return 1;
107 }
108 main() {
109   exit(! does_int64_work());
110 }],
111 [Ac_cachevar=yes],
112 [Ac_cachevar=no],
113 [# If cross-compiling, check the size reported by the compiler and
114 # trust that the arithmetic works.
115 AC_COMPILE_IFELSE([AC_LANG_BOOL_COMPILE_TRY([], [sizeof($1) == 8])],
116                   Ac_cachevar=yes,
117                   Ac_cachevar=no)])])
118
119 Ac_define=$Ac_cachevar
120 if test x"$Ac_cachevar" = xyes ; then
121   AC_DEFINE(Ac_define, 1, [Define to 1 if `]$1[' works and is 64 bits.])
122 fi
123 undefine([Ac_define])dnl
124 undefine([Ac_cachevar])dnl
125 ])# PGAC_TYPE_64BIT_INT
126
127
128
129 # PGAC_C_FUNCNAME_SUPPORT
130 # -----------------------
131 # Check if the C compiler understands __func__ (C99) or __FUNCTION__ (gcc).
132 # Define HAVE_FUNCNAME__FUNC or HAVE_FUNCNAME__FUNCTION accordingly.
133 AC_DEFUN([PGAC_C_FUNCNAME_SUPPORT],
134 [AC_CACHE_CHECK(for __func__, pgac_cv_funcname_func_support,
135 [AC_TRY_COMPILE([#include <stdio.h>],
136 [printf("%s\n", __func__);],
137 [pgac_cv_funcname_func_support=yes],
138 [pgac_cv_funcname_func_support=no])])
139 if test x"$pgac_cv_funcname_func_support" = xyes ; then
140 AC_DEFINE(HAVE_FUNCNAME__FUNC, 1,
141           [Define to 1 if your compiler understands __func__.])
142 else
143 AC_CACHE_CHECK(for __FUNCTION__, pgac_cv_funcname_function_support,
144 [AC_TRY_COMPILE([#include <stdio.h>],
145 [printf("%s\n", __FUNCTION__);],
146 [pgac_cv_funcname_function_support=yes],
147 [pgac_cv_funcname_function_support=no])])
148 if test x"$pgac_cv_funcname_function_support" = xyes ; then
149 AC_DEFINE(HAVE_FUNCNAME__FUNCTION, 1,
150           [Define to 1 if your compiler understands __FUNCTION__.])
151 fi
152 fi])# PGAC_C_FUNCNAME_SUPPORT
153
154
155
156 # PGAC_C_STATIC_ASSERT
157 # --------------------
158 # Check if the C compiler understands _Static_assert(),
159 # and define HAVE__STATIC_ASSERT if so.
160 #
161 # We actually check the syntax ({ _Static_assert(...) }), because we need
162 # gcc-style compound expressions to be able to wrap the thing into macros.
163 AC_DEFUN([PGAC_C_STATIC_ASSERT],
164 [AC_CACHE_CHECK(for _Static_assert, pgac_cv__static_assert,
165 [AC_TRY_LINK([],
166 [({ _Static_assert(1, "foo"); })],
167 [pgac_cv__static_assert=yes],
168 [pgac_cv__static_assert=no])])
169 if test x"$pgac_cv__static_assert" = xyes ; then
170 AC_DEFINE(HAVE__STATIC_ASSERT, 1,
171           [Define to 1 if your compiler understands _Static_assert.])
172 fi])# PGAC_C_STATIC_ASSERT
173
174
175
176 # PGAC_C_TYPES_COMPATIBLE
177 # -----------------------
178 # Check if the C compiler understands __builtin_types_compatible_p,
179 # and define HAVE__BUILTIN_TYPES_COMPATIBLE_P if so.
180 #
181 # We check usage with __typeof__, though it's unlikely any compiler would
182 # have the former and not the latter.
183 AC_DEFUN([PGAC_C_TYPES_COMPATIBLE],
184 [AC_CACHE_CHECK(for __builtin_types_compatible_p, pgac_cv__types_compatible,
185 [AC_TRY_COMPILE([],
186 [ int x; static int y[__builtin_types_compatible_p(__typeof__(x), int)]; ],
187 [pgac_cv__types_compatible=yes],
188 [pgac_cv__types_compatible=no])])
189 if test x"$pgac_cv__types_compatible" = xyes ; then
190 AC_DEFINE(HAVE__BUILTIN_TYPES_COMPATIBLE_P, 1,
191           [Define to 1 if your compiler understands __builtin_types_compatible_p.])
192 fi])# PGAC_C_TYPES_COMPATIBLE
193
194
195
196 # PGAC_C_BUILTIN_BSWAP32
197 # -------------------------
198 # Check if the C compiler understands __builtin_bswap32(),
199 # and define HAVE__BUILTIN_BSWAP32 if so.
200 AC_DEFUN([PGAC_C_BUILTIN_BSWAP32],
201 [AC_CACHE_CHECK(for __builtin_bswap32, pgac_cv__builtin_bswap32,
202 [AC_TRY_COMPILE([static unsigned long int x = __builtin_bswap32(0xaabbccdd);],
203 [],
204 [pgac_cv__builtin_bswap32=yes],
205 [pgac_cv__builtin_bswap32=no])])
206 if test x"$pgac_cv__builtin_bswap32" = xyes ; then
207 AC_DEFINE(HAVE__BUILTIN_BSWAP32, 1,
208           [Define to 1 if your compiler understands __builtin_bswap32.])
209 fi])# PGAC_C_BUILTIN_BSWAP32
210
211
212
213 # PGAC_C_BUILTIN_CONSTANT_P
214 # -------------------------
215 # Check if the C compiler understands __builtin_constant_p(),
216 # and define HAVE__BUILTIN_CONSTANT_P if so.
217 AC_DEFUN([PGAC_C_BUILTIN_CONSTANT_P],
218 [AC_CACHE_CHECK(for __builtin_constant_p, pgac_cv__builtin_constant_p,
219 [AC_TRY_COMPILE([static int x; static int y[__builtin_constant_p(x) ? x : 1];],
220 [],
221 [pgac_cv__builtin_constant_p=yes],
222 [pgac_cv__builtin_constant_p=no])])
223 if test x"$pgac_cv__builtin_constant_p" = xyes ; then
224 AC_DEFINE(HAVE__BUILTIN_CONSTANT_P, 1,
225           [Define to 1 if your compiler understands __builtin_constant_p.])
226 fi])# PGAC_C_BUILTIN_CONSTANT_P
227
228
229
230 # PGAC_C_BUILTIN_UNREACHABLE
231 # --------------------------
232 # Check if the C compiler understands __builtin_unreachable(),
233 # and define HAVE__BUILTIN_UNREACHABLE if so.
234 #
235 # NB: Don't get the idea of putting a for(;;); or such before the
236 # __builtin_unreachable() call.  Some compilers would remove it before linking
237 # and only a warning instead of an error would be produced.
238 AC_DEFUN([PGAC_C_BUILTIN_UNREACHABLE],
239 [AC_CACHE_CHECK(for __builtin_unreachable, pgac_cv__builtin_unreachable,
240 [AC_TRY_LINK([],
241 [__builtin_unreachable();],
242 [pgac_cv__builtin_unreachable=yes],
243 [pgac_cv__builtin_unreachable=no])])
244 if test x"$pgac_cv__builtin_unreachable" = xyes ; then
245 AC_DEFINE(HAVE__BUILTIN_UNREACHABLE, 1,
246           [Define to 1 if your compiler understands __builtin_unreachable.])
247 fi])# PGAC_C_BUILTIN_UNREACHABLE
248
249
250
251 # PGAC_C_VA_ARGS
252 # --------------
253 # Check if the C compiler understands C99-style variadic macros,
254 # and define HAVE__VA_ARGS if so.
255 AC_DEFUN([PGAC_C_VA_ARGS],
256 [AC_CACHE_CHECK(for __VA_ARGS__, pgac_cv__va_args,
257 [AC_TRY_COMPILE([#include <stdio.h>],
258 [#define debug(...) fprintf(stderr, __VA_ARGS__)
259 debug("%s", "blarg");
260 ],
261 [pgac_cv__va_args=yes],
262 [pgac_cv__va_args=no])])
263 if test x"$pgac_cv__va_args" = xyes ; then
264 AC_DEFINE(HAVE__VA_ARGS, 1,
265           [Define to 1 if your compiler understands __VA_ARGS__ in macros.])
266 fi])# PGAC_C_VA_ARGS
267
268
269
270 # PGAC_PROG_CC_CFLAGS_OPT
271 # -----------------------
272 # Given a string, check if the compiler supports the string as a
273 # command-line option. If it does, add the string to CFLAGS.
274 AC_DEFUN([PGAC_PROG_CC_CFLAGS_OPT],
275 [define([Ac_cachevar], [AS_TR_SH([pgac_cv_prog_cc_cflags_$1])])dnl
276 AC_CACHE_CHECK([whether $CC supports $1], [Ac_cachevar],
277 [pgac_save_CFLAGS=$CFLAGS
278 CFLAGS="$pgac_save_CFLAGS $1"
279 ac_save_c_werror_flag=$ac_c_werror_flag
280 ac_c_werror_flag=yes
281 _AC_COMPILE_IFELSE([AC_LANG_PROGRAM()],
282                    [Ac_cachevar=yes],
283                    [Ac_cachevar=no])
284 ac_c_werror_flag=$ac_save_c_werror_flag
285 CFLAGS="$pgac_save_CFLAGS"])
286 if test x"$Ac_cachevar" = x"yes"; then
287   CFLAGS="$CFLAGS $1"
288 fi
289 undefine([Ac_cachevar])dnl
290 ])# PGAC_PROG_CC_CFLAGS_OPT
291
292
293
294 # PGAC_PROG_CC_VAR_OPT
295 # -----------------------
296 # Given a variable name and a string, check if the compiler supports
297 # the string as a command-line option. If it does, add the string to
298 # the given variable.
299 AC_DEFUN([PGAC_PROG_CC_VAR_OPT],
300 [define([Ac_cachevar], [AS_TR_SH([pgac_cv_prog_cc_cflags_$2])])dnl
301 AC_CACHE_CHECK([whether $CC supports $2], [Ac_cachevar],
302 [pgac_save_CFLAGS=$CFLAGS
303 CFLAGS="$pgac_save_CFLAGS $2"
304 ac_save_c_werror_flag=$ac_c_werror_flag
305 ac_c_werror_flag=yes
306 _AC_COMPILE_IFELSE([AC_LANG_PROGRAM()],
307                    [Ac_cachevar=yes],
308                    [Ac_cachevar=no])
309 ac_c_werror_flag=$ac_save_c_werror_flag
310 CFLAGS="$pgac_save_CFLAGS"])
311 if test x"$Ac_cachevar" = x"yes"; then
312   $1="${$1} $2"
313 fi
314 undefine([Ac_cachevar])dnl
315 ])# PGAC_PROG_CC_CFLAGS_OPT
316
317
318
319 # PGAC_PROG_CC_LDFLAGS_OPT
320 # ------------------------
321 # Given a string, check if the compiler supports the string as a
322 # command-line option. If it does, add the string to LDFLAGS.
323 # For reasons you'd really rather not know about, this checks whether
324 # you can link to a particular function, not just whether you can link.
325 # In fact, we must actually check that the resulting program runs :-(
326 AC_DEFUN([PGAC_PROG_CC_LDFLAGS_OPT],
327 [define([Ac_cachevar], [AS_TR_SH([pgac_cv_prog_cc_ldflags_$1])])dnl
328 AC_CACHE_CHECK([whether $CC supports $1], [Ac_cachevar],
329 [pgac_save_LDFLAGS=$LDFLAGS
330 LDFLAGS="$pgac_save_LDFLAGS $1"
331 AC_RUN_IFELSE([AC_LANG_PROGRAM([extern void $2 (); void (*fptr) () = $2;],[])],
332               [Ac_cachevar=yes],
333               [Ac_cachevar=no],
334               [Ac_cachevar="assuming no"])
335 LDFLAGS="$pgac_save_LDFLAGS"])
336 if test x"$Ac_cachevar" = x"yes"; then
337   LDFLAGS="$LDFLAGS $1"
338 fi
339 undefine([Ac_cachevar])dnl
340 ])# PGAC_PROG_CC_LDFLAGS_OPT
341
342 # PGAC_HAVE_GCC__SYNC_CHAR_TAS
343 # -------------------------
344 # Check if the C compiler understands __sync_lock_test_and_set(char),
345 # and define HAVE_GCC__SYNC_CHAR_TAS
346 #
347 # NB: There are platforms where test_and_set is available but compare_and_swap
348 # is not, so test this separately.
349 # NB: Some platforms only do 32bit tas, others only do 8bit tas. Test both.
350 AC_DEFUN([PGAC_HAVE_GCC__SYNC_CHAR_TAS],
351 [AC_CACHE_CHECK(for builtin __sync char locking functions, pgac_cv_gcc_sync_char_tas,
352 [AC_TRY_LINK([],
353   [char lock = 0;
354    __sync_lock_test_and_set(&lock, 1);
355    __sync_lock_release(&lock);],
356   [pgac_cv_gcc_sync_char_tas="yes"],
357   [pgac_cv_gcc_sync_char_tas="no"])])
358 if test x"$pgac_cv_gcc_sync_char_tas" = x"yes"; then
359   AC_DEFINE(HAVE_GCC__SYNC_CHAR_TAS, 1, [Define to 1 if you have __sync_lock_test_and_set(char *) and friends.])
360 fi])# PGAC_HAVE_GCC__SYNC_CHAR_TAS
361
362 # PGAC_HAVE_GCC__SYNC_INT32_TAS
363 # -------------------------
364 # Check if the C compiler understands __sync_lock_test_and_set(),
365 # and define HAVE_GCC__SYNC_INT32_TAS
366 AC_DEFUN([PGAC_HAVE_GCC__SYNC_INT32_TAS],
367 [AC_CACHE_CHECK(for builtin __sync int32 locking functions, pgac_cv_gcc_sync_int32_tas,
368 [AC_TRY_LINK([],
369   [int lock = 0;
370    __sync_lock_test_and_set(&lock, 1);
371    __sync_lock_release(&lock);],
372   [pgac_cv_gcc_sync_int32_tas="yes"],
373   [pgac_cv_gcc_sync_int32_tas="no"])])
374 if test x"$pgac_cv_gcc_sync_int32_tas" = x"yes"; then
375   AC_DEFINE(HAVE_GCC__SYNC_INT32_TAS, 1, [Define to 1 if you have __sync_lock_test_and_set(int *) and friends.])
376 fi])# PGAC_HAVE_GCC__SYNC_INT32_TAS
377
378 # PGAC_HAVE_GCC__SYNC_INT32_CAS
379 # -------------------------
380 # Check if the C compiler understands __sync_compare_and_swap() for 32bit
381 # types, and define HAVE_GCC__SYNC_INT32_CAS if so.
382 AC_DEFUN([PGAC_HAVE_GCC__SYNC_INT32_CAS],
383 [AC_CACHE_CHECK(for builtin __sync int32 atomic operations, pgac_cv_gcc_sync_int32_cas,
384 [AC_TRY_LINK([],
385   [int val = 0;
386    __sync_val_compare_and_swap(&val, 0, 37);],
387   [pgac_cv_gcc_sync_int32_cas="yes"],
388   [pgac_cv_gcc_sync_int32_cas="no"])])
389 if test x"$pgac_cv_gcc_sync_int32_cas" = x"yes"; then
390   AC_DEFINE(HAVE_GCC__SYNC_INT32_CAS, 1, [Define to 1 if you have __sync_compare_and_swap(int *, int, int).])
391 fi])# PGAC_HAVE_GCC__SYNC_INT32_CAS
392
393 # PGAC_HAVE_GCC__SYNC_INT64_CAS
394 # -------------------------
395 # Check if the C compiler understands __sync_compare_and_swap() for 64bit
396 # types, and define HAVE_GCC__SYNC_INT64_CAS if so.
397 AC_DEFUN([PGAC_HAVE_GCC__SYNC_INT64_CAS],
398 [AC_CACHE_CHECK(for builtin __sync int64 atomic operations, pgac_cv_gcc_sync_int64_cas,
399 [AC_TRY_LINK([],
400   [PG_INT64_TYPE lock = 0;
401    __sync_val_compare_and_swap(&lock, 0, (PG_INT64_TYPE) 37);],
402   [pgac_cv_gcc_sync_int64_cas="yes"],
403   [pgac_cv_gcc_sync_int64_cas="no"])])
404 if test x"$pgac_cv_gcc_sync_int64_cas" = x"yes"; then
405   AC_DEFINE(HAVE_GCC__SYNC_INT64_CAS, 1, [Define to 1 if you have __sync_compare_and_swap(int64 *, int64, int64).])
406 fi])# PGAC_HAVE_GCC__SYNC_INT64_CAS
407
408 # PGAC_HAVE_GCC__ATOMIC_INT32_CAS
409 # -------------------------
410 # Check if the C compiler understands __atomic_compare_exchange_n() for 32bit
411 # types, and define HAVE_GCC__ATOMIC_INT32_CAS if so.
412 AC_DEFUN([PGAC_HAVE_GCC__ATOMIC_INT32_CAS],
413 [AC_CACHE_CHECK(for builtin __atomic int32 atomic operations, pgac_cv_gcc_atomic_int32_cas,
414 [AC_TRY_LINK([],
415   [int val = 0;
416    int expect = 0;
417    __atomic_compare_exchange_n(&val, &expect, 37, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);],
418   [pgac_cv_gcc_atomic_int32_cas="yes"],
419   [pgac_cv_gcc_atomic_int32_cas="no"])])
420 if test x"$pgac_cv_gcc_atomic_int32_cas" = x"yes"; then
421   AC_DEFINE(HAVE_GCC__ATOMIC_INT32_CAS, 1, [Define to 1 if you have __atomic_compare_exchange_n(int *, int *, int).])
422 fi])# PGAC_HAVE_GCC__ATOMIC_INT32_CAS
423
424 # PGAC_HAVE_GCC__ATOMIC_INT64_CAS
425 # -------------------------
426 # Check if the C compiler understands __atomic_compare_exchange_n() for 64bit
427 # types, and define HAVE_GCC__ATOMIC_INT64_CAS if so.
428 AC_DEFUN([PGAC_HAVE_GCC__ATOMIC_INT64_CAS],
429 [AC_CACHE_CHECK(for builtin __atomic int64 atomic operations, pgac_cv_gcc_atomic_int64_cas,
430 [AC_TRY_LINK([],
431   [PG_INT64_TYPE val = 0;
432    PG_INT64_TYPE expect = 0;
433    __atomic_compare_exchange_n(&val, &expect, 37, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);],
434   [pgac_cv_gcc_atomic_int64_cas="yes"],
435   [pgac_cv_gcc_atomic_int64_cas="no"])])
436 if test x"$pgac_cv_gcc_atomic_int64_cas" = x"yes"; then
437   AC_DEFINE(HAVE_GCC__ATOMIC_INT64_CAS, 1, [Define to 1 if you have __atomic_compare_exchange_n(int64 *, int *, int64).])
438 fi])# PGAC_HAVE_GCC__ATOMIC_INT64_CAS