This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] support: Add TEST_COMPARE macro


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]