[PATCH] support: Add TEST_COMPARE macro

Florian Weimer fweimer@redhat.com
Thu Nov 23 11:38:00 GMT 2017


On 11/23/2017 12:09 PM, Andreas Schwab wrote:
> On Nov 23 2017, Florian Weimer <fweimer@redhat.com> wrote:
> 
>> +/* Compare the two integers LEFT and RIGHT and report failure if they
>> +   are different.  */
>> +#define TEST_COMPARE(left, right)                                       \
>> +  ({                                                                    \
>> +    __typeof__ (left) __left_value = (left);                            \
>> +    __typeof__ (right) __right_value = (right);                         \
>> +    /* Prevent accidental use with larger-than-long long types.  */     \
>> +    _Static_assert (sizeof (__left_value) <= sizeof (long long),        \
>> +                    "left value fits into long long");                  \
>> +    _Static_assert (sizeof (__right_value) <= sizeof (long long),       \
>> +                    "right value fits into long long");                 \
>> +    /* Compare the value and the sign, to avoid false equality.  */     \
>> +    /* (A signed value could be converted to an unsigned type.)  */     \
> 
> Why do you need that?  Any signed value converted to unsigned is still
> unique.

Not sure I understand.

#include <stdio.h>

int
main (void)
{
   long long left = -1;
   unsigned long long right = -1;
   printf ("%d\n", left == right);
}

This prints

1

for me, although mathematically, the values are different.

Thanks,
Florian



More information about the Libc-alpha mailing list