added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / common / byteorder.c
1 /*
2 Copyright (C) 2005 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 #include <stdio.h>
20 #include "byteorder.h"
21
22 int8_t read_int8(FILE *fp) {
23         int8_t v;
24         fread(&v, 1, 1, fp);
25         return v;
26 }
27
28 int16_t read_int16(FILE *fp) {
29         int16_t v;
30         fread(&v, 2, 1, fp);
31         return v;
32 }
33
34 int16_t read_int16_inv(FILE *fp) {
35         int16_t v;
36         fread(&v, 2, 1, fp);
37         return v >> 8 | v << 8;
38 }
39
40 int32_t read_int32(FILE *fp) {
41         int32_t v;
42         fread(&v, 4, 1, fp);
43         return v;
44 }
45
46 int32_t read_int32_inv(FILE *fp) {
47         int32_t v;
48         fread(&v, 4, 1, fp);
49         return v >> 24 | (v & 0x00ff0000) >> 8 | (v & 0x0000ff00) << 8 | v << 24;
50 }
51
52 float read_float(FILE *fp) {
53         int32_t tmp = read_int32(fp);
54         return *((float*)&tmp);
55 }
56
57 float read_float_inv(FILE *fp) {
58         int32_t tmp = read_int32_inv(fp);
59         return *((float*)&tmp);
60 }
61
62 void write_int8(FILE *fp, int8_t v) {
63         fwrite(&v, 1, 1, fp);
64 }
65
66 void write_int16(FILE *fp, int16_t v) {
67         fwrite(&v, 2, 1, fp);
68 }
69
70 void write_int16_inv(FILE *fp, int16_t v) {
71         int16_t tmp = v >> 8 | v << 8;
72         fwrite(&tmp, 2, 1, fp);
73 }
74
75 void write_int32(FILE *fp, int32_t v) {
76         fwrite(&v, 4, 1, fp);
77 }
78
79 void write_int32_inv(FILE *fp, int32_t v) {
80         int32_t tmp = v >> 24 | (v & 0x00ff0000) >> 8 | (v & 0x0000ff00) << 8 | v << 24;
81         fwrite(&tmp, 4, 1, fp);
82 }
83
84 void write_float(FILE *fp, float v) {
85         write_int32(fp, *((int32_t*)&v));
86 }
87 void write_float_inv(FILE *fp, float v) {
88         write_int32_inv(fp, *((int32_t*)&v));
89 }