gettimeofday: port recent C++ fix to Emacs
[gnulib.git] / lib / ffsl.h
index 99e7d26..340f25e 100644 (file)
@@ -1,5 +1,5 @@
 /* ffsl.h -- find the first set bit in a word.
-   Copyright (C) 2011 Free Software Foundation, Inc.
+   Copyright (C) 2011-2013 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -19,6 +19,9 @@
 /* This file is meant to be included by ffsl.c and ffsll.c, after
    they have defined FUNC and TYPE.  */
 
+#include <config.h>
+
+/* Specification.  */
 #include <string.h>
 
 #include <limits.h>
@@ -34,34 +37,31 @@ FUNC (TYPE i)
 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) && defined GCC_BUILTIN
   return GCC_BUILTIN (i);
 #else
-  if (sizeof (TYPE) == sizeof (int))
-    return ffs (i);
-  else
+  unsigned TYPE j = i;
+  /* Split j into chunks, and look at one chunk after the other.  */
+  enum { chunk_bits = CHAR_BIT * sizeof (unsigned int) };
+  /* The number of chunks is ceil (sizeof (TYPE) / sizeof (unsigned int))
+     = (sizeof (TYPE) - 1) / sizeof (unsigned int) + 1. */
+  enum { chunk_count = (sizeof (TYPE) - 1) / sizeof (unsigned int) + 1 };
+
+  if (chunk_count > 1)
     {
-      unsigned TYPE j = i;
-      /* Split j into chunks, and look at one chunk after the other.  */
-      /* Define chunk_bits so as to avoid a GCC warning
-           "right shift count >= width of type"
-         if sizeof (TYPE) == sizeof (int).  */
-      enum
-        {
-          chunk_bits = (sizeof (TYPE) != sizeof (int)
-                        ? CHAR_BIT * sizeof (unsigned int)
-                        : 0)
-        };
-      int result = 0;
+      size_t k;
 
       /* It is tempting to write  if (!j)  here, but if we do this,
          Solaris 10/x86 "cc -O" miscompiles the code.  */
       if (!i)
         return 0;
-      while (1)
-        {
-          if ((unsigned int) j)
-            return result + ffs ((unsigned int) j);
-          j >>= chunk_bits;
-          result += chunk_bits;
-        }
+      /* Unroll the first loop round.  k = 0.  */
+      if ((unsigned int) j)
+        return ffs ((unsigned int) j);
+      /* Generic loop.  */
+      for (k = 1; k < chunk_count - 1; k++)
+        if ((unsigned int) (j >> (k * chunk_bits)) != 0)
+          return k * chunk_bits + ffs ((unsigned int) (j >> (k * chunk_bits)));
     }
+  /* Last loop round.  k = chunk_count - 1.  */
+  return (chunk_count - 1) * chunk_bits
+         + ffs ((unsigned int) (j >> ((chunk_count - 1) * chunk_bits)));
 #endif
 }