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 #ifndef mozilla_IntegerTypeTraits_h
8 #define mozilla_IntegerTypeTraits_h
12 #include <type_traits>
19 * StdintTypeForSizeAndSignedness returns the stdint integer type
20 * of given size (can be 1, 2, 4 or 8) and given signedness
21 * (false means unsigned, true means signed).
23 template <size_t Size
, bool Signedness
>
24 struct StdintTypeForSizeAndSignedness
;
27 struct StdintTypeForSizeAndSignedness
<1, true> {
32 struct StdintTypeForSizeAndSignedness
<1, false> {
37 struct StdintTypeForSizeAndSignedness
<2, true> {
42 struct StdintTypeForSizeAndSignedness
<2, false> {
43 typedef uint16_t Type
;
47 struct StdintTypeForSizeAndSignedness
<4, true> {
52 struct StdintTypeForSizeAndSignedness
<4, false> {
53 typedef uint32_t Type
;
57 struct StdintTypeForSizeAndSignedness
<8, true> {
62 struct StdintTypeForSizeAndSignedness
<8, false> {
63 typedef uint64_t Type
;
68 template <size_t Size
>
69 struct UnsignedStdintTypeForSize
70 : detail::StdintTypeForSizeAndSignedness
<Size
, false> {};
72 template <size_t Size
>
73 struct SignedStdintTypeForSize
74 : detail::StdintTypeForSizeAndSignedness
<Size
, true> {};
76 template <typename IntegerType
>
77 struct PositionOfSignBit
{
78 static_assert(std::is_integral_v
<IntegerType
>,
79 "PositionOfSignBit is only for integral types");
80 // 8 here should be CHAR_BIT from limits.h, but the world has moved on.
81 static const size_t value
= 8 * sizeof(IntegerType
) - 1;
84 } // namespace mozilla
86 #endif // mozilla_IntegerTypeTraits_h