Use Code Guards to Compile 32-bit Code for the Itanium Architecture

Submit New Article

March 31, 2009 9:00 PM PDT



Challenge

Designate specific blocks of 32-bit code to be compiled for the Itanium® architecture. Source code often needs local customization based on the target platform.


Solution

Use code guards, consisting of pre-processor macros and values, to provide conditional compilation. In C and C++, conditional compilation is used to guard, or choose, platform-specific blocks of code, so the code is compiled only for a particular target. Code guards are expressions that use pre-processor macros and values. Both the UNIX* and Windows* development environments establish predefined macros you can use in conditional definitions.

Win32* is a subset of Windows 2000 (64-bit)*, so include code guards for both in order to compile 32-bit code for the Itanium processor.

The following table lists a few of the #ifdef code guards; there are many others. Be sure you use the latest standard conditional compilation macros. 

__LP64__ (On some UNIX* platforms) compilation is using the 64-bit UNIX data model, where long and pointer are 64 bits, and int remains 32 bits
__M_IA64 Compiler will generate code for execution on Itanium architecture
__unix Compilation is being done for running code on a UNIX platform
__WIN64 Compiler will generate code for Win64 API
__WIN32 Compiler will generate code for Win32 API(which will also run under Windows 2000, 64-bit)
__WIN32_WINNT Compiler will generate code that runs only on the Windows NT* variant of Win32 (not Windows* 95 or 98)
__WORDSIZE (On other UNIX platforms) compiler sets this macro to either 64 or 32

 

The following example shows you how you might use a code guard for Itanium architecture:

Current Code Itanium® Processor Code
#if defined (WIN32)

...

#elif defined (_unix)









...

#else



#error unknown platform

- revise _FILE_ _LINE_

#endif
#if defined (WIN32) or defined (WIN32)

#if defined (__WIN64)

//64-bit specific Windows code

#else

//32-bit specific Windows code

#endif

#elif defined (__unix)

#if defined (__LP64__) || ( __WORDSIZE == 64)

//UNIX/64 code

#else

//32-bit UNIX code

#endif

#endif

 

To define the first section of code for both Win32 and Windows 2000 (64-bit), the _WIN64 conditional was added to the code. To include UNIX/64, _LP64 was added to the code.


Source

Preparing Code for the IA-64 Architecture (Code Clean)