From 71c63792dd7462cc6d1654bf3a3c82eb073cde03 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 29 Jan 2026 21:54:47 -0800 Subject: [PATCH] Move _BitInt to format.h --- include/fmt/base.h | 31 ------------------------------- include/fmt/format.h | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index d6d32e97..83e36d6c 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -409,23 +409,6 @@ inline auto map(int128_opt) -> monostate { return {}; } inline auto map(uint128_opt) -> monostate { return {}; } #endif -#ifdef FMT_USE_BITINT -// Use the provided definition. -#elif FMT_CLANG_VERSION >= 1500 && !defined(__CUDACC__) -# define FMT_USE_BITINT 1 -#else -# define FMT_USE_BITINT 0 -#endif - -#if FMT_USE_BITINT -FMT_PRAGMA_CLANG(diagnostic ignored "-Wbit-int-extension") -template using bitint = _BitInt(N); -template using ubitint = unsigned _BitInt(N); -#else -template struct bitint {}; -template struct ubitint {}; -#endif // FMT_USE_BITINT - // Casts a nonnegative integer to unsigned. template FMT_CONSTEXPR auto to_unsigned(Int value) -> make_unsigned_t { @@ -1177,11 +1160,6 @@ template struct type_mapper { static auto map(uint128_opt) -> uint128_opt; static auto map(bool) -> bool; - template - static auto map(bitint) -> conditional_t; - template - static auto map(ubitint) -> conditional_t; - template ::value)> static auto map(T) -> conditional_t< std::is_same::value || std::is_same::value, Char, void>; @@ -2130,15 +2108,6 @@ template class value { FMT_INLINE value(uint128_opt x FMT_BUILTIN) : uint128_value(x) {} constexpr FMT_INLINE value(bool x FMT_BUILTIN) : bool_value(x) {} - template - constexpr FMT_INLINE value(bitint x FMT_BUILTIN) : long_long_value(x) { - static_assert(N <= 64, "unsupported _BitInt"); - } - template - constexpr FMT_INLINE value(ubitint x FMT_BUILTIN) : ulong_long_value(x) { - static_assert(N <= 64, "unsupported _BitInt"); - } - template ::value)> constexpr FMT_INLINE value(T x FMT_BUILTIN) : char_value(x) { static_assert( diff --git a/include/fmt/format.h b/include/fmt/format.h index 1fd4a59e..57944612 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -198,6 +198,23 @@ struct is_contiguous> namespace detail { +#ifdef FMT_USE_BITINT +// Use the provided definition. +#elif FMT_CLANG_VERSION >= 1500 && !defined(__CUDACC__) +# define FMT_USE_BITINT 1 +#else +# define FMT_USE_BITINT 0 +#endif + +#if FMT_USE_BITINT +FMT_PRAGMA_CLANG(diagnostic ignored "-Wbit-int-extension") +template using bitint = _BitInt(N); +template using ubitint = unsigned _BitInt(N); +#else +template struct bitint {}; +template struct ubitint {}; +#endif // FMT_USE_BITINT + // __builtin_clz is broken in clang with Microsoft codegen: // https://github.com/fmtlib/fmt/issues/519. #if !FMT_MSC_VERSION @@ -4039,9 +4056,28 @@ class formatter, Char> : public formatter, Char> {}; template -struct formatter, Char> : formatter {}; +struct formatter, Char> : formatter { + static_assert(N <= 64, "unsupported _BitInt"); + static auto format_as(detail::bitint x) -> long long { + return static_cast(x); + } + template + auto format(detail::bitint x, Context& ctx) const -> decltype(ctx.out()) { + return formatter::format(format_as(x), ctx); + } +}; + template -struct formatter, Char> : formatter {}; +struct formatter, Char> : formatter { + static_assert(N <= 64, "unsupported _BitInt"); + static auto format_as(detail::ubitint x) -> ullong { + return static_cast(x); + } + template + auto format(detail::ubitint x, Context& ctx) const -> decltype(ctx.out()) { + return formatter::format(format_as(x), ctx); + } +}; template struct formatter