Re: Detecting Intel / PowerPPC?



In article <WKANf.46723$d5.203491@xxxxxxxxxxxxxxx>,
"Mike" <no@xxxxxxxx> wrote:

Hi,

Is there a system call (C++) which I can use to check whether the program is
executing on an Intel machine or a PowerPC?

A couple of files which my application loads from the hard disk contain
floating
point numbers and then they need to be swapped (low / big endian) when
they're
loaded if the user is on an Intel Mac.

No clue about any sort of system call or built-in that would do it, but
it seems to me one could check by doing something like this in "plain
vanilla" C (which should work just fine in C++):

// return 0 if machine is BigEndian, 1 if LittleEndian
// Caveat: Assumes ints are four bytes. Alter accordingly
// if that's not true for your project.
int CheckEndianness(void)
{
unsigned int foo;
Byte *fooPrime;
int ReturnValue = 0; // Assume BigEndian until we find out otherwise

foo = 0xFF11EE22;
fooPrime = (Byte *)&foo;
if (*fooPrime == 0xFF)
{
// We're already set up to return BigEndian;
}
else
{
ReturnValue = 1; // Say it's a LittleEndian machine
}
return ReturnValue;
}

Call it at initialization, then behave accordingly when you get to the
point of loading your data. Granted, it's not exactly elegant, but I'd
expect it to tell you which way things get crammed into RAM.

Fair warning: Coded "seat of the pants", with no Intel-based Mac to test
on. Give it a whirl - If it works, great. If not, <shrug> I'll refund
what you paid for it. :)

--
Don Bruder - dakidd@xxxxxxxxx - If your "From:" address isn't on my whitelist,
or the subject of the message doesn't contain the exact text "PopperAndShadow"
somewhere, any message sent to this address will go in the garbage without my
ever knowing it arrived. Sorry... <http://www.sonic.net/~dakidd> for more info
.



Relevant Pages

  • Re: Detecting Intel / PowerPPC?
    ... executing on an Intel machine or a PowerPC? ... int CheckEndianness ... rube-goldbergish solutions this question provokes. ...
    (comp.sys.mac.programmer.help)
  • Re: Detecting Intel / PowerPPC?
    ... executing on an Intel machine or a PowerPC? ... point numbers and then they need to be swapped (low / big endian) when they're ... int main ... bstevenson at remove this text dot apple dot com ...
    (comp.sys.mac.programmer.help)
  • Re: Boxing and Unboxing ??
    ... two integers that are stored in reference types? ... to 'int' to add them - and that unboxes them. ... Is that actual Intel machine specific assembly language, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: printing all variables in union
    ... > what will be output of following program on INTEL machine ... > union s obj; ... integer 256=0x100 will be stored on a little endian machine ... (assuming size of int to be 4) ...
    (comp.lang.c)

Loading