source: trunk/lib/builtin.hpp @ 1509

Last change on this file since 1509 was 1509, checked in by ksherdy, 8 years ago

Added builtin.hpp for compiler specific builtins. Prefer GCC supported builtins, function decorators over ICC.

File size: 1.9 KB
Line 
1/*
2    builtin - Compiler dependent builtins.
3
4    Idealized SIMD Operations with SSE versions
5    Copyright (C) 2011, Robert D. Cameron, Kenneth S. Herdy, Hua Huang and Nigel Medforth.
6    Licensed to the public under the Open Software License 3.0.
7    Licensed to International Characters Inc.
8       under the Academic Free License version 3.0.
9 */
10
11#ifndef BUILTIN_HPP_
12#define BUILTIN_HPP_
13
14#include "config.hpp"
15
16static IDISA_INLINE unsigned long cfzl(unsigned long x);
17static IDISA_INLINE unsigned long cbzl(unsigned long x);
18static IDISA_INLINE long likely(long x);
19static IDISA_INLINE long unlikely(long x);
20
21#if defined _MSC_VER
22        static IDISA_INLINE long likely(long x) {
23                return x;
24        }
25        static IDISA_INLINE long unlikely(long x) {
26                return x;
27        }
28        #include <intrin.h>
29        #pragma intrinsic(_BitScanForward)
30        static IDISA_INLINE unsigned long cfzl(unsigned long x) { // Precondition: x > 0
31                unsigned long zeroes;
32                _BitScanForward(&zeroes, x);
33                return zeroes;
34        }
35        static IDISA_INLINE unsigned long cbzl(unsigned long x) {
36                unsigned long zeroes;
37                _BitScanReverse(&zeroes, x);
38                return zeroes;
39        }
40#elif defined __GNUC__
41        static IDISA_INLINE long likely(long x) {
42                return __builtin_expect(x, 1);
43        }
44        static IDISA_INLINE long unlikely(long x) {
45                return __builtin_expect(x, 0);
46        }
47        static IDISA_INLINE unsigned long cfzl(unsigned long x) {
48                return __builtin_ctzl(x);
49        }
50        static IDISA_INLINE unsigned long cbzl(unsigned long x) {
51                return __builtin_clzl(x);
52        }
53#elif defined __INTEL_COMPILER
54        static IDISA_INLINE long likely(long x) {
55                return __builtin_expect(x, 1);
56        }
57        static IDISA_INLINE long unlikely(long x) {
58                return __builtin_expect(x, 0);
59        }
60        static IDISA_INLINE unsigned long cfzl(unsigned long x) {
61                return _bit_scan_forward(x);
62        }
63        static IDISA_INLINE unsigned long cbzl(unsigned long x) {
64                return _bit_scan_forward(int x)
65        }
66
67#else
68        #error "Compiler not supported. Aborting compilation."
69#endif
70
71#endif /* BUILTIN */
Note: See TracBrowser for help on using the repository browser.