This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/40399] New: Variable values calculated differently depending on storage class
- From: "goran dot steen at enea dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 10 Jun 2009 11:40:43 -0000
- Subject: [Bug c/40399] New: Variable values calculated differently depending on storage class
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
/************* 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=40399