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
14 #include <type_traits>
21 * StdintTypeForSizeAndSignedness returns the stdint integer type
22 * of given size (can be 1, 2, 4 or 8) and given signedness
23 * (false means unsigned, true means signed).
25 template <size_t Size
, bool Signedness
>
26 struct StdintTypeForSizeAndSignedness
;
29 struct StdintTypeForSizeAndSignedness
<1, true> {
34 struct StdintTypeForSizeAndSignedness
<1, false> {
39 struct StdintTypeForSizeAndSignedness
<2, true> {
44 struct StdintTypeForSizeAndSignedness
<2, false> {
45 typedef uint16_t Type
;
49 struct StdintTypeForSizeAndSignedness
<4, true> {
54 struct StdintTypeForSizeAndSignedness
<4, false> {
55 typedef uint32_t Type
;
59 struct StdintTypeForSizeAndSignedness
<8, true> {
64 struct StdintTypeForSizeAndSignedness
<8, false> {
65 typedef uint64_t Type
;
70 template <size_t Size
>
71 struct UnsignedStdintTypeForSize
72 : detail::StdintTypeForSizeAndSignedness
<Size
, false> {};
74 template <size_t Size
>
75 struct SignedStdintTypeForSize
76 : detail::StdintTypeForSizeAndSignedness
<Size
, true> {};
78 template <typename IntegerType
>
79 struct PositionOfSignBit
{
80 static_assert(std::is_integral_v
<IntegerType
>,
81 "PositionOfSignBit is only for integral types");
82 // 8 here should be CHAR_BIT from limits.h, but the world has moved on.
83 static const size_t value
= 8 * sizeof(IntegerType
) - 1;
86 } // namespace mozilla
88 #endif // mozilla_IntegerTypeTraits_h