CNL  2.0.2 (development)
Compositional Numeric Library
set_digits.h
1 
2 // Copyright John McFarlane 2018.
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_IMPL_NUM_TRAITS_SET_DIGITS_H)
11 #define CNL_IMPL_NUM_TRAITS_SET_DIGITS_H
12 
13 #include "../config.h"
14 #include "../cstdint/types.h"
15 #include "../numbers/signedness.h"
16 #include "digits.h"
17 
18 #include <concepts>
19 
20 namespace cnl {
22  // cnl::_impl::set_digits
23 
28  template<class T, int Digits>
29  struct set_digits;
30 
31  template<std::signed_integral T, int Digits>
32  struct set_digits<T, Digits> {
33  static_assert(Digits <= digits_v<intmax_t>, "digits exceeds widest signed integer");
34  };
35 
36  template<std::unsigned_integral T, int Digits>
37  struct set_digits<T, Digits> {
38  static_assert(Digits <= digits_v<uintmax_t>, "digits exceeds widest unsigned integer");
39  };
40 
41  template<std::signed_integral T, int Digits>
42  requires(Digits <= 7) struct set_digits<T, Digits> {
43  using type = std::int8_t;
44  };
45 
46  template<std::unsigned_integral T, int Digits>
47  requires(Digits <= 8) struct set_digits<T, Digits> {
48  using type = std::uint8_t;
49  };
50 
51  template<std::signed_integral T, int Digits>
52  requires(Digits > 7 && Digits <= 15) struct set_digits<T, Digits> {
53  using type = std::int16_t;
54  };
55 
56  template<std::unsigned_integral T, int Digits>
57  requires(Digits > 8 && Digits <= 16) struct set_digits<T, Digits> {
58  using type = std::uint16_t;
59  };
60 
61  template<std::signed_integral T, int Digits>
62  requires(Digits > 15 && Digits <= 31) struct set_digits<T, Digits> {
63  using type = std::int32_t;
64  };
65 
66  template<std::unsigned_integral T, int Digits>
67  requires(Digits > 16 && Digits <= 32) struct set_digits<T, Digits> {
68  using type = std::uint32_t;
69  };
70 
71  template<std::signed_integral T, int Digits>
72  requires(Digits > 31 && Digits <= 63) struct set_digits<T, Digits> {
73  using type = std::int64_t;
74  };
75 
76  template<std::unsigned_integral T, int Digits>
77  requires(Digits > 32 && Digits <= 64) struct set_digits<T, Digits> {
78  using type = std::uint64_t;
79  };
80 
81 #if defined(CNL_INT128_ENABLED)
82  template<std::signed_integral T, int Digits>
83  requires(Digits > 63 && Digits <= 127) struct set_digits<T, Digits> {
84  using type = int128_t;
85  };
86 
87  template<std::unsigned_integral T, int Digits>
88  requires(Digits > 64 && Digits <= 128) struct set_digits<T, Digits> {
89  using type = uint128_t;
90  };
91 #endif
92 
94  template<class T, int Digits>
96 }
97 
98 #endif // CNL_IMPL_NUM_TRAITS_SET_DIGITS_H
cnl::set_digits_t
typename set_digits< T, Digits >::type set_digits_t
Alias to set_digits.
Definition: set_digits.h:95
std::int8_t
cnl
compositional numeric library
Definition: abort.h:15
cnl::set_digits
convert a type to a similar type with the given number of digits
Definition: set_digits.h:29