Re: how to detect the compile is 32 bits or 64 bits?




J. J. Farrell wrote:
Walter Roberson wrote:
In article <0f3Bg.2584$9T3.1111@xxxxxxxxxxxxxxxxxxxxxxxxxx>,
Tim Prince <tprince@xxxxxxxxxxxxxxxxxxx> wrote:
steve yee wrote:
i want to detect if the compile is 32 bits or 64 bits in the source
code itself. so different code are compiled respectively. how to do
this?

You invite bugs, as well as taking the question off the topic of
standard C, if you write source code which has to change.

Not necessarily.

The usual
question is about (sizeof)<various pointer types>. If your question is
simply about (sizeof)int, you create problems by assuming it is
determined by whether you have a 32- or 64-bit platform. If you insist
on unions of pointers and ints, knowing whether it is 32 or 64 bits
won't save you. More so, if you are one of those who writes code which
depends on (sizeof)(size_t x).

You appear to have read a fair bit into the poster's question
that I don't think is justified by what the poster wrote.

Suppose I have an algorithm, such as a cryptography algorithm, that
operates on chunks of bits at a time. The algorithm is the same
(except perhaps for a few constants) whether I'm computing with 32 or
64 bits, but the chunk size differs for the two cases. In such a case,
I -could- write the code using only the minimum guaranteed size,
but on most platforms it would be noticably more efficient to use
the larger chunk size *if the platform supports it*. The constants
for the algorithm could probably be computed at run-time and a
generic algorithm used, but in the real world, having the constants
available at compile time increases compiler optimization opportunities
leading to faster code.

Indeed. How does knowing "if the compile is 32 bits or 64 bits" help
with this, though? What you're interested in is how big the available
types are.

In C, it is not an error to write int x = 40000; for use on
platforms that have an int of at least 17 bits. It is not maximally
portable, but it is not an error -- and the OP was asking for a
a compile-time method of selecting such code for platforms that allow it,
dropping back to smaller value assumptions when that is all the
platform supports.

If I'm building for my 17-bit-int machine, how is code which tells me
if the compile is 32 bit or 64 bit going to help, especially since the
answer is "no"? I need to know if ints have at least 17 value bits.

Knowing "if the compile is 32 bits or 64 bits" requires a lot of
subsequent non-portable assumptions for the information to be of any
use. Much better to check for the things you need to know rather than
making assumptions which will change from compiler to compiler. What
does the compile being 32 or 64 bit tell me about the size of a long,
for example?

in fact, what i want to know is the size of the native long type of the
c compiler, at compile time. for exampe, the following code

if (sizeof (long) == 8)
{
// handle 64 bits long type
}
else if (sizeof (long) == 4)
{
// handle 32 bits long type
}

but, you know, this is done at run time, not compile time. i would like
write code like this:

#if sizeof (long) == 8
{
// handle 64 bits long type
}
#elif sizeof (long) == 4
{
// handle 32 bits long type
}
#endif

but this simply doesn't work.

of course autoconf can help to achieve this. but i think the compile
should provide a way to determine the size of each native types that it
supports. in fact sizeof operator is compile time computing, but the
compile does not allow the sizeof operator to be used in #if
preprocesser statement. it should not have this limitation. so i wonder
if there's an alternative way to do this.

btw, on windown platform, autoconf is not generally avalible.

.



Relevant Pages

  • Re: Problem Cloning PUBLIC directory
    ... you must remove the feature from your platform and ... rebuild, otherwise, the binaries from the old PUBLIC atadisk driver still ... >I fixed the SOURCES file and now everything compiles correctly. ... >>> PlatformBuilder 5.0 and having some problems getting it to compile. ...
    (microsoft.public.windowsce.platbuilder)
  • Re: how to detect the compile is 32 bits or 64 bits?
    ... determined by whether you have a 32- or 64-bit platform. ... Suppose I have an algorithm, such as a cryptography algorithm, that ... but the chunk size differs for the two cases. ... available at compile time increases compiler optimization opportunities ...
    (comp.lang.c)
  • Re: I got SPDRP_ADDRESS error
    ... You can download the latest PSDK even if you aren't using it in VS6. ... so your code will compile with either PSDK in any compiler. ... it will work with any platform that implements this code value. ...
    (microsoft.public.vc.mfc)
  • Re: Serial programming
    ... I have no Hyperterminal-like on my pocket PC! ... > modifications required to make it compile). ... > right now have ARM processor support as a part of my compiler. ... > platform, and run another on a laptop, then presumably they can talk to ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: how to detect the compile is 32 bits or 64 bits?
    ... determined by whether you have a 32- or 64-bit platform. ... Suppose I have an algorithm, such as a cryptography algorithm, that ... but the chunk size differs for the two cases. ... How does knowing "if the compile is 32 bits or 64 bits" help ...
    (comp.lang.c)