fixed_point (deprecated)  rev.2
Binary Fixed-Point Arithmetic Library in C++
limits.h
1 
2 // Copyright John McFarlane 2015 - 2016.
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(SG14_LIMITS_H)
11 #define SG14_LIMITS_H 1
12 
13 #include "config.h"
14 
15 #include <climits>
16 #include <cstdint>
17 #include <limits>
18 
19 // SG14_NUMERIC_LIMITS_128_PROVIDED defined if
20 // standard library specializes std::numeric_limits for 128-bit integer
21 #if !defined(__clang__) && defined(__GNUG__) && (__cplusplus <= 201402L)
22 #define SG14_NUMERIC_LIMITS_128_PROVIDED
23 #elif defined(SG14_NUMERIC_LIMITS_128_PROVIDED)
24 #error SG14_NUMERIC_LIMITS_128_PROVIDED already defined
25 #endif
26 
27 #if defined(SG14_INT128_ENABLED) && !defined(SG14_NUMERIC_LIMITS_128_PROVIDED)
28 
29 namespace std {
30  template<>
31  struct numeric_limits<SG14_INT128> : numeric_limits<long long> {
32  static const int digits = CHAR_BIT*sizeof(SG14_INT128)-1;
33  static const int digits10 = 38;
34 
35  struct _s {
36  constexpr _s(uint64_t upper, uint64_t lower) : value(lower + (SG14_INT128{upper} << 64)) {}
37  constexpr operator SG14_INT128() const { return value; }
38  SG14_INT128 value;
39  };
40 
41  static constexpr SG14_INT128 min()
42  {
43  return _s(0x8000000000000000, 0x0000000000000000);
44  }
45 
46  static constexpr SG14_INT128 max()
47  {
48  return _s(0x7fffffffffffffff, 0xffffffffffffffff);
49  }
50 
51  static constexpr SG14_INT128 lowest()
52  {
53  return min();
54  }
55  };
56 
57  template<>
58  struct numeric_limits<SG14_UINT128> : numeric_limits<unsigned long long> {
59  static const int digits = CHAR_BIT*sizeof(SG14_INT128);
60  static const int digits10 = 38;
61 
62  struct _s {
63  constexpr _s(uint64_t upper, uint64_t lower) : value(lower + (SG14_UINT128{upper} << 64)) {}
64  constexpr operator SG14_INT128() const { return value; }
65  SG14_UINT128 value;
66  };
67 
68  static constexpr SG14_INT128 min()
69  {
70  return 0;
71  }
72 
73  static constexpr SG14_INT128 max()
74  {
75  return _s(0xffffffffffffffff, 0xffffffffffffffff);
76  }
77 
78  static constexpr SG14_INT128 lowest()
79  {
80  return min();
81  }
82  };
83 }
84 
85 #endif // SG14_INT128_ENABLED
86 
87 #endif // SG14_LIMITS_H
STL namespace.