source: trunk/lib/builtins.hpp @ 1835

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

Add count_forward_zeroes, count_backward_zeroes.

File size: 2.1 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);
20static IDISA_ALWAYS_INLINE unsigned long cfzll(unsigned long long x);
21static 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        #include <intrin.h>
32        #pragma intrinsic(_BitScanForward)
33        IDISA_ALWAYS_INLINE unsigned long cfzl(unsigned long x) { // Precondition: x > 0
34                unsigned long zeroes;
35                _BitScanForward(&zeroes, x);
36                return zeroes;
37        }
38
39        IDISA_ALWAYS_INLINE unsigned long cbzl(unsigned long x) {
40                unsigned long zeroes;
41                _BitScanReverse(&zeroes, x);
42                return zeroes;
43        }
44
45        IDISA_ALWAYS_INLINE unsigned long cfzll(unsigned long long x) { // TODO - test
46                unsigned __int64 zeroes;
47                _BitScanForward(&zeroes, x);
48                return zeroes;
49        }
50
51        IDISA_ALWAYS_INLINE unsigned long cbzll(unsigned long long x) { // TODO - test
52                unsigned __int64 zeroes;
53                _BitScanReverse(&zeroes, x);
54                return zeroes;
55        }
56
57#elif defined (__GNUC__)
58
59        IDISA_ALWAYS_INLINE long likely(long x) {
60                return __builtin_expect(x, 1);
61        }
62
63        IDISA_ALWAYS_INLINE long unlikely(long x) {
64                return __builtin_expect(x, 0);
65        }
66
67        IDISA_ALWAYS_INLINE unsigned long cfzl(unsigned long x) {
68                return __builtin_ctzl(x);
69        }
70
71        IDISA_ALWAYS_INLINE unsigned long cbzl(unsigned long x) {
72                return __builtin_clzl(x);
73        }
74
75        IDISA_ALWAYS_INLINE unsigned long cfzll(unsigned long long x) {
76                return __builtin_ctzll(x);
77        }
78
79        IDISA_ALWAYS_INLINE unsigned long cbzll(unsigned long long x) {
80                return __builtin_clzll(x);
81        }
82
83#endif
84
85#endif /* BUILTINS_HPP */
Note: See TracBrowser for help on using the repository browser.