2
* @brief Functions to handle endian specifics
5
* Copyright (C) 2005-2011 Christopher C. Hulbert
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions are met:
12
* 1. Redistributions of source code must retain the above copyright notice,
13
* this list of conditions and the following disclaimer.
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.
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.
32
#include "matio_private.h"
34
/** @brief swap the bytes @c a and @c b
35
* @ingroup mat_internal
37
#define swap(a,b) a^=b;b^=a;a^=b
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
46
Mat_int64Swap( mat_int64_t *a )
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] );
66
#endif /* HAVE_MAT_INT64_T */
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
75
Mat_uint64Swap( mat_uint64_t *a )
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] );
95
#endif /* HAVE_MAT_UINT64_T */
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
103
Mat_int32Swap( mat_int32_t *a )
113
swap( tmp.i1[0], tmp.i1[3] );
114
swap( tmp.i1[1], tmp.i1[2] );
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
128
Mat_uint32Swap( mat_uint32_t *a )
138
swap( tmp.i1[0], tmp.i1[3] );
139
swap( tmp.i1[1], tmp.i1[2] );
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
153
Mat_int16Swap( mat_int16_t *a )
163
swap( tmp.i1[0], tmp.i1[1] );
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
176
Mat_uint16Swap( mat_uint16_t *a )
186
swap( tmp.i1[0], tmp.i1[1] );
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
199
Mat_floatSwap( float *a )
209
swap( tmp.i1[0], tmp.i1[3] );
210
swap( tmp.i1[1], tmp.i1[2] );
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
223
Mat_doubleSwap( double *a )
225
#ifndef SIZEOF_DOUBLE
226
#define SIZEOF_DOUBLE 8
230
char a[SIZEOF_DOUBLE];
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] );