[Bug c/66296] New: Gcc generated wrong code with uint16_t add/sub even with no optimization

wanght at tsinghua dot org.cn gcc-bugzilla@gcc.gnu.org
Wed May 27 02:14:00 GMT 2015


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66296

            Bug ID: 66296
           Summary: Gcc generated wrong code with uint16_t  add/sub even
                    with no optimization
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wanght at tsinghua dot org.cn
  Target Milestone: ---

Here is  a small sample to demo this: 
f0() and f1() will have different return value on corner case!
-------------------------------------------
f.c
#include <stdint.h>


#define OFFSET_1 0x100
#define OFFSET_OTHER 0x200

uint16_t n=1;

int f0(uint16_t * a, uint16_t * b)
{

    if((*a)-(*b)==n)
        return OFFSET_1;
    else
        return OFFSET_OTHER;
}

int f1(uint16_t * a, uint16_t * b)
{
    uint16_t off=(*a)-(*b);

    if(off==n)
        return OFFSET_1;
    else
        return OFFSET_OTHER;
}
----------------------------------------
m.c

#include <stdio.h>
#include <stdint.h>



extern int f0(uint16_t * a, uint16_t * b);
extern int f1(uint16_t * a, uint16_t * b);


int main(int argc, char * argv[])
{

   uint16_t a=0x0;
   uint16_t b=0xffff;

   printf("f0()=0x%x\n",f0(&a,&b));
   printf("f1()=0x%x\n",f1(&a,&b));

   return 0;    

}
----------------------------------------------
Compiling:
gcc -o m m.c t.c

-----------------------------------------
Output:

f0()=0x200
f1()=0x100

--------------------------------------------
gcc version:

COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.9.2/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla
--enable-bootstrap --enable-shared --enable-threads=posix
--enable-checking=release --enable-multilib --with-system-zlib
--enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object
--enable-linker-build-id --with-linker-hash-style=gnu
--enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --enable-plugin
--enable-initfini-array --disable-libgcj
--with-isl=/builddir/build/BUILD/gcc-4.9.2-20150212/obj-x86_64-redhat-linux/isl-install
--with-cloog=/builddir/build/BUILD/gcc-4.9.2-20150212/obj-x86_64-redhat-linux/cloog-install
--enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686
--build=x86_64-redhat-linux
Thread model: posix
gcc version 4.9.2 20150212 (Red Hat 4.9.2-6) (GCC) 


------------------------------------
System Info:

 cat /etc/issue
Fedora release 21 (Twenty One)
Kernel \r on an \m (\l)



More information about the Gcc-bugs mailing list