Discussion:
INTVAL type
Paul Koning
2018-10-28 17:39:16 UTC
Permalink
I was reviewing some back end code that handles integer values of various sizes, and got confused reading about CONST_INT and CONST_DOUBLE.

It's pretty clear that CONST_DOUBLE is used for values bigger than HOST_WIDE_INT. But the description of INTVAL is contradictory. In some places, it states that its type is HOST_WIDE_INT; in others it says that it it "int". For example, in section 17.6 of the internals manual:

Be careful when doing this, because the result of INTVAL is an integer on the host machine. If the host machine has more bits in an int than the target machine has in the mode in which the constant will be used, then some of the bits you get from INTVAL will be superfluous.

A related problem is that it isn't clearly stated what the properties of HOST_WIDE_INT are. Judging by hwint.h, it is always specifically a 64 bit integer. If that is intended to be the case, it would be good for that to be documented. Alternatively, if the intent is that it is at least 64 bits but might be bigger, that should be stated. As it stands, I have some code I need to repair because it picks apart integer constants from INTVAL in a way that's incorrect if a 64-bit value is passed.

paul
Segher Boessenkool
2018-10-28 18:06:47 UTC
Permalink
Post by Paul Koning
I was reviewing some back end code that handles integer values of various sizes, and got confused reading about CONST_INT and CONST_DOUBLE.
Be careful when doing this, because the result of INTVAL is an integer on the host machine. If the host machine has more bits in an int than the target machine has in the mode in which the constant will be used, then some of the bits you get from INTVAL will be superfluous.
It is a HOST_WIDE_INT. The section (please mention the title btw., the
section numbers change over time: this is "C Statements for Assembler
Output") isn't very clear, and it was written when HOST_WIDE_INT could
still be 32 bit (nowadays it is always 64 bits).
Post by Paul Koning
A related problem is that it isn't clearly stated what the properties of HOST_WIDE_INT are. Judging by hwint.h, it is always specifically a 64 bit integer. If that is intended to be the case, it would be good for that to be documented. Alternatively, if the intent is that it is at least 64 bits but might be bigger, that should be stated. As it stands, I have some code I need to repair because it picks apart integer constants from INTVAL in a way that's incorrect if a 64-bit value is passed.
It is always exactly 64 bits.

HOST_WIDE_INT values are always sign-extended to the type it is meant as
(so 0x80 is expressed as 0xffffffffffffff80 for a QImode value, i.e. -0x80,
but it is just 0x80 for a larger mode value).


Segher

Loading...