how to detect CPU architecture..

R

rrs.matrix

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.
 
J

jaysome

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.
 
R

Richard Bos

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.)
 
D

dkrot

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

Walter Roberson

On 31 Aug 2006 23:14:23 -0700, (e-mail address removed) wrote:
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.
 
F

Frederick Gotham

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
 
J

jacob navia

Frederick said:
posted:





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
 
W

Walter Roberson

i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
[/QUOTE]
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 ?
 
F

Frederick Gotham

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.
 
K

Keith Thompson

dkrot said:
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.
 
K

Keith Thompson

Frederick Gotham said:
jacob navia posted:

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.
 
G

Gordon Burditt

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.
 
K

Keith Thompson

The above was posted by (e-mail address removed). 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?".
 
S

SM Ryan

(e-mail address removed) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top