BeeCrypt  4.2.1
endianness.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1998, 1999, 2000, 2001, 2004 X-Way Rights BV
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  */
19 
20 #ifndef _ENDIANNESS_H
21 #define _ENDIANNESS_H
22 
23 #include "beecrypt/beecrypt.h"
24 
25 #if defined(__cplusplus) || HAVE_INLINE
26 
27 static inline int16_t _swap16(int16_t n)
28 {
29  return ( ((n & 0xff) << 8) |
30  ((n & 0xff00) >> 8) );
31 }
32 # define swap16(n) _swap16(n)
33 
34 static inline uint16_t _swapu16(uint16_t n)
35 {
36  return ( ((n & 0xffU) << 8) |
37  ((n & 0xff00U) >> 8) );
38 }
39 # define swapu16(n) _swap16(n)
40 
41 # ifdef __arch__swab32
42 # define swap32(n) __arch__swab32(n)
43 # define swapu32(n) __arch__swab32(n)
44 # else
45 
46 static inline int32_t _swap32(int32_t n)
47 {
48  return ( ((n & 0xff) << 24) |
49  ((n & 0xff00) << 8) |
50  ((n & 0xff0000) >> 8) |
51  ((n & 0xff000000) >> 24) );
52 }
53 # define swap32(n) _swap32(n)
54 
55 static inline uint32_t _swapu32(uint32_t n)
56 {
57  return ( ((n & 0xffU) << 24) |
58  ((n & 0xff00U) << 8) |
59  ((n & 0xff0000U) >> 8) |
60  ((n & 0xff000000U) >> 24) );
61 }
62 # define swapu32(n) _swapu32(n)
63 
64 # endif
65 
66 # ifdef __arch__swab64
67 # define swap64(n) __arch__swab64(n)
68 # define swapu64(n) __arch__swab64(n)
69 # else
70 
71 static inline int64_t _swap64(int64_t n)
72 {
73  return ( ((n & ((int64_t) 0xff) ) << 56) |
74  ((n & ((int64_t) 0xff) << 8) << 40) |
75  ((n & ((int64_t) 0xff) << 16) << 24) |
76  ((n & ((int64_t) 0xff) << 24) << 8) |
77  ((n & ((int64_t) 0xff) << 32) >> 8) |
78  ((n & ((int64_t) 0xff) << 40) >> 24) |
79  ((n & ((int64_t) 0xff) << 48) >> 40) |
80  ((n & ((int64_t) 0xff) << 56) >> 56) );
81 }
82 # define swap64(n) _swap64(n)
83 
84 static inline uint64_t _swapu64(uint64_t n)
85 {
86  return ( ((n & ((uint64_t) 0xff) ) << 56) |
87  ((n & ((uint64_t) 0xff) << 8) << 40) |
88  ((n & ((uint64_t) 0xff) << 16) << 24) |
89  ((n & ((uint64_t) 0xff) << 24) << 8) |
90  ((n & ((uint64_t) 0xff) << 32) >> 8) |
91  ((n & ((uint64_t) 0xff) << 40) >> 24) |
92  ((n & ((uint64_t) 0xff) << 48) >> 40) |
93  ((n & ((uint64_t) 0xff) << 56) >> 56) );
94 }
95 # define swapu64(n) _swapu64(n)
96 
97 # endif
98 
99 #else
101  int16_t swap16 (int16_t);
103 uint16_t swapu16(uint16_t);
105  int32_t swap32 (int32_t);
107 uint32_t swapu32(uint32_t);
109  int64_t swap64 (int64_t);
111 uint64_t swapu64(uint64_t);
112 #endif
113 
114 #ifdef __cplusplus
115 extern "C" {
116 #endif
117 
118 #ifdef __cplusplus
119 }
120 #endif
121 
122 #endif