Patchwork win32: Add missing function ffs

login
register
mail settings
Submitter Stefan Weil
Date June 11, 2010, 8:57 p.m.
Message ID <1276289867-8014-1-git-send-email-weil@mail.berlios.de>
Download mbox | patch
Permalink /patch/55369/
State New
Headers show

Comments

Stefan Weil - June 11, 2010, 8:57 p.m.
mingw32 does not include function ffs.

Commit c6d29ad6e24533cc3762e1d654275607e1d03058 added a
declaration for ffs, but an implementation was missing.

For compilations with optimization, the compiler creates
inline code, so the implementation is not always needed.

Without optimization, linking fails without this patch.

Signed-off-by: Stefan Weil <weil@mail.berlios.de>
---
 osdep.c |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
Richard Henderson - June 11, 2010, 9:35 p.m.
On 06/11/2010 01:57 PM, Stefan Weil wrote:
> mingw32 does not include function ffs.
> 
> Commit c6d29ad6e24533cc3762e1d654275607e1d03058 added a
> declaration for ffs, but an implementation was missing.
> 
> For compilations with optimization, the compiler creates
> inline code, so the implementation is not always needed.
> 
> Without optimization, linking fails without this patch.
> 
> Signed-off-by: Stefan Weil <weil@mail.berlios.de>
> ---
>  osdep.c |   15 +++++++++++++++
>  1 files changed, 15 insertions(+), 0 deletions(-)
> 
> diff --git a/osdep.c b/osdep.c
> index abbc8a2..50b38e3 100644
> --- a/osdep.c
> +++ b/osdep.c
> @@ -167,6 +167,21 @@ int qemu_create_pidfile(const char *filename)
>  
>  #ifdef _WIN32
>  
> +/* mingw32 needs ffs for compilations without optimization. */
> +int ffs(int i)
> +{
> +    int position = 0;
> +    if (i != 0) {
> +        for (position = 1; i != 0; position++) {
> +            if (i & 1) {
> +                break;
> +            }
> +            i >>= 1;
> +        }
> +    }
> +    return position;
> +}

This is confusingly written.  You've already tested for zero.

  for (pos = 1; (i & 1) == 0; pos++) {
    i >>= 1;
  }

That said, is there any reason not to just do

int ffs(int i)
{
    return __builtin_ffs(i);
}

?


r~

Patch

diff --git a/osdep.c b/osdep.c
index abbc8a2..50b38e3 100644
--- a/osdep.c
+++ b/osdep.c
@@ -167,6 +167,21 @@  int qemu_create_pidfile(const char *filename)
 
 #ifdef _WIN32
 
+/* mingw32 needs ffs for compilations without optimization. */
+int ffs(int i)
+{
+    int position = 0;
+    if (i != 0) {
+        for (position = 1; i != 0; position++) {
+            if (i & 1) {
+                break;
+            }
+            i >>= 1;
+        }
+    }
+    return position;
+}
+
 /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
 #define _W32_FT_OFFSET (116444736000000000ULL)