Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-14-2008, 07:47 PM   #1
Registered: Oct 2002
Distribution: RedHat 9
Posts: 43

Rep: Reputation: 15
32-bit to 64-bit migration for c++ program


I am trying to port a large c++ program I wrote on a 32-bit machine to a 64-bit opteron system and, not surprisingly, I am having problems. The program doesn't crash or anything, it just spits out results that are completely wrong at the end. I do not have the option of using the "-m32" switch with the "g++" compiler I am using, although this would work, because that option has been disabled and is not supported on the machine I am migrating to. So the only option I can see is to sift through the code and fix all the migration issues (groan).

My question is this: does anyone know of g++ compiler switches, or some linux program that can flag potential 32-to-64-bit migration issues in a c++ code? This would make my job incredibly easier.

Many thanks in advance!
Old 01-15-2008, 04:33 AM   #2
LQ Newbie
Registered: Nov 2005
Location: Hatfield, Herts
Distribution: Ubuntu 10.10
Posts: 13

Rep: Reputation: 1
The closest thing to the tool you're after would be a static analysis tool (lint is the usual example for C, but doesn't do C++)
however you may not like the results as they flag up anything they find less than ideal about your code. They can often flag portability issues, but they will also find a lot more besides - not necessarily what you're after.

I'm afraid I don't know of a tool that does exactly what you want, but perhaps in future you may want to use the C99 fixed size types (uint8_t, int8_t, uint16_t, int16_t, etc...) to be sure of the size of data you're using (I work in embedded software where we often have this issue). The porting issues you're seeing are usually caused by programmer assumption about the size of a give type; for example the coder may assume that a pointer is the same size as an int, and cast between them, only to find on a new architecture that the cast becomes a truncation and loses information. (Neither C nor C++ guarantees the size of any traditional type, though int is *usually the native bus width of the system and char is usually a byte).

*when using gcc with the -m64 option an int is defined as 32bits on a 64 bit system - so breaks this rule.

Such programmer assumptions would be very difficult for a compiler to spot as the compiler cannot understand what assumptions you're making when writing the code. I think this is why the feature doesn't exist yet (AFAIK).
As regards finding the bugs, cast operations are often a prime suspect. If you've used the c++ style of casting - "xxxxx_cast<>()" then it will be fairly easy to grep for them and hopefully flush out some suspicious code. Other things to watch for, are if you're initialising variables which you use to mask others. For example if you tried to set a variable to all "1s", e.g

unsigned int bitmask = 0xFFFFFFFF;

This would work well for a 32bit int but only set half of a 64bit int. Two ways to do this sort of thing flexibly are:

unsigned int bitmask = static_cast<unsigned int>(-1);
unsigned int bitmask2 = ~0;

which will both set an unsigned var to all "1"s however big or small its type is. To make your mask you can then clear bits individually as required.

Apologies if you know this already - hopefully it will be of some help in future.

Good luck!

Last edited by rmallins; 01-15-2008 at 04:38 AM.
Old 01-15-2008, 04:50 AM   #3
Registered: Dec 2004
Location: Dublin Ireland via Cobh, Limerick.
Distribution: RedHat, Suse, Ubuntu.
Posts: 33

Rep: Reputation: 16
I haven't found a method to do this neatly I'm afraid.
Ported some things from 32bit to 64 over last while and it boiled down to a couple of common things. It was not quite as bad as I expected so just go for it!

Pay attention to warnings, sometimes they point to problems (but sometimes not really).
-Wall -Wcast-align

And if you want to quickly fix a good few problems turn off optimisation!
You might try a build without optimisation and see does it get further.
-O1 instead of -O2 or -O3 or whatever.

Did I keep a list? *rummage* Oh. Found some notes on the matter.
But they're rather specific to what I was doing.

dereferencing type-punned pointer will break strict-aliasing rules
cast from '<sometype>' to '<something else>' increases required alignment of target type
wikipedia/Type_punning wikipedia/Type_punning
So by removing these warnings we allow the compiler to better optimise.
static_cast to subclass
Old 01-15-2008, 07:27 PM   #4
Registered: Oct 2002
Distribution: RedHat 9
Posts: 43

Original Poster
Rep: Reputation: 15
Problem solved (I hope)!

Thank you rmallins and greeneagle. I took your suggestions to heart and went through the code looking for suspicious statements, and I found the culprit! It was all due to a single line where I was calling the "pow" function with "long double" arguments. I changed the "pow" to "powl" and now the code works under both 32-bit and 64-bit architectures.

Still, I consider myself very lucky to have found the problem this quickly. If you or anyone else finds a more systematic and automated way of flagging these potential migration problems at some point in the future, I would love to hear it. I'm never this lucky twice in a row...

Many thanks again!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
64 bit cpu-64 bit Ubuntu-are there 32 bit app issues? sofasurfer Ubuntu 7 04-09-2014 02:02 PM
Shell scripts migration from SunOS 5.9 to Red Hat Linux Advanced Server 4.0 64-bit devenr_2000 Linux - Newbie 1 11-13-2007 12:08 PM
Integrated 16-bit Sound Blaster Pro compatible audio 24-bit DAC jelgavchik Linux - Laptop and Netbook 2 11-16-2006 09:20 PM
Assembling a 32 bit sample program in a 64 bit world debiant Programming 0 10-14-2006 02:52 PM
32 bit or 64 bit install - is 32 bit easier for a newbie? dms05 Linux - Newbie 3 05-19-2006 03:05 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:56 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration