Debian Codegraph @

matio-1.5.30/src/endian.c

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