简体   繁体   中英

Does .NET promote Int32 to Int64 for arithmetics on 64-bit architectures?

I was reading this post and I was wondering, if the CLR converts bytes and Int16 to Int32 for arithmetics, does it convert all Byte, Int16 and Int32 to Int64 when running in 64-bit mode?

Edit: Since the following loop :

for (short i = 0; i < 10; i++) {
    // ...
}

will produce something like this in IL :

Int16 i = 0;
LOOP:
    Int32 temp0 = Convert_I16_To_I32(i); // !!!
    if (temp0 >= 10) goto END;
    ...
    Int32 temp1 = Convert_I16_To_I32(i); // !!!
    Int32 temp2 = temp1 + 1;
    i = Convert_I32_To_I16(temp2); // !!!
    goto LOOP;
END:

Will the following loop :

for (int i = 0; i < 10; i++) {
    // ...
}

Produce something like this on 64-bit architectures?

Int32 i = 0;
LOOP:
    Int64 temp0 = Convert_I32_To_I64(i); // !!!
    if (temp0 >= 10) goto END;
    ...
    Int64 temp1 = Convert_I32_To_I64(i); // !!!
    Int64 temp2 = temp1 + 1;
    i = Convert_I64_To_I32(temp2); // !!!
    goto LOOP;
END:

Actually, CLR doesn't do anything: C# compiler does. You see, CLR does support Byte and Int16 types, however it does not support arithmetic operations on them, so to perform arithmetic operations on Byte and Int16 , they initially should be transformed to smallest supported type ( Int32 ).

Behavior of this transformation is dependent on language. C# team choosed to automatically convert all computations on these type to Int32 , so you see corresponding IL. VB.NET choosed to save type information, so no type conversion is seen in code, however IL code in this case is more cumbersome: they have to use conversion to Int32 , check if overflow has happened and then convert back from Int32 .

So, it is dependent on language. C# specifications clearly state that arithmetic operations are size-independent, so on every platform you will see conversion to Int32 . CLR does specify that Int32 type is always take 4 bytes of memory space, but it's not limited by this: CLR also supports native int type, which size is dependent on platform. C# does not allow that, so you can use this type only in unsafe sections.

PS Signed/unsigned variancy is omitted for clarity

PPS IntPtr is safe, but it's implementation depend on platform: IntPtr.Size is 4 bytes on 32bit and 8 bytes on 64bit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM