File: systest.c

package info (click to toggle)
quakespasm 0.93.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,672 kB
  • sloc: ansic: 52,034; sh: 306; makefile: 263
file content (256 lines) | stat: -rw-r--r-- 7,283 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * stupid test tool that reports the type sizes and
 * their alignment offsets in structures, and the byte
 * order as detected at runtime and compile time.
 */

/*
 * endianness stuff: <sys/types.h> is supposed
 * to succeed in locating the correct endian.h
 * this BSD style may not work everywhere.
 */

#undef ENDIAN_GUESSED_SAFE
#undef ENDIAN_ASSUMED_UNSAFE

#include <sys/types.h>

#include <stddef.h>
#include <limits.h>
#include <stdio.h>

/* include more if it didn't work: */
#if !defined(BYTE_ORDER)

# if defined(__linux__) || defined(__linux)
#	include <endian.h>
# elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#	include <machine/endian.h>
# elif defined(__sun) || defined(__svr4__)
#	include <sys/byteorder.h>
# elif defined(_AIX)
#	include <sys/machine.h>
# elif defined(sgi)
#	include <sys/endian.h>
# elif defined(__DJGPP__)
#	include <machine/endian.h>
# endif

#endif	/* endian includes */


#if defined(__BYTE_ORDER) && !defined(BYTE_ORDER)
#define	BYTE_ORDER	__BYTE_ORDER
#endif	/* __BYTE_ORDER */

#if !defined(PDP_ENDIAN)
#if defined(__PDP_ENDIAN)
#define	PDP_ENDIAN	__PDP_ENDIAN
#else
#define	PDP_ENDIAN	3412
#endif
#endif	/* NUXI endian (not supported) */

#if defined(__LITTLE_ENDIAN) && !defined(LITTLE_ENDIAN)
#define	LITTLE_ENDIAN	__LITTLE_ENDIAN
#endif	/* __LITTLE_ENDIAN */

#if defined(__BIG_ENDIAN) && !defined(BIG_ENDIAN)
#define	BIG_ENDIAN	__BIG_ENDIAN
#endif	/* __LITTLE_ENDIAN */


#if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)

# if (BYTE_ORDER != LITTLE_ENDIAN) && (BYTE_ORDER != BIG_ENDIAN)
# error "Unsupported endianness."
# endif

#else	/* one of the definitions is mising. */

# undef BYTE_ORDER
# undef LITTLE_ENDIAN
# undef BIG_ENDIAN
# undef PDP_ENDIAN
# define LITTLE_ENDIAN	1234
# define BIG_ENDIAN	4321
# define PDP_ENDIAN	3412

#endif	/* byte order defs */


#if !defined(BYTE_ORDER)
/* supposedly safe assumptions: these may actually
 * be OS dependant and listing all possible compiler
 * macros here is impossible (the ones here are gcc
 * flags, mostly.) so, proceed carefully..
 */

# if defined(__DJGPP__) || defined(MSDOS) || defined(__MSDOS__)
#	define	BYTE_ORDER	LITTLE_ENDIAN	/* DOS */

# elif defined(__sun) || defined(__svr4__)	/* solaris */
#   if defined(_LITTLE_ENDIAN)	/* x86 */
#	define	BYTE_ORDER	LITTLE_ENDIAN
#   elif defined(_BIG_ENDIAN)	/* sparc */
#	define	BYTE_ORDER	BIG_ENDIAN
#   endif

# elif defined(__i386) || defined(__i386__) || defined(__386__) || defined(_M_IX86)
#	define	BYTE_ORDER	LITTLE_ENDIAN	/* any x86 */

# elif defined(__amd64) || defined(__x86_64__) || defined(_M_X64)
#	define	BYTE_ORDER	LITTLE_ENDIAN	/* any x64 */

# elif defined(_M_IA64)
#	define	BYTE_ORDER	LITTLE_ENDIAN	/* ia64 / Visual C */

# elif defined (__ppc__) || defined(__POWERPC__) || defined(_M_PPC)
#	define	BYTE_ORDER	BIG_ENDIAN	/* PPC: big endian */

# elif (defined(__alpha__) || defined(__alpha)) || defined(_M_ALPHA)
#	define	BYTE_ORDER	LITTLE_ENDIAN	/* should be safe */

# elif defined(_WIN32) || defined(_WIN64)	/* windows : */
#	define	BYTE_ORDER	LITTLE_ENDIAN	/* should be safe */

# elif defined(__hppa) || defined(__hppa__) || defined(__sparc) || defined(__sparc__)	/* others: check! */
#	define	BYTE_ORDER	BIG_ENDIAN

# endif

# if defined(BYTE_ORDER)
  /* raise a flag, just in case: */
#	define	ENDIAN_GUESSED_SAFE	BYTE_ORDER
# endif

#endif	/* supposedly safe assumptions */


#if !defined(BYTE_ORDER)

/* brain-dead fallback: default to little endian.
 * change if necessary!!!!
 */
# define BYTE_ORDER	LITTLE_ENDIAN
# define ENDIAN_ASSUMED_UNSAFE		BYTE_ORDER

#endif	/* fallback. */

#if defined(ENDIAN_ASSUMED_UNSAFE)
/*
# if (ENDIAN_ASSUMED_UNSAFE == LITTLE_ENDIAN)
#    warning "Cannot determine endianess. Using LIL endian as an UNSAFE default"
# elif (ENDIAN_ASSUMED_UNSAFE == PDP_ENDIAN)
#    warning "Cannot determine endianess. Using PDP (NUXI) as an UNSAFE default."
# elif (ENDIAN_ASSUMED_UNSAFE == BIG_ENDIAN)
#    warning "Cannot determine endianess. Using BIG endian as an UNSAFE default."
# endif
*/
#endif	/* ENDIAN_ASSUMED_UNSAFE */

#define	COMPILED_BYTEORDER	BYTE_ORDER

#include <stdio.h>
#include <stdlib.h>

int DetectByteorder (void)
{
	int	i = 0x12345678;
		/*    U N I X */

	/*
	BE_ORDER:  12 34 56 78
		   U  N  I  X

	LE_ORDER:  78 56 34 12
		   X  I  N  U

	PDP_ORDER: 34 12 78 56
		   N  U  X  I
	*/

	if ( *(char *)&i == 0x12 )
		return BIG_ENDIAN;
	else if ( *(char *)&i == 0x78 )
		return LITTLE_ENDIAN;
	else if ( *(char *)&i == 0x34 )
		return PDP_ENDIAN;

	return -1;
}

struct align_test_char		{ char dummy; char test; };
struct align_test_short		{ char dummy; short test; };
struct align_test_int		{ char dummy; int test; };
struct align_test_long		{ char dummy; long test; };
struct align_test_longlong	{ char dummy; long long test; };
struct align_test_float		{ char dummy; float test; };
struct align_test_double	{ char dummy; double test; };
struct align_test_longdouble	{ char dummy; long double test;};
struct align_test_voidptr	{ char dummy; void *test; };

int main (void)
{
	int	tmp = ((char) -1);

	printf ("char is signed type : %s - char is %s\n", (tmp < 0) ? "yes" : "no", (tmp < 0) ? "SIGNED" : "UNSIGNED");
	printf ("Type sizes and alignment within structures:\n");
	printf ("char       : %d, packing offset: %d\n", (int) sizeof(char),	 (int) ((size_t) &((struct align_test_char *)0)->test));
	printf ("short      : %d, packing offset: %d\n", (int) sizeof(short),	 (int) ((size_t) &((struct align_test_short *)0)->test));
	printf ("int        : %d, packing offset: %d\n", (int) sizeof(int),	 (int) ((size_t) &((struct align_test_int *)0)->test));
	printf ("long       : %d, packing offset: %d\n", (int) sizeof(long),	 (int) ((size_t) &((struct align_test_long *)0)->test));
	printf ("long long  : %d, packing offset: %d\n", (int) sizeof(long long),(int) ((size_t) &((struct align_test_longlong *)0)->test));
	printf ("void *ptr  : %d, packing offset: %d\n", (int) sizeof(void *),	 (int) ((size_t) &((struct align_test_voidptr *)0)->test));
	printf ("float      : %d, packing offset: %d\n", (int) sizeof(float),	 (int) ((size_t) &((struct align_test_float *)0)->test));
	printf ("double     : %d, packing offset: %d\n", (int) sizeof(double),	 (int) ((size_t) &((struct align_test_double *)0)->test));
	printf ("long double: %d, packing offset: %d\n", (int) sizeof(long double),(int)((size_t)&((struct align_test_longdouble *)0)->test));

	printf ("ENDIANNESS (BYTE ORDER):\n");

	tmp = DetectByteorder();
	printf ("Runtime detection     : ");
	switch (tmp)
	{
	case BIG_ENDIAN:
		printf ("Big Endian");
		break;
	case LITTLE_ENDIAN:
		printf ("Little Endian");
		break;
	case PDP_ENDIAN:
		printf ("PDP (NUXI) Endian");
		break;
	default:
		printf ("Unknown Endian");
		break;
	}
	printf ("\n");

	tmp = COMPILED_BYTEORDER;
	printf ("Compile time detection: ");
	switch (tmp)
	{
	case BIG_ENDIAN:
		printf ("Big Endian");
		break;
	case LITTLE_ENDIAN:
		printf ("Little Endian");
		break;
	case PDP_ENDIAN:
		printf ("PDP (NUXI) Endian");
		break;
	default:
		printf ("Unknown Endian");
		break;
	}
#if defined(ENDIAN_GUESSED_SAFE)
	printf (" (Safe guess)");
#elif defined(ENDIAN_ASSUMED_UNSAFE)
	printf (" (Unsafe assumption)");
#endif
	printf ("\n");

	return 0;
}