1 /***************************************************************************
4 * Functions for generalized, in-place byte swapping between LSBF and
7 * Some standard integer types are needed, namely uint8_t and
8 * uint32_t, (these are normally declared by including inttypes.h or
9 * stdint.h). Each function expects it's input to be a void pointer
10 * to a quantity of the appropriate size.
12 * There are two versions of most routines, one that works on
13 * quantities regardless of alignment (gswapX) and one that works on
14 * memory aligned quantities (gswapXa). The memory aligned versions
15 * (gswapXa) are much faster than the other versions (gswapX), but the
16 * memory *must* be aligned.
18 * Written by Chad Trabant,
19 * IRIS Data Management Center
22 ***************************************************************************/
24 #include "lmplatform.h"
26 /* Swap routines that work on any (aligned or not) quantities */
29 ms_gswap2 ( void *data2
)
38 memcpy( &dat
, data2
, 2 );
42 memcpy( data2
, &dat
, 2 );
47 ms_gswap3 ( void *data3
)
56 memcpy( &dat
, data3
, 3 );
60 memcpy( data3
, &dat
, 3 );
65 ms_gswap4 ( void *data4
)
73 memcpy( &dat
, data4
, 4 );
80 memcpy( data4
, &dat
, 4 );
85 ms_gswap8 ( void *data8
)
94 memcpy( &dat
, data8
, 8 );
110 memcpy( data8
, &dat
, 8 );
113 /* Swap routines that work on memory aligned quantities */
116 ms_gswap2a ( void *data2
)
118 uint16_t *data
= data2
;
120 *data
=(((*data
>>8)&0xff) | ((*data
&0xff)<<8));
125 ms_gswap4a ( void *data4
)
127 uint32_t *data
= data4
;
129 *data
=(((*data
>>24)&0xff) | ((*data
&0xff)<<24) |
130 ((*data
>>8)&0xff00) | ((*data
&0xff00)<<8));
135 ms_gswap8a ( void *data8
)
137 uint32_t *data4
= data8
;
141 h0
= (((h0
>>24)&0xff) | ((h0
&0xff)<<24) |
142 ((h0
>>8)&0xff00) | ((h0
&0xff00)<<8));
145 h1
= (((h1
>>24)&0xff) | ((h1
&0xff)<<24) |
146 ((h1
>>8)&0xff00) | ((h1
&0xff00)<<8));