added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / common / byteorder.h
1 /*
2 Copyright (C) 2004 John Tsiombikas <nuclear@siggraph.org>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19 /* byte order macros.
20  * author: John Tsiombikas 2004
21  * modified: John Tsiombikas 2006
22  */
23
24 #ifndef _BYTEORDER_H_
25 #define _BYTEORDER_H_
26
27 #include <stdio.h>
28 #include "types.h"
29
30 /*The byte order determination procedure is derived from SDL's SDL_byteorder.h
31  * SDL is free software (LGPL), copyright: Sam Lantinga
32  */
33
34 #if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
35
36 #if  defined(__i386__) || defined(__ia64__) || defined(WIN32) || \
37     (defined(__alpha__) || defined(__alpha)) || \
38      defined(__arm__) || \
39     (defined(__mips__) && defined(__MIPSEL__)) || \
40      defined(__SYMBIAN32__) || \
41      defined(__x86_64__) || \
42      defined(__LITTLE_ENDIAN__)
43         
44 /* little endian */
45 #define LITTLE_ENDIAN
46
47 #else
48 /* big endian */        
49 #define BIG_ENDIAN
50
51 #endif  /* endian check */
52 #endif  /* !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) */
53
54 #ifdef LITTLE_ENDIAN
55 /* little endian */
56 #define read_int8_le(f)         read_int8(f)
57 #define read_int8_be(f)         read_int8(f)
58 #define read_int16_le(f)        read_int16(f)
59 #define read_int16_be(f)        read_int16_inv(f)
60 #define read_int32_le(f)        read_int32(f)
61 #define read_int32_be(f)        read_int32_inv(f)
62 #define read_float_le(f)        read_float(f)
63 #define read_float_be(f)        read_float_inv(f)
64         
65 #define write_int8_le(f, v)     write_int8(f, v)
66 #define write_int8_be(f, v)     write_int8(f, v)
67 #define write_int16_le(f, v)    write_int16(f, v)
68 #define write_int16_be(f, v)    write_int16_inv(f, v)
69 #define write_int32_le(f, v)    write_int32(f, v)
70 #define write_int32_be(f, v)    write_int32_inv(f, v)
71 #define write_float_le(f, v)    write_float(f, v)
72 #define write_float_be(f, v)    write_float_inv(f, v)
73
74 #else
75
76 /* big endian */
77 #define read_int8_be(f)         read_int8(f)
78 #define read_int8_le(f)         read_int8(f)
79 #define read_int16_be(f)        read_int16(f)
80 #define read_int16_le(f)        read_int16_inv(f)
81 #define read_int32_be(f)        read_int32(f)
82 #define read_int32_le(f)        read_int32_inv(f)
83 #define read_float_be(f)        read_float(f)
84 #define read_float_le(f)        read_float_inv(f)
85         
86 #define write_int8_be(f, v)     write_int8(f, v)
87 #define write_int8_le(f, v)     write_int8(f, v)
88 #define write_int16_be(f, v)    write_int16(f, v)
89 #define write_int16_le(f, v)    write_int16_inv(f, v)
90 #define write_int32_be(f, v)    write_int32(f, v)
91 #define write_int32_le(f, v)    write_int32_inv(f, v)
92 #define write_float_be(f, v)    write_float(f, v)
93 #define write_float_le(f, v)    write_float_inv(f, v)
94
95 #endif  /* LITTLE_ENDIAN */
96
97 int8_t read_int8(FILE *fp);
98 int16_t read_int16(FILE *fp);
99 int16_t read_int16_inv(FILE *fp);
100 int32_t read_int32(FILE *fp);
101 int32_t read_int32_inv(FILE *fp);
102 float read_float(FILE *fp);
103 float read_float_inv(FILE *fp);
104
105 void write_int8(FILE *fp, int8_t v);
106 void write_int16(FILE *fp, int16_t v);
107 void write_int16_inv(FILE *fp, int16_t v);
108 void write_int32(FILE *fp, int32_t v);
109 void write_int32_inv(FILE *fp, int32_t v);
110 void write_float(FILE *fp, float v);
111 void write_float_inv(FILE *fp, float v);
112
113 #endif  /* _BYTEORDER_H_ */