You are not logged in.

#1 2008-03-24 04:58:08

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,383
Website

detect cpu type c++

I have a template class that hold numeric data (for simplicity think of it as a vector).   When I do addition, I want to have the type promotion handled automatically. 

The only problem is in the case of adding an unsigned int and a long.  On 32 bit computer, this should resulting in a unsigned int.  On an 64 bit computer it will result in a long.

My question is, how can I tell within my code whether it is being compiled on a 32 or 64 bit cpu?

Offline

#2 2008-03-24 07:04:04

wuischke
Member
From: Suisse Romande
Registered: 2007-01-06
Posts: 630

Re: detect cpu type c++

limits.h and inttypes.h use

#if __WORDSIZE == 64

to check for 32/64 bit systems on build time. The definition is in bits/wordsize.h where a check for

#if defined __x86_64__

is done.
The first variant is most probably more portable (think e.g. of PPC64), but I believe both are limited to gcc.

Last edited by wuischke (2008-03-24 07:05:04)

Offline

#3 2008-03-24 08:03:39

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,383
Website

Re: detect cpu type c++

Thanks for the ideas.  It appears that the "__WORDSIZE" definition is a glibc construct.   I see "__x86_64__" is used in libpng and mono so perhaps that is a gcc define.  Going to look into this now.

Offline

#4 2008-03-24 08:30:00

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,383
Website

Re: detect cpu type c++

Well, I just found this command to list all gcc internal definitions

echo "" | g++ -E -dM -x c - | sort | less

i386 (and variants __i386 and __i386__) are defined by gcc on linux and in mingw on windows.  Now I have to decide what platforms I would like support...

Offline

Board footer

Powered by FluxBB