eal: introduce ymm type for AVX 256-bit
[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 #if defined(__AVX__)
61 #include <immintrin.h>
62 #endif
63
64 #else
65
66 #include <x86intrin.h>
67
68 #endif
69
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73
74 #define DUMMY_MACRO     do {} while (0)
75
76 /*
77  * rte_common related.
78  */
79 #define __rte_unused    __attribute__((__unused__))
80
81 #define RTE_PTR_ADD(ptr, x)     ((typeof(ptr))((uintptr_t)(ptr) + (x)))
82
83 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
84         (typeof(ptr))((uintptr_t)(ptr) & ~((uintptr_t)(align) - 1))
85
86 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
87         RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(ptr, (align) - 1), align)
88
89 #define RTE_PTR_ALIGN(ptr, align)       RTE_PTR_ALIGN_CEIL(ptr, align)
90
91 #define RTE_ALIGN_FLOOR(val, align) \
92         (typeof(val))((val) & (~((typeof(val))((align) - 1))))
93
94 #define RTE_ALIGN_CEIL(val, align) \
95         RTE_ALIGN_FLOOR(((val) + ((typeof(val))(align) - 1)), align)
96
97 #define RTE_ALIGN(ptr, align)   RTE_ALIGN_CEIL(ptr, align)
98
99 #define RTE_MIN(a, b)   ({ \
100                 typeof(a) _a = (a); \
101                 typeof(b) _b = (b); \
102                 _a < _b ? _a : _b;   \
103         })
104
105 #define RTE_DIM(a)              (sizeof(a) / sizeof((a)[0]))
106
107 /**
108  * Searches the input parameter for the least significant set bit
109  * (starting from zero).
110  * If a least significant 1 bit is found, its bit index is returned.
111  * If the content of the input parameter is zero, then the content of the return
112  * value is undefined.
113  * @param v
114  *     input parameter, should not be zero.
115  * @return
116  *     least significant set bit in the input parameter.
117  */
118 static inline uint32_t
119 rte_bsf32(uint32_t v)
120 {
121         asm("bsf %1,%0"
122                 : "=r" (v)
123                 : "rm" (v));
124         return v;
125 }
126
127 /*
128  * rte_common_vect related.
129  */
130 typedef __m128i xmm_t;
131
132 #define XMM_SIZE        (sizeof(xmm_t))
133 #define XMM_MASK        (XMM_SIZE - 1)
134
135 typedef union rte_xmm {
136         xmm_t    x;
137         uint8_t  u8[XMM_SIZE / sizeof(uint8_t)];
138         uint16_t u16[XMM_SIZE / sizeof(uint16_t)];
139         uint32_t u32[XMM_SIZE / sizeof(uint32_t)];
140         uint64_t u64[XMM_SIZE / sizeof(uint64_t)];
141         double   pd[XMM_SIZE / sizeof(double)];
142 } rte_xmm_t;
143
144 #ifdef __AVX__
145
146 typedef __m256i ymm_t;
147
148 #define YMM_SIZE        (sizeof(ymm_t))
149 #define YMM_MASK        (YMM_SIZE - 1)
150
151 typedef union rte_ymm {
152         ymm_t    y;
153         xmm_t    x[YMM_SIZE / sizeof(xmm_t)];
154         uint8_t  u8[YMM_SIZE / sizeof(uint8_t)];
155         uint16_t u16[YMM_SIZE / sizeof(uint16_t)];
156         uint32_t u32[YMM_SIZE / sizeof(uint32_t)];
157         uint64_t u64[YMM_SIZE / sizeof(uint64_t)];
158         double   pd[YMM_SIZE / sizeof(double)];
159 } rte_ymm_t;
160
161 #endif /* __AVX__ */
162
163 #ifdef RTE_ARCH_I686
164 #define _mm_cvtsi128_si64(a) ({ \
165         rte_xmm_t m;            \
166         m.x = (a);              \
167         (m.u64[0]);             \
168 })
169 #endif
170
171 /*
172  * rte_cycles related.
173  */
174 static inline uint64_t
175 rte_rdtsc(void)
176 {
177         union {
178                 uint64_t tsc_64;
179                 struct {
180                         uint32_t lo_32;
181                         uint32_t hi_32;
182                 };
183         } tsc;
184
185         asm volatile("rdtsc" :
186                 "=a" (tsc.lo_32),
187                 "=d" (tsc.hi_32));
188         return tsc.tsc_64;
189 }
190
191 /*
192  * rte_lcore related.
193  */
194 #define rte_lcore_id()  (0)
195
196 /*
197  * rte_errno related.
198  */
199 #define rte_errno       errno
200 #define E_RTE_NO_TAILQ  (-1)
201
202 /*
203  * rte_rwlock related.
204  */
205 #define rte_rwlock_read_lock(x)         DUMMY_MACRO
206 #define rte_rwlock_read_unlock(x)       DUMMY_MACRO
207 #define rte_rwlock_write_lock(x)        DUMMY_MACRO
208 #define rte_rwlock_write_unlock(x)      DUMMY_MACRO
209
210 /*
211  * rte_memory related.
212  */
213 #define SOCKET_ID_ANY   -1                  /**< Any NUMA socket. */
214 #define RTE_CACHE_LINE_SIZE     64                  /**< Cache line size. */
215 #define RTE_CACHE_LINE_MASK     (RTE_CACHE_LINE_SIZE-1) /**< Cache line mask. */
216
217 /**
218  * Force alignment to cache line.
219  */
220 #define __rte_cache_aligned     __attribute__((__aligned__(RTE_CACHE_LINE_SIZE)))
221
222
223 /*
224  * rte_byteorder related.
225  */
226 #define rte_le_to_cpu_16(x)     (x)
227 #define rte_le_to_cpu_32(x)     (x)
228
229 #define rte_cpu_to_be_16(x)     \
230         (((x) & UINT8_MAX) << CHAR_BIT | ((x) >> CHAR_BIT & UINT8_MAX))
231 #define rte_cpu_to_be_32(x)     __builtin_bswap32(x)
232
233 /*
234  * rte_branch_prediction related.
235  */
236 #ifndef likely
237 #define likely(x)       __builtin_expect((x), 1)
238 #endif  /* likely */
239
240 #ifndef unlikely
241 #define unlikely(x)     __builtin_expect((x), 0)
242 #endif  /* unlikely */
243
244
245 /*
246  * rte_tailq related.
247  */
248
249 struct rte_tailq_entry {
250         TAILQ_ENTRY(rte_tailq_entry) next; /**< Pointer entries for a tailq list
251  */
252         void *data; /**< Pointer to the data referenced by this tailq entry */
253 };
254
255 static inline void *
256 rte_dummy_tailq(void)
257 {
258         static __thread TAILQ_HEAD(rte_dummy_head, rte_dummy) dummy_head;
259         TAILQ_INIT(&dummy_head);
260         return &dummy_head;
261 }
262
263 #define RTE_TAILQ_LOOKUP_BY_IDX(idx, struct_name)       rte_dummy_tailq()
264
265 #define RTE_EAL_TAILQ_REMOVE(idx, type, elm)    DUMMY_MACRO
266
267 /*
268  * rte_string related
269  */
270 #define snprintf(str, len, frmt, args...)       snprintf(str, len, frmt, ##args)
271
272 /*
273  * rte_log related
274  */
275 #define RTE_LOG(l, t, fmt, args...)     printf(fmt, ##args)
276
277 /*
278  * rte_malloc related
279  */
280 #define rte_free(x)     free(x)
281
282 static inline void *
283 rte_zmalloc_socket(__rte_unused const char *type, size_t size, unsigned align,
284         __rte_unused int socket)
285 {
286         void *ptr;
287         int rc;
288
289         align = (align != 0) ? align : RTE_CACHE_LINE_SIZE;
290         rc = posix_memalign(&ptr, align, size);
291         if (rc != 0) {
292                 rte_errno = rc;
293                 return NULL;
294         }
295
296         memset(ptr, 0, size);
297         return ptr;
298 }
299
300 #define rte_zmalloc(type, sz, align)    rte_zmalloc_socket(type, sz, align, 0)
301
302 /*
303  * rte_debug related
304  */
305 #define rte_panic(fmt, args...) do {         \
306         RTE_LOG(CRIT, EAL, fmt, ##args);     \
307         abort();                             \
308 } while (0)
309
310 #define rte_exit(err, fmt, args...)     do { \
311         RTE_LOG(CRIT, EAL, fmt, ##args);     \
312         exit(err);                           \
313 } while (0)
314
315 #define rte_cpu_get_flag_enabled(x)     (0)
316
317 #ifdef __cplusplus
318 }
319 #endif
320
321 #endif /* _RTE_ACL_OSDEP_ALONE_H_ */