add prefix to cache line macros
[dpdk.git] / lib / librte_acl / rte_acl_osdep_alone.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_ACL_OSDEP_ALONE_H_
35 #define _RTE_ACL_OSDEP_ALONE_H_
36
37 /**
38  * @file
39  *
40  * RTE ACL OS dependent file.
41  * An example how to build/use ACL library standalone
42  * (without rest of DPDK).
43  * Don't include that file on it's own, use <rte_acl_osdep.h>.
44  */
45
46 #if (defined(__ICC) || (__GNUC__ == 4 &&  __GNUC_MINOR__ < 4))
47
48 #ifdef __SSE__
49 #include <xmmintrin.h>
50 #endif
51
52 #ifdef __SSE2__
53 #include <emmintrin.h>
54 #endif
55
56 #if defined(__SSE4_2__) || defined(__SSE4_1__)
57 #include <smmintrin.h>
58 #endif
59
60 #else
61
62 #include <x86intrin.h>
63
64 #endif
65
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69
70 #define DUMMY_MACRO     do {} while (0)
71
72 /*
73  * rte_common related.
74  */
75 #define __rte_unused    __attribute__((__unused__))
76
77 #define RTE_PTR_ADD(ptr, x)     ((typeof(ptr))((uintptr_t)(ptr) + (x)))
78
79 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
80         (typeof(ptr))((uintptr_t)(ptr) & ~((uintptr_t)(align) - 1))
81
82 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
83         RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(ptr, (align) - 1), align)
84
85 #define RTE_PTR_ALIGN(ptr, align)       RTE_PTR_ALIGN_CEIL(ptr, align)
86
87 #define RTE_ALIGN_FLOOR(val, align) \
88         (typeof(val))((val) & (~((typeof(val))((align) - 1))))
89
90 #define RTE_ALIGN_CEIL(val, align) \
91         RTE_ALIGN_FLOOR(((val) + ((typeof(val))(align) - 1)), align)
92
93 #define RTE_ALIGN(ptr, align)   RTE_ALIGN_CEIL(ptr, align)
94
95 #define RTE_MIN(a, b)   ({ \
96                 typeof(a) _a = (a); \
97                 typeof(b) _b = (b); \
98                 _a < _b ? _a : _b;   \
99         })
100
101 #define RTE_DIM(a)              (sizeof(a) / sizeof((a)[0]))
102
103 /**
104  * Searches the input parameter for the least significant set bit
105  * (starting from zero).
106  * If a least significant 1 bit is found, its bit index is returned.
107  * If the content of the input parameter is zero, then the content of the return
108  * value is undefined.
109  * @param v
110  *     input parameter, should not be zero.
111  * @return
112  *     least significant set bit in the input parameter.
113  */
114 static inline uint32_t
115 rte_bsf32(uint32_t v)
116 {
117         asm("bsf %1,%0"
118                 : "=r" (v)
119                 : "rm" (v));
120         return v;
121 }
122
123 /*
124  * rte_common_vect related.
125  */
126 typedef __m128i xmm_t;
127
128 #define XMM_SIZE        (sizeof(xmm_t))
129 #define XMM_MASK        (XMM_SIZE - 1)
130
131 typedef union rte_mmsse {
132         xmm_t    m;
133         uint8_t  u8[XMM_SIZE / sizeof(uint8_t)];
134         uint16_t u16[XMM_SIZE / sizeof(uint16_t)];
135         uint32_t u32[XMM_SIZE / sizeof(uint32_t)];
136         uint64_t u64[XMM_SIZE / sizeof(uint64_t)];
137         double   pd[XMM_SIZE / sizeof(double)];
138 } rte_xmm_t;
139
140 /*
141  * rte_cycles related.
142  */
143 static inline uint64_t
144 rte_rdtsc(void)
145 {
146         union {
147                 uint64_t tsc_64;
148                 struct {
149                         uint32_t lo_32;
150                         uint32_t hi_32;
151                 };
152         } tsc;
153
154         asm volatile("rdtsc" :
155                 "=a" (tsc.lo_32),
156                 "=d" (tsc.hi_32));
157         return tsc.tsc_64;
158 }
159
160 /*
161  * rte_lcore related.
162  */
163 #define rte_lcore_id()  (0)
164
165 /*
166  * rte_errno related.
167  */
168 #define rte_errno       errno
169 #define E_RTE_NO_TAILQ  (-1)
170
171 /*
172  * rte_rwlock related.
173  */
174 #define rte_rwlock_read_lock(x)         DUMMY_MACRO
175 #define rte_rwlock_read_unlock(x)       DUMMY_MACRO
176 #define rte_rwlock_write_lock(x)        DUMMY_MACRO
177 #define rte_rwlock_write_unlock(x)      DUMMY_MACRO
178
179 /*
180  * rte_memory related.
181  */
182 #define SOCKET_ID_ANY   -1                  /**< Any NUMA socket. */
183 #define RTE_CACHE_LINE_SIZE     64                  /**< Cache line size. */
184 #define RTE_CACHE_LINE_MASK     (RTE_CACHE_LINE_SIZE-1) /**< Cache line mask. */
185
186 /**
187  * Force alignment to cache line.
188  */
189 #define __rte_cache_aligned     __attribute__((__aligned__(RTE_CACHE_LINE_SIZE)))
190
191
192 /*
193  * rte_byteorder related.
194  */
195 #define rte_le_to_cpu_16(x)     (x)
196 #define rte_le_to_cpu_32(x)     (x)
197
198 #define rte_cpu_to_be_16(x)     \
199         (((x) & UINT8_MAX) << CHAR_BIT | ((x) >> CHAR_BIT & UINT8_MAX))
200 #define rte_cpu_to_be_32(x)     __builtin_bswap32(x)
201
202 /*
203  * rte_branch_prediction related.
204  */
205 #ifndef likely
206 #define likely(x)       __builtin_expect((x), 1)
207 #endif  /* likely */
208
209 #ifndef unlikely
210 #define unlikely(x)     __builtin_expect((x), 0)
211 #endif  /* unlikely */
212
213
214 /*
215  * rte_tailq related.
216  */
217 static inline void *
218 rte_dummy_tailq(void)
219 {
220         static __thread TAILQ_HEAD(rte_dummy_head, rte_dummy) dummy_head;
221         TAILQ_INIT(&dummy_head);
222         return &dummy_head;
223 }
224
225 #define RTE_TAILQ_LOOKUP_BY_IDX(idx, struct_name)       rte_dummy_tailq()
226
227 #define RTE_EAL_TAILQ_REMOVE(idx, type, elm)    DUMMY_MACRO
228
229 /*
230  * rte_string related
231  */
232 #define snprintf(str, len, frmt, args...)       snprintf(str, len, frmt, ##args)
233
234 /*
235  * rte_log related
236  */
237 #define RTE_LOG(l, t, fmt, args...)     printf(fmt, ##args)
238
239 /*
240  * rte_malloc related
241  */
242 #define rte_free(x)     free(x)
243
244 static inline void *
245 rte_zmalloc_socket(__rte_unused const char *type, size_t size, unsigned align,
246         __rte_unused int socket)
247 {
248         void *ptr;
249         int rc;
250
251         rc = posix_memalign(&ptr, align, size);
252         if (rc != 0) {
253                 rte_errno = rc;
254                 return NULL;
255         }
256
257         memset(ptr, 0, size);
258         return ptr;
259 }
260
261 /*
262  * rte_debug related
263  */
264 #define rte_panic(fmt, args...) do {         \
265         RTE_LOG(CRIT, EAL, fmt, ##args);     \
266         abort();                             \
267 } while (0)
268
269 #define rte_exit(err, fmt, args...)     do { \
270         RTE_LOG(CRIT, EAL, fmt, ##args);     \
271         exit(err);                           \
272 } while (0)
273
274 #ifdef __cplusplus
275 }
276 #endif
277
278 #endif /* _RTE_ACL_OSDEP_ALONE_H_ */