]> Savannah Git Hosting - gnulib.git/commitdiff
integer_length: Optimize for MSVC.
authorBruno Haible <bruno@clisp.org>
Mon, 3 Aug 2020 22:40:24 +0000 (00:40 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 3 Aug 2020 22:40:24 +0000 (00:40 +0200)
* lib/integer_length.c: Include <intrin.h>.
(integer_length): With MSVC, use the _BitScanReverse built-in.

ChangeLog
lib/integer_length.c

index 51b2be7bca2d54585b654c3bffcc4314ab7a6994..b2169e369fa9158bd624557c1d9d8ff502a4aec2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2020-08-03  Bruno Haible  <bruno@clisp.org>
+
+       integer_length: Optimize for MSVC.
+       * lib/integer_length.c: Include <intrin.h>.
+       (integer_length): With MSVC, use the _BitScanReverse built-in.
+
 2020-08-03  Bruno Haible  <bruno@clisp.org>
 
        ffsll: Optimize for MSVC in 64-bit mode.
index edad54b2a9eb5c4e796c79a3b04cbef8496fb0c2..43d8f49d65dae259953167b803526d4bae3d7c0c 100644 (file)
 
 #include "float+.h"
 
-/* MSVC with option -fp:strict refuses to compile constant initializers that
-   contain floating-point operations.  Pacify this compiler.  */
-#ifdef _MSC_VER
-# pragma fenv_access (off)
+#if defined _MSC_VER
+# include <intrin.h>
 #endif
 
 #define NBITS (sizeof (unsigned int) * CHAR_BIT)
@@ -41,6 +39,14 @@ integer_length (unsigned int x)
     return 0;
   else
     return NBITS - __builtin_clz (x);
+#elif defined _MSC_VER
+  /* _BitScanReverse
+     <https://docs.microsoft.com/en-us/cpp/intrinsics/bitscanreverse-bitscanreverse64> */
+  unsigned long bit;
+  if (_BitScanReverse (&bit, x))
+    return bit + 1;
+  else
+    return 0;
 #else
 # if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
   if (NBITS <= DBL_MANT_BIT)