sext..to instruction

Hi,
I have a question about the "sext..to" instruction. In the document, I found
two examples:
%x = sext i8 -1 to i16
It means:
i8 -1 = 1111 1111 --> 1111 1111 1111 1111 = i16
how can it determinate, that the i16 value %x positive is (65535)?
And the second example:

%y = sext i1 true to i32
1 --> 1111 1111 1111 1111 1111 1111 1111 1111
In this example, %y is -1

I'm not sure about it, when sext to results a positve/negative value?
Thank for any advice
Quang

Le Anh Quang wrote:

Hi,
I have a question about the "sext..to" instruction. In the document, I found
two examples:
%x = sext i8 -1 to i16
It means:
i8 -1 = 1111 1111 --> 1111 1111 1111 1111 = i16
how can it determinate, that the i16 value %x positive is (65535)?

i16 65536 = i16 -1. They're both 1111 1111 1111 1111.

And the second example:

%y = sext i1 true to i32
1 --> 1111 1111 1111 1111 1111 1111 1111 1111
In this example, %y is -1

I'm not sure about it, when sext to results a positve/negative value?

Since they're the same bits, it doesn't matter at all until you perform you I/O, at which time your code makes the decision as to whether the top bit should be treated as a sign bit or not.

The LangRef should probably be updated, as LLVM's AsmPrinter will never print i16 65536, preferring to show all values as signed.

Nick Lewycky

I'm not sure about it, when sext to results a positve/negative value?

sext does signed-extension, zext does unsigned-extension.
This means that zext always extends by zero bits,
while with sext the additional bits are all copies of the
top bit of the original value. So with sext, if it was
negative in the original type when considered as a signed
value, then it will be negative in the new type too.

Best wishes,

Duncan.

Thanks , i see it now :slight_smile:
Quang