M
msnews.microsoft.com
Two simple questions:
1) Shouldn't the code snippets below produce the same IL?
2) Which will be faster?
TIA!
// Bit shift method
return (19 * (y >> 4)) + (x >> 4) + 1;
IL:
{
// Code size 13 (0xd)
.maxstack 8
IL_0000: ldc.i4.s 19
IL_0002: ldarg.2
IL_0003: ldc.i4.4
IL_0004: shr
IL_0005: mul
IL_0006: ldarg.1
IL_0007: ldc.i4.4
IL_0008: shr
IL_0009: add
IL_000a: ldc.i4.1
IL_000b: add
IL_000c: ret
}
// Mathematical equivalent
return (19 * (y /16) + (x / 16) + 1);
IL:
{
// Code size 20 (0x14)
.maxstack 3
.locals init ([0] int32 CS$1$0000)
IL_0000: nop
IL_0001: ldc.i4.s 19
IL_0003: ldarg.2
IL_0004: ldc.i4.s 16
IL_0006: div
IL_0007: mul
IL_0008: ldarg.1
IL_0009: ldc.i4.s 16
IL_000b: div
IL_000c: add
IL_000d: ldc.i4.1
IL_000e: add
IL_000f: stloc.0
IL_0010: br.s IL_0012
IL_0012: ldloc.0
IL_0013: ret
}
1) Shouldn't the code snippets below produce the same IL?
2) Which will be faster?
TIA!
// Bit shift method
return (19 * (y >> 4)) + (x >> 4) + 1;
IL:
{
// Code size 13 (0xd)
.maxstack 8
IL_0000: ldc.i4.s 19
IL_0002: ldarg.2
IL_0003: ldc.i4.4
IL_0004: shr
IL_0005: mul
IL_0006: ldarg.1
IL_0007: ldc.i4.4
IL_0008: shr
IL_0009: add
IL_000a: ldc.i4.1
IL_000b: add
IL_000c: ret
}
// Mathematical equivalent
return (19 * (y /16) + (x / 16) + 1);
IL:
{
// Code size 20 (0x14)
.maxstack 3
.locals init ([0] int32 CS$1$0000)
IL_0000: nop
IL_0001: ldc.i4.s 19
IL_0003: ldarg.2
IL_0004: ldc.i4.s 16
IL_0006: div
IL_0007: mul
IL_0008: ldarg.1
IL_0009: ldc.i4.s 16
IL_000b: div
IL_000c: add
IL_000d: ldc.i4.1
IL_000e: add
IL_000f: stloc.0
IL_0010: br.s IL_0012
IL_0012: ldloc.0
IL_0013: ret
}