这是一个非常简单的C#控制台应用程序/ Framework 4.5,使用Visual Studio 2013 Update 2进行编译。它从一个字节数组中读取一个double,并预先进行简单的范围检查。
class Program
{
private static byte[] fData = new byte[8];
public static unsafe double ReadDouble(int offset)
{
if ((offset + 8) > fData.Length)
return 0;
double value = 0;
fixed (byte* pBuffer = fData)
value = *((double*)(pBuffer));
Console.WriteLine("This code is never reached!");
return value;
}
static void Main(string[] args)
{
fData = BitConverter.GetBytes(1234.56789d);
double result = ReadDouble(0);
Console.WriteLine("result: {0}", result);
Console.ReadKey();
}
}
在没有 / optimize 标志的情况下进行编译时,任何CPU配置都输出以下内容(并且生成的IL似乎是出价躲闪):
result: 0
使用 / optimize 标志编译时,它会正确输出:
This code is never reached!
result: 1234,56789
关于为什么会出现这种情况的任何想法?
PS:如果范围检查被注释掉( if((offset ...)),那么它会再次输出正确的结果。
编辑:这里是为ReadDouble()函数生成的IL,注意没有Console.WriteLine
.method public hidebysig static
float64 ReadDouble (
int32 offset
) cil managed
{
// Method begins at RVA 0x2060
// Code size 81 (0x51)
.maxstack 2
.locals init (
[0] float64 'value',
[1] bool,
[2] float64,
[3] uint8& pinned pBuffer,
[4] uint8[]
)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.8
IL_0003: add
IL_0004: ldsfld uint8[] WeirdBug.Program::fData
IL_0009: ldlen
IL_000a: conv.i4
IL_000b: cgt
IL_000d: ldc.i4.0
IL_000e: ceq
IL_0010: stloc.1
IL_0011: ldloc.1
IL_0012: brtrue.s IL_0020
IL_0014: ldc.r8 0.0
IL_001d: stloc.2
IL_001e: br.s IL_004f
IL_0020: ldc.r8 0.0
IL_0029: stloc.0
IL_002a: ldsfld uint8[] WeirdBug.Program::fData
IL_002f: dup
IL_0030: stloc.s 4
IL_0032: brfalse.s IL_003a
IL_0034: ldloc.s 4
IL_0036: ldlen
IL_0037: conv.i4
IL_0038: brtrue.s IL_003f
IL_003a: ldc.i4.0
IL_003b: conv.u
IL_003c: stloc.3
IL_003d: br.s IL_0048
IL_003f: ldloc.s 4
IL_0041: ldc.i4.0
IL_0042: ldelema [mscorlib]System.Byte
IL_0047: stloc.3
IL_0048: ldloc.3
IL_0049: conv.i
IL_004a: ldind.r8
IL_004b: stloc.0
IL_004c: ldc.i4.0
IL_004d: conv.u
IL_004e: stloc.3
IL_004f: ldloc.2
IL_0050: ret
}
答案 0 :(得分:-1)
您可能遇到错位问题。一些英特尔浮点指令修复了你的错位,一些较新的(SIMD的)没有。
你可能应该坚持使用BitConverter
类。
另一种选择是将不安全的代码用于转换的另一个方向 - 实际数组将为double[]
,您可以将double*
设置为byte*
。一般来说,T*
到byte*
的投射是安全的,反之则不然。