Mac OS X/mach: Identifying architecture and CPU type


Platform independent endinanness check:

#include <stdio.h>
union foo
{
  char p[4];
  int k;
};

int main()
{
  int j;
  union foo bar;
  printf("$Id: endianness.c,v 1.1 2006/07/09 17:48:14 stany Exp stany $\nChecks endianness of your platform\n");
  printf("Bigendian platform (ie Mac OS X PPC) would return \"abcd\"\n");
  printf("Littleendian platform (ie Linux x86) would return \"dcba\"\n");
  printf("Your platform returned ");
  bar.k = 0x61626364;
  for(j=0; j<4 ; j++)
  {
  printf("%c",bar.p[j]);
  }

  printf("\n");
  return 0;

}

Platform dependent tell me everything check:

/*
 * $Id: cpuid.c,v 1.2 2002/08/03 23:38:39 stany Exp stany $
 */

#include <mach-o/arch.h>
#include <stdio.h>

const char *byte_order_strings[]  = {
        "Unknown",
        "Little Endian",
        "Big Endian",
};

int main() {

  const NXArchInfo *p=NXGetLocalArchInfo();
  printf("$Id: cpuid.c,v 1.2 2002/08/03 23:38:39 stany Exp stany $ \n");
  printf("Identifies Darwin CPU type\n");
  printf("Name: %s\n", p->name);
  printf("Description: %s\n", p->description);
  printf("ByteOrder: %s\n", byte_order_strings[p->byteorder]);
  printf("CPUtype: %d\n", p->cputype);
  printf("CPUSubtype: %d\n\n", p->cpusubtype);
  printf("\nFor scary explanation of what CPUSubtype and CPUtype stand for, \nlook into /usr/include/mach/machine.h\n\n
ppc750\t-\tG3\nppc7400\t-\tslower G4\nppc7450\t-\tfaster G4\nppc970\t-\tG5\n");

return 0;

Comments are closed.