| 1 | /* |
| 2 | * Copyright (C) 2000 Ross Combs ([email protected]) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | /*****/ |
| 21 | #ifndef JUST_NEED_TYPES |
| 22 | #ifndef INCLUDED_INTROTATE_PROTOS |
| 23 | #define INCLUDED_INTROTATE_PROTOS |
| 24 | |
| 25 | /* |
| 26 | * ROTL(x,n,w) rotates "w" bit wide value "x" by "n" bits to the left |
| 27 | * |
| 28 | * The expression passed in as x must have a type at least as wide as w. |
| 29 | * The type should probably be unsigned for this to be guaranteed to work |
| 30 | * properly. If n or w is signed and larger than x remember that the |
| 31 | * promotion rules would promote x to be of the same (signed) type. |
| 32 | * |
| 33 | * Unfortunately C doesn't have rotate operations and they can be difficult |
| 34 | * to implement when handling rotates by zero, negative numbers, or numbers |
| 35 | * greater or equal to the bit width of the number. This is because ANSI/ISO |
| 36 | * C makes weak guarantees about the left and right shift operators. We |
| 37 | * would like to not depend on word size, endianness, or how negative |
| 38 | * integers are represented. Unfortunately only some of those goals may be |
| 39 | * achieved. As for optimization, a really good compiler might be able to |
| 40 | * recognize what we are doing and turn it into a single machine instruction. |
| 41 | */ |
| 42 | |
| 43 | /* valid for 0<n<w and w>0 */ |
| 44 | /*#define ROTL(x,n,w) (((x)<<(n)) | ((x)>>((w)-(n))))*/ |
| 45 | |
| 46 | /* valid for 0<=n<w and w>0 */ |
| 47 | /*#define ROTL(x,n,w) (((x)<<(n)) | ((x)>>(((-(n))&(w-1)))))*/ |
| 48 | |
| 49 | /* valid for 0<=n and w>0 , depends on 2's complement */ |
| 50 | #define ROTL(x,n,w) (((x)<<((n)&(w-1))) | ((x)>>(((-(n))&(w-1))))) |
| 51 | |
| 52 | /* valid for 0<=n and w>0 , uses three mods and an ugly conditional */ |
| 53 | /* FIXME: and also a bug because it doesn't work on PPC */ |
| 54 | /*#define ROTL(x,n,w) (((n)%(w)) ? (((x)<<((n)%(w))) | ((x)>>((w)-((n)%(w))))) : (x))*/ |
| 55 | |
| 56 | #define ROTL32(x,n) ROTL(x,n,32) |
| 57 | #define ROTL16(x,n) ROTL(x,n,16) |
| 58 | |
| 59 | #endif |
| 60 | #endif |