xref: /titanic_52/usr/src/common/crypto/edonr/edonr_byteorder.h (revision f64ca10231919db05e806441ccd6186ea9c6e734)
1 /*
2  * IDI,NTNU
3  *
4  * CDDL HEADER START
5  *
6  * The contents of this file are subject to the terms of the
7  * Common Development and Distribution License (the "License").
8  * You may not use this file except in compliance with the License.
9  *
10  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11  * or http://opensource.org/licenses/CDDL-1.0.
12  * See the License for the specific language governing permissions
13  * and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL HEADER in each
16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17  * If applicable, add the following below this CDDL HEADER, with the
18  * fields enclosed by brackets "[]" replaced with your own identifying
19  * information: Portions Copyright [yyyy] [name of copyright owner]
20  *
21  * CDDL HEADER END
22  *
23  * Copyright (C) 2009, 2010, Jorn Amundsen <jorn.amundsen@ntnu.no>
24  *
25  * C header file to determine compile machine byte order. Take care when cross
26  * compiling.
27  *
28  * $Id: byteorder.h 517 2013-02-17 20:34:39Z joern $
29  */
30 /*
31  * Portions copyright (c) 2013, Saso Kiselkov, All rights reserved
32  */
33 
34 #ifndef _CRYPTO_EDONR_BYTEORDER_H
35 #define	_CRYPTO_EDONR_BYTEORDER_H
36 
37 #if defined(__linux)
38 #include <endian.h>
39 #else
40 #include <sys/param.h>
41 #endif
42 
43 #if defined(__BYTE_ORDER)
44 #if (__BYTE_ORDER == __BIG_ENDIAN)
45 #define	MACHINE_IS_BIG_ENDIAN
46 #elif (__BYTE_ORDER == __LITTLE_ENDIAN)
47 #define	MACHINE_IS_LITTLE_ENDIAN
48 #endif
49 #elif defined(BYTE_ORDER)
50 #if (BYTE_ORDER == BIG_ENDIAN)
51 #define	MACHINE_IS_BIG_ENDIAN
52 #elif (BYTE_ORDER == LITTLE_ENDIAN)
53 #define	MACHINE_IS_LITTLE_ENDIAN
54 #endif
55 #endif /* __BYTE_ORDER || BYTE_ORDER */
56 
57 #if !defined(MACHINE_IS_BIG_ENDIAN) && !defined(MACHINE_IS_LITTLE_ENDIAN)
58 #if defined(_BIG_ENDIAN) || defined(_MIPSEB)
59 #define	MACHINE_IS_BIG_ENDIAN
60 #endif
61 #if defined(_LITTLE_ENDIAN) || defined(_MIPSEL)
62 #define	MACHINE_IS_LITTLE_ENDIAN
63 #endif
64 #endif /* !MACHINE_IS_BIG_ENDIAN && !MACHINE_IS_LITTLE_ENDIAN */
65 
66 #if !defined(MACHINE_IS_BIG_ENDIAN) && !defined(MACHINE_IS_LITTLE_ENDIAN)
67 #error unknown machine byte sex
68 #endif
69 
70 #define	BYTEORDER_INCLUDED
71 
72 #if defined(MACHINE_IS_BIG_ENDIAN)
73 /*
74  * Byte swapping macros for big endian architectures and compilers,
75  * add as appropriate for other architectures and/or compilers.
76  *
77  *     ld_swap64(src,dst) : uint64_t dst = *(src)
78  *     st_swap64(src,dst) : *(dst)       = uint64_t src
79  */
80 
81 #if defined(__PPC__) || defined(_ARCH_PPC)
82 
83 #if defined(__64BIT__)
84 #if defined(_ARCH_PWR7)
85 #define	aix_ld_swap64(s64, d64)\
86 	__asm__("ldbrx %0,0,%1" : "=r"(d64) : "r"(s64))
87 #define	aix_st_swap64(s64, d64)\
88 	__asm__ volatile("stdbrx %1,0,%0" : : "r"(d64), "r"(s64))
89 #else
90 #define	aix_ld_swap64(s64, d64)						\
91 {									\
92 	uint64_t *s4, h;						\
93 									\
94 	__asm__("addi %0,%3,4;lwbrx %1,0,%3;lwbrx %2,0,%0;rldimi %1,%2,32,0"\
95 		: "+r"(s4), "=r"(d64), "=r"(h) : "b"(s64));		\
96 }
97 
98 #define	aix_st_swap64(s64, d64)						\
99 {									\
100 	uint64_t *s4, h;						\
101 	h = (s64) >> 32;						\
102 	__asm__ volatile("addi %0,%3,4;stwbrx %1,0,%3;stwbrx %2,0,%0"	\
103 		: "+r"(s4) : "r"(s64), "r"(h), "b"(d64));		\
104 }
105 #endif /* 64BIT && PWR7 */
106 #else
107 #define	aix_ld_swap64(s64, d64)						\
108 {									\
109 	uint32_t *s4, h, l;						\
110 	__asm__("addi %0,%3,4;lwbrx %1,0,%3;lwbrx %2,0,%0"		\
111 		: "+r"(s4), "=r"(l), "=r"(h) : "b"(s64));		\
112 	d64 = ((uint64_t)h<<32) | l;					\
113 }
114 
115 #define	aix_st_swap64(s64, d64)						\
116 {									\
117 	uint32_t *s4, h, l;						\
118 	l = (s64) & 0xfffffffful, h = (s64) >> 32;			\
119 	__asm__ volatile("addi %0,%3,4;stwbrx %1,0,%3;stwbrx %2,0,%0"	\
120 		: "+r"(s4) : "r"(l), "r"(h), "b"(d64));			\
121 }
122 #endif /* __64BIT__ */
123 #define	aix_ld_swap32(s32, d32)\
124 	__asm__("lwbrx %0,0,%1" : "=r"(d32) : "r"(s32))
125 #define	aix_st_swap32(s32, d32)\
126 	__asm__ volatile("stwbrx %1,0,%0" : : "r"(d32), "r"(s32))
127 #define	ld_swap32(s, d) aix_ld_swap32(s, d)
128 #define	st_swap32(s, d) aix_st_swap32(s, d)
129 #define	ld_swap64(s, d) aix_ld_swap64(s, d)
130 #define	st_swap64(s, d) aix_st_swap64(s, d)
131 #endif /* __PPC__ || _ARCH_PPC */
132 
133 #if defined(__sparc)
134 #if !defined(__arch64__) && !defined(__sparcv8) && defined(__sparcv9)
135 #define	__arch64__
136 #endif
137 #if defined(__GNUC__) || (defined(__SUNPRO_C) && __SUNPRO_C > 0x590)
138 /* need Sun Studio C 5.10 and above for GNU inline assembly */
139 #if defined(__arch64__)
140 #define	sparc_ld_swap64(s64, d64)					\
141 	__asm__("ldxa [%1]0x88,%0" : "=r"(d64) : "r"(s64))
142 #define	sparc_st_swap64(s64, d64)					\
143 	__asm__ volatile("stxa %0,[%1]0x88" : : "r"(s64), "r"(d64))
144 #define	st_swap64(s, d) sparc_st_swap64(s, d)
145 #else
146 #define	sparc_ld_swap64(s64, d64)					\
147 {									\
148 	uint32_t *s4, h, l;						\
149 	__asm__("add %3,4,%0\n\tlda [%3]0x88,%1\n\tlda [%0]0x88,%2"	\
150 		: "+r"(s4), "=r"(l), "=r"(h) : "r"(s64));		\
151 	d64 = ((uint64_t)h<<32) | l;					\
152 }
153 #define	sparc_st_swap64(s64, d64)					\
154 {									\
155 	uint32_t *s4, h, l;						\
156 	l = (s64) & 0xfffffffful, h = (s64) >> 32;			\
157 	__asm__ volatile("add %3,4,%0\n\tsta %1,[%3]0x88\n\tsta %2,[%0]0x88"\
158 		: "+r"(s4) : "r"(l), "r"(h), "r"(d64));			\
159 }
160 #endif /* sparc64 */
161 #define	sparc_ld_swap32(s32, d32)\
162 	__asm__("lda [%1]0x88,%0" : "=r"(d32) : "r"(s32))
163 #define	sparc_st_swap32(s32, d32)\
164 	__asm__ volatile("sta %0,[%1]0x88" : : "r"(s32), "r"(d32))
165 #define	ld_swap32(s, d) sparc_ld_swap32(s, d)
166 #define	st_swap32(s, d) sparc_st_swap32(s, d)
167 #define	ld_swap64(s, d) sparc_ld_swap64(s, d)
168 #define	st_swap64(s, d) sparc_st_swap64(s, d)
169 #endif /* GCC || Sun Studio C > 5.9 */
170 #endif /* sparc */
171 
172 /* GCC fallback */
173 #if ((__GNUC__ >= 4) || defined(__PGIC__)) && !defined(ld_swap32)
174 #define	ld_swap32(s, d) (d = __builtin_bswap32(*(s)))
175 #define	st_swap32(s, d) (*(d) = __builtin_bswap32(s))
176 #endif /* GCC4/PGIC && !swap32 */
177 #if ((__GNUC__ >= 4) || defined(__PGIC__)) && !defined(ld_swap64)
178 #define	ld_swap64(s, d) (d = __builtin_bswap64(*(s)))
179 #define	st_swap64(s, d) (*(d) = __builtin_bswap64(s))
180 #endif /* GCC4/PGIC && !swap64 */
181 
182 /* generic fallback */
183 #if !defined(ld_swap32)
184 #define	ld_swap32(s, d)							\
185 	(d = (*(s) >> 24) | (*(s) >> 8 & 0xff00) |			\
186 	(*(s) << 8 & 0xff0000) | (*(s) << 24))
187 #define	st_swap32(s, d)							\
188 	(*(d) = ((s) >> 24) | ((s) >> 8 & 0xff00) |			\
189 	((s) << 8 & 0xff0000) | ((s) << 24))
190 #endif
191 #if !defined(ld_swap64)
192 #define	ld_swap64(s, d)							\
193 	(d = (*(s) >> 56) | (*(s) >> 40 & 0xff00) |			\
194 	(*(s) >> 24 & 0xff0000) | (*(s) >> 8 & 0xff000000) |		\
195 	(*(s) & 0xff000000) << 8 | (*(s) & 0xff0000) << 24 |		\
196 	(*(s) & 0xff00) << 40 | *(s) << 56)
197 #define	st_swap64(s, d)							\
198 	(*(d) = ((s) >> 56) | ((s) >> 40 & 0xff00) |			\
199 	((s) >> 24 & 0xff0000) | ((s) >> 8 & 0xff000000) |		\
200 	((s) & 0xff000000) << 8 | ((s) & 0xff0000) << 24 |		\
201 	((s) & 0xff00) << 40 | (s) << 56)
202 #endif
203 
204 #endif /* MACHINE_IS_BIG_ENDIAN */
205 
206 
207 #if defined(MACHINE_IS_LITTLE_ENDIAN)
208 /* replace swaps with simple assignments on little endian systems */
209 #undef	ld_swap32
210 #undef	st_swap32
211 #define	ld_swap32(s, d) (d = *(s))
212 #define	st_swap32(s, d) (*(d) = s)
213 #undef	ld_swap64
214 #undef	st_swap64
215 #define	ld_swap64(s, d) (d = *(s))
216 #define	st_swap64(s, d) (*(d) = s)
217 #endif /* MACHINE_IS_LITTLE_ENDIAN */
218 
219 #endif /* _CRYPTO_EDONR_BYTEORDER_H */
220