By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

how to detect CPU architecture..

P: n/a
hi
i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..

can anyone please help me..

thanks.

Sep 1 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On 31 Aug 2006 23:14:23 -0700, rr********@gmail.com wrote:
>hi
i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..

can anyone please help me..

thanks.
Standard C does not provide any way to detect whether the type of CPU
is 32-bit or 64-bit or even 8-bit or 16-bit or 128-bit. Since this
newsgroup is about Standard C, no one here can really help you.

--
jay
Sep 1 '06 #2

P: n/a
rr********@gmail.com wrote:
i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..
It can't. The CPU is 8-bit.

Richard






(Well, it could be. I still have computers like that. And define "the
type". And explain why you want to know. You probably _think_ you need
to, but you're equally probably mistaken.)
Sep 1 '06 #3

P: n/a
Try to use ULONG_MAX in gcc macro if you use Unix.
or sizeof(long): 4 for 32bit, 8 for 64bit arch.

Sep 1 '06 #4

P: n/a
In article <7a********************************@4ax.com>,
jaysome <ja*****@spamcop.netwrote:
>On 31 Aug 2006 23:14:23 -0700, rr********@gmail.com wrote:
>>i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..
>Standard C does not provide any way to detect whether the type of CPU
is 32-bit or 64-bit or even 8-bit or 16-bit or 128-bit. Since this
newsgroup is about Standard C, no one here can really help you.
Furthermore, Standard C does not provide any what to determine what any
particular person -means- by a CPU being 32 bit or 64 bit or something
else. Bits in int? Bits in long? Bits in long long? Bits in a pointer?
Maximum theoretical addressible memory? Bits in the data bus between
the CPU and memory? Maximum number of bits in a result produced by an
operation? (that's a trick question -- integer multiple and divide
instructions are often exceptions to the rest of the architecture.)

Then there are systems like all of the SGI MIPS-based computers
produced in the last decade, all of which have CPUs which can
switch between 32 bit pointers and 64 bit pointers (per process),
any particular test run would not tell you what the CPU is capable
of, only how that particular test program was set up to run.
--
Prototypes are supertypes of their clones. -- maplesoft
Sep 1 '06 #5

P: n/a
posted:
hi
i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..

I suppose the closest thing might be something like:

#include <limits.h>

#if UINT_MAX == 4294967295U
/* We have 32-Bit */
#elsif UINT_MAX == 18446744073709551615U
/* We have 64-Bit */
#else
/* Neither */
#endif

--

Frederick Gotham
Sep 1 '06 #6

P: n/a
Frederick Gotham wrote:
posted:

>>hi
i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..

I suppose the closest thing might be something like:

#include <limits.h>

#if UINT_MAX == 4294967295U
/* We have 32-Bit */
#elsif UINT_MAX == 18446744073709551615U
/* We have 64-Bit */
#else
/* Neither */
#endif
No. This would "detect" a 32 bit system in a 64 bit
unix. Use "long" for that. In 64 bit Unix, you have
(in some implementations, specifically gcc)
long 64 bits
int 32 bits

64 bit windows has
long 32 bits
int 32 bits

jacob
Sep 1 '06 #7

P: n/a
In article <FI*******************@news.indigo.ie>,
Frederick Gotham <fg*******@SPAM.comwrote:
[someone not named by Frederick posted]
>i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
>I suppose the closest thing might be something like:
>#include <limits.h>
>#if UINT_MAX == 4294967295U
/* We have 32-Bit */
#elsif UINT_MAX == 18446744073709551615U
/* We have 64-Bit */
#else
/* Neither */
#endif

SGI IRIX in -64 mode would have UINT_MAX as 4294967295U .

Perhaps you meant to use ULONG_MAX instead of UINT_MAX ?
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Sep 1 '06 #8

P: n/a
jacob navia posted:
No. This would "detect" a 32 bit system in a 64 bit
unix.

Yes, I realise that. It was a merely fully-portable attempt to detect CPU
bitness.

--

Frederick Gotham
Sep 1 '06 #9

P: n/a
"dkrot" <j.*****@rambler.ruwrites:
Try to use ULONG_MAX in gcc macro if you use Unix.
or sizeof(long): 4 for 32bit, 8 for 64bit arch.
Please quote enough context so we can tell what you're talking about
even if we haven't seen the parent article.

ULONG_MAX (which is standard C, not specific to either gcc or Unix)
only tells you what the compiler writer has chosen as the maximum
value of type unsigned long. It tells you nothing about the
underlying CPU architecture. The compiler writer's choices for the
attributes of the predefined types are typically guided by the CPU
architecture, but they're not mandated by it.

It's not even clear what "32-bit architecture" or "64-bit
architecture" actually means. It might have some accepted meaning for
particular sets of architectures (e.g., the x86 family), but that
won't necessarily apply to other architectures (PowerPC, SPARC,
AS/400, DS9K, etc.).

And even if you can rigorously define the terms, there's not likely to
be any way to distinguish between them in portable C.

The entire question is largely meaningless and off-topic.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 1 '06 #10

P: n/a
Frederick Gotham <fg*******@SPAM.comwrites:
jacob navia posted:
>No. This would "detect" a 32 bit system in a 64 bit
unix.

Yes, I realise that. It was a merely fully-portable attempt to detect CPU
bitness.
Portability isn't a virtue if your program doesn't solve the problem.

Here's another fully-portable attempt to detect CPU bitness:

#include <stdio.h>
int main(void)
{
printf("Hello, world\n");
return 0;
}

Yours, I'm afraid, was only marginally better.

The fact is, there is no fully-portable solution, and it's a waste of
time trying to construct one.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 1 '06 #11

P: n/a
>i have to detect the type of CPU.
>whether it is 32-bit or 64-bit..
how can this be done..
An N-bit CPU architecture is an *ADVERTISING* and *MARKETING*
concept. It can change with no physical change in the CPU, and it
can change before the CPU is even built.

What is it you think is indicated by "this CPU has an N-bit
architecture", and why do you need to know?

1. This CPU uses N bits for char.
2. This CPU uses N bits for int.
3. This CPU uses N bits for long.
4. This CPU uses N bits for long long.
5. This CPU uses N bits for pointers.
6. This CPU uses an N-bit-wide memory bus.

These won't necessarily occur together.

#1 won't happen very often, especially on desktop computers or servers,
as opposed to embedded processors.
#6 is likely detectable from software only by measuring performance,
and possibly not even then, unless software can read the invoice for
the purchase of the system.
#4 is unlikely to indicate much as lots of machines provide a 64-bit long
long with multiple-precision arithmetic, even on machines arguably of
16/32 bit architecture.

I think the most likely meaning of "64-bit architecture" is that
*POINTERS* are 64 bits, so no nasty 4GB addressing space limit.
There is no agreement on that.

Sep 1 '06 #12

P: n/a
go***********@burditt.org (Gordon Burditt) writes:
>>i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..
The above was posted by rr********@gmail.com. Gordon insists on
deleting attribution lines, which is extraordinarily rude.
An N-bit CPU architecture is an *ADVERTISING* and *MARKETING*
concept. It can change with no physical change in the CPU, and it
can change before the CPU is even built.

What is it you think is indicated by "this CPU has an N-bit
architecture", and why do you need to know?

1. This CPU uses N bits for char.
2. This CPU uses N bits for int.
3. This CPU uses N bits for long.
4. This CPU uses N bits for long long.
5. This CPU uses N bits for pointers.
6. This CPU uses an N-bit-wide memory bus.

These won't necessarily occur together.
And most of these are choice of the compiler implementer, not
necessarily attributes of the CPU. Two different compilers, or the
same compiler in two different modes, can choose different sizes for
int on the same CPU.

[...]
I think the most likely meaning of "64-bit architecture" is that
*POINTERS* are 64 bits, so no nasty 4GB addressing space limit.
There is no agreement on that.
Yes, on most CPUs, pointer size is fairly likely to match what most
people think of as the "bitness" of the architecture. But it's far
from universal. There have been, and still are, plenty of 8-bit CPUs,
but few if any of them have 8-bit pointers.

What's probably relevant is that the "32-bit" and "64-bit" versions of
a given architecture may have different APIs (or different flavors of
the same API, or whatever). As a programmer, what you really need to
know is which API to use. There may be some attribute you can test to
determine this, but it's likely to vary from one architecture family
to another; the details are system-specific and therefore off-topic
here.

If your first question is "Is this a 32-bit or a 64-bit CPU?", your
zeroth question should be "Why do I want to know this?".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 1 '06 #13

P: n/a
rr********@gmail.com wrote:
# hi
# i have to detect the type of CPU.
# whether it is 32-bit or 64-bit..
# how can this be done..

Not from C by itself, but if you know, for example that on one cpu
long is 4 bytes and 8 bytes on the other, then you can use
if (sizeof(long)==4) {
32 bit code
}else if (sizeof(long)==8) {
64 bit code
}else }
...
}

You can also pull some information from limits.h

--
SM Ryan http://www.rawbw.com/~wyrmwif/
She broke your heart and inadvertently drove men to deviant lifestyles.
Sep 1 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.