Bumping manifests a=b2g-bump
[gecko.git] / mfbt / IntegerTypeTraits.h
blob9414451f913c230ec70034664f740458b6dbc1d4
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Helpers to manipulate integer types that don't fit in TypeTraits.h */
9 #ifndef mozilla_IntegerTypeTraits_h
10 #define mozilla_IntegerTypeTraits_h
12 #include "mozilla/TypeTraits.h"
13 #include <stdint.h>
15 namespace mozilla {
17 namespace detail {
19 /**
20 * StdintTypeForSizeAndSignedness returns the stdint integer type
21 * of given size (can be 1, 2, 4 or 8) and given signedness
22 * (false means unsigned, true means signed).
24 template<size_t Size, bool Signedness>
25 struct StdintTypeForSizeAndSignedness;
27 template<>
28 struct StdintTypeForSizeAndSignedness<1, true>
30 typedef int8_t Type;
33 template<>
34 struct StdintTypeForSizeAndSignedness<1, false>
36 typedef uint8_t Type;
39 template<>
40 struct StdintTypeForSizeAndSignedness<2, true>
42 typedef int16_t Type;
45 template<>
46 struct StdintTypeForSizeAndSignedness<2, false>
48 typedef uint16_t Type;
51 template<>
52 struct StdintTypeForSizeAndSignedness<4, true>
54 typedef int32_t Type;
57 template<>
58 struct StdintTypeForSizeAndSignedness<4, false>
60 typedef uint32_t Type;
63 template<>
64 struct StdintTypeForSizeAndSignedness<8, true>
66 typedef int64_t Type;
69 template<>
70 struct StdintTypeForSizeAndSignedness<8, false>
72 typedef uint64_t Type;
75 } // namespace detail
77 template<size_t Size>
78 struct UnsignedStdintTypeForSize
79 : detail::StdintTypeForSizeAndSignedness<Size, false>
80 {};
82 template<typename IntegerType>
83 struct PositionOfSignBit
85 static_assert(IsIntegral<IntegerType>::value,
86 "PositionOfSignBit is only for integral types");
87 // 8 here should be CHAR_BIT from limits.h, but the world has moved on.
88 static const size_t value = 8 * sizeof(IntegerType) - 1;
91 /**
92 * MinValue returns the minimum value of the given integer type as a
93 * compile-time constant, which std::numeric_limits<IntegerType>::min()
94 * cannot do in c++98.
96 template<typename IntegerType>
97 struct MinValue
99 private:
100 static_assert(IsIntegral<IntegerType>::value,
101 "MinValue is only for integral types");
103 typedef typename MakeUnsigned<IntegerType>::Type UnsignedIntegerType;
104 static const size_t PosOfSignBit = PositionOfSignBit<IntegerType>::value;
106 public:
107 // Bitwise ops may return a larger type, that's why we cast explicitly.
108 // In C++, left bit shifts on signed values is undefined by the standard
109 // unless the shifted value is representable.
110 // Notice that signed-to-unsigned conversions are always well-defined in
111 // the standard as the value congruent to 2**n, as expected. By contrast,
112 // unsigned-to-signed is only well-defined if the value is representable.
113 static const IntegerType value =
114 IsSigned<IntegerType>::value
115 ? IntegerType(UnsignedIntegerType(1) << PosOfSignBit)
116 : IntegerType(0);
120 * MaxValue returns the maximum value of the given integer type as a
121 * compile-time constant, which std::numeric_limits<IntegerType>::max()
122 * cannot do in c++98.
124 template<typename IntegerType>
125 struct MaxValue
127 static_assert(IsIntegral<IntegerType>::value,
128 "MaxValue is only for integral types");
130 // Tricksy, but covered by the CheckedInt unit test.
131 // Relies on the type of MinValue<IntegerType>::value
132 // being IntegerType.
133 static const IntegerType value = ~MinValue<IntegerType>::value;
136 } // namespace mozilla
138 #endif // mozilla_IntegerTypeTraits_h