SST 16.0.0
Structural Simulation Toolkit
bit_util.h
1// Copyright 2009-2026 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2026, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_SST_UTLI_BIT_UTIL_H
13#define SST_CORE_SST_UTLI_BIT_UTIL_H
14
15#include <cstdint>
16#include <limits>
17#include <type_traits>
18
19namespace SST::bit_util {
20
21
22/////////////////////////////////////////////////////////////////////////////////////////////////////
23// Templates to get the maximum value of a type //
24/////////////////////////////////////////////////////////////////////////////////////////////////////
25
26/**
27 Gets the maximum value of a type. This version can take the variable of the type to infer the needed type.
28
29 @param var Unused in function. Only used to infer the correct type
30
31 @return Maximum value of the templated type
32*/
33// template <typename T>
34// constexpr T type_max_v(T& var [[maybe_unused]])
35// {
36// static_assert(std::is_integral<T>::value, "Template parameter must be an integral type.");
37// return std::numeric_limits<T>::max();
38// }
39
40template <typename T>
41constexpr T type_max = std::numeric_limits<T>::max();
42
43// Mask with the specified number of top bits set to all ones
44template <typename T, unsigned N = sizeof(T) * 8 / 2>
45inline constexpr T upper_mask = ~static_cast<T>(0) << ((sizeof(T) * 8) - N);
46
47// Mask with the specified number of bottom bits set to all ones
48template <typename T, unsigned N = sizeof(T) * 8 / 2>
49inline constexpr T lower_mask =
50 static_cast<T>(~static_cast<typename std::make_unsigned<T>::type>(0) >> ((sizeof(T) * 8) - N));
51
52
53} // namespace SST::bit_util
54
55#endif // SST_CORE_SST_TYPES_H