updates.
[silc.git] / includes / bitmove.h
1 /*
2
3   bitmove.h 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2001 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #ifndef BITMOVE_H
21 #define BITMOVE_H
22
23 #define GET_WORD(cp) ((uint32)(uint8)(cp)[0]) << 24     \
24                     | ((uint32)(uint8)(cp)[1] << 16)    \
25                     | ((uint32)(uint8)(cp)[2] << 8)     \
26                     | ((uint32)(uint8)(cp)[3])
27
28 /* Returns eight 8-bit bytes, most significant bytes first. */
29 #define SILC_GET64_MSB(l, cp)                   \
30        (l) = ((((uint64)GET_WORD((cp))) << 32) |        \
31               ((uint64)GET_WORD((cp) + 4)))
32 #define SILC_PUT64_MSB(l, cp)                           \
33 do {                                                    \
34   SILC_PUT32_MSB((uint32)((uint64)(l) >> 32), (cp));    \
35   SILC_PUT32_MSB((uint32)(l), (cp) + 4);                \
36 } while(0)
37
38
39 /* Returns four 8-bit bytes, most significant bytes first. */
40 #define SILC_GET32_MSB(l, cp)                   \
41         (l) = ((uint32)(uint8)(cp)[0]) << 24    \
42             | ((uint32)(uint8)(cp)[1] << 16)    \
43             | ((uint32)(uint8)(cp)[2] << 8)     \
44             | ((uint32)(uint8)(cp)[3])
45 #define SILC_PUT32_MSB(l, cp)                   \
46         (cp)[0] = l >> 24;                      \
47         (cp)[1] = l >> 16;                      \
48         (cp)[2] = l >> 8;                       \
49         (cp)[3] = l;
50
51
52 /* Returns four 8-bit bytes, less significant bytes first. */
53 #define SILC_GET32_LSB(l, cp)                   \
54         (l) = ((uint32)(uint8)(cp)[0])          \
55             | ((uint32)(uint8)(cp)[1] << 8)     \
56             | ((uint32)(uint8)(cp)[2] << 16)    \
57             | ((uint32)(uint8)(cp)[3] << 24)
58 /* same as upper but XOR the result always */
59 #define SILC_GET32_X_LSB(l, cp)                 \
60         (l) ^= ((uint32)(uint8)(cp)[0])         \
61             | ((uint32)(uint8)(cp)[1] << 8)     \
62             | ((uint32)(uint8)(cp)[2] << 16)    \
63             | ((uint32)(uint8)(cp)[3] << 24)
64 #define SILC_PUT32_LSB(l, cp)                   \
65         (cp)[0] = l;                            \
66         (cp)[1] = l >> 8;                       \
67         (cp)[2] = l >> 16;                      \
68         (cp)[3] = l >> 24;
69
70
71 /* Returns two 8-bit bytes, most significant bytes first. */
72 #define SILC_GET16_MSB(l, cp)                   \
73         (l) = ((uint32)(uint8)(cp)[0] << 8)     \
74             | ((uint32)(uint8)(cp)[1])
75 #define SILC_PUT16_MSB(l, cp)                   \
76         (cp)[0] = l >> 8;                       \
77         (cp)[1] = l;
78
79 /* Returns two 8-bit bytes, less significant bytes first. */
80 #define SILC_GET16_LSB(l, cp)                   \
81         (l) = ((uint32)(uint8)(cp)[0])          \
82             | ((uint32)(uint8)(cp)[1] << 8)
83 #define SILC_PUT16_LSB(l, cp)                   \
84         (cp)[0] = l;                            \
85         (cp)[1] = l >> 8;
86
87 #endif