~ubuntu-branches/ubuntu/intrepid/raidutils/intrepid

« back to all changes in this revision

Viewing changes to raideng/swap_em.c

  • Committer: Bazaar Package Importer
  • Author(s): Barak Pearlmutter
  • Date: 2004-05-18 11:33:42 UTC
  • Revision ID: james.westby@ubuntu.com-20040518113342-tyqavmso5q351xi2
Tags: upstream-0.0.4
ImportĀ upstreamĀ versionĀ 0.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 1996-2004, Adaptec Corporation
 
2
 * All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
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.
 
15
 *
 
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.
 
27
 */
 
28
 
 
29
/*File - SWAP_EM.CPP                                                         */
 
30
/*****************************************************************************/
 
31
/*                                                                           */
 
32
/*Description:                                                               */
 
33
/*                                                                           */
 
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.                                          */
 
37
/*                                                                           */
 
38
/*                                                                           */
 
39
/*Author:     Doug Anderson                                                  */
 
40
/*Date:          9/16/93                                                     */
 
41
/*                                                                           */
 
42
/*Editors:                                                                   */
 
43
/*                                                                           */
 
44
/*Remarks:                                                                   */
 
45
/*                                                                           */
 
46
/*                                                                           */
 
47
/*****************************************************************************/
 
48
 
 
49
 
 
50
/*Include Files -------------------------------------------------------------*/
 
51
 
 
52
#include     <osd_util.h>
 
53
 
 
54
 
 
55
/*Function - swap2 - start                                                   */
 
56
/*===========================================================================*/
 
57
/*                                                                           */
 
58
/*Description:                                                               */
 
59
/*                                                                           */
 
60
/*    This function swaps the MSB & LSB of a two byte variable.              */
 
61
/*                                                                           */
 
62
/*Parameters:                                                                */
 
63
/*                                                                           */
 
64
/*Return Value:                                                              */
 
65
/*                                                                           */
 
66
/*Global Variables Affected:                                                 */
 
67
/*                                                                           */
 
68
/*Remarks: (Side effects, Assumptions, Warnings...)                          */
 
69
/*                                                                           */
 
70
/*                                                                           */
 
71
/*---------------------------------------------------------------------------*/
 
72
 
 
73
uSHORT     osdSwap2(uSHORT *inShort)
 
74
{
 
75
#if defined ( _DPT_BIG_ENDIAN )
 
76
 
 
77
#   if (defined ( _DPT_STRICT_ALIGN ) )
 
78
        uSHORT i;
 
79
 
 
80
        i = (unsigned short)*((unsigned char *)inShort);
 
81
        i <<= 8;
 
82
        i |= (unsigned short)*(((unsigned char *)inShort)+1);
 
83
        return (i);
 
84
#   else
 
85
        return(*inShort);
 
86
#   endif
 
87
 
 
88
 
 
89
#else
 
90
  uSHORT i;
 
91
 
 
92
  i = *inShort & 0x0ff;
 
93
  *inShort >>= 8;
 
94
  *inShort |= (i << 8);
 
95
  return(*inShort);
 
96
 
 
97
#endif
 
98
}
 
99
/*swap2() - end                                                              */
 
100
 
 
101
 
 
102
/*Function - swap4 - start                                                   */
 
103
/*===========================================================================*/
 
104
/*                                                                           */
 
105
/*Description:                                                               */
 
106
/*                                                                           */
 
107
/*    This function reverses the byte ordering of a four byte variable.      */
 
108
/*                                                                           */
 
109
/*Parameters:                                                                */
 
110
/*                                                                           */
 
111
/*Return Value:                                                              */
 
112
/*                                                                           */
 
113
/*Global Variables Affected:                                                 */
 
114
/*                                                                           */
 
115
/*Remarks: (Side effects, Assumptions, Warnings...)                          */
 
116
/*                                                                           */
 
117
/*                                                                           */
 
118
/*---------------------------------------------------------------------------*/
 
119
 
 
120
uLONG     osdSwap4(uLONG *inLong)
 
121
{
 
122
 
 
123
#if defined ( _DPT_BIG_ENDIAN )
 
124
 
 
125
#   if (defined ( _DPT_STRICT_ALIGN ))
 
126
        uLONG i;
 
127
 
 
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);
 
132
        return (i);
 
133
#   else
 
134
        return(*inLong);
 
135
#   endif
 
136
 
 
137
#else
 
138
  char src[4], *dst;
 
139
  int i;
 
140
 
 
141
  *(uLONG *)src =  *inLong;
 
142
  dst = (char *)inLong;
 
143
  for(i = 0; i < 4; ++i)
 
144
   {
 
145
     dst[i] = src[3 - i];
 
146
   }
 
147
  return(*inLong);
 
148
 
 
149
#endif
 
150
}
 
151
 
 
152
 
 
153
/*swap4() - end                                                              */
 
154
 
 
155
uLONG   osdSwap3(uLONG *inLong)
 
156
{
 
157
 
 
158
#if defined ( _DPT_BIG_ENDIAN )
 
159
 
 
160
#   if (defined ( _DPT_STRICT_ALIGN ))
 
161
        uLONG i;
 
162
 
 
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);
 
166
        return (i);
 
167
#   else
 
168
        return(*inLong);
 
169
#   endif
 
170
 
 
171
#else
 
172
 
 
173
  return((osdSwap4(inLong) >> 8));
 
174
 
 
175
#endif
 
176
}
 
177
 
 
178
 
 
179
/*Function - trueSwap2 - start                                               */
 
180
/*===========================================================================*/
 
181
/*                                                                           */
 
182
/*Description:                                                               */
 
183
/*                                                                           */
 
184
/*    This function ALWAYS swaps the MSB & LSB of a two byte variable.       */
 
185
/*                                                                           */
 
186
/*Parameters:                                                                */
 
187
/*                                                                           */
 
188
/*Return Value:                                                              */
 
189
/*                                                                           */
 
190
/*Global Variables Affected:                                                 */
 
191
/*                                                                           */
 
192
/*Remarks: (Side effects, Assumptions, Warnings...)                          */
 
193
/*                                                                           */
 
194
/*                                                                           */
 
195
/*---------------------------------------------------------------------------*/
 
196
 
 
197
uSHORT     trueSwap2(uSHORT *inShort)
 
198
{
 
199
 
 
200
  uSHORT outShort;
 
201
  char *src, *dst;
 
202
  int i;
 
203
 
 
204
  src = (char *)inShort;
 
205
  dst = (char *)&outShort;
 
206
 
 
207
  dst[0] = src[1];
 
208
  dst[1] = src[0];
 
209
 
 
210
  src[0] = dst[0];
 
211
  src[1] = dst[1];
 
212
 
 
213
  return outShort;
 
214
 
 
215
}
 
216
/* trueSwap2() - end                                                         */
 
217
 
 
218
 
 
219
/*Function - trueSwap4 - start                                               */
 
220
/*===========================================================================*/
 
221
/*                                                                           */
 
222
/*Description:                                                               */
 
223
/*                                                                           */
 
224
/* This function ALWAYS reverses the byte ordering of a four byte variable.  */
 
225
/*                                                                           */
 
226
/*Parameters:                                                                */
 
227
/*                                                                           */
 
228
/*Return Value:                                                              */
 
229
/*                                                                           */
 
230
/*Global Variables Affected:                                                 */
 
231
/*                                                                           */
 
232
/*Remarks: (Side effects, Assumptions, Warnings...)                          */
 
233
/*                                                                           */
 
234
/*                                                                           */
 
235
/*---------------------------------------------------------------------------*/
 
236
 
 
237
uLONG     trueSwap4(uLONG *inLong)
 
238
{
 
239
 
 
240
  uLONG outLong;
 
241
  char *src, *dst;
 
242
  int i;
 
243
 
 
244
  src = (char *)inLong;
 
245
  dst = (char *)&outLong;
 
246
  for(i = 0; i < 4; ++i)
 
247
   {
 
248
     dst[i] = src[3 - i];
 
249
   }
 
250
 
 
251
  for(i = 0; i < 4; ++i)
 
252
   {
 
253
     src[i] = dst[i];
 
254
   }
 
255
 
 
256
  return outLong;
 
257
 
 
258
}
 
259
/* trueSwap4() - end                                                        */
 
260
 
 
261
//Function - netSwap4() - start
 
262
//===========================================================================
 
263
//
 
264
//Description:
 
265
//
 
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.
 
270
//
 
271
//Parameters:
 
272
//
 
273
//   val = 4 byte variable to be byte reversed.
 
274
//
 
275
//Return Value:
 
276
//
 
277
//   The variable in network order.
 
278
//
 
279
//Global Variables Affected:
 
280
//
 
281
//Remarks: (Side effects, Assumptions, Warnings...)
 
282
//
 
283
//---------------------------------------------------------------------------
 
284
uLONG netSwap4(uLONG val)
 
285
{
 
286
#if defined (_DPT_BIG_ENDIAN)
 
287
 
 
288
   // we need to swap the value and return it
 
289
   uSHORT  wd0, wd1;
 
290
 
 
291
   wd0 = (uSHORT) val;
 
292
   wd1 = (uSHORT) (val >> 16);
 
293
   wd0 = (wd0 >> 8) | (wd0 << 8);
 
294
   wd1 = (wd1 >> 8) | (wd1 << 8);
 
295
 
 
296
   return (((uLONG)wd0 << 16) | wd1);
 
297
 
 
298
#else
 
299
 
 
300
   // just return the value for little-endian machines
 
301
   return val;
 
302
 
 
303
#endif  // big_endian
 
304
}
 
305
//netSwap4() - end
 
306