summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordj <dj@138bc75d-0d04-0410-961f-82ee72b054a4>2004-06-08 19:24:07 +0000
committerdj <dj@138bc75d-0d04-0410-961f-82ee72b054a4>2004-06-08 19:24:07 +0000
commit2f8c398164724cd8cdc439c871cec8d3664d663e (patch)
tree887165390ca703d20364aad9a0a6ada538084ea9
parent442ab8352a0a09b6da05f5079f9ee2bb2a246807 (diff)
downloadgcc-2f8c398164724cd8cdc439c871cec8d3664d663e.tar.gz
* toplev.c (floor_log2_wide): Replace loop with faster bit
operations. (exact_log2_wide): Define in terms of the above. * toplev.h (floor_log2): Use _builtin_clz family of builtins if available. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@82778 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r--gcc/ChangeLog8
-rw-r--r--gcc/toplev.c50
-rw-r--r--gcc/toplev.h23
3 files changed, 62 insertions, 19 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index b251c45ad0c..d150c60822d 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,11 @@
+2004-06-08 DJ Delorie <dj@redhat.com>
+
+ * toplev.c (floor_log2_wide): Replace loop with faster bit
+ operations.
+ (exact_log2_wide): Define in terms of the above.
+ * toplev.h (floor_log2): Use _builtin_clz family of builtins if
+ available.
+
2004-06-08 Andrew Pinski <pinskia@physics.uc.edu>
* config/rs6000/rs6000.c (print_operand, <case 'z'>):
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 2741be5047f..e1803943d9c 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1248,36 +1248,48 @@ read_integral_parameter (const char *p, const char *pname, const int defval)
return atoi (p);
}
-/* Return the logarithm of X, base 2, considering X unsigned,
- if X is a power of 2. Otherwise, returns -1.
+/* 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 `exact_log2' macro. */
+ This should be used via the floor_log2 macro. */
int
-exact_log2_wide (unsigned HOST_WIDE_INT x)
+floor_log2_wide (unsigned HOST_WIDE_INT x)
{
- int log = 0;
- /* Test for 0 or a power of 2. */
- if (x == 0 || x != (x & -x))
+ int t=0;
+ if (x == 0)
return -1;
- while ((x >>= 1) != 0)
- log++;
- return log;
+ if (sizeof (HOST_WIDE_INT)*8 > 64)
+ if (x >= (unsigned HOST_WIDE_INT)(1 << (t+64)))
+ t += 64;
+ if (sizeof (HOST_WIDE_INT)*8 > 32)
+ if (x >= (unsigned HOST_WIDE_INT)(1 << (t+32)))
+ t += 32;
+ if (x >= (unsigned HOST_WIDE_INT)(1 << (t+16)))
+ t += 16;
+ if (x >= (unsigned HOST_WIDE_INT)(1 << (t+8)))
+ t += 8;
+ if (x >= (unsigned HOST_WIDE_INT)(1 << (t+4)))
+ t += 4;
+ if (x >= (unsigned HOST_WIDE_INT)(1 << (t+2)))
+ t += 2;
+ if (x >= (unsigned HOST_WIDE_INT)(1 << (t+1)))
+ t += 1;
+ return t;
}
-/* Given X, an unsigned number, return the largest int Y such that 2**Y <= X.
- If X is 0, return -1.
+/* 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 floor_log2 macro. */
+ This should be used via the `exact_log2' macro. */
int
-floor_log2_wide (unsigned HOST_WIDE_INT x)
+exact_log2_wide (unsigned HOST_WIDE_INT x)
{
- int log = -1;
- while (x != 0)
- log++,
- x >>= 1;
- return log;
+ /* Test for 0 or a power of 2. */
+ if (x == 0 || x != (x & -x))
+ return -1;
+ return floor_log2_wide (x);
}
/* Handler for fatal signals, such as SIGSEGV. These are transformed
diff --git a/gcc/toplev.h b/gcc/toplev.h
index aca355771fb..f12102e9f7b 100644
--- a/gcc/toplev.h
+++ b/gcc/toplev.h
@@ -152,8 +152,31 @@ extern bool fast_math_flags_set_p (void);
#ifndef exact_log2
#define exact_log2(N) exact_log2_wide ((unsigned HOST_WIDE_INT) (N))
+
+#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)
+{
+ if (n)
+ return (sizeof(FL2T__)*8-1) - (int)FL2T_CLZ__(n);
+ return -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);