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 #if !defined(BYTEORDER_INCLUDED)
35
36 #if defined(__linux)
37 #include <endian.h>
38 #else
39 #include <sys/param.h>
40 #endif
41
42 #if defined(__BYTE_ORDER)
43 #if (__BYTE_ORDER == __BIG_ENDIAN)
44 #define MACHINE_IS_BIG_ENDIAN
45 #elif (__BYTE_ORDER == __LITTLE_ENDIAN)
46 #define MACHINE_IS_LITTLE_ENDIAN
47 #endif
48 #elif defined(BYTE_ORDER)
49 #if (BYTE_ORDER == BIG_ENDIAN)
50 #define MACHINE_IS_BIG_ENDIAN
51 #elif (BYTE_ORDER == LITTLE_ENDIAN)
52 #define MACHINE_IS_LITTLE_ENDIAN
53 #endif
54 #endif /* __BYTE_ORDER || BYTE_ORDER */
55
56 #if !defined(MACHINE_IS_BIG_ENDIAN) && !defined(MACHINE_IS_LITTLE_ENDIAN)
57 #if defined(_BIG_ENDIAN) || defined(_MIPSEB)
58 #define MACHINE_IS_BIG_ENDIAN
59 #endif
60 #if defined(_LITTLE_ENDIAN) || defined(_MIPSEL)
61 #define MACHINE_IS_LITTLE_ENDIAN
62 #endif
63 #endif /* !MACHINE_IS_BIG_ENDIAN && !MACHINE_IS_LITTLE_ENDIAN */
64
65 #if !defined(MACHINE_IS_BIG_ENDIAN) && !defined(MACHINE_IS_LITTLE_ENDIAN)
66 #error unknown machine byte sex
67 #endif
68
69 #define BYTEORDER_INCLUDED
70
71 #if defined(MACHINE_IS_BIG_ENDIAN)
72 /*
73 * Byte swapping macros for big endian architectures and compilers,
74 * add as appropriate for other architectures and/or compilers.
75 *
76 * ld_swap64(src,dst) : uint64_t dst = *(src)
77 * st_swap64(src,dst) : *(dst) = uint64_t src
78 */
79
80 #if defined(__PPC__) || defined(_ARCH_PPC)
81
82 #if defined(__64BIT__)
83 #if defined(_ARCH_PWR7)
84 #define aix_ld_swap64(s64, d64)\
85 __asm__("ldbrx %0,0,%1" : "=r"(d64) : "r"(s64))
86 #define aix_st_swap64(s64, d64)\
87 __asm__ volatile("stdbrx %1,0,%0" : : "r"(d64), "r"(s64))
88 #else
89 #define aix_ld_swap64(s64, d64)\
90 {\
91 uint64_t *s4, h;\
92 \
93 __asm__("addi %0,%3,4;lwbrx %1,0,%3;lwbrx %2,0,%0;rldimi %1,%2,32,0"\
94 : "+r"(s4), "=r"(d64), "=r"(h) : "b"(s64));\
95 }
96
97 #define aix_st_swap64(s64, d64)\
98 {\
99 uint64_t *s4, h;\
100 \
101 h = (s64) >> 32;\
102 \
103 __asm__ volatile("addi %0,%3,4;stwbrx %1,0,%3;stwbrx %2,0,%0"\
104 : "+r"(s4) : "r"(s64), "r"(h), "b"(d64));\
105 }
106 #endif /* 64BIT && PWR7 */
107 #else
108 #define aix_ld_swap64(s64, d64)\
109 {\
110 uint32_t *s4, h, l;\
111 \
112 __asm__("addi %0,%3,4;lwbrx %1,0,%3;lwbrx %2,0,%0"\
113 : "+r"(s4), "=r"(l), "=r"(h) : "b"(s64));\
114 \
115 d64 = ((uint64_t)h<<32) | l;\
116 }
117
118 #define aix_st_swap64(s64, d64)\
119 {\
120 uint32_t *s4, h, l;\
121 \
122 l = (s64) & 0xfffffffful, h = (s64) >> 32;\
123 \
124 __asm__ volatile("addi %0,%3,4;stwbrx %1,0,%3;stwbrx %2,0,%0"\
125 : "+r"(s4) : "r"(l), "r"(h), "b"(d64));\
126 }
127 #endif /* __64BIT__ */
128 #define aix_ld_swap32(s32, d32)\
129 __asm__("lwbrx %0,0,%1" : "=r"(d32) : "r"(s32))
130 #define aix_st_swap32(s32, d32)\
131 __asm__ volatile("stwbrx %1,0,%0" : : "r"(d32), "r"(s32))
132 #define ld_swap32(s, d) aix_ld_swap32(s, d)
133 #define st_swap32(s, d) aix_st_swap32(s, d)
134 #define ld_swap64(s, d) aix_ld_swap64(s, d)
135 #define st_swap64(s, d) aix_st_swap64(s, d)
136 #endif /* __PPC__ || _ARCH_PPC */
137
138 #if defined(__sparc)
139 #if !defined(__arch64__) && !defined(__sparcv8) && defined(__sparcv9)
140 #define __arch64__
141 #endif
142 #if defined(__GNUC__) || (defined(__SUNPRO_C) && __SUNPRO_C > 0x590)
143 /* need Sun Studio C 5.10 and above for GNU inline assembly */
144 #if defined(__arch64__)
145 #define sparc_ld_swap64(s64, d64)\
146 __asm__("ldxa [%1]0x88,%0" : "=r"(d64) : "r"(s64))
147 #define sparc_st_swap64(s64, d64)\
148 __asm__ volatile("stxa %0,[%1]0x88" : : "r"(s64), "r"(d64))
149 #define st_swap64(s, d) sparc_st_swap64(s, d)
150 #else
151 #define sparc_ld_swap64(s64, d64)\
152 {\
153 uint32_t *s4, h, l;\
154 \
155 __asm__("add %3,4,%0\n\tlda [%3]0x88,%1\n\tlda [%0]0x88,%2"\
156 : "+r"(s4), "=r"(l), "=r"(h) : "r"(s64));\
157 \
158 d64 = ((uint64_t)h<<32) | l;\
159 }
160 #define sparc_st_swap64(s64, d64)\
161 {\
162 uint32_t *s4, h, l;\
163 \
164 l = (s64) & 0xfffffffful, h = (s64) >> 32;\
165 \
166 __asm__ volatile("add %3,4,%0\n\tsta %1,[%3]0x88\n\tsta %2,[%0]0x88"\
167 : "+r"(s4) : "r"(l), "r"(h), "r"(d64));\
168 }
169 #endif /* sparc64 */
170 #define sparc_ld_swap32(s32, d32)\
171 __asm__("lda [%1]0x88,%0" : "=r"(d32) : "r"(s32))
172 #define sparc_st_swap32(s32, d32)\
173 __asm__ volatile("sta %0,[%1]0x88" : : "r"(s32), "r"(d32))
174 #define ld_swap32(s, d) sparc_ld_swap32(s, d)
175 #define st_swap32(s, d) sparc_st_swap32(s, d)
176 #define ld_swap64(s, d) sparc_ld_swap64(s, d)
177 #define st_swap64(s, d) sparc_st_swap64(s, d)
178 #endif /* GCC || Sun Studio C > 5.9 */
179 #endif /* sparc */
180
181 /* GCC fallback */
182 #if ((__GNUC__ >= 4) || defined(__PGIC__)) && !defined(ld_swap32)
183 #define ld_swap32(s, d) (d = __builtin_bswap32(*(s)))
184 #define st_swap32(s, d) (*(d) = __builtin_bswap32(s))
185 #endif /* GCC4/PGIC && !swap32 */
186 #if ((__GNUC__ >= 4) || defined(__PGIC__)) && !defined(ld_swap64)
187 #define ld_swap64(s, d) (d = __builtin_bswap64(*(s)))
188 #define st_swap64(s, d) (*(d) = __builtin_bswap64(s))
189 #endif /* GCC4/PGIC && !swap64 */
190
191 /* generic fallback */
192 #if !defined(ld_swap32)
193 #define ld_swap32(s, d) (d = \
194 (*(s)>>24)|(*(s)>>8&0xff00)|(*(s)<<8&0xff0000)|(*(s)<<24))
195 #define st_swap32(s, d) (*(d) = \
196 ((s)>>24)|((s)>>8&0xff00)|((s)<<8&0xff0000)|((s)<<24))
197 #endif
198 #if !defined(ld_swap64)
199 #define ld_swap64(s, d) (d = (*(s)>>56)|(*(s)>>40&0xff00)|(*(s)>>24&0xff0000)|\
200 (*(s)>>8&0xff000000)|(*(s)&0xff000000)<<8|(*(s)&0xff0000)<<24|\
201 (*(s)&0xff00)<<40|*(s)<<56)
202 #define st_swap64(s, d) (*(d) = ((s)>>56)|((s)>>40&0xff00)|((s)>>24&0xff0000)|\
203 ((s)>>8&0xff000000)|((s)&0xff000000)<<8|((s)&0xff0000)<<24|\
204 ((s)&0xff00)<<40|(s)<<56)
205 #endif
206
207 #endif /* MACHINE_IS_BIG_ENDIAN */
208
209
210 #if defined(MACHINE_IS_LITTLE_ENDIAN)
211 /* replace swaps with simple assignments on little endian systems */
212 #undef ld_swap32
213 #undef st_swap32
214 #define ld_swap32(s, d) (d = *(s))
215 #define st_swap32(s, d) (*(d) = s)
216 #undef ld_swap64
217 #undef st_swap64
218 #define ld_swap64(s, d) (d = *(s))
219 #define st_swap64(s, d) (*(d) = s)
220 #endif /* MACHINE_IS_LITTLE_ENDIAN */
221
222 #endif /* BYTEORDER_INCLUDED */