how to detect CPU architecture..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rrs.matrix@gmail.com

    how to detect CPU architecture..

    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.

  • jaysome

    #2
    Re: how to detect CPU architecture..

    On 31 Aug 2006 23:14:23 -0700, rrs.matrix@gmai l.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

    Comment

    • Richard Bos

      #3
      Re: how to detect CPU architecture..

      rrs.matrix@gmai l.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.)

      Comment

      • dkrot

        #4
        Re: how to detect CPU architecture..

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

        Comment

        • Walter Roberson

          #5
          Re: how to detect CPU architecture..

          In article <7alff2d9ehgnu6 7h7v2kt1mnp91d2 n1dti@4ax.com>,
          jaysome <jaysome@spamco p.netwrote:
          >On 31 Aug 2006 23:14:23 -0700, rrs.matrix@gmai l.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

          Comment

          • Frederick Gotham

            #6
            Re: how to detect CPU architecture..

            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 == 184467440737095 51615U
            /* We have 64-Bit */
            #else
            /* Neither */
            #endif

            --

            Frederick Gotham

            Comment

            • jacob navia

              #7
              Re: how to detect CPU architecture..

              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 == 184467440737095 51615U
              /* 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

              Comment

              • Walter Roberson

                #8
                Re: how to detect CPU architecture..

                In article <FIYJg.13355$j7 .326840@news.in digo.ie>,
                Frederick Gotham <fgothamNO@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 == 184467440737095 51615U
                /* 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

                Comment

                • Frederick Gotham

                  #9
                  Re: how to detect CPU architecture..

                  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

                  Comment

                  • Keith Thompson

                    #10
                    Re: how to detect CPU architecture..

                    "dkrot" <j.kisel@ramble r.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_Keit h) kst-u@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.

                    Comment

                    • Keith Thompson

                      #11
                      Re: how to detect CPU architecture..

                      Frederick Gotham <fgothamNO@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_Keit h) kst-u@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.

                      Comment

                      • Gordon Burditt

                        #12
                        Re: how to detect CPU architecture..

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

                        Comment

                        • Keith Thompson

                          #13
                          Re: how to detect CPU architecture..

                          gordonb.j90gu@b urditt.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 rrs.matrix@gmai l.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_Keit h) kst-u@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.

                          Comment

                          • SM Ryan

                            #14
                            Re: how to detect CPU architecture..

                            rrs.matrix@gmai l.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.

                            Comment

                            Working...