source: trunk/lib/builtins.hpp @ 2220

Last change on this file since 2220 was 2220, checked in by cameron, 7 years ago

Configuration for MS x64

File size: 2.9 KB
RevLine 
[1950]1#ifndef BUILTINS_HPP_
2#define BUILTINS_HPP_
3
4/*=============================================================================
[1528]5    builtin - Compiler dependent builtin function wrappers.
[1509]6
7    Idealized SIMD Operations with SSE versions
[1528]8    Copyright (C) 2011, Robert D. Cameron, Kenneth S. Herdy.
[1509]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.
[1950]12=============================================================================*/
[1509]13
14#include "config.hpp"
15
[1550]16static IDISA_ALWAYS_INLINE long likely(long x);
17static IDISA_ALWAYS_INLINE long unlikely(long x);
[1509]18
[1528]19#if defined (_MSC_VER)
[1550]20        IDISA_ALWAYS_INLINE long likely(long x) {
[1509]21                return x;
22        }
[1550]23        IDISA_ALWAYS_INLINE long unlikely(long x) {
[1509]24                return x;
25        }
[1528]26
27#elif defined (__GNUC__)
[1550]28
29        IDISA_ALWAYS_INLINE long likely(long x) {
[1509]30                return __builtin_expect(x, 1);
31        }
[1528]32
[1550]33        IDISA_ALWAYS_INLINE long unlikely(long x) {
[1509]34                return __builtin_expect(x, 0);
35        }
[1528]36
[1509]37#endif
38
[1848]39#include <stdint.h>
40#if defined (_MSC_VER)
41        #include <intrin.h>
42
[2220]43        #ifdef _M_X64
[1885]44        typedef unsigned __int64 scanword_t;
[1848]45        #define ScanForwardIntrinsic _BitScanForward64
46        #define ScanReverseIntrinsic _BitScanReverse64
47        #else
[2220]48        typedef unsigned __int32 scanword_t;
[1848]49        #define ScanForwardIntrinsic _BitScanForward
50        #define ScanReverseIntrinsic _BitScanReverse
51        #endif
52
53        #pragma intrinsic(ScanForwardIntrinsic)
54        #pragma intrinsic(ScanReverseIntrinsic)
[1888]55        IDISA_ALWAYS_INLINE uint32_t scan_forward_zeroes(scanword_t x) { // Precondition: x != 0
[1848]56                unsigned long zeroes;
57                ScanForwardIntrinsic(&zeroes, x);
58                return (uint32_t) zeroes;
59        }
60
[1888]61        IDISA_ALWAYS_INLINE uint32_t scan_backward_zeroes(scanword_t x) { // Precondition: x != 0
[1904]62                unsigned long pos;
[1905]63                ScanReverseIntrinsic(&pos, x);
[1904]64                return (uint32_t) (8* sizeof(scanword_t) - pos - 1);
[1848]65        }
66
[1853]67        #undef ScanForwardIntrinsic
68        #undef ScanReverseIntrinsic
69
[1848]70#elif defined (__GNUC__)
71
[1862]72        #if __x86_64__ /* 64-bit architecture */
[1848]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
[1888]83        IDISA_ALWAYS_INLINE uint32_t scan_forward_zeroes(scanword_t x) { // Precondition: x != 0
[1848]84                return (uint32_t) ScanForwardIntrinsic((scanword_t) x);
85        }
86
[1888]87        IDISA_ALWAYS_INLINE uint32_t scan_backward_zeroes(scanword_t x) { // Precondition: x != 0
[1856]88                return (uint32_t) ScanReverseIntrinsic((scanword_t) x);
[1848]89        }
90
[1853]91        #undef ScanForwardIntrinsic
92        #undef ScanReverseIntrinsic
93
[1848]94#else
95  #warning "Neither _MSC_VER nor __GNUC__ defined: scan_forward/backward_zeroes not implemented."
96#endif
97
[1950]98#endif // BUILTINS_HPP
Note: See TracBrowser for help on using the repository browser.