This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
simplify ifdefery around floor_log2, exact_log2
- From: Richard Henderson <rth at twiddle dot net>
- To: gcc-patches at gcc dot gnu dot org
- Date: Thu, 11 Nov 2004 18:26:31 -0800
- Subject: simplify ifdefery around floor_log2, exact_log2
I noticed this a couple of days ago when pointing Nathan at the
proper solution for the bitmap routines.
r~
* toplev.c (floor_log2): Rename from floor_log2_wide, use CLZ_HWI.
(exact_log2): Rename from exact_log2_wide, use CTZ_HWI.
* toplev.h (FL2T__): Remove.
(CLZ_HWI): Rename from FL2T_CLZ__.
(CTZ_HWI): New.
(floor_log2): Simplify.
(exact_log2): New.
Index: toplev.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.c,v
retrieving revision 1.930
diff -u -p -r1.930 toplev.c
--- toplev.c 2 Nov 2004 02:41:36 -0000 1.930
+++ toplev.c 12 Nov 2004 02:18:59 -0000
@@ -537,20 +537,23 @@ read_integral_parameter (const char *p,
}
/* Given X, an unsigned number, return the largest int Y such that 2**Y <= X.
- If X is 0, return -1.
-
- This should be used via the floor_log2 macro. */
+ If X is 0, return -1. */
int
-floor_log2_wide (unsigned HOST_WIDE_INT x)
+floor_log2 (unsigned HOST_WIDE_INT x)
{
- int t=0;
+ int t = 0;
+
if (x == 0)
return -1;
- if (sizeof (HOST_WIDE_INT) * 8 > 64)
+
+#ifdef CLZ_HWI
+ t = HOST_BITS_PER_WIDE_INT - 1 - (int) CLZ_HWI (x);
+#else
+ if (HOST_BITS_PER_WIDE_INT > 64)
if (x >= (unsigned HOST_WIDE_INT) 1 << (t + 64))
t += 64;
- if (sizeof (HOST_WIDE_INT) * 8 > 32)
+ if (HOST_BITS_PER_WIDE_INT > 32)
if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 32))
t += 32;
if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 16))
@@ -563,21 +566,24 @@ floor_log2_wide (unsigned HOST_WIDE_INT
t += 2;
if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 1))
t += 1;
+#endif
+
return t;
}
/* Return the logarithm of X, base 2, considering X unsigned,
- if X is a power of 2. Otherwise, returns -1.
-
- This should be used via the `exact_log2' macro. */
+ if X is a power of 2. Otherwise, returns -1. */
int
-exact_log2_wide (unsigned HOST_WIDE_INT x)
+exact_log2 (unsigned HOST_WIDE_INT x)
{
- /* Test for 0 or a power of 2. */
- if (x == 0 || x != (x & -x))
+ if (x != (x & -x))
return -1;
- return floor_log2_wide (x);
+#ifdef CTZ_HWI
+ return x ? CTZ_HWI (x) : -1;
+#else
+ return floor_log2 (x);
+#endif
}
/* Handler for fatal signals, such as SIGSEGV. These are transformed
Index: toplev.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.h,v
retrieving revision 1.131
diff -u -p -r1.131 toplev.h
--- toplev.h 4 Nov 2004 23:26:35 -0000 1.131
+++ toplev.h 12 Nov 2004 02:18:59 -0000
@@ -158,38 +158,37 @@ extern void decode_d_option (const char
/* Return true iff flags are set as if -ffast-math. */
extern bool fast_math_flags_set_p (void);
-/* The following functions accept a wide integer argument. Rather
- than having to cast on every function call, we use a macro instead. */
+/* Return log2, or -1 if not exact. */
+extern int exact_log2 (unsigned HOST_WIDE_INT);
-#ifndef exact_log2
-#define exact_log2(N) exact_log2_wide ((unsigned HOST_WIDE_INT) (N))
+/* Return floor of log2, with -1 for zero. */
+extern int floor_log2 (unsigned HOST_WIDE_INT);
-#if (__GNUC__ * 1000 + __GNUC_MINOR__) >= 3004
-#if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
-#define FL2T__ HOST_WIDE_INT
-#define FL2T_CLZ__ __builtin_clzll
-#else
-#if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
-#define FL2T__ HOST_WIDE_INT
-#define FL2T_CLZ__ __builtin_clzl
-#else
-#define FL2T__ int
-#define FL2T_CLZ__ __builtin_clz
-#endif
-#endif
-static inline int floor_log2(FL2T__ n)
+/* Inline versions of the above for speed. */
+#if GCC_VERSION >= 3004
+# if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
+# define CLZ_HWI __builtin_clzl
+# define CTZ_HWI __builtin_ctzl
+# elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
+# define CLZ_HWI __builtin_clzll
+# define CTZ_HWI __builtin_ctzll
+# else
+# define CLZ_HWI __builtin_clz
+# define CTZ_HWI __builtin_ctz
+# endif
+
+extern inline int
+floor_log2 (unsigned HOST_WIDE_INT x)
+{
+ return x ? HOST_BITS_PER_WIDE_INT - 1 - (int) CLZ_HWI (x) : -1;
+}
+
+extern inline int
+exact_log2 (unsigned HOST_WIDE_INT x)
{
- if (n)
- return (sizeof(FL2T__)*8-1) - (int)FL2T_CLZ__(n);
- return -1;
+ return x == (x & -x) && x ? (int) CTZ_HWI (x) : -1;
}
-#else
-#define floor_log2(N) floor_log2_wide ((unsigned HOST_WIDE_INT) (N))
-#endif
-
-#endif
-extern int exact_log2_wide (unsigned HOST_WIDE_INT);
-extern int floor_log2_wide (unsigned HOST_WIDE_INT);
+#endif /* GCC_VERSION >= 3004 */
/* Functions used to get and set GCC's notion of in what directory
compilation was started. */