Software Tuning, Performance Optimization & Platform Monitoring
Discussion regarding monitoring and software tuning methodologies, Performance Monitoring Unit (PMU) of Intel microprocessors, and platform updating.

Tips for Porting software to a 64-bit platform

SergeyKostrov
Valued Contributor II
7,006 Views

I'd like to share some experience on porting a C/C++ project to a 64-bit platform. It was started last week and I already
have lots of different problems.

Here is a list of Tips, Detected Problems and Issues:

1. I recommend to take a look at the following topics on MSDN if you're about to start a 64-bit programming:

"Platform SDK: 64-bit Windows Programming"
"Migration Tips"
"64-Bit Programming with Visual C++"
"How to: Configure Projects to Target Platforms"

2. Turn on a /Wp64 compiler option for Microsoft compatible C++ compilers. It detects 64-bit
portability problems on types that are marked with the __w64 keyword;

3. Issue - LONGLONG data type:
...
#if !defined( _M_IX86 )
typedef __int64 LONGLONG;
#else
typedef double LONGLONG;
#endif
...

4. Issue - Date & Time CRT-functions and data types

5. Issue - A 'size_t' type is used in 'new' C++ operators to pass a size of a memory to allocate and
it is 8-bytes long on a 64-bit platform

6. Microsoft C++ compiler ( a test case ):
...
int iInt32 = 0xffffeeeeddddcccc; // -> iInt32 = 0xddddcccc - This is simplya verification of truncation
...

7. Problem - MinGW C++ compiler ( a test case ):
...
int iInt32 = 0xffffeeeeddddcccc; // -> Error: integer constant is too large for "long" type
...

8. Use explicit 'ifdef's for different platforms ( pseudo codes ):
...
#ifdef _16BIT_PLATFORM_
//...
#endif
#ifdef _32BIT_PLATFORM_
//...
#endif
#ifdef _64BIT_PLATFORM_
//...
#endif
...

9. Default ( turn on ) the following warnigs for Microsoft compatible C++ compilers:

#pragma warning ( default : 4239 )// Nonstandard extension used : 'type cast' : conversion from 'type1' to 'type2'
#pragma warning ( default : 4242 )// Conversion from 'type1' to 'type2', possible loss of data
#pragma warning ( default : 4244 )// Conversion from 'type1' to 'type2', possible loss of data
#pragma warning ( default : 4305 )// 'Initializing' : truncation from 'type1' to 'type2'
#pragma warning ( default : 4309 )// 'Initializing' : truncation of constant value
#pragma warning ( default : 4311 )// 'Type cast' : pointer truncation from 'type1' to 'type2'

10. Verify how the __w64 keyword works for Microsoft compatible C++ compilers:

// Test Case - Verification of __w64 keyword - based on MSDN example
// 'V64C' stands for 'Verify 64-bit Compatibility'

...
#if ( defined ( _WIN32_MSC ) || defined ( _WIN32_ICC ) )
#define _RTV64C__w64
#endif
#if ( defined ( _WIN32CE_MSC ) || defined ( _WIN32_BCC ) || defined ( _WIN32_MGW ) ||\\
defined ( _COS16_TCC ) )
#define _RTV64C
#endif
...

#pragma warning ( default : 4244 )// Conversion from 'type1' to 'type2', possible loss of data

#define _WIN64
//#undef _WIN64

typedef int Int_32;

#ifdef _WIN64
typedef __int64 Int_Native;
#else
typedef int _RTV64C Int_Native;
#endif

Int_32 iInt_32 = 0xffeeddcc;
Int_Native iInt_Native = 0xffffeeeeddddcccc;
iInt_32 = iInt_Native; // MSDN - Warning C4244: 64-bit int assigned to 32-bit int
// Compiler - Warning C4244: '=' : conversion from 'Int_Native' to 'Int_32', possible loss of data
#undef _WIN64

#pragma warning ( disable : 4244 )
...

11. If a project has Test Cases ALL of them must be verified on a 64-bit platform

0 Kudos
61 Replies
SergeyKostrov
Valued Contributor II
2,356 Views
>>...If the precision control mask _MCW_PC is used on a 64-bit platform a Debug Assertion Failed message will be displayed... This is how it looks like:
debugassertionfailed.jpg
The message comes from msvcr80d.dll. You can press Ignore button because the message is actually a warning.
0 Kudos
SergeyKostrov
Valued Contributor II
2,356 Views
26. CRT-function _set_SSE2_enable is Not supported for 64-bit Windows platforms ( see declaration ): [ math.h ] ... #if defined( _M_IX86 ) ... _CRTIMP int __cdecl _set_SSE2_enable(__in int _Flag); ... #endif ...
0 Kudos
SergeyKostrov
Valued Contributor II
2,356 Views
27. Declaration of Naked functions with _declspec( naked ) is Not supported on 64-bit Windows platforms ( it is Not clear if a similar feature exists in C++ compilers for Linux platforms ). This is an example of a compilation error of Microsoft C++ compiler: ... SomeHeader.h(83) : error C2485: 'naked' : unrecognized extended attribute ...
0 Kudos
SergeyKostrov
Valued Contributor II
2,356 Views
28. Inline assembler ( keywords _asm or __asm ) is Not supported in C/C++ source files on 64-bit Windows platforms. This is an example of a compilation error of Microsoft C++ compiler: ... SomeHeader.h(98) : error C4235: nonstandard extension used : '__asm' keyword not supported on this architecture ... [ A quote from MSDN ] ... Inline assembler is not supported on the Itanium and x64 processors. ... Programs with inline assembler code are not fully portable to other hardware platforms. If you are designing for portability, avoid using inline assembler. ...
0 Kudos
SergeyKostrov
Valued Contributor II
2,356 Views
29. Take into account that with Microsoft compatible C++ compilers _M_X64 and _M_AMD64 macros are defined (!): ... // INTEL _M_X64 ( 64-bit ) // AMD _M_AMD64 ( 64-bit ) ... [ Example from a Visual Studio Output Window ] ------ Build All started: Project: GenTestApp, Configuration: Debug x64 ------ Compiling... Stdphf.cpp ... *** Message: Compiling with Visual Studio 2005 *** *** Message: Configuration - Desktop - _WIN32_MSC - DEBUG *** ... *** Message: Compiling for Intel Processing Unit ( 64-bit ) *** *** Message: Compiling for AMD Processing Unit ( 64-bit ) *** ... Compiling... GenTestApp.cpp Compiling manifest to resources... Linking... Creating library x64\Debug/GenTestAppD.lib and object x64\Debug/GenTestAppD.exp GenTestApp - 0 error(s), 0 warning(s)
0 Kudos
Bernard
Valued Contributor I
2,356 Views

>>>... Programs with inline assembler code are not fully portable to other hardware platforms. If you are designing for portability, avoid using inline assembler>>>

There is also no compiler optimization performed inside _asm block.This is related to x86 architecture.

0 Kudos
SergeyKostrov
Valued Contributor II
2,356 Views
>>...There is also no compiler optimization performed inside _asm block.This is related to x86 architecture. The thread is related to porting 32-bit ( x86 ) codes on 64-bit ( x64 ) platforms only.
0 Kudos
SergeyKostrov
Valued Contributor II
2,356 Views
>>...2. Turn on a /Wp64 compiler option for Microsoft compatible C++ compilers. It detects 64-bit >>portability problems on types that are marked with the __w64 keyword; Visual Studio 2008 gives a warning that '...option 'Wp64' has been deprecated and will be removed in a future release...' of a Visual Studio. Here is additional information: Visual Studio 2003 - 'Wp64' possibly supported ( very obsolete version ) Visual Studio 2005 - 'Wp64' supported ( still in use ) Visual Studio 2008 - 'Wp64' supported ( warning is given ) Visual Studio 2010 - 'Wp64' should not be supported ( I didn't verify it ) Visual Studio 2012 - 'Wp64' should not be supported ( I didn't verify it )
0 Kudos
SergeyKostrov
Valued Contributor II
2,356 Views
Update... >>... >>SomeHeader.h(98) : error C4235: nonstandard extension used : '__asm' keyword not supported on this architecture >>... >> >>[ A quote from MSDN ] >>... >>Inline assembler is not supported on the Itanium and x64 processors. >>... >>Programs with inline assembler code are not fully portable to other hardware platforms. If you are designing for portability, >>avoid using inline assembler. >>... Intel C++ compiler supports (!) inline assembler in C/C++ codes targeted for 64-bit Windows platforms!
0 Kudos
SergeyKostrov
Valued Contributor II
2,356 Views
>>>>Programs with inline assembler code are not fully portable to other hardware platforms. If you are designing for portability, >>>>avoid using inline assembler. >>>>... >> >>Intel C++ compiler supports (!) inline assembler in C/C++ codes targeted for 64-bit Windows platforms! A test project for Visual Studio 2008 is attached ( Intel C++ compiler is set as a default compiler ). Here is a screenshot: inlineassembleronx64.jpg
0 Kudos
Bernard
Valued Contributor I
2,356 Views

I<<<ntel C++ compiler supports (!) inline assembler in C/C++ codes targeted for 64-bit Windows platforms>>>

0 Kudos
Bernard
Valued Contributor I
2,356 Views

@Sergey

Thank you for verifying this.

0 Kudos
SergeyKostrov
Valued Contributor II
2,356 Views
All these tips are results of some real port of the software from a 32-bit to 64-bit Windows platform. It wasn't just a matter of verification if some feature of a C++ compiler supported or not.
0 Kudos
Bernard
Valued Contributor I
2,356 Views

Sergey Kostrov wrote:

All these tips are results of some real port of the software from a 32-bit to 64-bit Windows platform. It wasn't just a matter of verification if some feature of a C++ compiler supported or not.

Yes it is obvious:)

0 Kudos
SergeyKostrov
Valued Contributor II
2,356 Views
30. Adding a new 64-bit platform to some already existing Visual Studio project(s) ( with Win32, Windows CE or Windows Mobile platforms ) is a very easy procedure: [ In Visual Studio ] -> Build -> Configuration Manager -> Active solution platform ( select New ) -> Add x64 platform ( Copy settings from Win32 platform & check 'Create new project platform' )
0 Kudos
SergeyKostrov
Valued Contributor II
2,356 Views
31. There is a report about issues with Microsoft's CRT-function exp when porting some codes to a 64-bit Windows platform: Forum Topic: MKL vs Microsoft exp() function Web-link: software.intel.com/en-us/forums/topic/392924 Even if a user claims that there is inconsistency in results a solid test case and results are Not provided. I've spent some time on investigation and my results are very consistent.
0 Kudos
AndreyKarpov
New Contributor I
2,356 Views

Lessons on development of 64-bit C/C++ applications

The course is devoted to creation of 64-bit applications in C/C++ language and is intended for the Windows developers who use Visual Studio 2005/2008/2010 environment. Developers working with other 64-bit operating systems will learn much interesting as well. The course will consider all the steps of creating a new safe 64-bit application or migrating the existing 32-bit code to a 64-bit system.

The course is composed of 28 lessons devoted to introduction to 64-bit systems, issues of building 64-bit applications, methods of searching errors specific to 64-bit code and code optimization. Such questions are also considered as estimate of the cost of moving to 64-bit systems and rationality of this move.

The contents of the course

  • Lesson 01. What 64-bit systems are.
  • Lesson 02. Support of 32-bit applications.
  • Lesson 03. Porting code to 64-bit systems. The pros and cons.
  • Lesson 04. Creating the 64-bit configuration.
  • Lesson 05. Building a 64-bit application.
  • Lesson 06. Errors in 64-bit code.
  • Lesson 07. The issues of detecting 64-bit errors.
  • Lesson 08. Static analysis for detecting 64-bit errors.
  • Lesson 09. Pattern 01. Magic numbers.
  • Lesson 10. Pattern 02. Functions with variable number of arguments.
  • Lesson 11. Pattern 03. Shift operations.
  • Lesson 12. Pattern 04. Virtual functions.
  • Lesson 13. Pattern 05. Address arithmetic.
  • Lesson 14. Pattern 06. Changing an array's type.
  • Lesson 15. Pattern 07. Pointer packing.
  • Lesson 16. Pattern 08. Memsize-types in unions.
  • Lesson 17. Pattern 09. Mixed arithmetic.
  • Lesson 18. Pattern 10. Storage of integer values in double.
  • Lesson 19. Pattern 11. Serialization and data interchange.
  • Lesson 20. Pattern 12. Exceptions.
  • Lesson 21. Pattern 13. Data alignment.
  • Lesson 22. Pattern 14. Overloaded functions.
  • Lesson 23. Pattern 15. Growth of structures' sizes.
  • Lesson 24. Phantom errors.
  • Lesson 25. Working with patterns of 64-bit errors in practice.
  • Lesson 26. Optimization of 64-bit programs.
  • Lesson 27. Peculiarities of creating installers for a 64-bit environment.
  • Lesson 28. Estimating the cost of 64-bit migration of C/C++ applications.

The course's duration:the course implies that you study each of the 28 lessons on your own in 20-40 minutes. The total time of studying the material is about 18 hours.

You may open all the lessons in one file (the print version as well). This single file may be printed with the help of a common printer or converted into a pdf-file with the help of a pdf-printer.

0 Kudos
Bernard
Valued Contributor I
2,356 Views

Great resources.

Thanks for sharing.

0 Kudos
SergeyKostrov
Valued Contributor II
2,321 Views
This is related to 64-bit programming and take a look... I recently had a very interesting question about size of a union, like: ... typedef union tagSomeUnion { char A; char B; char C; size_t D; } SomeUnion; ... Even if it looks too easy to answer there is No a single answer to that question and this is why: 8-bit platform: sizeof( SomeUnion ) = 1 byte 16-bit platform: sizeof( SomeUnion ) = 2 bytes 32-bit platform: sizeof( SomeUnion ) = 4 bytes 64-bit platform: sizeof( SomeUnion ) = 8 bytes and so on.
0 Kudos
SergeyKostrov
Valued Contributor II
2,321 Views
32. I've recently detected an issue with size_t type in a highly portable C/C++ codes ( for 16-bit, 32-bit and 64-bit platforms ) and I'll provide some additional technical details later.
0 Kudos
Reply