public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/40394]  New: Variable values calculated differently depending on storage class
@ 2009-06-10 10:39 goran dot steen at enea dot com
  2009-06-10 12:42 ` [Bug c/40394] " goran dot steen at enea dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: goran dot steen at enea dot com @ 2009-06-10 10:39 UTC (permalink / raw)
  To: gcc-bugs

/************* CODE EXAMPLE ***************/ 
#include <stdio.h> 
#include <string.h> 

typedef unsigned int U32; 
typedef unsigned short U16; 
typedef unsigned char U8; 

#ifndef __GNUC__ 
#define __attribute__(a) 
#endif /* __GNUC__ */ 
#define APP6DR_PACK_STRUCT __attribute__((packed)) 

#define APP6DR_INLINE_FUNC(func) \ 
   __inline__ func __attribute__((always_inline)); \ 
   extern __inline__ __attribute__((gnu_inline)) func 

struct app6drIpHdr 
{ 
   unsigned int ip_v :4; 
   unsigned int ip_hl :4; 
   U8 ip_tos; 
   U16 ip_len; 
   U16 ip_id; 
   U16 ip_off; 
   U8 ip_ttl; 
   U8 ip_p; 
   U16 ip_sum; 
   struct 
   { 
      U32 ip_sa; 
      U32 ip_da; 
   } ip_addr_r; 
} APP6DR_PACK_STRUCT; 
typedef struct app6drIpHdr App6drIpHdr; 

#define APP6DR_CALC_CSUM(hdr_p,len,cs) App6dr_calcCs16((const char*)(hdr_p),
(len), &(cs)) 
APP6DR_INLINE_FUNC(void App6dr_calcCs16(const char* hdr_p, 
                                        U32 len, 
                                        volatile U16* cs_p)) 
{ 
   U32 i, sum; 
   for(i=0, sum=0; i<(len)/2; i++) 
   { 
      U32 val = ((U32*)hdr_p)[i]; 
      sum += ((val&0xffff) + (val>>16)); 
   } 
   if ((len) & 1) sum += ((U16*)hdr_p)[(len)-1]; 
   while(sum>>16) sum = (sum&0xffff)+(sum>>16); 
   *cs_p = (U16)(~sum); 
} 

#define APP6DR_CALC_IPV4_CSUM(ipv4_p) \ 
   (ipv4_p)->ip_sum = 0x0000; \ 
   APP6DR_CALC_CSUM((char*)(ipv4_p),sizeof(App6drIpHdr)/2,(ipv4_p)->ip_sum) 


#define DOIT() \ 
  memset((void*)&iph, 0xfe, sizeof(App6drIpHdr)); \ 
  iph.ip_v = 4; \ 
  iph.ip_hl = 5; \ 
  iph.ip_tos = 0; \ 
  iph.ip_len = 0; \ 
  iph.ip_id = 0; \ 
  iph.ip_off = 0; \ 
  iph.ip_ttl = 64; /* XXX */ \ 
  iph.ip_p = 50; /* ESP */ \ 
  iph.ip_addr_r.ip_sa = (U32) sa; \ 
  iph.ip_addr_r.ip_da = (U32) da; \ 
  APP6DR_CALC_IPV4_CSUM(&iph);\ 
  fprintf(stderr, "checksum = 0x%04x\n", iph.ip_sum); 


void testchksum2(U32 sa, U32 da) 
{ 
  static App6drIpHdr iph; 
  DOIT(); 
} 

void testchksum1(U32 sa, U32 da) 
{ 
  App6drIpHdr iph; 
  DOIT(); 
} 

int main() 
{ 
testchksum1(0x0a0a0a14, 0x0a0a0a01); 
testchksum2(0x0a0a0a14, 0x0a0a0a01); 
return 0; 
} 
/************ END OF CODE EXAMPLE **************/ 


Compiled with 
gcc -Os -Wall -pedantic main.c -o runme

Output:
checksum = 0xa89c
checksum = 0xa542

The only difference between outputs are that the variables made calculations on
have different storage classes, ie automatic and static.


-- 
           Summary: Variable values calculated differently depending on
                    storage class
           Product: gcc
           Version: 4.2.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: goran dot steen at enea dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40394


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2009-06-12  6:57 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-06-10 10:39 [Bug c/40394] New: Variable values calculated differently depending on storage class goran dot steen at enea dot com
2009-06-10 12:42 ` [Bug c/40394] " goran dot steen at enea dot com
2009-06-10 12:42 ` goran dot steen at enea dot com
2009-06-10 12:43 ` goran dot steen at enea dot com
2009-06-10 12:43 ` goran dot steen at enea dot com
2009-06-10 12:43 ` goran dot steen at enea dot com
2009-06-10 12:44 ` goran dot steen at enea dot com
2009-06-10 14:03 ` rguenth at gcc dot gnu dot org
2009-06-12  6:20 ` goran dot steen at enea dot com
2009-06-12  6:57 ` pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).