src/endian_check.cpp
author KUDr
Fri, 12 Jan 2007 15:43:00 +0000
changeset 5620 eab6f02899a0
parent 5587 167d9a91ef02
child 6125 a6fff965707c
permissions -rw-r--r--
(svn r8079) -Fix [YAPF]: float division by zero when calculating stats (YAPF cache hit ratio). Caused BSOD on Win9x. (thanks 3iff for report, Darkvater for help)
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1692
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1692
diff changeset
     2
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
     3
#include <stdio.h>
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
     4
#include <string.h>
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
     5
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
     6
// This pretty simple file checks if the system is LITTLE_ENDIAN or BIG_ENDIAN
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
     7
//  it does that by putting a 1 and a 0 in an array, and read it out as one
200
03b8104d1479 (svn r201) -Fix: [1025836] Company values bigger dan int32 were put to negative
truelight
parents: 157
diff changeset
     8
//  number. If it is 1, it is LITTLE_ENDIAN, if it is 256, it is BIG_ENDIAN
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
     9
//
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    10
// After that it outputs the contents of an include files (endian.h)
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    11
//  that says or TTD_LITTLE_ENDIAN, or TTD_BIG_ENDIAN. Makefile takes
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    12
//  care of the real writing to the file.
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    13
1692
0b52df38cabc (svn r2196) -Change: updated the Makefile, now it works for crossplatform compiling
TrueLight
parents: 200
diff changeset
    14
int main (int argc, char *argv[]) {
2713
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    15
	unsigned char EndianTest[2] = { 1, 0 };
2719
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    16
	int force_BE = 0, force_LE = 0, force_PREPROCESSOR = 0;
1692
0b52df38cabc (svn r2196) -Change: updated the Makefile, now it works for crossplatform compiling
TrueLight
parents: 200
diff changeset
    17
2713
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    18
	if (argc > 1 && strcmp(argv[1], "BE") == 0)
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    19
		force_BE = 1;
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    20
	if (argc > 1 && strcmp(argv[1], "LE") == 0)
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    21
		force_LE = 1;
2719
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    22
	if (argc > 1 && strcmp(argv[1], "PREPROCESSOR") == 0)
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    23
		force_PREPROCESSOR = 1;
1692
0b52df38cabc (svn r2196) -Change: updated the Makefile, now it works for crossplatform compiling
TrueLight
parents: 200
diff changeset
    24
2713
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    25
	printf("#ifndef ENDIAN_H\n#define ENDIAN_H\n");
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    26
2713
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    27
	if (force_LE == 1) {
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    28
		printf("#define TTD_LITTLE_ENDIAN\n");
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    29
	} else {
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    30
		if (force_BE == 1) {
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    31
			printf("#define TTD_BIG_ENDIAN\n");
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    32
		} else {
2719
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    33
			if (force_PREPROCESSOR == 1) {
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    34
				// adding support for universal binaries on OSX
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    35
				// Universal binaries supports both PPC and x86
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    36
				// If a compiler for OSX gets this setting, it will always pick the correct endian and no test is needed
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    37
				printf("#ifdef __BIG_ENDIAN__\n");
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    38
				printf("#define TTD_BIG_ENDIAN\n");
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    39
				printf("#else\n");
2713
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    40
				printf("#define TTD_LITTLE_ENDIAN\n");
2719
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    41
				printf("#endif\n");
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    42
			} else {
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    43
				if ( *(short *) EndianTest == 1 )
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    44
					printf("#define TTD_LITTLE_ENDIAN\n");
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    45
				else
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    46
					printf("#define TTD_BIG_ENDIAN\n");
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    47
			}
2713
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    48
		}
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    49
	}
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    50
	printf("#endif\n");
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    51
2713
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    52
	return 0;
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    53
}