1
/* Copyright (c) 1996-2004, Adaptec Corporation
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* - Redistributions of source code must retain the above copyright notice, this
8
* list of conditions and the following disclaimer.
9
* - Redistributions in binary form must reproduce the above copyright notice,
10
* this list of conditions and the following disclaimer in the documentation
11
* and/or other materials provided with the distribution.
12
* - Neither the name of the Adaptec Corporation nor the names of its
13
* contributors may be used to endorse or promote products derived from this
14
* software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
* POSSIBILITY OF SUCH DAMAGE.
29
/*File - SWAP_EM.CPP */
30
/*****************************************************************************/
34
/* This files provides two functions to swap the bytes of two and */
35
/*four byte variables. These functions can be used to convert between */
36
/*Motorola and Intel byte ordering. */
39
/*Author: Doug Anderson */
47
/*****************************************************************************/
50
/*Include Files -------------------------------------------------------------*/
55
/*Function - swap2 - start */
56
/*===========================================================================*/
60
/* This function swaps the MSB & LSB of a two byte variable. */
66
/*Global Variables Affected: */
68
/*Remarks: (Side effects, Assumptions, Warnings...) */
71
/*---------------------------------------------------------------------------*/
73
uSHORT osdSwap2(uSHORT *inShort)
75
#if defined ( _DPT_BIG_ENDIAN )
77
# if (defined ( _DPT_STRICT_ALIGN ) )
80
i = (unsigned short)*((unsigned char *)inShort);
82
i |= (unsigned short)*(((unsigned char *)inShort)+1);
102
/*Function - swap4 - start */
103
/*===========================================================================*/
107
/* This function reverses the byte ordering of a four byte variable. */
113
/*Global Variables Affected: */
115
/*Remarks: (Side effects, Assumptions, Warnings...) */
118
/*---------------------------------------------------------------------------*/
120
uLONG osdSwap4(uLONG *inLong)
123
#if defined ( _DPT_BIG_ENDIAN )
125
# if (defined ( _DPT_STRICT_ALIGN ))
128
i = (unsigned long)*((unsigned char *)inLong) << 24;
129
i |= (unsigned long)*(((unsigned char *)inLong)+1) << 16;
130
i |= (unsigned long)*(((unsigned char *)inLong)+2) << 8;
131
i |= (unsigned long)*(((unsigned char *)inLong)+3);
141
*(uLONG *)src = *inLong;
142
dst = (char *)inLong;
143
for(i = 0; i < 4; ++i)
155
uLONG osdSwap3(uLONG *inLong)
158
#if defined ( _DPT_BIG_ENDIAN )
160
# if (defined ( _DPT_STRICT_ALIGN ))
163
i = (unsigned long)*(((unsigned char *)inLong)+0) << 16;
164
i |= (unsigned long)*(((unsigned char *)inLong)+1) << 8;
165
i |= (unsigned long)*(((unsigned char *)inLong)+2);
173
return((osdSwap4(inLong) >> 8));
179
/*Function - trueSwap2 - start */
180
/*===========================================================================*/
184
/* This function ALWAYS swaps the MSB & LSB of a two byte variable. */
190
/*Global Variables Affected: */
192
/*Remarks: (Side effects, Assumptions, Warnings...) */
195
/*---------------------------------------------------------------------------*/
197
uSHORT trueSwap2(uSHORT *inShort)
204
src = (char *)inShort;
205
dst = (char *)&outShort;
216
/* trueSwap2() - end */
219
/*Function - trueSwap4 - start */
220
/*===========================================================================*/
224
/* This function ALWAYS reverses the byte ordering of a four byte variable. */
230
/*Global Variables Affected: */
232
/*Remarks: (Side effects, Assumptions, Warnings...) */
235
/*---------------------------------------------------------------------------*/
237
uLONG trueSwap4(uLONG *inLong)
244
src = (char *)inLong;
245
dst = (char *)&outLong;
246
for(i = 0; i < 4; ++i)
251
for(i = 0; i < 4; ++i)
259
/* trueSwap4() - end */
261
//Function - netSwap4() - start
262
//===========================================================================
266
// This function ensures that the input four byte variable is returned
267
// in DPT network order (little-endian). On big-endian machines this
268
// function will swap the four byte variable. On little-endian machines
269
// this function will return the input value.
273
// val = 4 byte variable to be byte reversed.
277
// The variable in network order.
279
//Global Variables Affected:
281
//Remarks: (Side effects, Assumptions, Warnings...)
283
//---------------------------------------------------------------------------
284
uLONG netSwap4(uLONG val)
286
#if defined (_DPT_BIG_ENDIAN)
288
// we need to swap the value and return it
292
wd1 = (uSHORT) (val >> 16);
293
wd0 = (wd0 >> 8) | (wd0 << 8);
294
wd1 = (wd1 >> 8) | (wd1 << 8);
296
return (((uLONG)wd0 << 16) | wd1);
300
// just return the value for little-endian machines