Riyaz Puthiyapurayil via llvm-dev
2018-Apr-04 04:34 UTC
[llvm-dev] llvm::PointerIntPair -- is this by design or a bug?
llvm::PointerIntPair<double*, 3, signed> P; P.setInt(-4); Ideally, the value range for a 3-bit signed integer should be [-4,3]. But the above call to setInt will fail. Essentially, the signed int field in PointerIntPair is behaving the same as an 3-bit unsigned field which has the legal value range of [0,7]. Is this by design? Are negative values not allowed in PointerIntPair? /Riyaz -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180404/85ccf480/attachment.html>
Florian Hahn via llvm-dev
2018-Apr-04 10:01 UTC
[llvm-dev] llvm::PointerIntPair -- is this by design or a bug?
Hi, On 04/04/2018 05:34, Riyaz Puthiyapurayil via llvm-dev wrote:> llvm::PointerIntPair<double*, 3, signed> P; > > P.setInt(-4); > > Ideally, the value range for a 3-bit signed integer should be [-4,3]. > But the above call to setInt will fail. Essentially, the signed int > field in PointerIntPair is behaving the same as an 3-bit unsigned field > which has the legal value range of [0,7]. Is this by design? Are > negative values not allowed in PointerIntPair? >I think that's by design. setInt only allows you to set integer values that fit into the available bits. It won't move the sign bit, so negative values won't fit, unless you have a 3 bit signed type ;) Cheers, Florian
David Chisnall via llvm-dev
2018-Apr-04 10:15 UTC
[llvm-dev] llvm::PointerIntPair -- is this by design or a bug?
On 4 Apr 2018, at 11:01, Florian Hahn via llvm-dev <llvm-dev at lists.llvm.org> wrote:> > Hi, > > On 04/04/2018 05:34, Riyaz Puthiyapurayil via llvm-dev wrote: >> llvm::PointerIntPair<double*, 3, signed> P; >> P.setInt(-4); >> Ideally, the value range for a 3-bit signed integer should be [-4,3]. But the above call to setInt will fail. Essentially, the signed int field in PointerIntPair is behaving the same as an 3-bit unsigned field which has the legal value range of [0,7]. Is this by design? Are negative values not allowed in PointerIntPair? > > I think that's by design. setInt only allows you to set integer values that fit into the available bits. It won't move the sign bit, so negative values won't fit, unless you have a 3 bit signed type ;)That doesn’t sound right (for any computer made in the last few decades), the representation of -3 will be 1111…1111101. Storing the low bits will yield 101, which is a 3-bit negative three. When you then sign extend this to any other signed type, you will get -3 in that representation. It sounds as if the signed specialisation of PointerIntPair is simply not doing the sign extension. David