source: trunk/lib/builtins.hpp @ 2196

Last change on this file since 2196 was 1950, checked in by ksherdy, 7 years ago

Updated comments format.

File size: 2.9 KB
Line 
1#ifndef BUILTINS_HPP_
2#define BUILTINS_HPP_
3
4/*=============================================================================
5    builtin - Compiler dependent builtin function wrappers.
6
7    Idealized SIMD Operations with SSE versions
8    Copyright (C) 2011, Robert D. Cameron, Kenneth S. Herdy.
9    Licensed to the public under the Open Software License 3.0.
10    Licensed to International Characters Inc.
11       under the Academic Free License version 3.0.
12=============================================================================*/
13
14#include "config.hpp"
15
16static IDISA_ALWAYS_INLINE long likely(long x);
17static IDISA_ALWAYS_INLINE long unlikely(long x);
18
19#if defined (_MSC_VER)
20        IDISA_ALWAYS_INLINE long likely(long x) {
21                return x;
22        }
23        IDISA_ALWAYS_INLINE long unlikely(long x) {
24                return x;
25        }
26
27#elif defined (__GNUC__)
28
29        IDISA_ALWAYS_INLINE long likely(long x) {
30                return __builtin_expect(x, 1);
31        }
32
33        IDISA_ALWAYS_INLINE long unlikely(long x) {
34                return __builtin_expect(x, 0);
35        }
36
37#endif
38
39#include <stdint.h>
40#if defined (_MSC_VER)
41        #include <intrin.h>
42
43        #ifdef _WIN64
44        typedef unsigned __int64 scanword_t;
45        #define ScanForwardIntrinsic _BitScanForward64
46        #define ScanReverseIntrinsic _BitScanReverse64
47        #else
48        typedef unsigned long scanword_t;
49        #define ScanForwardIntrinsic _BitScanForward
50        #define ScanReverseIntrinsic _BitScanReverse
51        #endif
52
53        #pragma intrinsic(ScanForwardIntrinsic)
54        #pragma intrinsic(ScanReverseIntrinsic)
55        IDISA_ALWAYS_INLINE uint32_t scan_forward_zeroes(scanword_t x) { // Precondition: x != 0
56                unsigned long zeroes;
57                ScanForwardIntrinsic(&zeroes, x);
58                return (uint32_t) zeroes;
59        }
60
61        IDISA_ALWAYS_INLINE uint32_t scan_backward_zeroes(scanword_t x) { // Precondition: x != 0
62                unsigned long pos;
63                ScanReverseIntrinsic(&pos, x);
64                return (uint32_t) (8* sizeof(scanword_t) - pos - 1);
65        }
66
67        #undef ScanForwardIntrinsic
68        #undef ScanReverseIntrinsic
69
70#elif defined (__GNUC__)
71
72        #if __x86_64__ /* 64-bit architecture */
73        typedef uint64_t scanword_t;
74        #define ScanForwardIntrinsic __builtin_ctzll
75        #define ScanReverseIntrinsic __builtin_clzll
76
77        #else
78        typedef uint32_t scanword_t;
79        #define ScanForwardIntrinsic __builtin_ctzl
80        #define ScanReverseIntrinsic __builtin_clzl
81        #endif
82
83        IDISA_ALWAYS_INLINE uint32_t scan_forward_zeroes(scanword_t x) { // Precondition: x != 0
84                return (uint32_t) ScanForwardIntrinsic((scanword_t) x);
85        }
86
87        IDISA_ALWAYS_INLINE uint32_t scan_backward_zeroes(scanword_t x) { // Precondition: x != 0
88                return (uint32_t) ScanReverseIntrinsic((scanword_t) x);
89        }
90
91        #undef ScanForwardIntrinsic
92        #undef ScanReverseIntrinsic
93
94#else
95  #warning "Neither _MSC_VER nor __GNUC__ defined: scan_forward/backward_zeroes not implemented."
96#endif
97
98#endif // BUILTINS_HPP
Note: See TracBrowser for help on using the repository browser.