Fix rounding for DIV_UNc()
[profile/ivi/pixman.git] / pixman / pixman-compiler.h
1 /* Pixman uses some non-standard compiler features. This file ensures
2  * they exist
3  *
4  * The features are:
5  *
6  *    FUNC           must be defined to expand to the current function
7  *    PIXMAN_EXPORT  should be defined to whatever is required to
8  *                   export functions from a shared library
9  *    limits         limits for various types must be defined
10  *    inline         must be defined
11  *    force_inline   must be defined
12  */
13 #if defined (__GNUC__)
14 #  define FUNC     ((const char*) (__PRETTY_FUNCTION__))
15 #elif defined (__sun) || (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
16 #  define FUNC     ((const char*) (__func__))
17 #else
18 #  define FUNC     ((const char*) ("???"))
19 #endif
20
21 #if defined (__GNUC__)
22 #  define MAYBE_UNUSED  __attribute__((unused))
23 #else
24 #  define MAYBE_UNUSED
25 #endif
26
27 #ifndef INT16_MIN
28 # define INT16_MIN              (-32767-1)
29 #endif
30
31 #ifndef INT16_MAX
32 # define INT16_MAX              (32767)
33 #endif
34
35 #ifndef INT32_MIN
36 # define INT32_MIN              (-2147483647-1)
37 #endif
38
39 #ifndef INT32_MAX
40 # define INT32_MAX              (2147483647)
41 #endif
42
43 #ifndef UINT32_MIN
44 # define UINT32_MIN             (0)
45 #endif
46
47 #ifndef UINT32_MAX
48 # define UINT32_MAX             (4294967295U)
49 #endif
50
51 #ifndef INT64_MIN
52 # define INT64_MIN              (-9223372036854775807-1)
53 #endif
54
55 #ifndef INT64_MAX
56 # define INT64_MAX              (9223372036854775807)
57 #endif
58
59
60 #ifndef M_PI
61 # define M_PI                   3.14159265358979323846
62 #endif
63
64 #ifdef _MSC_VER
65 /* 'inline' is available only in C++ in MSVC */
66 #   define inline __inline
67 #   define force_inline __forceinline
68 #   define noinline __declspec(noinline)
69 #elif defined __GNUC__ || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
70 #   define inline __inline__
71 #   define force_inline __inline__ __attribute__ ((__always_inline__))
72 #   define noinline __attribute__((noinline))
73 #else
74 #   ifndef force_inline
75 #      define force_inline inline
76 #   endif
77 #   ifndef noinline
78 #      define noinline
79 #   endif
80 #endif
81
82 /* GCC visibility */
83 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(_WIN32)
84 #   define PIXMAN_EXPORT __attribute__ ((visibility("default")))
85 /* Sun Studio 8 visibility */
86 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
87 #   define PIXMAN_EXPORT __global
88 #else
89 #   define PIXMAN_EXPORT
90 #endif
91
92 /* TLS */
93 #if defined(PIXMAN_NO_TLS)
94
95 #   define PIXMAN_DEFINE_THREAD_LOCAL(type, name)                       \
96     static type name
97 #   define PIXMAN_GET_THREAD_LOCAL(name)                                \
98     (&name)
99
100 #elif defined(TOOLCHAIN_SUPPORTS__THREAD)
101
102 #   define PIXMAN_DEFINE_THREAD_LOCAL(type, name)                       \
103     static __thread type name
104 #   define PIXMAN_GET_THREAD_LOCAL(name)                                \
105     (&name)
106
107 #elif defined(__MINGW32__)
108
109 #   define _NO_W32_PSEUDO_MODIFIERS
110 #   include <windows.h>
111
112 #   define PIXMAN_DEFINE_THREAD_LOCAL(type, name)                       \
113     static volatile int tls_ ## name ## _initialized = 0;               \
114     static void *tls_ ## name ## _mutex = NULL;                         \
115     static unsigned tls_ ## name ## _index;                             \
116                                                                         \
117     static type *                                                       \
118     tls_ ## name ## _alloc (void)                                       \
119     {                                                                   \
120         type *value = calloc (1, sizeof (type));                        \
121         if (value)                                                      \
122             TlsSetValue (tls_ ## name ## _index, value);                \
123         return value;                                                   \
124     }                                                                   \
125                                                                         \
126     static force_inline type *                                          \
127     tls_ ## name ## _get (void)                                         \
128     {                                                                   \
129         type *value;                                                    \
130         if (!tls_ ## name ## _initialized)                              \
131         {                                                               \
132             if (!tls_ ## name ## _mutex)                                \
133             {                                                           \
134                 void *mutex = CreateMutexA (NULL, 0, NULL);             \
135                 if (InterlockedCompareExchangePointer (                 \
136                         &tls_ ## name ## _mutex, mutex, NULL) != NULL)  \
137                 {                                                       \
138                     CloseHandle (mutex);                                \
139                 }                                                       \
140             }                                                           \
141             WaitForSingleObject (tls_ ## name ## _mutex, 0xFFFFFFFF);   \
142             if (!tls_ ## name ## _initialized)                          \
143             {                                                           \
144                 tls_ ## name ## _index = TlsAlloc ();                   \
145                 tls_ ## name ## _initialized = 1;                       \
146             }                                                           \
147             ReleaseMutex (tls_ ## name ## _mutex);                      \
148         }                                                               \
149         if (tls_ ## name ## _index == 0xFFFFFFFF)                       \
150             return NULL;                                                \
151         value = TlsGetValue (tls_ ## name ## _index);                   \
152         if (!value)                                                     \
153             value = tls_ ## name ## _alloc ();                          \
154         return value;                                                   \
155     }
156
157 #   define PIXMAN_GET_THREAD_LOCAL(name)                                \
158     tls_ ## name ## _get ()
159
160 #elif defined(_MSC_VER)
161
162 #   define PIXMAN_DEFINE_THREAD_LOCAL(type, name)                       \
163     static __declspec(thread) type name
164 #   define PIXMAN_GET_THREAD_LOCAL(name)                                \
165     (&name)
166
167 #elif defined(HAVE_PTHREAD_SETSPECIFIC)
168
169 #include <pthread.h>
170
171 #  define PIXMAN_DEFINE_THREAD_LOCAL(type, name)                        \
172     static pthread_once_t tls_ ## name ## _once_control = PTHREAD_ONCE_INIT; \
173     static pthread_key_t tls_ ## name ## _key;                          \
174                                                                         \
175     static void                                                         \
176     tls_ ## name ## _destroy_value (void *value)                        \
177     {                                                                   \
178         free (value);                                                   \
179     }                                                                   \
180                                                                         \
181     static void                                                         \
182     tls_ ## name ## _make_key (void)                                    \
183     {                                                                   \
184         pthread_key_create (&tls_ ## name ## _key,                      \
185                             tls_ ## name ## _destroy_value);            \
186     }                                                                   \
187                                                                         \
188     static type *                                                       \
189     tls_ ## name ## _alloc (void)                                       \
190     {                                                                   \
191         type *value = calloc (1, sizeof (type));                        \
192         if (value)                                                      \
193             pthread_setspecific (tls_ ## name ## _key, value);          \
194         return value;                                                   \
195     }                                                                   \
196                                                                         \
197     static force_inline type *                                          \
198     tls_ ## name ## _get (void)                                         \
199     {                                                                   \
200         type *value = NULL;                                             \
201         if (pthread_once (&tls_ ## name ## _once_control,               \
202                           tls_ ## name ## _make_key) == 0)              \
203         {                                                               \
204             value = pthread_getspecific (tls_ ## name ## _key);         \
205             if (!value)                                                 \
206                 value = tls_ ## name ## _alloc ();                      \
207         }                                                               \
208         return value;                                                   \
209     }
210
211 #   define PIXMAN_GET_THREAD_LOCAL(name)                                \
212     tls_ ## name ## _get ()
213
214 #else
215
216 #    error "Unknown thread local support for this system. Pixman will not work with multiple threads. Define PIXMAN_NO_TLS to acknowledge and accept this limitation and compile pixman without thread-safety support."
217
218 #endif