source: trunk/lib/builtins.hpp @ 1853

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

Removed count forward, count reverse wrappers, and introduced
scan_word_t type for compile time architecture dependent scans.

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