Debian Codegraph @

matio-1.5.0/src/endian.c

raw
1 /** @file endian.c
2 * @brief Functions to handle endian specifics
3 */
4 /*
5 * Copyright (C) 2005-2011 Christopher C. Hulbert
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY CHRISTOPHER C. HULBERT ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL CHRISTOPHER C. HULBERT OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <stdlib.h>
32 #include "matio_private.h"
33
34 /** @brief swap the bytes @c a and @c b
35 * @ingroup mat_internal
36 */
37 #define swap(a,b) a^=b;b^=a;a^=b
38
39 #ifdef HAVE_MAT_INT64_T
40 /** @brief swap the bytes of a 64-bit signed integer
41 * @ingroup mat_internal
42 * @param a pointer to integer to swap
43 * @return the swapped integer
44 */
45 mat_int64_t
46 Mat_int64Swap( mat_int64_t *a )
47 {
48
49 union {
50 mat_int8_t i1[8];
51 mat_int64_t i8;
52 } tmp;
53
54 tmp.i8 = *a;
55
56 swap( tmp.i1[0], tmp.i1[7] );
57 swap( tmp.i1[1], tmp.i1[6] );
58 swap( tmp.i1[2], tmp.i1[5] );
59 swap( tmp.i1[3], tmp.i1[4] );
60
61 *a = tmp.i8;
62
63 return *a;
64
65 }
66 #endif /* HAVE_MAT_INT64_T */
67
68 #ifdef HAVE_MAT_UINT64_T
69 /** @brief swap the bytes of a 64-bit unsigned integer
70 * @ingroup mat_internal
71 * @param a pointer to integer to swap
72 * @return the swapped integer
73 */
74 mat_uint64_t
75 Mat_uint64Swap( mat_uint64_t *a )
76 {
77
78 union {
79 mat_uint8_t i1[8];
80 mat_uint64_t i8;
81 } tmp;
82
83 tmp.i8 = *a;
84
85 swap( tmp.i1[0], tmp.i1[7] );
86 swap( tmp.i1[1], tmp.i1[6] );
87 swap( tmp.i1[2], tmp.i1[5] );
88 swap( tmp.i1[3], tmp.i1[4] );
89
90 *a = tmp.i8;
91
92 return *a;
93
94 }
95 #endif /* HAVE_MAT_UINT64_T */
96
97 /** @brief swap the bytes of a 32-bit signed integer
98 * @ingroup mat_internal
99 * @param a pointer to integer to swap
100 * @return the swapped integer
101 */
102 mat_int32_t
103 Mat_int32Swap( mat_int32_t *a )
104 {
105
106 union {
107 mat_int8_t i1[4];
108 mat_int32_t i4;
109 } tmp;
110
111 tmp.i4 = *a;
112
113 swap( tmp.i1[0], tmp.i1[3] );
114 swap( tmp.i1[1], tmp.i1[2] );
115
116 *a = tmp.i4;
117
118 return *a;
119
120 }
121
122 /** @brief swap the bytes of a 32-bit unsigned integer
123 * @ingroup mat_internal
124 * @param a pointer to integer to swap
125 * @return the swapped integer
126 */
127 mat_uint32_t
128 Mat_uint32Swap( mat_uint32_t *a )
129 {
130
131 union {
132 mat_uint8_t i1[4];
133 mat_uint32_t i4;
134 } tmp;
135
136 tmp.i4 = *a;
137
138 swap( tmp.i1[0], tmp.i1[3] );
139 swap( tmp.i1[1], tmp.i1[2] );
140
141 *a = tmp.i4;
142
143 return *a;
144
145 }
146
147 /** @brief swap the bytes of a 16-bit signed integer
148 * @ingroup mat_internal
149 * @param a pointer to integer to swap
150 * @return the swapped integer
151 */
152 mat_int16_t
153 Mat_int16Swap( mat_int16_t *a )
154 {
155
156 union {
157 mat_int8_t i1[2];
158 mat_int16_t i2;
159 } tmp;
160
161 tmp.i2 = *a;
162
163 swap( tmp.i1[0], tmp.i1[1] );
164
165 *a = tmp.i2;
166 return *a;
167
168 }
169
170 /** @brief swap the bytes of a 16-bit unsigned integer
171 * @ingroup mat_internal
172 * @param a pointer to integer to swap
173 * @return the swapped integer
174 */
175 mat_uint16_t
176 Mat_uint16Swap( mat_uint16_t *a )
177 {
178
179 union {
180 mat_uint8_t i1[2];
181 mat_uint16_t i2;
182 } tmp;
183
184 tmp.i2 = *a;
185
186 swap( tmp.i1[0], tmp.i1[1] );
187
188 *a = tmp.i2;
189 return *a;
190
191 }
192
193 /** @brief swap the bytes of a 4 byte single-precision float
194 * @ingroup mat_internal
195 * @param a pointer to integer to swap
196 * @return the swapped integer
197 */
198 float
199 Mat_floatSwap( float *a )
200 {
201
202 union {
203 char i1[4];
204 float r4;
205 } tmp;
206
207 tmp.r4 = *a;
208
209 swap( tmp.i1[0], tmp.i1[3] );
210 swap( tmp.i1[1], tmp.i1[2] );
211
212 *a = tmp.r4;
213 return *a;
214
215 }
216
217 /** @brief swap the bytes of a 4 or 8 byte double-precision float
218 * @ingroup mat_internal
219 * @param a pointer to integer to swap
220 * @return the swapped integer
221 */
222 double
223 Mat_doubleSwap( double *a )
224 {
225 #ifndef SIZEOF_DOUBLE
226 #define SIZEOF_DOUBLE 8
227 #endif
228
229 union {
230 char a[SIZEOF_DOUBLE];
231 double b;
232 } tmp;
233
234 tmp.b = *a;
235
236 #if SIZEOF_DOUBLE == 4
237 swap( tmp.a[0], tmp.a[3] );
238 swap( tmp.a[1], tmp.a[2] );
239 #elif SIZEOF_DOUBLE == 8
240 swap( tmp.a[0], tmp.a[7] );
241 swap( tmp.a[1], tmp.a[6] );
242 swap( tmp.a[2], tmp.a[5] );
243 swap( tmp.a[3], tmp.a[4] );
244 #elif SIZEOF_DOUBLE == 16
245 swap( tmp.a[0], tmp.a[15] );
246 swap( tmp.a[1], tmp.a[14] );
247 swap( tmp.a[2], tmp.a[13] );
248 swap( tmp.a[3], tmp.a[12] );
249 swap( tmp.a[4], tmp.a[11] );
250 swap( tmp.a[5], tmp.a[10] );
251 swap( tmp.a[6], tmp.a[9] );
252 swap( tmp.a[7], tmp.a[8] );
253 #endif
254 *a = tmp.b;
255 return *a;
256
257 }