@@ -190,19 +190,17 @@ mlib_extern_c_begin ();
190190// clang-format off
191191// Generates an 0b11111 bit pattern for appropriate size:
192192#define _mlibMaxofUnsigned (V ) \
193- (sizeof(V) == sizeof(uintmax_t) \
194- ? UINTMAX_MAX /* No funny bit math, just return the max of the max int */ \
195- /* Generate an 0b11111... bit pattern */ \
196- : (UINTMAX_C (1 ) << ( \
197- /* Guard against an over-shift if V is uintmax_t: */ \
198- (sizeof (V ) < sizeof (uintmax_t )) \
199- * (sizeof (V ) * CHAR_BIT )) \
200- ) - 1 )
193+ /* NOLINTNEXTLINE(bugprone-sizeof-expression) */ \
194+ mlib_bits(mlib_bitsizeof((V)), 0)
201195
202196// Generates an 0b01111 bit pattern for the two's complement max value:
203- #define _mlibMaxofSigned (V ) (_mlibMaxofUnsigned (V) >> 1ull)
197+ #define _mlibMaxofSigned (V ) \
198+ /* NOLINTNEXTLINE(bugprone-sizeof-expression) */ \
199+ mlib_bits(mlib_bitsizeof(V) - 1u, 0)
204200// Generates an 0b10000... bit pattern for the two's complement min value:
205- #define _mlibMinofSigned (V ) ((0 & (V)) - (UINTMAX_C (1) << ((sizeof (V) * CHAR_BIT) - 1)))
201+ #define _mlibMinofSigned (V ) \
202+ /* NOLINTNEXTLINE(bugprone-sizeof-expression) */ \
203+ (0 - mlib_bits(1, mlib_bitsizeof(V) - 1u))
206204// For completeness:
207205#define _mlibMinofUnsigned (V ) 0
208206// Yields true iff the operand expression has a signed type, but requires that
@@ -265,7 +263,7 @@ static inline bool (mlib_add) (uintmax_t *dst, bool dst_signed, bool a_signed, u
265263 // Perform regular wrapping arithmetic on the unsigned value. The bit pattern
266264 // is equivalent if there is two's complement signed arithmetic.
267265 const uintmax_t sum = * dst = a + b ;
268- const uintmax_t signbit = ( UINTMAX_C (1 ) << (( sizeof ( intmax_t ) * CHAR_BIT ) - 1 ) );
266+ const uintmax_t signbit = mlib_bits (1 , mlib_bitsizeof ( uintmax_t ) - 1u );
269267 // Now we check whether that overflowed according to the sign configuration.
270268 // We use some bit fiddling magic that treat the signbit as a boolean for
271269 // "is this number negative?" or "is this number “large” (i.e. bigger than signed-max)?"
@@ -359,7 +357,7 @@ static inline bool (mlib_sub) (uintmax_t *dst, bool dst_signed, bool a_signed, u
359357{
360358 // Perform the subtraction using regular wrapping arithmetic
361359 const uintmax_t diff = * dst = a - b ;
362- const uintmax_t signbit = ( UINTMAX_C (1 ) << (( sizeof ( intmax_t ) * CHAR_BIT ) - 1 ) );
360+ const uintmax_t signbit = mlib_bits (1 , mlib_bitsizeof ( uintmax_t ) - 1u );
363361 // Test whether the operation overflowed for the given sign configuration
364362 // (See mlib_add for more details on why we do this bit fiddling)
365363 if (dst_signed ) {
@@ -460,7 +458,7 @@ static inline bool (mlib_mul) (uintmax_t *dst, bool dst_signed, bool a_signed, u
460458 mlib_noexcept
461459{
462460 // Multiplication is a lot more subtle
463- const uintmax_t signbit = ( UINTMAX_C (1 ) << (( sizeof ( intmax_t ) * CHAR_BIT ) - 1 ) );
461+ const uintmax_t signbit = mlib_bits (1 , mlib_bitsizeof ( uintmax_t ) - 1u );
464462 if (dst_signed ) {
465463 if (a_signed ) {
466464 if (b_signed ) {
@@ -576,7 +574,7 @@ _mlib_ckdint (void *dst,
576574{
577575 // Perform the arithmetic on uintmax_t, for wrapping behavior
578576 uintmax_t tmp ;
579- bool ovr = fn (& tmp , minval < 0 , a .is_signed , a .i . u , b .is_signed , b .i . u );
577+ bool ovr = fn (& tmp , minval < 0 , a .is_signed , a .bits . as_unsigned , b .is_signed , b .bits . as_unsigned );
580578 // Endian-adjusting for writing the result
581579 const char * copy_from = (const char * ) & tmp ;
582580 if (!mlib_is_little_endian ()) {
@@ -651,7 +649,7 @@ _mlib_checked_cast (intmax_t min_,
651649 here .lineno ,
652650 here .func ,
653651 expr ,
654- (long long ) val .i . s ,
652+ (long long ) val .bits . as_signed ,
655653 typename_ );
656654 } else {
657655 fprintf (stderr ,
@@ -660,16 +658,16 @@ _mlib_checked_cast (intmax_t min_,
660658 here .lineno ,
661659 here .func ,
662660 expr ,
663- (unsigned long long ) val .i . u ,
661+ (unsigned long long ) val .bits . as_unsigned ,
664662 typename_ );
665663 }
666664 fflush (stderr );
667665 abort ();
668666 }
669667 if (val .is_signed ) {
670- return (uintmax_t ) val .i . s ;
668+ return (uintmax_t ) val .bits . as_signed ;
671669 }
672- return val .i . u ;
670+ return val .bits . as_unsigned ;
673671}
674672
675673mlib_extern_c_end ();
0 commit comments