User account creation filtered due to spam.

Bug 44260 - Strange behavior on bit fields structures
Summary: Strange behavior on bit fields structures
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.4.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-24 13:24 UTC by Dennis Yurichev
Modified: 2010-05-24 15:48 UTC (History)
38 users (show)

See Also:
Host: 4.4.3
Target: 4.4.3
Build: 4.4.3
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dennis Yurichev 2010-05-24 13:24:18 UTC
If compiled without -O3 option, this code snippet works fine (running executable will print "i=11223344, bswap=44332211")
If compiled with -O3 option, executable will print "i=11223344, bswap=00000000".
Checked on 4.4.1 x86 and 4.4.3 x64.

#include <stdio.h>

struct int32_bytes
{
	int byte1:8;
	int byte2:8;
	int byte3:8;
	int byte4:8;
};

unsigned int bswap (unsigned int in)
{
	struct int32_bytes *tmp_in;
	struct int32_bytes *tmp_out;
	unsigned int rt;

	tmp_in=(struct int32_bytes *)&in;
	tmp_out=(struct int32_bytes *)&rt;

	tmp_out->byte1 = tmp_in->byte4;
	tmp_out->byte2 = tmp_in->byte3;
	tmp_out->byte3 = tmp_in->byte2;
	tmp_out->byte4 = tmp_in->byte1;

	return rt;
};

int main()
{
	for (int i=0x11223344; i<=0x112233FF; i++)
	{
		if (i==0x11223344)
			printf ("i=%08X, bswap=%08X\n", i, bswap (i));
	};
};
Comment 1 Dennis Yurichev 2010-05-24 13:30:18 UTC
The code 4.4.1 x86 generating (with -O3 option) for bswap() function I mentioned earlier is strange too:


; bswap(unsigned int)
                public _Z5bswapj
_Z5bswapj       proc near

var_4           = dword ptr -4
arg_0           = dword ptr  8

                push    ebp
                mov     ebp, esp
                sub     esp, 10h
                mov     eax, [ebp+arg_0]
                mov     edx, eax
                sar     edx, 18h
                mov     byte ptr [ebp+var_4], dl
                mov     edx, eax
                shr     edx, 10h
                mov     byte ptr [ebp+var_4+1], dl
                movsx   edx, ah
                mov     byte ptr [ebp+var_4+3], al
                mov     eax, [ebp+var_4]           ; returning var_4
                mov     byte ptr [ebp+var_4+2], dl ; set last byte to var_4
                leave
                retn
_Z5bswapj       endp
Comment 2 Wesley Huang 2010-05-24 13:51:43 UTC
Subject: Re:  Strange behavior on bit fields sructures.

My gcc 4.4.4 generate the correct binary and get the correct result
even with -O3 option.

Wesley

2010/5/24 dennis at conus dot info <gcc-bugzilla@gcc.gnu.org>:
>
>
> ------- Comment #1 from dennis at conus dot info  2010-05-24 13:30 -------
> The code 4.4.1 x86 generating (with -O3 option) for bswap() function I
> mentioned earlier is strange too:
>
>
> ; bswap(unsigned int)
>                public _Z5bswapj
> _Z5bswapj       proc near
>
> var_4           = dword ptr -4
> arg_0           = dword ptr  8
>
>                push    ebp
>                mov     ebp, esp
>                sub     esp, 10h
>                mov     eax, [ebp+arg_0]
>                mov     edx, eax
>                sar     edx, 18h
>                mov     byte ptr [ebp+var_4], dl
>                mov     edx, eax
>                shr     edx, 10h
>                mov     byte ptr [ebp+var_4+1], dl
>                movsx   edx, ah
>                mov     byte ptr [ebp+var_4+3], al
>                mov     eax, [ebp+var_4]           ; returning var_4
>                mov     byte ptr [ebp+var_4+2], dl ; set last byte to var_4
>                leave
>                retn
> _Z5bswapj       endp
>
>
> --
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44260
>
>
Comment 3 Wesley Huang 2010-05-24 13:53:59 UTC
Subject: Re:  Strange behavior on bit fields structures

Sorry I made a mistake here, it works on 32bit mode, but failed on the
64bit mode.

Wesley

2010/5/24 xinping dot huang at gmail dot com <gcc-bugzilla@gcc.gnu.org>:
>
>
> ------- Comment #2 from xinping dot huang at gmail dot com  2010-05-24 13:51 -------
> Subject: Re:  Strange behavior on bit fields sructures.
>
> My gcc 4.4.4 generate the correct binary and get the correct result
> even with -O3 option.
>
> Wesley
>
> 2010/5/24 dennis at conus dot info <gcc-bugzilla@gcc.gnu.org>:
>>
>>
>> ------- Comment #1 from dennis at conus dot info  2010-05-24 13:30 -------
>> The code 4.4.1 x86 generating (with -O3 option) for bswap() function I
>> mentioned earlier is strange too:
>>
>>
>> ; bswap(unsigned int)
>>                public _Z5bswapj
>> _Z5bswapj       proc near
>>
>> var_4           = dword ptr -4
>> arg_0           = dword ptr  8
>>
>>                push    ebp
>>                mov     ebp, esp
>>                sub     esp, 10h
>>                mov     eax, [ebp+arg_0]
>>                mov     edx, eax
>>                sar     edx, 18h
>>                mov     byte ptr [ebp+var_4], dl
>>                mov     edx, eax
>>                shr     edx, 10h
>>                mov     byte ptr [ebp+var_4+1], dl
>>                movsx   edx, ah
>>                mov     byte ptr [ebp+var_4+3], al
>>                mov     eax, [ebp+var_4]           ; returning var_4
>>                mov     byte ptr [ebp+var_4+2], dl ; set last byte to var_4
>>                leave
>>                retn
>> _Z5bswapj       endp
>>
>>
>> --
>>
>>
>> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44260
>>
>>
>
>
> --
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44260
>
>
Comment 4 Andreas Schwab 2010-05-24 15:48:31 UTC

*** This bug has been marked as a duplicate of 21920 ***