Would making plain int 64-bit break a lot of reasonable code?

后端 未结 8 2081
独厮守ぢ
独厮守ぢ 2021-02-19 03:12

Until recently, I\'d considered the decision by most systems implementors/vendors to keep plain int 32-bit even on 64-bit machines a sort of expedient wart. With mo

8条回答
  •  生来不讨喜
    2021-02-19 03:57

    There's one code idiom that would break if ints were 64-bits, and I see it often enough that I think it could be called reasonable:

    • checking if a value is negative by testing if ((val & 0x80000000) != 0)

    This is commonly found in checking error codes. Many error code standards (like Window's HRESULT) uses bit 31 to represent an error. And code will sometimes check for that error either by testing bit 31 or sometimes by checking if the error is a negative number.

    Microsoft's macros for testing HRESULT use both methods - and I'm sure there's a ton of code out there that does similar without using the SDK macros. If MS had moved to ILP64, this would be one area that caused porting headaches that are completely avoided with the LLP64 model (or the LP64 model).

    Note: if you're not familiar with terms like "ILP64", please see the mini-glossary at the end of the answer.

    I'm pretty sure there's a lot of code (not necessarily Windows-oriented) out there that uses plain-old-int to hold error codes, assuming that those ints are 32-bits in size. And I bet there's a lot of code with that error status scheme that also uses both kinds of checks (< 0 and bit 31 being set) and which would break if moved to an ILP64 platform. These checks could be made to continue to work correctly either way if the error codes were carefully constructed so that sign-extension took place, but again, many such systems I've seen construct the error values by or-ing together a bunch a bitfields.

    Anyway, I don't think this is an unsolvable problem by any means, but I do think it's a fairly common coding practice that would cause a lot of code to require fixing up if moved to an ILP64 platform.

    Note that I also don't think this was one of the foremost reasons for Microsoft to choose the LLP64 model (I think that decision was largely driven by binary data compatibility between 32-bit and 64-bit processes, as mentioned in MSDN and on Raymond Chen's blog).


    Mini-Glossary for the 64-bit Platform Programming Model terminology:

    • ILP64: int, long, pointers are 64-bits
    • LP64: long and pointers are 64-bits, int is 32-bits (used by many (most?) Unix platforms)
    • LLP64: long long and pointers are 64-bits, int and long remain 32-bits (used on Win64)

    For more information on 64-bit programming models, see "64-bit Programming Models: Why LP64?"

提交回复
热议问题