C++的x64移植

[转载自:http://www.cppblog.com/mzty/archive/2006/12/07/16101.html]

主要是两个问题:指针截断和数据对齐。

C/C++移植的问题

1 新的数据类型和函数
1)固定精度:(跟X86或X64无关)

Term Description
DWORD32 32-bit unsigned integer
DWORD64 64-bit unsigned integer
INT32 32-bit signed integer
INT64 64-bit signed integer
LONG32 32-bit signed integer
LONG64 64-bit signed integer
UINT32 Unsigned INT32
UINT64 Unsigned INT64
ULONG32 Unsigned LONG32
ULONG64 Unsigned LONG64
2)平台相关
Term Description
DWORD_PTR Unsigned long type for pointer precision.
HALF_PTR Half the size of a pointer. Use within a structure that contains a pointer and two small fields.
INT_PTR Signed integer type for pointer precision.
LONG_PTR Signed long type for pointer precision.
SIZE_T The maximum number of bytes to which a pointer can refer. Use for a count that must span the full range of a pointer.
SSIZE_T Signed SIZE_T.
UHALF_PTR Unsigned HALF_PTR.
UINT_PTR Unsigned INT_PTR.
ULONG_PTR Unsigned LONG_PTR.
3) 平台相关指针
Term Description
POINTER_32 A 32-bit pointer. On 32-bit Windows, this is a native pointer. On 64-bit Windows, this is a truncated 64-bit pointer.
POINTER_64 A 64-bit pointer. On 64-bit Windows, this is a native pointer. On 32-bit Windows, this is a sign-extended 32-bit pointer.

Note that it is not safe to assume the state of the high pointer bit.


2 指针的截断

[MSDN]An int and a long are 32-bit values on 64-bit Windows operating systems. For programs that you plan to compile for 64-bit platforms, you should be careful not to assign pointers to 32-bit variables. Pointers are 64-bit on 64-bit platforms, and you will truncate the pointer value if you assign it to a 32-bit variable.

在以前的32位系统中,指针的为32位,在新的64位系统中指针为64位,这样的话,我们以前编 程中常用的指针与int或long等的直接转化,放在新的64位的系统中就会出错,指针高位的值就会丢失,从而产生错误。例如下面的2行代码,在32位上 正确,但是在64位上就会发生指针截断,出现错误:

o_64bit36.JPG

下面是我们使用新的动态数据类型,从而实现不用修改代码,直接在32位和64位上直接编译:

o_64bit37.JPG


3 虚拟地址空间 (解决方法:在64位的指针中只不使用高32位,也就等于原来32位中的32位指针了)
在 32位的系统中,一共可以是用的内存为4G,但是对于用户的应用一般只可以使用2G,其他的2G为系统使用,当然你也可以打开3G的开关,这样的话最多就 能使用3G.对于高精度的浮点运算,高强度的数据库处理等,就需要更大的内存,这时候64位给我们带来了福音,在64位上我们最多可以使用16T的内存, 这样就大大提高了性能。但是对于一些没有使用到超过2G的内存,但当中却大量的使用了指针与整型等的强制转化的应用程序,我们可以使用一种简单的方法,使 用编译器的开关VBS:/Largeadressaware:no,使32位的程序移植到64位上。但是这种简单的移植方法,会带来一些弊端:如果被真的 64位所调用,就有可能真的产生指针的截断,同时也没有解决对齐问题和大内存的使用问题。

4 数据类型对齐和补齐

由于对 于不同的cpu架够有不同的数据对齐策列,而且数据的对齐也影响程序的性能和正确性。常用的2个宏:TYPE_ALIGNMENT(type)和 FIELD_OFFSET(type,member)分别用来计算指定类型的对齐值和某复合变量中成员的偏移量。对于复合数据类型,采用递归的计算方法。 对于复合数据类型如果没有对齐,则补齐,保证结尾处地址是该复合数据类型的整数倍。(在C++中可以使用#Program()来设置对齐方式)

5 CPU架构与对齐意外
对 于我们以前的32位,CPU自动解决对齐问题,在X64中CPU也会处理对齐问题,但是有性能消耗,但是对于IPF则CPU没有处理数据的对齐问题,所以 如果程序中没有处理,则会导致程序Crash。所以最后是我们在64编程中程序处理对齐问题,在程序中使用__aligned_malloc,这样也更有 利于在不同的64位架够间的移植。

6 优化方案
建议使用编译器的优化选项来优化64程序:whole program optimization  ,profile-guided optimization.


三 总结

对于C++的64位移植,主要的问题就是指针的截断和数据的对齐,希望从现在开始我们就养成良好的习惯,使用动态指针类型,和程序处理数据的对齐问题,这样更有利于程序的64位移植。

 

 

posted @ 2009-04-26 16:27  能巴  阅读(1948)  评论(0编辑  收藏  举报