1551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer//===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
263b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman//
3b2109ce97881269a610fa4afbcbca350e975174dJohn Criswell//                     The LLVM Compiler Infrastructure
4b2109ce97881269a610fa4afbcbca350e975174dJohn Criswell//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
763b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman//
8b2109ce97881269a610fa4afbcbca350e975174dJohn Criswell//===----------------------------------------------------------------------===//
954ea60c69e69b8e5a464a1d7688ceec5c68bacd5Chris Lattner//
1054ea60c69e69b8e5a464a1d7688ceec5c68bacd5Chris Lattner// This file contains some functions that are useful for math stuff.
1154ea60c69e69b8e5a464a1d7688ceec5c68bacd5Chris Lattner//
1254ea60c69e69b8e5a464a1d7688ceec5c68bacd5Chris Lattner//===----------------------------------------------------------------------===//
13cee8f9ae67104576b2028125b56e9ba4856a1d66Chris Lattner
14551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#ifndef LLVM_SUPPORT_MATHEXTRAS_H
15551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#define LLVM_SUPPORT_MATHEXTRAS_H
16cee8f9ae67104576b2028125b56e9ba4856a1d66Chris Lattner
17a0cfb8f21baf94fb8c40ff90bfc043de1ef700ecMichael J. Spencer#include "llvm/Support/Compiler.h"
181f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/SwapByteOrder.h"
19de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar#include <algorithm>
2036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include <cassert>
2136fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer#include <cstring>
2236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include <type_traits>
23de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar#include <limits>
24cee8f9ae67104576b2028125b56e9ba4856a1d66Chris Lattner
25c8b909ae499e5b0d7e38ba5c54c35e98c936300bMichael J. Spencer#ifdef _MSC_VER
26eb3602472026dc029beb45ccbe09bc84162ba949Aaron Ballman#include <intrin.h>
27c8b909ae499e5b0d7e38ba5c54c35e98c936300bMichael J. Spencer#endif
28c8b909ae499e5b0d7e38ba5c54c35e98c936300bMichael J. Spencer
296948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#ifdef __ANDROID_NDK__
306948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#include <android/api-level.h>
316948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#endif
326948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
33d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
3436fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer/// \brief The behavior an operation has on an input of 0.
3536fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencerenum ZeroBehavior {
3636fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  /// \brief The returned value is undefined.
3736fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  ZB_Undefined,
3836fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  /// \brief The returned value is numeric_limits<T>::max()
3936fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  ZB_Max,
4036fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  /// \brief The returned value is numeric_limits<T>::digits
4136fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  ZB_Width
4236fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer};
4336fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
44ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesnamespace detail {
45ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T, std::size_t SizeOfT> struct TrailingZerosCounter {
46ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static std::size_t count(T Val, ZeroBehavior) {
47ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (!Val)
48ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      return std::numeric_limits<T>::digits;
49ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (Val & 0x1)
50ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      return 0;
51ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
52ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    // Bisection method.
53ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    std::size_t ZeroBits = 0;
54ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    T Shift = std::numeric_limits<T>::digits >> 1;
55ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    T Mask = std::numeric_limits<T>::max() >> Shift;
56ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    while (Shift) {
57ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      if ((Val & Mask) == 0) {
58ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        Val >>= Shift;
59ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        ZeroBits |= Shift;
60ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      }
61ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      Shift >>= 1;
62ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      Mask >>= Shift;
6336fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer    }
64ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return ZeroBits;
6536fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  }
66ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines};
6736fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
68f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#if __GNUC__ >= 4 || defined(_MSC_VER)
69ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T> struct TrailingZerosCounter<T, 4> {
70ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static std::size_t count(T Val, ZeroBehavior ZB) {
71ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (ZB != ZB_Undefined && Val == 0)
72ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      return 32;
7336fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
7437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#if __has_builtin(__builtin_ctz) || LLVM_GNUC_PREREQ(4, 0, 0)
75ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return __builtin_ctz(Val);
76f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#elif defined(_MSC_VER)
77ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    unsigned long Index;
78ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    _BitScanForward(&Index, Val);
79ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return Index;
8036fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer#endif
81ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  }
82ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines};
8336fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
84a0cfb8f21baf94fb8c40ff90bfc043de1ef700ecMichael J. Spencer#if !defined(_MSC_VER) || defined(_M_X64)
85ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T> struct TrailingZerosCounter<T, 8> {
86ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static std::size_t count(T Val, ZeroBehavior ZB) {
87ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (ZB != ZB_Undefined && Val == 0)
88ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      return 64;
8936fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
9037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#if __has_builtin(__builtin_ctzll) || LLVM_GNUC_PREREQ(4, 0, 0)
91ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return __builtin_ctzll(Val);
92f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#elif defined(_MSC_VER)
93ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    unsigned long Index;
94ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    _BitScanForward64(&Index, Val);
95ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return Index;
9636fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer#endif
97ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  }
98ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines};
9936fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer#endif
100a0cfb8f21baf94fb8c40ff90bfc043de1ef700ecMichael J. Spencer#endif
101ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines} // namespace detail
10236fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
103ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \brief Count number of 0's from the least significant bit to the most
10436fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer///   stopping at the first 1.
10536fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer///
10636fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer/// Only unsigned integral types are allowed.
10736fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer///
10836fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer/// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
10936fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer///   valid arguments.
11036fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencertemplate <typename T>
111ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstd::size_t countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
112ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static_assert(std::numeric_limits<T>::is_integer &&
113ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                    !std::numeric_limits<T>::is_signed,
114ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                "Only unsigned integral types are allowed.");
115ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return detail::TrailingZerosCounter<T, sizeof(T)>::count(Val, ZB);
116ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines}
117ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
118ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesnamespace detail {
119ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T, std::size_t SizeOfT> struct LeadingZerosCounter {
120ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static std::size_t count(T Val, ZeroBehavior) {
121ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (!Val)
122ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      return std::numeric_limits<T>::digits;
123ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
124ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    // Bisection method.
125ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    std::size_t ZeroBits = 0;
126ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
127ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      T Tmp = Val >> Shift;
128ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      if (Tmp)
129ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        Val = Tmp;
130ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      else
131ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        ZeroBits |= Shift;
132ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    }
133ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return ZeroBits;
13436fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  }
135ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines};
13636fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
137f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#if __GNUC__ >= 4 || defined(_MSC_VER)
138ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T> struct LeadingZerosCounter<T, 4> {
139ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static std::size_t count(T Val, ZeroBehavior ZB) {
140ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (ZB != ZB_Undefined && Val == 0)
141ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      return 32;
14236fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
14337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#if __has_builtin(__builtin_clz) || LLVM_GNUC_PREREQ(4, 0, 0)
144ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return __builtin_clz(Val);
145f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#elif defined(_MSC_VER)
146ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    unsigned long Index;
147ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    _BitScanReverse(&Index, Val);
148ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return Index ^ 31;
14936fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer#endif
150ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  }
151ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines};
15236fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
153a0cfb8f21baf94fb8c40ff90bfc043de1ef700ecMichael J. Spencer#if !defined(_MSC_VER) || defined(_M_X64)
154ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T> struct LeadingZerosCounter<T, 8> {
155ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static std::size_t count(T Val, ZeroBehavior ZB) {
156ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (ZB != ZB_Undefined && Val == 0)
157ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      return 64;
15836fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
15937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#if __has_builtin(__builtin_clzll) || LLVM_GNUC_PREREQ(4, 0, 0)
160ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return __builtin_clzll(Val);
161f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#elif defined(_MSC_VER)
162ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    unsigned long Index;
163ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    _BitScanReverse64(&Index, Val);
164ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return Index ^ 63;
16536fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer#endif
166ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  }
167ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines};
16836fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer#endif
169a0cfb8f21baf94fb8c40ff90bfc043de1ef700ecMichael J. Spencer#endif
170ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines} // namespace detail
171ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
172ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \brief Count number of 0's from the most significant bit to the least
173ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines///   stopping at the first 1.
174ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines///
175ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Only unsigned integral types are allowed.
176ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines///
177ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
178ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines///   valid arguments.
179ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T>
180ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstd::size_t countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
181ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static_assert(std::numeric_limits<T>::is_integer &&
182ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                    !std::numeric_limits<T>::is_signed,
183ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                "Only unsigned integral types are allowed.");
184ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return detail::LeadingZerosCounter<T, sizeof(T)>::count(Val, ZB);
185ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines}
18636fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
18736fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer/// \brief Get the index of the first set bit starting from the least
18836fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer///   significant bit.
18936fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer///
19036fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer/// Only unsigned integral types are allowed.
19136fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer///
19236fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer/// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
19336fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer///   valid arguments.
194ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T> T findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) {
19536fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  if (ZB == ZB_Max && Val == 0)
19636fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer    return std::numeric_limits<T>::max();
19736fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
19836fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  return countTrailingZeros(Val, ZB_Undefined);
19936fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer}
20036fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
20136fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer/// \brief Get the index of the last set bit starting from the least
20236fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer///   significant bit.
20336fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer///
20436fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer/// Only unsigned integral types are allowed.
20536fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer///
20636fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer/// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
20736fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer///   valid arguments.
208ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T> T findLastSet(T Val, ZeroBehavior ZB = ZB_Max) {
20936fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  if (ZB == ZB_Max && Val == 0)
21036fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer    return std::numeric_limits<T>::max();
21136fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
21236fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  // Use ^ instead of - because both gcc and llvm can remove the associated ^
21336fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  // in the __builtin_clz intrinsic on x86.
21436fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  return countLeadingZeros(Val, ZB_Undefined) ^
21536fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer         (std::numeric_limits<T>::digits - 1);
21636fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer}
21736fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
21836fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer/// \brief Macro compressed bit reversal table for 256 bits.
21936fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer///
22036fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer/// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
22136fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencerstatic const unsigned char BitReverseTable256[256] = {
22236fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer#define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
22336fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer#define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
22436fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer#define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
22536fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  R6(0), R6(2), R6(1), R6(3)
226c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#undef R2
227c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#undef R4
228c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#undef R6
22936fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer};
23036fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer
23136fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer/// \brief Reverse the bits in \p Val.
23236fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencertemplate <typename T>
23336fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. SpencerT reverseBits(T Val) {
23436fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  unsigned char in[sizeof(Val)];
23536fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  unsigned char out[sizeof(Val)];
23636fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  std::memcpy(in, &Val, sizeof(Val));
23736fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  for (unsigned i = 0; i < sizeof(Val); ++i)
23836fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer    out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
23936fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  std::memcpy(&Val, out, sizeof(Val));
24036fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer  return Val;
24136fe3f2b5651882b12e24b49dc7818ebb1a5d79fMichael J. Spencer}
242d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
243fe2cce63aa26d0916fa7be32c6bf7fa8fb059ee7Misha Brukman// NOTE: The following support functions use the _32/_64 extensions instead of
24489bfcd34cbd2f4c6bb2cafff0a5c2bff147fae11Chris Lattner// type overloading so that signed and unsigned integers can be used without
24589bfcd34cbd2f4c6bb2cafff0a5c2bff147fae11Chris Lattner// ambiguity.
24688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner
24749e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// Hi_32 - This function returns the high 32 bits of a 64 bit value.
248c3c395cf5e401328836e81c18cb70eef1b9ea5acJeff Coheninline uint32_t Hi_32(uint64_t Value) {
249c3c395cf5e401328836e81c18cb70eef1b9ea5acJeff Cohen  return static_cast<uint32_t>(Value >> 32);
25088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner}
25188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner
25249e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// Lo_32 - This function returns the low 32 bits of a 64 bit value.
253c3c395cf5e401328836e81c18cb70eef1b9ea5acJeff Coheninline uint32_t Lo_32(uint64_t Value) {
254c3c395cf5e401328836e81c18cb70eef1b9ea5acJeff Cohen  return static_cast<uint32_t>(Value);
25588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner}
25688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner
257c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines/// Make_64 - This functions makes a 64-bit integer from a high / low pair of
258c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines///           32-bit integers.
259c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesinline uint64_t Make_64(uint32_t High, uint32_t Low) {
260c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  return ((uint64_t)High << 32) | (uint64_t)Low;
261c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines}
262c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
26334247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer/// isInt - Checks if an integer fits into the given bit width.
264d6eb635d1a1317fc3d218056ec77ec242c2413cbJakob Stoklund Olesentemplate<unsigned N>
265d6eb635d1a1317fc3d218056ec77ec242c2413cbJakob Stoklund Oleseninline bool isInt(int64_t x) {
266d4c00c0f558193b492aed9ab6aacf84bf1d3fb4eJakob Stoklund Olesen  return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
267d6eb635d1a1317fc3d218056ec77ec242c2413cbJakob Stoklund Olesen}
26834247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer// Template specializations to get better code for common cases.
26934247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramertemplate<>
27034247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramerinline bool isInt<8>(int64_t x) {
27134247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer  return static_cast<int8_t>(x) == x;
27234247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer}
27334247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramertemplate<>
27434247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramerinline bool isInt<16>(int64_t x) {
27534247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer  return static_cast<int16_t>(x) == x;
27634247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer}
27734247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramertemplate<>
27834247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramerinline bool isInt<32>(int64_t x) {
27934247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer  return static_cast<int32_t>(x) == x;
28034247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer}
281d6eb635d1a1317fc3d218056ec77ec242c2413cbJakob Stoklund Olesen
282b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// isShiftedInt<N,S> - Checks if a signed integer is an N bit number shifted
283b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum///                     left by S.
284b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumtemplate<unsigned N, unsigned S>
285b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicuminline bool isShiftedInt(int64_t x) {
286b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return isInt<N+S>(x) && (x % (1<<S) == 0);
287b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
288b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
28934247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer/// isUInt - Checks if an unsigned integer fits into the given bit width.
290d6eb635d1a1317fc3d218056ec77ec242c2413cbJakob Stoklund Olesentemplate<unsigned N>
29134247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramerinline bool isUInt(uint64_t x) {
29266f1f30725eb20760069823ec015234cce6b887cReid Kleckner  return N >= 64 || x < (UINT64_C(1)<<(N));
293d6eb635d1a1317fc3d218056ec77ec242c2413cbJakob Stoklund Olesen}
29434247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer// Template specializations to get better code for common cases.
29534247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramertemplate<>
29634247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramerinline bool isUInt<8>(uint64_t x) {
29734247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer  return static_cast<uint8_t>(x) == x;
29834247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer}
29934247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramertemplate<>
30034247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramerinline bool isUInt<16>(uint64_t x) {
30134247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer  return static_cast<uint16_t>(x) == x;
30234247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer}
30334247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramertemplate<>
30434247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramerinline bool isUInt<32>(uint64_t x) {
30534247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer  return static_cast<uint32_t>(x) == x;
30634247a0f356edf45ae3ad9ce04e1f90a77c6dba7Benjamin Kramer}
307d6eb635d1a1317fc3d218056ec77ec242c2413cbJakob Stoklund Olesen
308b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// isShiftedUInt<N,S> - Checks if a unsigned integer is an N bit number shifted
309b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum///                     left by S.
310b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumtemplate<unsigned N, unsigned S>
311b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicuminline bool isShiftedUInt(uint64_t x) {
312b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return isUInt<N+S>(x) && (x % (1<<S) == 0);
313b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
314b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
315de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// Gets the maximum value for a N-bit unsigned integer.
316de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarinline uint64_t maxUIntN(uint64_t N) {
317de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  assert(N > 0 && N <= 64 && "integer width out of range");
318de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
319de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  return (UINT64_C(1) << N) - 1;
320de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar}
321de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
322de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// Gets the minimum value for a N-bit signed integer.
323de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarinline int64_t minIntN(int64_t N) {
324de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  assert(N > 0 && N <= 64 && "integer width out of range");
325de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
326de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  return -(INT64_C(1)<<(N-1));
327de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar}
328de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
329de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// Gets the maximum value for a N-bit signed integer.
330de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarinline int64_t maxIntN(int64_t N) {
331de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  assert(N > 0 && N <= 64 && "integer width out of range");
332de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
333de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  return (INT64_C(1)<<(N-1)) - 1;
334de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar}
335de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
3365005e27f9714f0eaa5b8b7a5a1f6751afa163f07Dan Gohman/// isUIntN - Checks if an unsigned integer fits into the given (dynamic)
3375005e27f9714f0eaa5b8b7a5a1f6751afa163f07Dan Gohman/// bit width.
3385005e27f9714f0eaa5b8b7a5a1f6751afa163f07Dan Gohmaninline bool isUIntN(unsigned N, uint64_t x) {
339de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  return N >= 64 || x <= maxUIntN(N);
3405005e27f9714f0eaa5b8b7a5a1f6751afa163f07Dan Gohman}
3415005e27f9714f0eaa5b8b7a5a1f6751afa163f07Dan Gohman
342c42a7754bb6249c33c509e6fa6e8b3c9344e72d8Rafael Espindola/// isIntN - Checks if an signed integer fits into the given (dynamic)
343b35d56c2fe39064a33d0e4e7faf5464b6d8a7352Rafael Espindola/// bit width.
344b35d56c2fe39064a33d0e4e7faf5464b6d8a7352Rafael Espindolainline bool isIntN(unsigned N, int64_t x) {
345de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  return N >= 64 || (minIntN(N) <= x && x <= maxIntN(N));
346b35d56c2fe39064a33d0e4e7faf5464b6d8a7352Rafael Espindola}
347b35d56c2fe39064a33d0e4e7faf5464b6d8a7352Rafael Espindola
3480c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar/// isMask_32 - This function returns true if the argument is a non-empty
3490c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar/// sequence of ones starting at the least significant bit with the remainder
3500c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar/// zero (32 bit version).  Ex. isMask_32(0x0000FFFFU) == true.
35118a4c74136a9919dc3235d03c0e060f32d01f3b0Chris Lattnerinline bool isMask_32(uint32_t Value) {
35288c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner  return Value && ((Value + 1) & Value) == 0;
35388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner}
35488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner
3550c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar/// isMask_64 - This function returns true if the argument is a non-empty
3560c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar/// sequence of ones starting at the least significant bit with the remainder
3570c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar/// zero (64 bit version).
35818a4c74136a9919dc3235d03c0e060f32d01f3b0Chris Lattnerinline bool isMask_64(uint64_t Value) {
35988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner  return Value && ((Value + 1) & Value) == 0;
36088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner}
36188c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner
362fe2cce63aa26d0916fa7be32c6bf7fa8fb059ee7Misha Brukman/// isShiftedMask_32 - This function returns true if the argument contains a
3630c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar/// non-empty sequence of ones with the remainder zero (32 bit version.)
36449e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// Ex. isShiftedMask_32(0x0000FF00U) == true.
36518a4c74136a9919dc3235d03c0e060f32d01f3b0Chris Lattnerinline bool isShiftedMask_32(uint32_t Value) {
3660c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  return Value && isMask_32((Value - 1) | Value);
36788c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner}
36888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner
369fe2cce63aa26d0916fa7be32c6bf7fa8fb059ee7Misha Brukman/// isShiftedMask_64 - This function returns true if the argument contains a
3700c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar/// non-empty sequence of ones with the remainder zero (64 bit version.)
37118a4c74136a9919dc3235d03c0e060f32d01f3b0Chris Lattnerinline bool isShiftedMask_64(uint64_t Value) {
3720c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  return Value && isMask_64((Value - 1) | Value);
37388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner}
37488c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner
375fe2cce63aa26d0916fa7be32c6bf7fa8fb059ee7Misha Brukman/// isPowerOf2_32 - This function returns true if the argument is a power of
37649e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// two > 0. Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
377c3c395cf5e401328836e81c18cb70eef1b9ea5acJeff Coheninline bool isPowerOf2_32(uint32_t Value) {
37888c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner  return Value && !(Value & (Value - 1));
37988c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner}
38088c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner
38149e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// isPowerOf2_64 - This function returns true if the argument is a power of two
38249e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// > 0 (64 bit edition.)
38388c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattnerinline bool isPowerOf2_64(uint64_t Value) {
38419b7e0e0cabfa6dfc559c64e3d6ed053832c4047Reid Spencer  return Value && !(Value & (Value - int64_t(1L)));
38588c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner}
38688c606eb0cc5415ed367c24e073f7bb478501d34Chris Lattner
38749e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// ByteSwap_16 - This function returns a byte-swapped representation of the
38849e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// 16-bit argument, Value.
389c3c395cf5e401328836e81c18cb70eef1b9ea5acJeff Coheninline uint16_t ByteSwap_16(uint16_t Value) {
3903afc385042fb0d121e9454347f975e4f1a5f5bfdChris Lattner  return sys::SwapByteOrder_16(Value);
3916fb3bd6a658940287789198d3207b0da04c0a4e6Nate Begeman}
3926fb3bd6a658940287789198d3207b0da04c0a4e6Nate Begeman
39349e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// ByteSwap_32 - This function returns a byte-swapped representation of the
39449e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// 32-bit argument, Value.
395c3c395cf5e401328836e81c18cb70eef1b9ea5acJeff Coheninline uint32_t ByteSwap_32(uint32_t Value) {
3963afc385042fb0d121e9454347f975e4f1a5f5bfdChris Lattner  return sys::SwapByteOrder_32(Value);
3976fb3bd6a658940287789198d3207b0da04c0a4e6Nate Begeman}
3986fb3bd6a658940287789198d3207b0da04c0a4e6Nate Begeman
39949e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// ByteSwap_64 - This function returns a byte-swapped representation of the
40049e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// 64-bit argument, Value.
4016fb3bd6a658940287789198d3207b0da04c0a4e6Nate Begemaninline uint64_t ByteSwap_64(uint64_t Value) {
4023afc385042fb0d121e9454347f975e4f1a5f5bfdChris Lattner  return sys::SwapByteOrder_64(Value);
4036fb3bd6a658940287789198d3207b0da04c0a4e6Nate Begeman}
4046fb3bd6a658940287789198d3207b0da04c0a4e6Nate Begeman
405ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \brief Count the number of ones from the most significant bit to the first
406ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// zero bit.
407ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines///
408ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Ex. CountLeadingOnes(0xFF0FFF00) == 8.
409ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Only unsigned integral types are allowed.
410ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines///
411ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \param ZB the behavior on an input of all ones. Only ZB_Width and
412ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// ZB_Undefined are valid arguments.
413ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T>
414ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstd::size_t countLeadingOnes(T Value, ZeroBehavior ZB = ZB_Width) {
415ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static_assert(std::numeric_limits<T>::is_integer &&
416ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                    !std::numeric_limits<T>::is_signed,
417ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                "Only unsigned integral types are allowed.");
418ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return countLeadingZeros(~Value, ZB);
419ca2a0e1062545651efd20dca1f647b864ede4a39Dan Gohman}
420ca2a0e1062545651efd20dca1f647b864ede4a39Dan Gohman
421ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \brief Count the number of ones from the least significant bit to the first
422ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// zero bit.
423ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines///
424ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Ex. countTrailingOnes(0x00FF00FF) == 8.
425ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Only unsigned integral types are allowed.
426ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines///
427ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \param ZB the behavior on an input of all ones. Only ZB_Width and
428ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// ZB_Undefined are valid arguments.
429ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T>
430ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstd::size_t countTrailingOnes(T Value, ZeroBehavior ZB = ZB_Width) {
431ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static_assert(std::numeric_limits<T>::is_integer &&
432ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                    !std::numeric_limits<T>::is_signed,
433ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                "Only unsigned integral types are allowed.");
434ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return countTrailingZeros(~Value, ZB);
435ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines}
436ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
437ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesnamespace detail {
438ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T, std::size_t SizeOfT> struct PopulationCounter {
439ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static unsigned count(T Value) {
440ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    // Generic version, forward to 32 bits.
441ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    static_assert(SizeOfT <= 4, "Not implemented!");
4422a934cb6072bb0840b39e899e02ad9dea95b18dcOwen Anderson#if __GNUC__ >= 4
443ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return __builtin_popcount(Value);
4442a934cb6072bb0840b39e899e02ad9dea95b18dcOwen Anderson#else
445ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    uint32_t v = Value;
446ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    v = v - ((v >> 1) & 0x55555555);
447ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
448ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
4492a934cb6072bb0840b39e899e02ad9dea95b18dcOwen Anderson#endif
450ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  }
451ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines};
45216d6ea526482e733fe3bc63929e94c9e88b6708dNate Begeman
453ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T> struct PopulationCounter<T, 8> {
454ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static unsigned count(T Value) {
4552a934cb6072bb0840b39e899e02ad9dea95b18dcOwen Anderson#if __GNUC__ >= 4
456ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return __builtin_popcountll(Value);
4572a934cb6072bb0840b39e899e02ad9dea95b18dcOwen Anderson#else
458ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    uint64_t v = Value;
459ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    v = v - ((v >> 1) & 0x5555555555555555ULL);
460ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
461ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
462ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
4632a934cb6072bb0840b39e899e02ad9dea95b18dcOwen Anderson#endif
464ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  }
465ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines};
466ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines} // namespace detail
467ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
468ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \brief Count the number of set bits in a value.
469ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Ex. countPopulation(0xF000F000) = 8
470ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Returns 0 if the word is zero.
471ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestemplate <typename T>
472ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesinline unsigned countPopulation(T Value) {
473ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  static_assert(std::numeric_limits<T>::is_integer &&
474ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                    !std::numeric_limits<T>::is_signed,
475ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                "Only unsigned integral types are allowed.");
476ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return detail::PopulationCounter<T, sizeof(T)>::count(Value);
47716d6ea526482e733fe3bc63929e94c9e88b6708dNate Begeman}
47816d6ea526482e733fe3bc63929e94c9e88b6708dNate Begeman
4796948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar/// Log2 - This function returns the log base 2 of the specified value
4806948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainarinline double Log2(double Value) {
4816948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#if defined(__ANDROID_API__) && __ANDROID_API__ < 18
4826948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  return __builtin_log(Value) / __builtin_log(2.0);
4836948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#else
4846948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  return log2(Value);
4856948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#endif
4866948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar}
4876948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
488fe2cce63aa26d0916fa7be32c6bf7fa8fb059ee7Misha Brukman/// Log2_32 - This function returns the floor log base 2 of the specified value,
48949e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// -1 if the value is zero. (32 bit edition.)
49049e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
491c3c395cf5e401328836e81c18cb70eef1b9ea5acJeff Coheninline unsigned Log2_32(uint32_t Value) {
492c6af2432c802d241c8fffbe0371c023e6c58844eMichael J. Spencer  return 31 - countLeadingZeros(Value);
49316d6ea526482e733fe3bc63929e94c9e88b6708dNate Begeman}
49454ea60c69e69b8e5a464a1d7688ceec5c68bacd5Chris Lattner
495fe2cce63aa26d0916fa7be32c6bf7fa8fb059ee7Misha Brukman/// Log2_64 - This function returns the floor log base 2 of the specified value,
49649e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// -1 if the value is zero. (64 bit edition.)
4972be12faabb3ce2d2d3979c73ac65d466fdea5ec5Chris Lattnerinline unsigned Log2_64(uint64_t Value) {
498c6af2432c802d241c8fffbe0371c023e6c58844eMichael J. Spencer  return 63 - countLeadingZeros(Value);
499cee8f9ae67104576b2028125b56e9ba4856a1d66Chris Lattner}
500cee8f9ae67104576b2028125b56e9ba4856a1d66Chris Lattner
50149e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// Log2_32_Ceil - This function returns the ceil log base 2 of the specified
50249e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// value, 32 if the value is zero. (32 bit edition).
50349e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
504c3c395cf5e401328836e81c18cb70eef1b9ea5acJeff Coheninline unsigned Log2_32_Ceil(uint32_t Value) {
505c6af2432c802d241c8fffbe0371c023e6c58844eMichael J. Spencer  return 32 - countLeadingZeros(Value - 1);
5060683c8cad920d241d44b126972d5cdd164dcc213Chris Lattner}
5070683c8cad920d241d44b126972d5cdd164dcc213Chris Lattner
50855b42513e1548554bfacc72d48ec3483c73fddf9Dan Gohman/// Log2_64_Ceil - This function returns the ceil log base 2 of the specified
50955b42513e1548554bfacc72d48ec3483c73fddf9Dan Gohman/// value, 64 if the value is zero. (64 bit edition.)
5100683c8cad920d241d44b126972d5cdd164dcc213Chris Lattnerinline unsigned Log2_64_Ceil(uint64_t Value) {
511c6af2432c802d241c8fffbe0371c023e6c58844eMichael J. Spencer  return 64 - countLeadingZeros(Value - 1);
5120683c8cad920d241d44b126972d5cdd164dcc213Chris Lattner}
5130683c8cad920d241d44b126972d5cdd164dcc213Chris Lattner
51449e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// GreatestCommonDivisor64 - Return the greatest common divisor of the two
51549e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// values using Euclid's algorithm.
51649e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattnerinline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
51749e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner  while (B) {
51849e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner    uint64_t T = B;
51949e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner    B = A % B;
52049e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner    A = T;
52149e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner  }
52249e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner  return A;
52349e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner}
524fe2cce63aa26d0916fa7be32c6bf7fa8fb059ee7Misha Brukman
52549e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// BitsToDouble - This function takes a 64-bit integer and returns the bit
52649e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// equivalent double.
52759b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskeyinline double BitsToDouble(uint64_t Bits) {
52859b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  union {
52959b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey    uint64_t L;
53059b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey    double D;
53159b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  } T;
53259b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  T.L = Bits;
53359b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  return T.D;
53459b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey}
53559b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey
53649e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// BitsToFloat - This function takes a 32-bit integer and returns the bit
53749e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// equivalent float.
538104338913500d007996056ad092e195009883a84Jim Laskeyinline float BitsToFloat(uint32_t Bits) {
53959b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  union {
540104338913500d007996056ad092e195009883a84Jim Laskey    uint32_t I;
54159b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey    float F;
54259b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  } T;
54359b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  T.I = Bits;
54459b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  return T.F;
54559b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey}
54659b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey
54749e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// DoubleToBits - This function takes a double and returns the bit
548541ed9fd02ea48d2739f4a9dd681ba2d5da26886Dale Johannesen/// equivalent 64-bit integer.  Note that copying doubles around
549541ed9fd02ea48d2739f4a9dd681ba2d5da26886Dale Johannesen/// changes the bits of NaNs on some hosts, notably x86, so this
550541ed9fd02ea48d2739f4a9dd681ba2d5da26886Dale Johannesen/// routine cannot be used if these bits are needed.
55159b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskeyinline uint64_t DoubleToBits(double Double) {
55259b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  union {
55359b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey    uint64_t L;
55459b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey    double D;
55559b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  } T;
55659b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  T.D = Double;
55759b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  return T.L;
55859b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey}
55959b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey
56049e6a9bc94a115d674502009b396c1a22fb1b1a1Chris Lattner/// FloatToBits - This function takes a float and returns the bit
561541ed9fd02ea48d2739f4a9dd681ba2d5da26886Dale Johannesen/// equivalent 32-bit integer.  Note that copying floats around
562541ed9fd02ea48d2739f4a9dd681ba2d5da26886Dale Johannesen/// changes the bits of NaNs on some hosts, notably x86, so this
563541ed9fd02ea48d2739f4a9dd681ba2d5da26886Dale Johannesen/// routine cannot be used if these bits are needed.
564104338913500d007996056ad092e195009883a84Jim Laskeyinline uint32_t FloatToBits(float Float) {
56559b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  union {
566104338913500d007996056ad092e195009883a84Jim Laskey    uint32_t I;
56759b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey    float F;
56859b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  } T;
56959b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  T.F = Float;
57059b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey  return T.I;
57159b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey}
57259b8fcfa5f736dff4a08ebcac032935b6fd92f34Jim Laskey
573fd617d0143a158bc1c996445262d409280e7b0ccDuncan Sands/// MinAlign - A and B are either alignments or offsets.  Return the minimum
574fd617d0143a158bc1c996445262d409280e7b0ccDuncan Sands/// alignment that may be assumed after adding the two together.
575305b515c2787f47adecbe120e4b4bef55c5e5525Chandler Carruthinline uint64_t MinAlign(uint64_t A, uint64_t B) {
576fd617d0143a158bc1c996445262d409280e7b0ccDuncan Sands  // The largest power of 2 that divides both A and B.
577b3508821533854b1879e7d6a83824b4ba84ce633Aaron Ballman  //
578f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  // Replace "-Value" by "1+~Value" in the following commented code to avoid
579b3508821533854b1879e7d6a83824b4ba84ce633Aaron Ballman  // MSVC warning C4146
580b3508821533854b1879e7d6a83824b4ba84ce633Aaron Ballman  //    return (A | B) & -(A | B);
581b3508821533854b1879e7d6a83824b4ba84ce633Aaron Ballman  return (A | B) & (1 + ~(A | B));
582fd617d0143a158bc1c996445262d409280e7b0ccDuncan Sands}
583d98a45d29a6047bd47d3a3cd83c13ac0dac851fbOwen Anderson
58437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// \brief Aligns \c Addr to \c Alignment bytes, rounding up.
58536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines///
58636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// Alignment should be a power of two.  This method rounds up, so
58737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// alignAddr(7, 4) == 8 and alignAddr(8, 4) == 8.
5886948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainarinline uintptr_t alignAddr(const void *Addr, size_t Alignment) {
58936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  assert(Alignment && isPowerOf2_64((uint64_t)Alignment) &&
59036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines         "Alignment is not a power of two!");
59136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
59237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  assert((uintptr_t)Addr + Alignment - 1 >= (uintptr_t)Addr);
59337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
59437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));
59537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
59637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
59737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// \brief Returns the necessary adjustment for aligning \c Ptr to \c Alignment
59837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// bytes, rounding up.
5996948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainarinline size_t alignmentAdjustment(const void *Ptr, size_t Alignment) {
60037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;
60136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
60236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
603d98a45d29a6047bd47d3a3cd83c13ac0dac851fbOwen Anderson/// NextPowerOf2 - Returns the next power of two (in 64-bits)
604d98a45d29a6047bd47d3a3cd83c13ac0dac851fbOwen Anderson/// that is strictly greater than A.  Returns zero on overflow.
605305b515c2787f47adecbe120e4b4bef55c5e5525Chandler Carruthinline uint64_t NextPowerOf2(uint64_t A) {
606d98a45d29a6047bd47d3a3cd83c13ac0dac851fbOwen Anderson  A |= (A >> 1);
607d98a45d29a6047bd47d3a3cd83c13ac0dac851fbOwen Anderson  A |= (A >> 2);
608d98a45d29a6047bd47d3a3cd83c13ac0dac851fbOwen Anderson  A |= (A >> 4);
609d98a45d29a6047bd47d3a3cd83c13ac0dac851fbOwen Anderson  A |= (A >> 8);
610d98a45d29a6047bd47d3a3cd83c13ac0dac851fbOwen Anderson  A |= (A >> 16);
611d98a45d29a6047bd47d3a3cd83c13ac0dac851fbOwen Anderson  A |= (A >> 32);
612d98a45d29a6047bd47d3a3cd83c13ac0dac851fbOwen Anderson  return A + 1;
613d98a45d29a6047bd47d3a3cd83c13ac0dac851fbOwen Anderson}
614e2a8dfefe5740377dbc323a84337c45d37410ea8Daniel Dunbar
61536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// Returns the power of two which is less than or equal to the given value.
61636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// Essentially, it is a floor operation across the domain of powers of two.
61736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesinline uint64_t PowerOf2Floor(uint64_t A) {
61836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (!A) return 0;
61936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  return 1ull << (63 - countLeadingZeros(A, ZB_Undefined));
62036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
62136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
6222d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko/// Returns the next integer (mod 2**64) that is greater than or equal to
6232d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko/// \p Value and is a multiple of \p Align. \p Align must be non-zero.
624e2a8dfefe5740377dbc323a84337c45d37410ea8Daniel Dunbar///
625f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar/// If non-zero \p Skew is specified, the return value will be a minimal
626f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar/// integer that is greater than or equal to \p Value and equal to
627f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar/// \p Align * N + \p Skew for some integer N. If \p Skew is larger than
628f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar/// \p Align, its value is adjusted to '\p Skew mod \p Align'.
629f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar///
630e2a8dfefe5740377dbc323a84337c45d37410ea8Daniel Dunbar/// Examples:
6312d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko/// \code
632de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar///   alignTo(5, 8) = 8
633de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar///   alignTo(17, 8) = 24
634de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar///   alignTo(~0LL, 8) = 0
635de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar///   alignTo(321, 255) = 510
636f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar///
637de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar///   alignTo(5, 8, 7) = 7
638de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar///   alignTo(17, 8, 1) = 17
639de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar///   alignTo(~0LL, 8, 3) = 3
640de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar///   alignTo(321, 255, 42) = 552
6412d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko/// \endcode
642de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarinline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
643f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  Skew %= Align;
644f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  return (Value + Align - 1 - Skew) / Align * Align + Skew;
645e2a8dfefe5740377dbc323a84337c45d37410ea8Daniel Dunbar}
646fe2cce63aa26d0916fa7be32c6bf7fa8fb059ee7Misha Brukman
647de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// Returns the largest uint64_t less than or equal to \p Value and is
648de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// \p Skew mod \p Align. \p Align must be non-zero
649de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarinline uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
650de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Skew %= Align;
651de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  return (Value - Skew) / Align * Align + Skew;
652de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar}
653de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
6542d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko/// Returns the offset to the next integer (mod 2**64) that is greater than
6552d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko/// or equal to \p Value and is a multiple of \p Align. \p Align must be
6562d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko/// non-zero.
657eccf22528f8b4c21cdbd2f620cbe39dbb38ea6e1Daniel Dunbarinline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
658de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  return alignTo(Value, Align) - Value;
659eccf22528f8b4c21cdbd2f620cbe39dbb38ea6e1Daniel Dunbar}
660eccf22528f8b4c21cdbd2f620cbe39dbb38ea6e1Daniel Dunbar
661b68a3ee82a8a34f7bae1d68d76f574e76a5535efJohnny Chen/// SignExtend32 - Sign extend B-bit number x to 32-bit int.
662b68a3ee82a8a34f7bae1d68d76f574e76a5535efJohnny Chen/// Usage int32_t r = SignExtend32<5>(x);
663adc6e06ff0c5950abda86574c2da1fc6b863b75cJakob Stoklund Olesentemplate <unsigned B> inline int32_t SignExtend32(uint32_t x) {
664adc6e06ff0c5950abda86574c2da1fc6b863b75cJakob Stoklund Olesen  return int32_t(x << (32 - B)) >> (32 - B);
665b68a3ee82a8a34f7bae1d68d76f574e76a5535efJohnny Chen}
666b68a3ee82a8a34f7bae1d68d76f574e76a5535efJohnny Chen
6671144af3c9b4da48cd581156e05b24261c8de366aRichard Smith/// \brief Sign extend number in the bottom B bits of X to a 32-bit int.
6681144af3c9b4da48cd581156e05b24261c8de366aRichard Smith/// Requires 0 < B <= 32.
6691144af3c9b4da48cd581156e05b24261c8de366aRichard Smithinline int32_t SignExtend32(uint32_t X, unsigned B) {
6701144af3c9b4da48cd581156e05b24261c8de366aRichard Smith  return int32_t(X << (32 - B)) >> (32 - B);
6711144af3c9b4da48cd581156e05b24261c8de366aRichard Smith}
6721144af3c9b4da48cd581156e05b24261c8de366aRichard Smith
673b68a3ee82a8a34f7bae1d68d76f574e76a5535efJohnny Chen/// SignExtend64 - Sign extend B-bit number x to 64-bit int.
674b68a3ee82a8a34f7bae1d68d76f574e76a5535efJohnny Chen/// Usage int64_t r = SignExtend64<5>(x);
675adc6e06ff0c5950abda86574c2da1fc6b863b75cJakob Stoklund Olesentemplate <unsigned B> inline int64_t SignExtend64(uint64_t x) {
676adc6e06ff0c5950abda86574c2da1fc6b863b75cJakob Stoklund Olesen  return int64_t(x << (64 - B)) >> (64 - B);
677b68a3ee82a8a34f7bae1d68d76f574e76a5535efJohnny Chen}
678b68a3ee82a8a34f7bae1d68d76f574e76a5535efJohnny Chen
6791144af3c9b4da48cd581156e05b24261c8de366aRichard Smith/// \brief Sign extend number in the bottom B bits of X to a 64-bit int.
6801144af3c9b4da48cd581156e05b24261c8de366aRichard Smith/// Requires 0 < B <= 64.
6811144af3c9b4da48cd581156e05b24261c8de366aRichard Smithinline int64_t SignExtend64(uint64_t X, unsigned B) {
6821144af3c9b4da48cd581156e05b24261c8de366aRichard Smith  return int64_t(X << (64 - B)) >> (64 - B);
6831144af3c9b4da48cd581156e05b24261c8de366aRichard Smith}
6841144af3c9b4da48cd581156e05b24261c8de366aRichard Smith
685de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// \brief Subtract two unsigned integers, X and Y, of type T and return their
686de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// absolute value.
687de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainartemplate <typename T>
688de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainartypename std::enable_if<std::is_unsigned<T>::value, T>::type
689de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga NainarAbsoluteDifference(T X, T Y) {
690de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  return std::max(X, Y) - std::min(X, Y);
691de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar}
692de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
693f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar/// \brief Add two unsigned integers, X and Y, of type T.
694f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar/// Clamp the result to the maximum representable value of T on overflow.
695f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar/// ResultOverflowed indicates if the result is larger than the maximum
696f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar/// representable value of type T.
697f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainartemplate <typename T>
698f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainartypename std::enable_if<std::is_unsigned<T>::value, T>::type
699f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarSaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) {
700f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  bool Dummy;
701f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
702f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  // Hacker's Delight, p. 29
703f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  T Z = X + Y;
704f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  Overflowed = (Z < X || Z < Y);
705f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  if (Overflowed)
706f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return std::numeric_limits<T>::max();
707f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  else
708f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return Z;
709f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
710f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
711f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar/// \brief Multiply two unsigned integers, X and Y, of type T.
712f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar/// Clamp the result to the maximum representable value of T on overflow.
713f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar/// ResultOverflowed indicates if the result is larger than the maximum
714f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar/// representable value of type T.
715f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainartemplate <typename T>
716f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainartypename std::enable_if<std::is_unsigned<T>::value, T>::type
717f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarSaturatingMultiply(T X, T Y, bool *ResultOverflowed = nullptr) {
718f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  bool Dummy;
719f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
720f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
721f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  // Hacker's Delight, p. 30 has a different algorithm, but we don't use that
722f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  // because it fails for uint16_t (where multiplication can have undefined
723f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  // behavior due to promotion to int), and requires a division in addition
724f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  // to the multiplication.
725f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
726f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  Overflowed = false;
727f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
728f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  // Log2(Z) would be either Log2Z or Log2Z + 1.
729f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  // Special case: if X or Y is 0, Log2_64 gives -1, and Log2Z
730f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  // will necessarily be less than Log2Max as desired.
731f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  int Log2Z = Log2_64(X) + Log2_64(Y);
732f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  const T Max = std::numeric_limits<T>::max();
733f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  int Log2Max = Log2_64(Max);
734f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  if (Log2Z < Log2Max) {
735f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return X * Y;
736f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
737f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  if (Log2Z > Log2Max) {
738f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    Overflowed = true;
739f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return Max;
740f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
741f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
742f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  // We're going to use the top bit, and maybe overflow one
743f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  // bit past it. Multiply all but the bottom bit then add
744f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  // that on at the end.
745f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  T Z = (X >> 1) * Y;
746f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  if (Z & ~(Max >> 1)) {
747f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    Overflowed = true;
748f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return Max;
749f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
750f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  Z <<= 1;
751f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  if (X & 1)
752f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    return SaturatingAdd(Z, Y, ResultOverflowed);
753f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
754f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  return Z;
755f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
756f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
757de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// \brief Multiply two unsigned integers, X and Y, and add the unsigned
758de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// integer, A to the product. Clamp the result to the maximum representable
759de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// value of T on overflow. ResultOverflowed indicates if the result is larger
760de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// than the maximum representable value of type T.
761de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// Note that this is purely a convenience function as there is no distinction
762de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// where overflow occurred in a 'fused' multiply-add for unsigned numbers.
763de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainartemplate <typename T>
764de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainartypename std::enable_if<std::is_unsigned<T>::value, T>::type
765de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga NainarSaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) {
766de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  bool Dummy;
767de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
768de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
769de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  T Product = SaturatingMultiply(X, Y, &Overflowed);
770de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  if (Overflowed)
771de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    return Product;
772de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
773de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  return SaturatingAdd(A, Product, &Overflowed);
774de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar}
775de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
77637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesextern const float huge_valf;
777d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
778d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
77954ea60c69e69b8e5a464a1d7688ceec5c68bacd5Chris Lattner#endif
780