1 |
/* Copyright (C) 1998,1999 Nikos Mavroyanopoulos |
2 |
* This library is free software; you can redistribute it and/or |
3 |
* modify it under the terms of the GNU Library General Public |
4 |
* License as published by the Free Software Foundation; either |
5 |
* version 2 of the License, or (at your option) any later version. |
6 |
* |
7 |
* This library is distributed in the hope that it will be useful, |
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
10 |
* Library General Public License for more details. |
11 |
* |
12 |
* You should have received a copy of the GNU Library General Public |
13 |
* License along with this library; if not, write to the |
14 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
15 |
* Boston, MA 02111-1307, USA. |
16 |
*/ |
17 |
|
18 |
/* $Id: swap.c,v 1.1.1.1 1999/10/15 22:49:23 nmav Exp $ */ |
19 |
|
20 |
#ifndef LIBDEFS_H |
21 |
# define LIBDEFS_H |
22 |
# include <libdefs.h> |
23 |
#endif |
24 |
|
25 |
#include <bzero.h> |
26 |
#include <swap.h> |
27 |
#include <xmemory.h> |
28 |
|
29 |
#ifndef FAST |
30 |
|
31 |
word32 rotl(word32 v, word32 cnt) |
32 |
{ |
33 |
cnt &= (32 - 1); |
34 |
while (cnt--) |
35 |
v = ((v << 1) | (v >> (32 - 1))); |
36 |
return v; |
37 |
} |
38 |
/* rotate right */ |
39 |
word32 rotr(word32 v, word32 cnt) |
40 |
{ |
41 |
cnt &= (32 - 1); |
42 |
while (cnt--) |
43 |
v = ((v >> 1) | (v << (32 - 1))); |
44 |
return v; |
45 |
} |
46 |
|
47 |
word16 rotl16(word16 v, word16 cnt) |
48 |
{ |
49 |
cnt &= (16 - 1); |
50 |
while (cnt--) |
51 |
v = ((v << 1) | (v >> (16 - 1))); |
52 |
return v; |
53 |
} |
54 |
/* rotate right */ |
55 |
word16 rotr16(word16 v, word16 cnt) |
56 |
{ |
57 |
cnt &= (16 - 1); |
58 |
while (cnt--) |
59 |
v = ((v >> 1) | (v << (16 - 1))); |
60 |
return v; |
61 |
} |
62 |
|
63 |
#endif |
64 |
|
65 |
#ifndef FAST |
66 |
/* Byte swap a 32bit integer */ |
67 |
word32 byteswap(word32 x) |
68 |
{ |
69 |
return ((rotl(x, 8) & 0x00ff00ff) | (rotr(x, 8) & 0xff00ff00)); |
70 |
} |
71 |
|
72 |
|
73 |
#endif |
74 |
|
75 |
/* Byte swap a 16bit integer */ |
76 |
word16 byteswap_16(word16 x) |
77 |
{ |
78 |
return ( (rotl16(x, 8) & 0x00ff) | (rotr16(x, 8) & 0xff00) ); |
79 |
} |