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,072 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,609 Views
Here are a couple of screenshots that demonstrate the truncation of a 64-bit value when
assigned to a 32-bit variable:

State 1 - Values are uninitialized


State 2 - Values initialized


State 3 - A 64-bit value was assigned to a 32-bit variable
0 Kudos
SergeyKostrov
Valued Contributor II
2,609 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...

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

Please take a look at a thread:

Is a "Y2K38 disaster" looming? Issues with a 'time_t' type

http://software.intel.com/en-us/forums/showthread.php?t=105868&o=a&s=lr

for more technical details.
0 Kudos
SergeyKostrov
Valued Contributor II
2,609 Views
Everybody is welcomed to sharea 64-bit programming experience!

Best regards,
Sergey
0 Kudos
SergeyKostrov
Valued Contributor II
2,609 Views
12.Issues with a'fseek' CRT-function detected:


For a 32-bit platform it is declared as follows:
int fseek( FILE *stream, long offset, int origin );

For a 64-bit platform it is declared as follows:
int _fseeki64( FILE *stream, __int64 offset, int origin );

A macro-wrapper is needed to make codes more portable.

0 Kudos
SergeyKostrov
Valued Contributor II
2,609 Views

13. Issues with some intrinsic functions detected:

Header: 'nmmintrin.h'
...
// Calculate a number of bits set to 1
...
#if defined( _M_X64 )
extern __int64 _mm_popcnt_u64( unsigned __int64 v );
#endif
...

Header: 'smmintrin.h'
...
// Insert integer into packed integer array element
// selected by index
...
#if defined( _M_X64 )
extern __m128i _mm_insert_epi64( __m128i dst, __int64 s, const int ndx );
#endif

// Extract integer from packed integer array element
// selected by index
...
#if defined( _M_X64 )
extern __int64 _mm_extract_epi64( __m128i src, const int ndx );
#endif
...

Even if __int64 and __m128i data types could be used on a 32-bit platform these intrinsic functions are not available.

0 Kudos
SergeyKostrov
Valued Contributor II
2,609 Views

14. Forced truncations of Single- or Double-precision values to integer type:

In the following example:
...
float fPI = 3.141592653589793f;

float fPIint = ( int )fPI;
...

there is nothing wrong, but ifMicrosoftor Intel C++ compilers are useda warning:

C4244: '=' : conversion from 'float' to 'int', possible loss of data

will be displayed.

0 Kudos
SergeyKostrov
Valued Contributor II
2,609 Views
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'


In case of Intel C++ compiler default ( turn on )a warning 2259:

#pragma warning (default : 2259 ) // Non-pointer conversion from "type1" to "type2" may lose significant bits
0 Kudos
Maycon_Oliveira
Beginner
2,609 Views
Thank you for the information! :-)
0 Kudos
SergeyKostrov
Valued Contributor II
2,609 Views
15. A version 3 of the 'MemTestApp' that could be used to test allocation of large blocks of memory is attached.

Please take a look at a post:

http://software.intel.com/en-us/forums/showpost.php?p=189214

for more technical details.
0 Kudos
Arthur_Moroz
Beginner
2,609 Views
16. Integer literal constant:


__int64 test = 1<<48;
The result is 0! To have correct result it should look like following
__int64 test = 1LL<<48;
0 Kudos
SergeyKostrov
Valued Contributor II
2,609 Views
Quoting Arthur Moroz
16. Integer literal constant:

__int64 test = 1<<48;

The result is 0! To have correct result it should look like following

__int64 test = 1LL<<48;


Thank you and it is a really good note. I also had a similarissue and'LL' or 'ULL' resolved it.

0 Kudos
SergeyKostrov
Valued Contributor II
2,609 Views
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'


In case of Intel C++ compiler default ( turn on )a warning 2259:

#pragma warning (default : 2259 ) // Non-pointer conversion from "type1" to "type2" may lose significant bits


A couple of notes regarding warnings. Take into account that as soon as these warnings are 'default'ed
( turned on ) you could be overwhelmed by a number of warnings during compilation of source codes.

Here is example:

- A software development environment has one Visual Studio solution and a number of different
C/C++ projects is 36. Since every project is configured for different platforms a total number of
projects to be compiled is 130

- All warnings related to 64-bit conversions are 'default'ed

- In case of rebuilding all projects a total number of warning messages is 1484

- A summary from a Visual Studio Output Window is as follows:

[cpp] ----- Build started: Project: , Configuration: Release ------ ... - 0 error(s), 2 warning(s) - 0 error(s), 160 warning(s) - 0 error(s), 1 warning(s) - 0 error(s), 2 warning(s) - 0 error(s), 1 warning(s) - 0 error(s), 2 warning(s) - 0 error(s), 83 warning(s) - 0 error(s), 380 warning(s) - 0 error(s), 97 warning(s) - 0 error(s), 160 warning(s) - 0 error(s), 99 warning(s) - 0 error(s), 61 warning(s) - 0 error(s), 2 warning(s) - 0 error(s), 260 warning(s) - 0 error(s), 4 warning(s) - 0 error(s), 3 warning(s) - 0 error(s), 167 warning(s) ... ===== Build: 130 succeeded, 0 failed, 2 up-to-date, 0 skipped ========== [/cpp]
0 Kudos
SergeyKostrov
Valued Contributor II
2,609 Views
7. Problem - MinGW C++ compiler ( a test case ):
...
int iInt32 = 0xffffeeeeddddcccc; // -> Error: integer constant is too large for "long" type
...


There isa workaround and avalue 18446725308424768716( with LL or ULL )has to be used instead of 0xffffeeeeddddcccc.

0 Kudos
SergeyKostrov
Valued Contributor II
2,609 Views
15. A version 3 of the 'MemTestApp' that could be used to test allocation of large blocks of memory is attached.

Please take a look at a post:

http://software.intel.com/en-us/forums/showpost.php?p=189214

for more technical details.


Test Results:

OS : Windows 7 64-bit Home Premium
CPU: AMD ( 4 cores )
RAM: 6GB
VM: 96GB initial size \ 128GB maximum size
APP: MemTestApp64.exe \ Release configuration

Attempts to allocate memory:

0.25GB - Allocated
0.50GB - Allocated
1.GB - Allocated
2.GB - Allocated
4.GB - Allocated
8.GB - Allocated
14.GB - Allocated ( Target for a 61000x61000 Single-Precision matrix )
16.GB - Allocated
28.GB - Allocated ( Target for a 61000x61000 Double-Precision matrix )
32.GB - Allocated
64.GB - Allocated

0 Kudos
SergeyKostrov
Valued Contributor II
2,609 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;
...


This is simply to note that 'Wp64' compiler option is deprecatedand Visual Studio 2008 ( Professional & Express Editions)displays
awarning message:
...
cl : Command line warning D9035 : option 'Wp64' has been deprecated and will be removed in a future release
...

0 Kudos
SergeyKostrov
Valued Contributor II
2,609 Views

18. Intel 64-bit Mode Coding Guidelines

Intel 64 and IA-32 Architectures Optimization Reference Manual
November 2007

Chapter 9. 64-bit Mode Coding Guidelines

Note: 5 pages only

0 Kudos
SergeyKostrov
Valued Contributor II
2,609 Views
17. Issues - When porting a 32-bit project with IDL ( Interface Definition Language ) files for MIDL compiler.

MIDL - Microsoft Interface Definition Language
0 Kudos
SergeyKostrov
Valued Contributor II
2,609 Views
19. Regarding /LARGEADDRESSAWARE linker option: . This is what MSDN says: ... The /LARGEADDRESSAWARE option tells the linker that an application supports addresses larger than 2 gigabytes. ... However, it doesn't mean that a 32-bit application that was built with enabled /LARGEADDRESSAWARE linker option could allocate greater than 2GB of memory. It is simply not possible on a 32-bit Windows platform without AWE and on 64-bit Windows platforms. . AWE - Address Windowing Extensions
0 Kudos
TimP
Honored Contributor III
2,609 Views
Ability to allocate beyond the 2GB barrier in 32-bit Windows was introduced with the XP /3GB boot switch. The space is fragmented. After the 2GB is used up, additional non-contiguous allocations within the additional 1GB space are supported, with the additional linker switch. If it were not for mobile devices, one would think 32-bit Windows obsolete.
0 Kudos
SergeyKostrov
Valued Contributor II
2,508 Views
I would like to mention that AWE is only supported on some Windows operating systems, like Server or Enterprise Editions, and it is NOT supported on Home Editions. . >>...one would think 32-bit Windows obsolete... . There are still too many users with 32-bit Windows. So, it will be used for many years.
0 Kudos
Reply