source: trunk/lib/builtins.hpp @ 1854

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

Removed #if 0.

File size: 2.8 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);
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 uint64_t scanword_t;
45        #define ScanForwardIntrinsic _BitScanForward64
46        #define ScanReverseIntrinsic _BitScanReverse64
47        #else
48        typedef uint32_t 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 zeroes;
63                ScanReverseIntrinsic(&zeroes, x);
64                return (uint32_t) zeroes;
65        }
66
67        #undef ScanForwardIntrinsic
68        #undef ScanReverseIntrinsic
69
70#elif defined (__GNUC__)
71
72        #if UINTPTR_MAX == 0xffffffffffffffff  /* 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) ScanBackwardIntrinsic((scanword_t) x);
89        }
90
91        #undef ScanForwardIntrinsic
92        #undef ScanReverseIntrinsic
93
94#else
95# ifndef _MSC_VER
96  #warning "Neither _MSC_VER nor __GNUC__ defined: scan_forward/backward_zeroes not implemented."
97# endif
98#endif
99
100#endif /* BUILTINS_HPP */
Note: See TracBrowser for help on using the repository browser.