1 /************************************************************************** 2 * 3 * Copyright 2007-2008 VMware, Inc. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28 #ifndef P_COMPILER_H 29 #define P_COMPILER_H 30 31 32 33 #include "p_config.h" 34 35 #include <stdlib.h> 36 #include <string.h> 37 #include <stddef.h> 38 #include <stdarg.h> 39 #include <limits.h> 40 #include <sys/types.h> 41 42 43 #if defined(_WIN32) && !defined(__WIN32__) 44 #define __WIN32__ 45 #endif 46 47 #if defined(_MSC_VER) 48 49 /* Avoid 'expression is always true' warning */ 50 #pragma warning(disable: 4296) 51 52 #endif /* _MSC_VER */ 53 54 55 /* 56 * Alternative stdint.h and stdbool.h headers are supplied in include/c99 for 57 * systems that lack it. 58 */ 59 #ifndef __STDC_LIMIT_MACROS 60 #define __STDC_LIMIT_MACROS 1 61 #endif 62 #include <stdint.h> 63 #include <stdbool.h> 64 65 66 #ifdef __cplusplus 67 extern "C" { 68 #endif 69 70 71 #if !defined(__HAIKU__) && !defined(__USE_MISC) 72 #if !defined(PIPE_OS_ANDROID) 73 typedef unsigned int uint; 74 #endif 75 typedef unsigned short ushort; 76 #endif 77 typedef unsigned char ubyte; 78 79 typedef unsigned char boolean; 80 #ifndef TRUE 81 #define TRUE true 82 #endif 83 #ifndef FALSE 84 #define FALSE false 85 #endif 86 87 #ifndef va_copy 88 #ifdef __va_copy 89 #define va_copy(dest, src) __va_copy((dest), (src)) 90 #else 91 #define va_copy(dest, src) (dest) = (src) 92 #endif 93 #endif 94 95 /* Function visibility */ 96 #ifndef PUBLIC 97 # if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)) 98 # define PUBLIC __attribute__((visibility("default"))) 99 # elif defined(_MSC_VER) 100 # define PUBLIC __declspec(dllexport) 101 # else 102 # define PUBLIC 103 # endif 104 #endif 105 106 107 /* XXX: Use standard `__func__` instead */ 108 #ifndef __FUNCTION__ 109 # define __FUNCTION__ __func__ 110 #endif 111 112 113 /* This should match linux gcc cdecl semantics everywhere, so that we 114 * just codegen one calling convention on all platforms. 115 */ 116 #ifdef _MSC_VER 117 #define PIPE_CDECL __cdecl 118 #else 119 #define PIPE_CDECL 120 #endif 121 122 123 124 #if defined(__GNUC__) 125 #define PIPE_DEPRECATED __attribute__((__deprecated__)) 126 #else 127 #define PIPE_DEPRECATED 128 #endif 129 130 131 132 /* Macros for data alignment. */ 133 #if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)) || defined(__SUNPRO_CC) 134 135 /* See http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Type-Attributes.html */ 136 #define PIPE_ALIGN_TYPE(_alignment, _type) _type __attribute__((aligned(_alignment))) 137 138 /* See http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Variable-Attributes.html */ 139 #define PIPE_ALIGN_VAR(_alignment) __attribute__((aligned(_alignment))) 140 141 #if (__GNUC__ > 4 || (__GNUC__ == 4 &&__GNUC_MINOR__>1)) && !defined(PIPE_ARCH_X86_64) 142 #define PIPE_ALIGN_STACK __attribute__((force_align_arg_pointer)) 143 #else 144 #define PIPE_ALIGN_STACK 145 #endif 146 147 #elif defined(_MSC_VER) 148 149 /* See http://msdn.microsoft.com/en-us/library/83ythb65.aspx */ 150 #define PIPE_ALIGN_TYPE(_alignment, _type) __declspec(align(_alignment)) _type 151 #define PIPE_ALIGN_VAR(_alignment) __declspec(align(_alignment)) 152 153 #define PIPE_ALIGN_STACK 154 155 #elif defined(SWIG) 156 157 #define PIPE_ALIGN_TYPE(_alignment, _type) _type 158 #define PIPE_ALIGN_VAR(_alignment) 159 160 #define PIPE_ALIGN_STACK 161 162 #else 163 164 #error "Unsupported compiler" 165 166 #endif 167 168 169 #if defined(__GNUC__) 170 171 #define PIPE_READ_WRITE_BARRIER() __asm__("":::"memory") 172 173 #elif defined(_MSC_VER) 174 175 void _ReadWriteBarrier(void); 176 #pragma intrinsic(_ReadWriteBarrier) 177 #define PIPE_READ_WRITE_BARRIER() _ReadWriteBarrier() 178 179 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) 180 181 #define PIPE_READ_WRITE_BARRIER() __machine_rw_barrier() 182 183 #else 184 185 #warning "Unsupported compiler" 186 #define PIPE_READ_WRITE_BARRIER() /* */ 187 188 #endif 189 190 191 /* You should use these macros to mark if blocks where the if condition 192 * is either likely to be true, or unlikely to be true. 193 * 194 * This will inform human readers of this fact, and will also inform 195 * the compiler, who will in turn inform the CPU. 196 * 197 * CPUs often start executing code inside the if or the else blocks 198 * without knowing whether the condition is true or not, and will have 199 * to throw the work away if they find out later they executed the 200 * wrong part of the if. 201 * 202 * If these macros are used, the CPU is more likely to correctly predict 203 * the right path, and will avoid speculatively executing the wrong branch, 204 * thus not throwing away work, resulting in better performance. 205 * 206 * In light of this, it is also a good idea to mark as "likely" a path 207 * which is not necessarily always more likely, but that will benefit much 208 * more from performance improvements since it is already much faster than 209 * the other path, or viceversa with "unlikely". 210 * 211 * Example usage: 212 * if(unlikely(do_we_need_a_software_fallback())) 213 * do_software_fallback(); 214 * else 215 * render_with_gpu(); 216 * 217 * The macros follow the Linux kernel convention, and more examples can 218 * be found there. 219 * 220 * Note that profile guided optimization can offer better results, but 221 * needs an appropriate coverage suite and does not inform human readers. 222 */ 223 #ifndef likely 224 # if defined(__GNUC__) 225 # define likely(x) __builtin_expect(!!(x), 1) 226 # define unlikely(x) __builtin_expect(!!(x), 0) 227 # else 228 # define likely(x) (x) 229 # define unlikely(x) (x) 230 # endif 231 #endif 232 233 234 /** 235 * Static (compile-time) assertion. 236 * Basically, use COND to dimension an array. If COND is false/zero the 237 * array size will be -1 and we'll get a compilation error. 238 */ 239 #define STATIC_ASSERT(COND) \ 240 do { \ 241 (void) sizeof(char [1 - 2*!(COND)]); \ 242 } while (0) 243 244 245 #if defined(__cplusplus) 246 } 247 #endif 248 249 250 #endif /* P_COMPILER_H */ 251