问题
On most implementations of this day and age, a signed integer value that has a bit pattern of 1 for the sign bit and all 0 for the value bits tends to represent the lowest possible value for that signed integer type.
However, as 6.2.6.2p2 states, that's not a requirement:
Which of these applies is implementation-defined, as is whether the value with sign bit 1 and all value bits zero (for the first two), or with sign bit and all value bits 1 (for ones' complement), is a trap representation or a normal value.
My first question is simple: Are there any implementations that use this bit pattern for a negative zero or a trap representation? If the answer to this question is 'no', then the answer to my follow-up questions must also be 'no'.
Following on from that question, 6.2.6.2p3 states that when a negative zero is assigned to an object, it might (or might not) be converted to a regular zero:
It is unspecified whether these cases actually generate a negative zero or a normal zero, and whether a negative zero becomes a normal zero when stored in an object.
My follow-up questions:
- Are there any implementations that use a trap representation rather than a negative zero for that bit pattern?
- Are there any implementations that use a negative zero that is stored as a distinct value?
- Are there any implementations that use a negative zero that is stored as a regular zero?
edit for clarification: I am NOT asking for what is theoretically possible in a system that uses ones complement, twos complement or sign and magnitude representations for signed integers. I can find (and have found) that information in the sections I've cited from earlier in this question. I'm asking about what has actually been done.
回答1:
As brought to my attention by Iskar Jarak within the comments, the OS2200, which was most recently released in February of 2015, has an implementation of C known as UCS C, of which the manual documents
- a conforming C implementation where-by "... CONFORMANCE/TWOSARITH is not used, negative zero is equivalent to zero in all 36-bit unsigned integer comparisons using less than (<), or equal (=). In unsigned comparisons using greater than (>), (236)-1 is greater than zero."
- a questionably-conforming C implementation where-by "CONFORMANCE/TWOSARITH causes negative zero, (236)-1, to be considered a large unsigned integer by the generated code." As this violates several C standard clauses (6.2.6.2p2 stating "the sign bit has the value -(2M) (two's complement)" and "the sign bit has the value -(2M-1) (ones' complement)", 6.3.1.1p3 stating "The integer promotions preserve value including sign", in contradiction to the conversion to a large unsigned integer specified within the UCS/OS2200 doc) it follows that this could only conform if the negative zero bit pattern is treated as a trap representation, where-by undefined behaviour is invoked and so the constraints of the standard no longer apply.
In summary, ...
Are there any implementations that use this bit pattern for a negative zero or a trap representation?
Yes. The example provided can use this bit pattern for a negative zero or a trap representation, depending upon compiler conformance options.
For the follow-up questions 1&2, it follows that UCS C on OS2200 is an example of both of these.
As for question 3, to be answered another day... My time has come to end here :)
来源:https://stackoverflow.com/questions/30586217/are-there-any-implementations-that-support-a-negative-zero-or-reserve-it-as-a-t