about summary refs log tree commit diff homepage
path: root/include/klee/ADT/Bits.h
blob: ba25f94f08ac0d941aa15e0002b0d4c5d4e8bc76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//===-- Bits.h --------------------------------------------------*- C++ -*-===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef KLEE_BITS_H
#define KLEE_BITS_H

#include "klee/Config/Version.h"

#include "llvm/Support/DataTypes.h"

#include <cassert>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>

namespace klee {
  namespace bits32 {
    // @pre(0 <= N <= 32)
    // @post(retval = max([truncateToNBits(i,N) for i in naturals()]))
    inline unsigned maxValueOfNBits(unsigned N) {
      assert(N <= 32);
      if (N==0)
        return 0;
      return (UINT32_C(-1)) >> (32 - N);
    }

    // @pre(0 < N <= 32)
    inline unsigned truncateToNBits(unsigned x, unsigned N) {
      assert(N > 0 && N <= 32);
      return x&((UINT32_C(-1)) >> (32 - N));
    }

    inline unsigned withoutRightmostBit(unsigned x) {
      return x&(x-1);
    }

    inline unsigned isolateRightmostBit(unsigned x) {
      return x&-x;
    }

    inline unsigned isPowerOfTwo(unsigned x) {
      if (x==0) return 0;
      return !(x&(x-1));
    }

    // @pre(withoutRightmostBit(x) == 0)
    // @post((1 << retval) == x)
    inline unsigned indexOfSingleBit(unsigned x) {
      assert(withoutRightmostBit(x) == 0);
      unsigned res = 0;
      if (x&0xFFFF0000) res += 16;
      if (x&0xFF00FF00) res += 8;
      if (x&0xF0F0F0F0) res += 4;
      if (x&0xCCCCCCCC) res += 2;
      if (x&0xAAAAAAAA) res += 1;
      assert(res < 32);
      assert((UINT32_C(1) << res) == x);
      return res;
    }
  }

  namespace bits64 {
    // @pre(0 <= N <= 64)
    // @post(retval = max([truncateToNBits(i,N) for i in naturals()]))
    inline uint64_t maxValueOfNBits(unsigned N) {
      assert(N <= 64);
      if (N==0)
        return 0;
      return ((UINT64_C(-1)) >> (64 - N));
    }
    
    // @pre(0 < N <= 64)
    inline uint64_t truncateToNBits(uint64_t x, unsigned N) {
      assert(N > 0 && N <= 64);
      return x&((UINT64_C(-1)) >> (64 - N));
    }

    inline uint64_t withoutRightmostBit(uint64_t x) {
      return x&(x-1);
    }

    inline uint64_t isolateRightmostBit(uint64_t x) {
      return x&-x;
    }

    inline uint64_t isPowerOfTwo(uint64_t x) {
      if (x==0) return 0;
      return !(x&(x-1));
    }

    // @pre((x&(x-1)) == 0)
    // @post((1 << retval) == x)
    inline unsigned indexOfSingleBit(uint64_t x) {
      assert((x & (x - 1)) == 0);
      unsigned res = bits32::indexOfSingleBit((unsigned) (x | (x>>32)));
      if (x & (UINT64_C(0xFFFFFFFF) << 32))
        res += 32;
      assert(res < 64);
      assert((UINT64_C(1) << res) == x);
      return res;
    }
  }

  template <typename T>
  [[nodiscard]] static constexpr inline auto countLeadingZeroes(T &&x) noexcept
      -> std::enable_if_t<!std::numeric_limits<std::decay_t<T>>::is_signed &&
                              std::numeric_limits<std::decay_t<T>>::digits ==
                                  std::numeric_limits<unsigned>::digits,
                          int> {
      assert(x > 0);
      return __builtin_clz(static_cast<unsigned>(x));
  }

  template <typename T>
  [[nodiscard]] static constexpr inline auto countLeadingZeroes(T &&x) noexcept
      -> std::enable_if_t<!std::numeric_limits<std::decay_t<T>>::is_signed &&
                              std::numeric_limits<std::decay_t<T>>::digits ==
                                  std::numeric_limits<unsigned long>::digits &&
                              std::numeric_limits<unsigned>::digits !=
                                  std::numeric_limits<unsigned long>::digits,
                          int> {
      assert(x > 0);
      return __builtin_clzl(static_cast<unsigned long>(x));
  }

  template <typename T>
  [[nodiscard]] static constexpr inline auto countLeadingZeroes(T &&x) noexcept
      -> std::enable_if_t<
          !std::numeric_limits<std::decay_t<T>>::is_signed &&
              std::numeric_limits<std::decay_t<T>>::digits ==
                  std::numeric_limits<unsigned long long>::digits &&
              std::numeric_limits<unsigned>::digits !=
                  std::numeric_limits<unsigned long long>::digits &&
              std::numeric_limits<unsigned long>::digits !=
                  std::numeric_limits<unsigned long long>::digits,
          int> {
      assert(x > 0);
      return __builtin_clzll(static_cast<unsigned long long>(x));
  }

  template <typename T>
  [[nodiscard]] static constexpr inline auto countTrailingZeroes(T &&x) noexcept
      -> std::enable_if_t<!std::numeric_limits<std::decay_t<T>>::is_signed &&
                              std::numeric_limits<std::decay_t<T>>::digits ==
                                  std::numeric_limits<unsigned>::digits,
                          int> {
      assert(x > 0);
      return __builtin_ctz(static_cast<unsigned>(x));
  }

  template <typename T>
  [[nodiscard]] static constexpr inline auto countTrailingZeroes(T &&x) noexcept
      -> std::enable_if_t<!std::numeric_limits<std::decay_t<T>>::is_signed &&
                              std::numeric_limits<std::decay_t<T>>::digits ==
                                  std::numeric_limits<unsigned long>::digits &&
                              std::numeric_limits<unsigned>::digits !=
                                  std::numeric_limits<unsigned long>::digits,
                          int> {
      assert(x > 0);
      return __builtin_ctzl(static_cast<unsigned long>(x));
  }

  template <typename T>
  [[nodiscard]] static constexpr inline auto countTrailingZeroes(T &&x) noexcept
      -> std::enable_if_t<
          !std::numeric_limits<std::decay_t<T>>::is_signed &&
              std::numeric_limits<std::decay_t<T>>::digits ==
                  std::numeric_limits<unsigned long long>::digits &&
              std::numeric_limits<unsigned>::digits !=
                  std::numeric_limits<unsigned long long>::digits &&
              std::numeric_limits<unsigned long>::digits !=
                  std::numeric_limits<unsigned long long>::digits,
          int> {
      assert(x > 0);
      return __builtin_ctzll(static_cast<unsigned long long>(x));
  }

  [[nodiscard]] static constexpr inline std::size_t
  roundUpToMultipleOf4096(std::size_t const x) {
      return ((x - 1) | static_cast<std::size_t>(4096 - 1)) + 1;
  }
} // End klee namespace

#endif /* KLEE_BITS_H */