From b1c2576c2097482d3dab912dd6e7e462dcb40c1f Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 25 Mar 2023 18:36:32 -0700 Subject: [PATCH] ircd::util: Add sign cast convenience suite. --- include/ircd/util/typography.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/include/ircd/util/typography.h b/include/ircd/util/typography.h index 87c49a016..7d8ce65b2 100644 --- a/include/ircd/util/typography.h +++ b/include/ircd/util/typography.h @@ -140,6 +140,34 @@ struct is_specialization_of, T> :std::true_type {}; +// +// Convenience signedness cast template +// + +template +using enable_if_s2u = std::enable_if::value, typename std::make_unsigned::type>; + +template +[[using gnu: always_inline, gnu_inline, artificial]] +extern inline typename enable_if_s2u::type * +sign_cast(T *const t) +{ + using type = typename std::make_unsigned::type; + return reinterpret_cast(t); +} + +template +using enable_if_u2s = std::enable_if::value, typename std::make_signed::type>; + +template +[[using gnu: always_inline, gnu_inline, artificial]] +extern inline typename enable_if_u2s::type * +sign_cast(T *const t) +{ + using type = typename std::make_signed::type; + return reinterpret_cast(t); +} + // // Convenience const_cast deduction template //