[llvm-bugs] [Bug 32915] New: issue with unsigned long operations. (hweight64)

via llvm-bugs llvm-bugs at lists.llvm.org
Thu May 4 01:07:21 PDT 2017


            Bug ID: 32915
           Summary: issue with unsigned long operations. (hweight64)
           Product: clang
           Version: 3.9
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: bogdan.becheriu at yahoo.com
                CC: llvm-bugs at lists.llvm.org

Created attachment 18406
  --> https://bugs.llvm.org/attachment.cgi?id=18406&action=edit
the source code affected

compile the following code. (also attached)

/**  compute number of 1 bits in generic data type
 *   Copyright (C) 2017  Bogdan Becheriu
 *   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
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   GNU General Public License for more details.
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.

#define HWEIGHT_IMPL(x) \
static __inline__ unsigned int hweight ## x(u ## x w)\
        unsigned int k;\
        for (k = 1; k < sizeof(w) << 3; k <<= 1) {\
                u ## x mask = ((u ## x)-1) / ((1 << k) + 1);\
                w = (w & mask) + ((w >> k) & mask);\
        return w;\

typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long u64;


/* @@ demo */
#include "stdio.h"

int main(int argc, char *argv[], char **envp)
        printf("bits set in %d: %d %d %d %d\n", -1, hweight8(-1),
hweight16(-1), hweight32(-1), hweight64(-1));
        return 0;


it happens only when enabling -O2 or higher on the hweight64 implementation
(unsigned long type).
please see output of executable on both gcc and clang below.

me at gentoo ~/probablyuseless-code $ gcc -Wall -Werror -pedantic -O3 -ansi
me at gentoo ~/probablyuseless-code $ ./a.out
bits set in -1: 8 16 32 64

me at gentoo ~/probablyuseless-code $ clang -Wall -Werror -pedantic -O3 -ansi
me at gentoo ~/probablyuseless-code $ ./a.out
bits set in -1: 8 16 32 0

clang version 3.9.1 (tags/RELEASE_391/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20170504/418af886/attachment.html>

More information about the llvm-bugs mailing list