I was looking at “trunc” to downcast a signed integer, say sint32 to sint16, but it seems to handle unsigned integers only. How do you downcast a signed integer?
P.S. This question is for my “Mapping High-Level Constructs to LLVM IR” document
I was looking at "trunc" to downcast a signed integer, say sint32 to sint16,
but it seems to handle unsigned integers only.
No. In twos-complement notation (which LLVM assumes) there no
difference between signed and unsigned truncation - you just throw out
the spare sign bits and that's all.
Please note that that the "numbers" in LLVM IR is neither signed nor
unsigned. They are just raw bits. It's up to the instruction in
question how to interpret them. I'd suggest to clearly outline this in
the document, since this is the important point.
Doh! I was thinking in one’s complement, even though I know perfectly well that LLVM and almost all contemporary CPUs use two’s complement. Thank you!