]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c-cppbuiltin.c (print_bits_of_hex): New.
authorDJ Delorie <dj@redhat.com>
Thu, 8 May 2014 03:08:12 +0000 (23:08 -0400)
committerDJ Delorie <dj@gcc.gnu.org>
Thu, 8 May 2014 03:08:12 +0000 (23:08 -0400)
* c-cppbuiltin.c (print_bits_of_hex): New.
(builtin_define_type_minmax): Print values using hex so as not to
require a pre-computed list of string values.

From-SVN: r210202

gcc/c-family/ChangeLog
gcc/c-family/c-cppbuiltin.c

index 4673a16b10c10cd07dfe945bbe215f333f06d1a0..b7c65d35ff254041ba850b2b617590fd058dc536 100644 (file)
@@ -1,3 +1,9 @@
+2014-05-07  DJ Delorie  <dj@redhat.com>
+
+       * c-cppbuiltin.c (print_bits_of_hex): New.
+       (builtin_define_type_minmax): Print values using hex so as not to
+       require a pre-computed list of string values.
+
 2014-05-06  Kenneth Zadeck  <zadeck@naturalbridge.com>
            Mike Stump  <mikestump@comcast.net>
            Richard Sandiford  <rdsandiford@googlemail.com>
index 2f2e7bae8248a6225be8b7e6a54ad46d58992b61..d07c9809eab3c27b058a1309b006dd1b02d3d888 100644 (file)
@@ -1288,6 +1288,50 @@ builtin_define_type_max (const char *macro, tree type)
   builtin_define_type_minmax (NULL, macro, type);
 }
 
+/* Given a value with COUNT LSBs set, fill BUF with a hexidecimal
+   representation of that value.  For example, a COUNT of 10 would
+   return "0x3ff".  */
+
+static void
+print_bits_of_hex (char *buf, int bufsz, int count)
+{
+  gcc_assert (bufsz > 3);
+  *buf++ = '0';
+  *buf++ = 'x';
+  bufsz -= 2;
+
+  gcc_assert (count > 0);
+
+  switch (count % 4) {
+  case 0:
+    break;
+  case 1:
+    *buf++ = '1';
+    bufsz --;
+    count -= 1;
+    break;
+  case 2:
+    *buf++ = '3';
+    bufsz --;
+    count -= 2;
+    break;
+  case 3:
+    *buf++ = '7';
+    bufsz --;
+    count -= 3;
+    break;
+  }
+  while (count >= 4)
+    {
+      gcc_assert (bufsz > 1);
+      *buf++ = 'f';
+      bufsz --;
+      count -= 4;
+    }
+  gcc_assert (bufsz > 0);
+  *buf++ = 0;
+}
+
 /* Define MIN_MACRO (if not NULL) and MAX_MACRO for TYPE based on the
    precision of the type.  */
 
@@ -1295,32 +1339,17 @@ static void
 builtin_define_type_minmax (const char *min_macro, const char *max_macro,
                            tree type)
 {
-  static const char *const values[]
-    = { "127", "255",
-       "32767", "65535",
-       "2147483647", "4294967295",
-       "9223372036854775807", "18446744073709551615",
-       "170141183460469231731687303715884105727",
-       "340282366920938463463374607431768211455" };
-
-  const char *value, *suffix;
+#define PBOH_SZ (MAX_BITSIZE_MODE_ANY_INT/4+4)
+  char value[PBOH_SZ];
+
+  const char *suffix;
   char *buf;
-  size_t idx;
+  int bits;
 
-  /* Pre-rendering the values mean we don't have to futz with printing a
-     multi-word decimal value.  There are also a very limited number of
-     precisions that we support, so it's really a waste of time.  */
-  switch (TYPE_PRECISION (type))
-    {
-    case 8:    idx = 0; break;
-    case 16:   idx = 2; break;
-    case 32:   idx = 4; break;
-    case 64:   idx = 6; break;
-    case 128:  idx = 8; break;
-    default:    gcc_unreachable ();
-    }
+  bits = TYPE_PRECISION (type) + (TYPE_UNSIGNED (type) ? 0 : -1);
+
+  print_bits_of_hex (value, PBOH_SZ, bits);
 
-  value = values[idx + TYPE_UNSIGNED (type)];
   suffix = type_suffix (type);
 
   buf = (char *) alloca (strlen (max_macro) + 1 + strlen (value)