c99_compat.h revision 01e04c3f
1af69d88dSmrg/**************************************************************************
2af69d88dSmrg *
3af69d88dSmrg * Copyright 2007-2013 VMware, Inc.
4af69d88dSmrg * All Rights Reserved.
5af69d88dSmrg *
6af69d88dSmrg * Permission is hereby granted, free of charge, to any person obtaining a
7af69d88dSmrg * copy of this software and associated documentation files (the
8af69d88dSmrg * "Software"), to deal in the Software without restriction, including
9af69d88dSmrg * without limitation the rights to use, copy, modify, merge, publish,
10af69d88dSmrg * distribute, sub license, and/or sell copies of the Software, and to
11af69d88dSmrg * permit persons to whom the Software is furnished to do so, subject to
12af69d88dSmrg * the following conditions:
13af69d88dSmrg *
14af69d88dSmrg * The above copyright notice and this permission notice (including the
15af69d88dSmrg * next paragraph) shall be included in all copies or substantial portions
16af69d88dSmrg * of the Software.
17af69d88dSmrg *
18af69d88dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19af69d88dSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20af69d88dSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21af69d88dSmrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22af69d88dSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23af69d88dSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24af69d88dSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25af69d88dSmrg *
26af69d88dSmrg **************************************************************************/
27af69d88dSmrg
2801e04c3fSmrg#include "no_extern_c.h"
2901e04c3fSmrg
30af69d88dSmrg#ifndef _C99_COMPAT_H_
31af69d88dSmrg#define _C99_COMPAT_H_
32af69d88dSmrg
33af69d88dSmrg
34af69d88dSmrg/*
35af69d88dSmrg * MSVC hacks.
36af69d88dSmrg */
37af69d88dSmrg#if defined(_MSC_VER)
3801e04c3fSmrg
3901e04c3fSmrg#  if _MSC_VER < 1900
4001e04c3fSmrg#    error "Microsoft Visual Studio 2015 or higher required"
4101e04c3fSmrg#  endif
4201e04c3fSmrg
43af69d88dSmrg   /*
4401e04c3fSmrg    * Visual Studio will complain if we define the `inline` keyword, but
45af69d88dSmrg    * actually it only supports the keyword on C++.
46af69d88dSmrg    *
47af69d88dSmrg    * To avoid this the _ALLOW_KEYWORD_MACROS must be set.
48af69d88dSmrg    */
4901e04c3fSmrg#  if !defined(_ALLOW_KEYWORD_MACROS)
50af69d88dSmrg#    define _ALLOW_KEYWORD_MACROS
51af69d88dSmrg#  endif
52af69d88dSmrg
53af69d88dSmrg   /*
54af69d88dSmrg    * XXX: MSVC has a `__restrict` keyword, but it also has a
55af69d88dSmrg    * `__declspec(restrict)` modifier, so it is impossible to define a
56af69d88dSmrg    * `restrict` macro without interfering with the latter.  Furthermore the
57af69d88dSmrg    * MSVC standard library uses __declspec(restrict) under the _CRTRESTRICT
58af69d88dSmrg    * macro.  For now resolve this issue by redefining _CRTRESTRICT, but going
59af69d88dSmrg    * forward we should probably should stop using restrict, especially
60af69d88dSmrg    * considering that our code does not obbey strict aliasing rules any way.
61af69d88dSmrg    */
62af69d88dSmrg#  include <crtdefs.h>
63af69d88dSmrg#  undef _CRTRESTRICT
64af69d88dSmrg#  define _CRTRESTRICT
65af69d88dSmrg#endif
66af69d88dSmrg
67af69d88dSmrg
68af69d88dSmrg/*
69af69d88dSmrg * C99 inline keyword
70af69d88dSmrg */
71af69d88dSmrg#ifndef inline
72af69d88dSmrg#  ifdef __cplusplus
73af69d88dSmrg     /* C++ supports inline keyword */
74af69d88dSmrg#  elif defined(__GNUC__)
75af69d88dSmrg#    define inline __inline__
76af69d88dSmrg#  elif defined(_MSC_VER)
77af69d88dSmrg#    define inline __inline
78af69d88dSmrg#  elif defined(__ICL)
79af69d88dSmrg#    define inline __inline
80af69d88dSmrg#  elif defined(__INTEL_COMPILER)
81af69d88dSmrg     /* Intel compiler supports inline keyword */
82af69d88dSmrg#  elif defined(__WATCOMC__) && (__WATCOMC__ >= 1100)
83af69d88dSmrg#    define inline __inline
84af69d88dSmrg#  elif (__STDC_VERSION__ >= 199901L)
85af69d88dSmrg     /* C99 supports inline keyword */
86af69d88dSmrg#  else
87af69d88dSmrg#    define inline
88af69d88dSmrg#  endif
89af69d88dSmrg#endif
90af69d88dSmrg
91af69d88dSmrg
92af69d88dSmrg/*
93af69d88dSmrg * C99 restrict keyword
94af69d88dSmrg *
95af69d88dSmrg * See also:
96af69d88dSmrg * - http://cellperformance.beyond3d.com/articles/2006/05/demystifying-the-restrict-keyword.html
97af69d88dSmrg */
98af69d88dSmrg#ifndef restrict
99af69d88dSmrg#  if (__STDC_VERSION__ >= 199901L)
100af69d88dSmrg     /* C99 */
101af69d88dSmrg#  elif defined(__GNUC__)
102af69d88dSmrg#    define restrict __restrict__
103af69d88dSmrg#  elif defined(_MSC_VER)
104af69d88dSmrg#    define restrict __restrict
105af69d88dSmrg#  else
106af69d88dSmrg#    define restrict /* */
107af69d88dSmrg#  endif
108af69d88dSmrg#endif
109af69d88dSmrg
110af69d88dSmrg
111af69d88dSmrg/*
112af69d88dSmrg * C99 __func__ macro
113af69d88dSmrg */
114af69d88dSmrg#ifndef __func__
115af69d88dSmrg#  if (__STDC_VERSION__ >= 199901L)
116af69d88dSmrg     /* C99 */
117af69d88dSmrg#  elif defined(__GNUC__)
11801e04c3fSmrg#    define __func__ __FUNCTION__
119af69d88dSmrg#  elif defined(_MSC_VER)
12001e04c3fSmrg#    define __func__ __FUNCTION__
121af69d88dSmrg#  else
122af69d88dSmrg#    define __func__ "<unknown>"
123af69d88dSmrg#  endif
124af69d88dSmrg#endif
125af69d88dSmrg
126af69d88dSmrg
127af69d88dSmrg/* Simple test case for debugging */
128af69d88dSmrg#if 0
129af69d88dSmrgstatic inline const char *
130af69d88dSmrgtest_c99_compat_h(const void * restrict a,
131af69d88dSmrg                  const void * restrict b)
132af69d88dSmrg{
133af69d88dSmrg   return __func__;
134af69d88dSmrg}
135af69d88dSmrg#endif
136af69d88dSmrg
137af69d88dSmrg
13801e04c3fSmrg/* Fallback definitions, for build systems other than autoconfig which don't
13901e04c3fSmrg * auto-detect these things. */
14001e04c3fSmrg#ifdef HAVE_NO_AUTOCONF
14101e04c3fSmrg
14201e04c3fSmrg#  ifndef _WIN32
14301e04c3fSmrg#    define HAVE_PTHREAD
14401e04c3fSmrg#    define HAVE_POSIX_MEMALIGN
14501e04c3fSmrg#  endif
14601e04c3fSmrg
14701e04c3fSmrg#  ifdef __GNUC__
14801e04c3fSmrg#    if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 2)
14901e04c3fSmrg#      error "GCC version 4.2 or higher required"
15001e04c3fSmrg#    endif
15101e04c3fSmrg
15201e04c3fSmrg     /* https://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Other-Builtins.html */
15301e04c3fSmrg#    define HAVE___BUILTIN_CLZ 1
15401e04c3fSmrg#    define HAVE___BUILTIN_CLZLL 1
15501e04c3fSmrg#    define HAVE___BUILTIN_CTZ 1
15601e04c3fSmrg#    define HAVE___BUILTIN_EXPECT 1
15701e04c3fSmrg#    define HAVE___BUILTIN_FFS 1
15801e04c3fSmrg#    define HAVE___BUILTIN_FFSLL 1
15901e04c3fSmrg#    define HAVE___BUILTIN_POPCOUNT 1
16001e04c3fSmrg#    define HAVE___BUILTIN_POPCOUNTLL 1
16101e04c3fSmrg     /* https://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Function-Attributes.html */
16201e04c3fSmrg#    define HAVE_FUNC_ATTRIBUTE_FLATTEN 1
16301e04c3fSmrg#    define HAVE_FUNC_ATTRIBUTE_UNUSED 1
16401e04c3fSmrg#    define HAVE_FUNC_ATTRIBUTE_FORMAT 1
16501e04c3fSmrg#    define HAVE_FUNC_ATTRIBUTE_PACKED 1
16601e04c3fSmrg#    define HAVE_FUNC_ATTRIBUTE_ALIAS 1
16701e04c3fSmrg#    define HAVE_FUNC_ATTRIBUTE_NORETURN 1
16801e04c3fSmrg
16901e04c3fSmrg#    if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
17001e04c3fSmrg       /* https://gcc.gnu.org/onlinedocs/gcc-4.3.6/gcc/Other-Builtins.html */
17101e04c3fSmrg#      define HAVE___BUILTIN_BSWAP32 1
17201e04c3fSmrg#      define HAVE___BUILTIN_BSWAP64 1
17301e04c3fSmrg#    endif
17401e04c3fSmrg
17501e04c3fSmrg#    if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
17601e04c3fSmrg#      define HAVE___BUILTIN_UNREACHABLE 1
17701e04c3fSmrg#    endif
17801e04c3fSmrg
17901e04c3fSmrg#  endif /* __GNUC__ */
18001e04c3fSmrg
18101e04c3fSmrg#endif /* !HAVE_AUTOCONF */
18201e04c3fSmrg
18301e04c3fSmrg
184af69d88dSmrg#endif /* _C99_COMPAT_H_ */
185