source: trunk/lib/builtins.hpp @ 1854

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

Removed #if 0.

File size: 2.8 KB
RevLine 
[1509]1/*
[1528]2    builtin - Compiler dependent builtin function wrappers.
[1509]3
4    Idealized SIMD Operations with SSE versions
[1528]5    Copyright (C) 2011, Robert D. Cameron, Kenneth S. Herdy.
[1509]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
[1528]11#ifndef BUILTINS_HPP_
12#define BUILTINS_HPP_
[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
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
[1853]67        #undef ScanForwardIntrinsic
68        #undef ScanReverseIntrinsic
69
[1848]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
[1853]91        #undef ScanForwardIntrinsic
92        #undef ScanReverseIntrinsic
93
[1848]94#else
[1850]95# ifndef _MSC_VER
[1848]96  #warning "Neither _MSC_VER nor __GNUC__ defined: scan_forward/backward_zeroes not implemented."
[1850]97# endif
[1848]98#endif
99
[1528]100#endif /* BUILTINS_HPP */
Note: See TracBrowser for help on using the repository browser.