| Message ID | 20251030165816.1095180-6-barnabas.pocze@ideasonboard.com |
|---|---|
| State | New |
| Headers | show |
| Series |
|
| Related | show |
Quoting Barnabás Pőcze (2025-10-30 16:57:59) > `has_single_bit()` checks if the given unsigned integer has a single > bit set, that is, whether the number is a power of two or not. > > Not all requirements of the C++20 standard are implemented. > > See https://en.cppreference.com/w/cpp/numeric/has_single_bit > > Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com> > Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> > --- > include/libcamera/base/internal/cxx20.h | 16 ++++++++++++++++ > 1 file changed, 16 insertions(+) > > diff --git a/include/libcamera/base/internal/cxx20.h b/include/libcamera/base/internal/cxx20.h > index 1f4caf56f8..d47168a70a 100644 > --- a/include/libcamera/base/internal/cxx20.h > +++ b/include/libcamera/base/internal/cxx20.h > @@ -7,6 +7,8 @@ > > #pragma once > > +#include <type_traits> > + > /** > * \internal > * \file cxx20.h > @@ -39,4 +41,18 @@ template<typename T> struct type_identity { > */ > template<typename T> using type_identity_t = typename type_identity<T>::type; > > +/** > + * \internal > + * \brief std::has_single_bit() > + * > + * Implementation of std::has_single_bit() for C++17. > + */ > +template<typename T> > +constexpr bool has_single_bit(T x) noexcept > +{ > + static_assert(std::is_unsigned_v<T>); Can't this be forced to unsigned with an enable_if or such in the template? But I guess compile time static assert is just the same and perhaps gives a nicer error so: Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > + > + return x != 0 && (x & (x - 1)) == 0; > +} > + > } /* namespace libcamera::internal::cxx20 */ > -- > 2.51.1 >
2025. 10. 31. 12:22 keltezéssel, Kieran Bingham írta: > Quoting Barnabás Pőcze (2025-10-30 16:57:59) >> `has_single_bit()` checks if the given unsigned integer has a single >> bit set, that is, whether the number is a power of two or not. >> >> Not all requirements of the C++20 standard are implemented. >> >> See https://en.cppreference.com/w/cpp/numeric/has_single_bit >> >> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com> >> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> >> --- >> include/libcamera/base/internal/cxx20.h | 16 ++++++++++++++++ >> 1 file changed, 16 insertions(+) >> >> diff --git a/include/libcamera/base/internal/cxx20.h b/include/libcamera/base/internal/cxx20.h >> index 1f4caf56f8..d47168a70a 100644 >> --- a/include/libcamera/base/internal/cxx20.h >> +++ b/include/libcamera/base/internal/cxx20.h >> @@ -7,6 +7,8 @@ >> >> #pragma once >> >> +#include <type_traits> >> + >> /** >> * \internal >> * \file cxx20.h >> @@ -39,4 +41,18 @@ template<typename T> struct type_identity { >> */ >> template<typename T> using type_identity_t = typename type_identity<T>::type; >> >> +/** >> + * \internal >> + * \brief std::has_single_bit() >> + * >> + * Implementation of std::has_single_bit() for C++17. >> + */ >> +template<typename T> >> +constexpr bool has_single_bit(T x) noexcept >> +{ >> + static_assert(std::is_unsigned_v<T>); > > Can't this be forced to unsigned with an enable_if or such in the > template? But I guess compile time static assert is just the same and > perhaps gives a nicer error so: Yes. But if there are no overloads, then I think a static_assert provides better error messages. > > Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > >> + >> + return x != 0 && (x & (x - 1)) == 0; >> +} >> + >> } /* namespace libcamera::internal::cxx20 */ >> -- >> 2.51.1 >>
Quoting Barnabás Pőcze (2025-10-31 11:31:57) > 2025. 10. 31. 12:22 keltezéssel, Kieran Bingham írta: > > Quoting Barnabás Pőcze (2025-10-30 16:57:59) > >> `has_single_bit()` checks if the given unsigned integer has a single > >> bit set, that is, whether the number is a power of two or not. > >> > >> Not all requirements of the C++20 standard are implemented. > >> > >> See https://en.cppreference.com/w/cpp/numeric/has_single_bit > >> > >> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com> > >> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> > >> --- > >> include/libcamera/base/internal/cxx20.h | 16 ++++++++++++++++ > >> 1 file changed, 16 insertions(+) > >> > >> diff --git a/include/libcamera/base/internal/cxx20.h b/include/libcamera/base/internal/cxx20.h > >> index 1f4caf56f8..d47168a70a 100644 > >> --- a/include/libcamera/base/internal/cxx20.h > >> +++ b/include/libcamera/base/internal/cxx20.h > >> @@ -7,6 +7,8 @@ > >> > >> #pragma once > >> > >> +#include <type_traits> > >> + > >> /** > >> * \internal > >> * \file cxx20.h > >> @@ -39,4 +41,18 @@ template<typename T> struct type_identity { > >> */ > >> template<typename T> using type_identity_t = typename type_identity<T>::type; > >> > >> +/** > >> + * \internal > >> + * \brief std::has_single_bit() > >> + * > >> + * Implementation of std::has_single_bit() for C++17. > >> + */ > >> +template<typename T> > >> +constexpr bool has_single_bit(T x) noexcept > >> +{ > >> + static_assert(std::is_unsigned_v<T>); > > > > Can't this be forced to unsigned with an enable_if or such in the > > template? But I guess compile time static assert is just the same and > > perhaps gives a nicer error so: > > Yes. But if there are no overloads, then I think a static_assert provides > better error messages. > Agreed, this looks good to me. > > > > > Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > > > >> + > >> + return x != 0 && (x & (x - 1)) == 0; > >> +} > >> + > >> } /* namespace libcamera::internal::cxx20 */ > >> -- > >> 2.51.1 > >> >
diff --git a/include/libcamera/base/internal/cxx20.h b/include/libcamera/base/internal/cxx20.h index 1f4caf56f8..d47168a70a 100644 --- a/include/libcamera/base/internal/cxx20.h +++ b/include/libcamera/base/internal/cxx20.h @@ -7,6 +7,8 @@ #pragma once +#include <type_traits> + /** * \internal * \file cxx20.h @@ -39,4 +41,18 @@ template<typename T> struct type_identity { */ template<typename T> using type_identity_t = typename type_identity<T>::type; +/** + * \internal + * \brief std::has_single_bit() + * + * Implementation of std::has_single_bit() for C++17. + */ +template<typename T> +constexpr bool has_single_bit(T x) noexcept +{ + static_assert(std::is_unsigned_v<T>); + + return x != 0 && (x & (x - 1)) == 0; +} + } /* namespace libcamera::internal::cxx20 */