103b705cfSriastradh/*
203b705cfSriastradh * Copyright (c) 2011 Intel Corporation
303b705cfSriastradh *
403b705cfSriastradh * Permission is hereby granted, free of charge, to any person obtaining a
503b705cfSriastradh * copy of this software and associated documentation files (the "Software"),
603b705cfSriastradh * to deal in the Software without restriction, including without limitation
703b705cfSriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
803b705cfSriastradh * and/or sell copies of the Software, and to permit persons to whom the
903b705cfSriastradh * Software is furnished to do so, subject to the following conditions:
1003b705cfSriastradh *
1103b705cfSriastradh * The above copyright notice and this permission notice (including the next
1203b705cfSriastradh * paragraph) shall be included in all copies or substantial portions of the
1303b705cfSriastradh * Software.
1403b705cfSriastradh *
1503b705cfSriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1603b705cfSriastradh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1703b705cfSriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1803b705cfSriastradh * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1903b705cfSriastradh * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2003b705cfSriastradh * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2103b705cfSriastradh * SOFTWARE.
2203b705cfSriastradh *
2303b705cfSriastradh * Authors:
2403b705cfSriastradh *    Chris Wilson <chris@chris-wilson.co.uk>
2503b705cfSriastradh *
2603b705cfSriastradh */
2703b705cfSriastradh
2803b705cfSriastradh#ifndef _SNA_COMPILER_H_
2903b705cfSriastradh#define _SNA_COMPILER_H_
3003b705cfSriastradh
3103b705cfSriastradh#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
3203b705cfSriastradh#define likely(expr) (__builtin_expect (!!(expr), 1))
3303b705cfSriastradh#define unlikely(expr) (__builtin_expect (!!(expr), 0))
3403b705cfSriastradh#define noinline __attribute__((noinline))
35fe8aea9eSmrg#define force_inline inline /* __attribute__((always_inline)) */
3603b705cfSriastradh#define fastcall __attribute__((regparm(3)))
3703b705cfSriastradh#define must_check __attribute__((warn_unused_result))
3803b705cfSriastradh#define constant __attribute__((const))
3903b705cfSriastradh#define pure __attribute__((pure))
4003b705cfSriastradh#define tightly_packed __attribute__((__packed__))
4103b705cfSriastradh#define flatten __attribute__((flatten))
42fe8aea9eSmrg#define nonnull __attribute__((nonnull))
4303b705cfSriastradh#define page_aligned __attribute__((aligned(4096)))
4403b705cfSriastradh#else
4503b705cfSriastradh#define likely(expr) (expr)
4603b705cfSriastradh#define unlikely(expr) (expr)
4703b705cfSriastradh#define noinline
4803b705cfSriastradh#define force_inline inline
4903b705cfSriastradh#define fastcall
5003b705cfSriastradh#define must_check
5103b705cfSriastradh#define constant
5203b705cfSriastradh#define pure
5303b705cfSriastradh#define tighly_packed
5403b705cfSriastradh#define flatten
55fe8aea9eSmrg#define nonnull
5603b705cfSriastradh#define page_aligned
5703b705cfSriastradh#endif
5803b705cfSriastradh
5903b705cfSriastradh#define HAS_GCC(major, minor) defined(__GNUC__) && (__GNUC__ > (major) || __GNUC__ == (major) && __GNUC_MINOR__ >= (minor))
6003b705cfSriastradh
6103b705cfSriastradh#if HAS_GCC(4, 5)
62fe8aea9eSmrg#define sse2 fast __attribute__((target("sse2,fpmath=sse")))
63fe8aea9eSmrg#define sse4_2 fast __attribute__((target("sse4.2,sse2,fpmath=sse")))
6403b705cfSriastradh#endif
6503b705cfSriastradh
6603b705cfSriastradh#if HAS_GCC(4, 6) && defined(__OPTIMIZE__)
6703b705cfSriastradh#define fast __attribute__((optimize("Ofast")))
6803b705cfSriastradh#else
6903b705cfSriastradh#define fast
7003b705cfSriastradh#endif
7103b705cfSriastradh
72fe8aea9eSmrg#if HAS_GCC(4, 7)
73fe8aea9eSmrg#define avx2 fast __attribute__((target("avx2,avx,sse4.2,sse2,fpmath=sse")))
74fe8aea9eSmrg#define assume_aligned(ptr, align) __builtin_assume_aligned((ptr), (align))
75fe8aea9eSmrg#define assume_misaligned(ptr, align, offset) __builtin_assume_aligned((ptr), (align), (offset))
76fe8aea9eSmrg#else
77fe8aea9eSmrg#define assume_aligned(ptr, align) (ptr)
78fe8aea9eSmrg#define assume_misaligned(ptr, align, offset) (ptr)
79fe8aea9eSmrg#endif
80fe8aea9eSmrg
81fe8aea9eSmrg#if HAS_GCC(4, 5) && defined(__OPTIMIZE__)
82fe8aea9eSmrg#define fast_memcpy fast __attribute__((target("inline-all-stringops")))
8303b705cfSriastradh#else
8403b705cfSriastradh#define fast_memcpy
8503b705cfSriastradh#endif
8603b705cfSriastradh
8703b705cfSriastradh#ifdef HAVE_VALGRIND
8803b705cfSriastradh#define VG(x) x
8903b705cfSriastradh#else
9003b705cfSriastradh#define VG(x)
9103b705cfSriastradh#endif
9203b705cfSriastradh
9303b705cfSriastradh#define VG_CLEAR(s) VG(memset(&s, 0, sizeof(s)))
9403b705cfSriastradh
9503b705cfSriastradh#define COMPILE_TIME_ASSERT(E) ((void)sizeof(char[1 - 2*!(E)]))
9603b705cfSriastradh
9703b705cfSriastradh#endif /* _SNA_COMPILER_H_ */
98