summaryrefslogtreecommitdiffstats
path: root/src/include/builtins.h
blob: aa7846ede0625faf0bb2e797c063cfc84110ed91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/include/builtins.h $                                      */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2011,2014              */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#include <stdint.h>

#ifndef _BUILTINS_H
#define _BUILTINS_H

#include <util/pp/for_each.h>

#ifdef __cplusplus
extern "C"
{
#endif

/**
 * This file should be the home of any use of gcc compiler builtins
 */

/**
 * Use of this macro will ensure that the functions object code never gets generated without being inlined
 */
#define ALWAYS_INLINE __attribute__((always_inline))

/**
 * Use of this macro will ensure that a function is never inlined.
 */
#define NEVER_INLINE __attribute__((noinline))

/**
 * Use of this macro will ensure a data structure is aligned on a cacheline boundary
 */
#define ALIGN_CACHELINE __attribute__((aligned (128)))

/**
 * Function declaration macro that tells the compiler to use printf format checking semantics
 *
 * TODO Could make this a function macro and pass in the 3 args rather than hard coding
 */
#define FORMAT_PRINTF __attribute__((format(printf, 1, 2)))

/**
 * Function delaration macro that tells the compiler that this function never returns.
 */
#define NO_RETURN __attribute__((noreturn))

/**
 * Function / variable declaration macro that tells the compiler what section this symbol should go into.
 */
#define SYMB_SECTION(x) __attribute__((section(#x)))

/**
 * Use of this macro will ensure a data structure is not padded
 */
#define PACKED __attribute__((packed))

/**
 * Use of this macro will hide compile errors when a variable is not used,
 * usually because it is used in debug / assert statements only.
 */
#define SUPPRESS_UNUSED_VARIABLE(...) \
    PREPROCESSOR_FOR_EACH((void),##__VA_ARGS__)

/**
 * Compiler hint for branch conditions. "condition is likely to be true"
 */
#define likely(expr) __builtin_expect((expr),1)

/**
 * Compiler hint for branch conditions. "condition is likely to be false"
 */
#define unlikely(expr) __builtin_expect((expr),0)

/**
 * Get the value of the link register
 *
 * @return the value of the link register
 */
ALWAYS_INLINE
static inline void *linkRegister()
{
    return __builtin_return_address(0);
}

/**
 * Get the value of the stack-frame pointer
 *
 * @return The value of the frame pointer.
 */
ALWAYS_INLINE
static inline void *framePointer()
{
    return __builtin_frame_address(0);
}

/**
 * Counts leading zeros of a uint64_t value
 *
 * @param value to check
 *
 * @return the number of leading zeros
 */
ALWAYS_INLINE
static inline uint64_t cntlzd(uint64_t value)
{
    return __builtin_clzl(value);
}

/**
 * Counts trailing zeros of a uint64_t value
 *
 * @param value to check
 *
 * @return the number of trailing zeros
 */
ALWAYS_INLINE
static inline uint64_t cnttzd(uint64_t value)
{
    return __builtin_ctzl(value);
}

#ifdef __cplusplus
};
#endif


#endif
OpenPOWER on IntegriCloud