CNL  2.0.2 (development)
Compositional Numeric Library
numeric.h
Go to the documentation of this file.
1 
2 // Copyright John McFarlane 2015 - 2017.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file ../LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
9 
10 #if !defined(CNL_NUMERIC_H)
11 #define CNL_NUMERIC_H
12 
13 #include "bit.h"
14 
15 #include "_impl/num_traits/unwrap.h"
16 #include "_impl/used_digits.h"
17 
18 #include <limits>
19 
21 namespace cnl {
23  // cnl::trailing_bits
24 
25  namespace _numeric_impl {
26  template<class Integer, bool IsSigned>
27  struct trailing_bits {
28  [[nodiscard]] constexpr auto operator()(Integer const& n) const noexcept
29  {
30  return countr_zero(n);
31  }
32  };
33 
34  template<class Integer>
35  struct trailing_bits<Integer, true> {
36  [[nodiscard]] constexpr auto operator()(Integer const& n) const noexcept
37  {
38  using unsigned_type = numbers::set_signedness_t<Integer, false>;
39  return countr_zero(static_cast<unsigned_type>(n));
40  }
41  };
42  }
43 
44  // count of the right redundant trailing bits
45  template<class Integer>
46  [[nodiscard]] constexpr auto trailing_bits(Integer const& value)
47  {
48  return value ? _numeric_impl::trailing_bits<Integer, numbers::signedness_v<Integer>>()(value)
49  : 0;
50  }
51 
53  // cnl::used_digits
54 
55  template<typename Integer>
56  [[nodiscard]] constexpr auto used_digits(
57  Integer const& value, int radix = std::numeric_limits<Integer>::radix)
58  {
59  return _impl::used_digits_signed<numbers::signedness_v<Integer>>{}(unwrap(value), radix);
60  }
61 
63  // cnl::leading_bits
64 
65  template<class Integer>
66  [[nodiscard]] constexpr auto leading_bits(Integer const& value)
67  {
68  return digits_v<Integer> - cnl::used_digits(value);
69  }
70 }
71 
72 #endif // CNL_NUMERIC_H
cnl
compositional numeric library
Definition: abort.h:15
bit.h
file containing definitions equivalent to those in
std::numeric_limits