Willy Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * include/common/compiler.h |
| 3 | * This files contains some compiler-specific settings. |
| 4 | * |
| 5 | * Copyright (C) 2000-2009 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
Willy Tarreau | f3bfede | 2011-07-25 11:38:17 +0200 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
Willy Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef _COMMON_COMPILER_H |
| 23 | #define _COMMON_COMPILER_H |
| 24 | |
| 25 | |
| 26 | /* |
| 27 | * Gcc before 3.0 needs [0] to declare a variable-size array |
| 28 | */ |
| 29 | #ifndef VAR_ARRAY |
| 30 | #if __GNUC__ < 3 |
| 31 | #define VAR_ARRAY 0 |
| 32 | #else |
| 33 | #define VAR_ARRAY |
| 34 | #endif |
| 35 | #endif |
| 36 | |
| 37 | |
| 38 | /* Support passing function parameters in registers. For this, the |
| 39 | * CONFIG_REGPARM macro has to be set to the maximal number of registers |
| 40 | * allowed. Some functions have intentionally received a regparm lower than |
| 41 | * their parameter count, it is in order to avoid register clobbering where |
| 42 | * they are called. |
| 43 | */ |
| 44 | #ifndef REGPRM1 |
| 45 | #if CONFIG_REGPARM >= 1 && __GNUC__ >= 3 |
| 46 | #define REGPRM1 __attribute__((regparm(1))) |
| 47 | #else |
| 48 | #define REGPRM1 |
| 49 | #endif |
| 50 | #endif |
| 51 | |
| 52 | #ifndef REGPRM2 |
| 53 | #if CONFIG_REGPARM >= 2 && __GNUC__ >= 3 |
| 54 | #define REGPRM2 __attribute__((regparm(2))) |
| 55 | #else |
| 56 | #define REGPRM2 REGPRM1 |
| 57 | #endif |
| 58 | #endif |
| 59 | |
| 60 | #ifndef REGPRM3 |
| 61 | #if CONFIG_REGPARM >= 3 && __GNUC__ >= 3 |
| 62 | #define REGPRM3 __attribute__((regparm(3))) |
| 63 | #else |
| 64 | #define REGPRM3 REGPRM2 |
| 65 | #endif |
| 66 | #endif |
| 67 | |
| 68 | |
| 69 | /* By default, gcc does not inline large chunks of code, but we want it to |
| 70 | * respect our choices. |
| 71 | */ |
| 72 | #if !defined(forceinline) |
| 73 | #if __GNUC__ < 3 |
| 74 | #define forceinline inline |
| 75 | #else |
| 76 | #define forceinline inline __attribute__((always_inline)) |
| 77 | #endif |
| 78 | #endif |
| 79 | |
Willy Tarreau | dbd25fc | 2017-11-20 21:22:17 +0100 | [diff] [blame] | 80 | /* silence the "unused" warnings without having to place painful #ifdefs. |
| 81 | * For use with variables or functions. |
| 82 | */ |
| 83 | #define __maybe_unused __attribute__((unused)) |
Willy Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * Gcc >= 3 provides the ability for the programme to give hints to the |
| 87 | * compiler about what branch of an if is most likely to be taken. This |
| 88 | * helps the compiler produce the most compact critical paths, which is |
| 89 | * generally better for the cache and to reduce the number of jumps. |
| 90 | */ |
| 91 | #if !defined(likely) |
| 92 | #if __GNUC__ < 3 |
| 93 | #define __builtin_expect(x,y) (x) |
| 94 | #define likely(x) (x) |
| 95 | #define unlikely(x) (x) |
Willy Tarreau | c939835 | 2017-10-08 22:26:03 +0200 | [diff] [blame] | 96 | #elif __GNUC__ < 4 || __GNUC__ >= 5 |
| 97 | /* gcc 3.x and 5.x do the best job at this */ |
Willy Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 98 | #define likely(x) (__builtin_expect((x) != 0, 1)) |
| 99 | #define unlikely(x) (__builtin_expect((x) != 0, 0)) |
| 100 | #else |
| 101 | /* GCC 4.x is stupid, it performs the comparison then compares it to 1, |
| 102 | * so we cheat in a dirty way to prevent it from doing this. This will |
| 103 | * only work with ints and booleans though. |
| 104 | */ |
| 105 | #define likely(x) (x) |
| 106 | #define unlikely(x) (__builtin_expect((unsigned long)(x), 0)) |
| 107 | #endif |
| 108 | #endif |
| 109 | |
Olivier Houchard | 928fbfa | 2018-01-24 18:17:06 +0100 | [diff] [blame] | 110 | #ifndef __GNUC_PREREQ__ |
| 111 | #if defined(__GNUC__) && !defined(__INTEL_COMPILER) |
| 112 | #define __GNUC_PREREQ__(ma, mi) \ |
| 113 | (__GNUC__ > (ma) || __GNUC__ == (ma) && __GNUC_MINOR__ >= (mi)) |
| 114 | #else |
| 115 | #define __GNUC_PREREQ__(ma, mi) 0 |
| 116 | #endif |
| 117 | #endif |
| 118 | |
| 119 | #ifndef offsetof |
| 120 | #if __GNUC_PREREQ__(4, 1) |
| 121 | #define offsetof(type, field) __builtin_offsetof(type, field) |
| 122 | #else |
| 123 | #define offsetof(type, field) \ |
Willy Tarreau | c03ea40 | 2018-07-30 11:47:35 +0200 | [diff] [blame] | 124 | ((size_t)(uintptr_t)((const volatile void *)&((type *)0)->field)) |
Olivier Houchard | 928fbfa | 2018-01-24 18:17:06 +0100 | [diff] [blame] | 125 | #endif |
| 126 | #endif |
| 127 | |
Willy Tarreau | cc05fba | 2009-10-27 21:40:18 +0100 | [diff] [blame] | 128 | |
| 129 | #endif /* _COMMON_COMPILER_H */ |