You can't represent 0x80000000 using a 32-bit value just because it is postive.
I am not talking about bit representations but their mathematical values.
The C standard does not care about bit representations of signed integers, 
either.

I would suggest you cast them to 'unsigned' instead of 'int' for semantical 
correctness reasons.

------------------                               
Best regards,
lh_mouse
2016-04-24

-------------------------------------------------------------
发件人:LRN <[email protected]>
发送日期:2016-04-24 18:36
收件人:mingw-w64-public
抄送:
主题:Re: [Mingw-w64-public] [PATCH] Cast certain enums to int

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

On 24.04.2016 12:40, lh_mouse wrote:
> On 2016-04-23 03:38, LRN wrote:
>> On 22.04.2016 20:24, LRN wrote:
>>> This matches the behaviour of MSVC better in cases when there is a
>>> mix of values < 0 and values > 0x7FFFFFFF in the same enum, which,
>>> without this change, prompts GCC to increase enum size to 8
>>> bytes.
>> 
>>> Fixes bug #456
>> 
>>> patch is attached
>> 
>> Yeah, and another thing: i don't have x86_64 gcc, nor did i test
>> this with x64 msvc. No idea what sizeof(any enum) on 64-bit
>> architecture is for these two compilers, or how that interacts with
>> sizeof(int) being 8 there.
>> 
> 
> Within both MS ABI and Itanium ABI for x64, sizeof(int) is 4.
> 
> And FWIW, here is the reason of the enumeration problem:
> 
> Quoting from WG14 N1570 (Programming languages — C) [quote] 6.7.2.2
> Enumeration specifiers 4 Each enumerated type shall be compatible with
> char, a signed integer type, or an unsigned integer type. The choice
> of type is implementation-defined,128) but shall be capable of
> representing the values of all the members of the enumeration. The 
> enumerated type is incomplete until immediately after the } that
> terminates the list of enumerator declarations, and complete
> thereafter. [/quote]
> 
> Here if an enum has both an enumerator less than zero and another one
> greater than 0x7FFFFFFF, neither `int` nor `unsigned` could be an
> option because neither is  'capable of representing the values of all
> the members of the enumeration'. GCC is doing the correct thing to use
> a 64-bit integer type.
> 

Yeah, but the problem here is not being correct. The problem here is
being MSVC-compatible (for obvious reasons).

I'm not even sure MSVC is entirely incorrect. If one thinks not of
numeric values but of byte representations of values, it is possible to
represent both negative (between 0x80000000 and 0xFFFFFFFF) and positive
(between 0x00000000 and 0x7FFFFFFF) values with a signed 32-bit integer
as well as unsigned one - bytes are the same, only their interpretation
changes. If you can guarantee that 0xFFFFFFFF, when used in any place
where this enumeration value is expected, is casted to -1 (when actual
math is done; if no math is done, then its value is irrelevant, as long
at it's always the same), then that is just as good as actually
specifying -1 instead of 0xFFFFFFFF.

It seems that the implementation of casting by both GCC and MSVC does
provide that guarantee. I've made a test program that shoves enumeration
members into 32-/64-bit signed/unsigned integer variables and then prints
out their contents. When enumeration members are casted to (int) like i
proposed, the output for GCC and MSVC is identical in all cases.

I also got around to download x86_64-gcc (first time in the last few
years when i've used a W32 gcc that i have no built myself) and wrestled
a bit with x64 MSVC compiler, managing to compile the test program for
x86_64. Again, the output is identical for both GCC and MSVC.

- -- 
O< ascii ribbon - stop html email! - www.asciiribbon.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBCAAGBQJXHKGTAAoJEOs4Jb6SI2CwLxkH/3Ia4+RWoRlhc0e5TAXpxlpc
dbQxP6aNkMDI7URBXQt3vdeu26J76sKx4yjFdamXiTCN9YXJ0Uc8yT/uCu2jMmcb
9EFZMVnEx+CVGVxpHmLHb3Bc/9gjE+ONy7A6a9xqxrzQrb+Awcxy+GUo5dtXnaVV
pbI7WnpfjI9YVlmw1UkkG6YZt3TSSb4wNOGEwxgmoOhHoSTauG/nYhzB5PTPLCg3
KFl8+qr093t39gua9xwLTlz362L5Ga6pCIazBWL3HkwhDE/dvnS0DhJDf6v6sWax
U8HVflCDUL4YPz2qkq/7j1//Mtl7QG4gRyn56OKSN80HsUA9kbVnDo6y/sngaWU=
=JEgw
-----END PGP SIGNATURE-----

------------------------------------------------------------------------------
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial!
https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public



------------------------------------------------------------------------------
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial!
https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to