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 h = (s64) >> 32; \ 101 __asm__ volatile("addi %0,%3,4;stwbrx %1,0,%3;stwbrx %2,0,%0" \ 102 : "+r"(s4) : "r"(s64), "r"(h), "b"(d64)); \ 103 } 104 #endif /* 64BIT && PWR7 */ 105 #else 106 #define aix_ld_swap64(s64, d64) \ 107 { \ 108 uint32_t *s4, h, l; \ 109 __asm__("addi %0,%3,4;lwbrx %1,0,%3;lwbrx %2,0,%0" \ 110 : "+r"(s4), "=r"(l), "=r"(h) : "b"(s64)); \ 111 d64 = ((uint64_t)h<<32) | l; \ 112 } 113 114 #define aix_st_swap64(s64, d64) \ 115 { \ 116 uint32_t *s4, h, l; \ 117 l = (s64) & 0xfffffffful, h = (s64) >> 32; \ 118 __asm__ volatile("addi %0,%3,4;stwbrx %1,0,%3;stwbrx %2,0,%0" \ 119 : "+r"(s4) : "r"(l), "r"(h), "b"(d64)); \ 120 } 121 #endif /* __64BIT__ */ 122 #define aix_ld_swap32(s32, d32)\ 123 __asm__("lwbrx %0,0,%1" : "=r"(d32) : "r"(s32)) 124 #define aix_st_swap32(s32, d32)\ 125 __asm__ volatile("stwbrx %1,0,%0" : : "r"(d32), "r"(s32)) 126 #define ld_swap32(s, d) aix_ld_swap32(s, d) 127 #define st_swap32(s, d) aix_st_swap32(s, d) 128 #define ld_swap64(s, d) aix_ld_swap64(s, d) 129 #define st_swap64(s, d) aix_st_swap64(s, d) 130 #endif /* __PPC__ || _ARCH_PPC */ 131 132 #if defined(__sparc) 133 #if !defined(__arch64__) && !defined(__sparcv8) && defined(__sparcv9) 134 #define __arch64__ 135 #endif 136 #if defined(__GNUC__) || (defined(__SUNPRO_C) && __SUNPRO_C > 0x590) 137 /* need Sun Studio C 5.10 and above for GNU inline assembly */ 138 #if defined(__arch64__) 139 #define sparc_ld_swap64(s64, d64) \ 140 __asm__("ldxa [%1]0x88,%0" : "=r"(d64) : "r"(s64)) 141 #define sparc_st_swap64(s64, d64) \ 142 __asm__ volatile("stxa %0,[%1]0x88" : : "r"(s64), "r"(d64)) 143 #define st_swap64(s, d) sparc_st_swap64(s, d) 144 #else 145 #define sparc_ld_swap64(s64, d64) \ 146 { \ 147 uint32_t *s4, h, l; \ 148 __asm__("add %3,4,%0\n\tlda [%3]0x88,%1\n\tlda [%0]0x88,%2" \ 149 : "+r"(s4), "=r"(l), "=r"(h) : "r"(s64)); \ 150 d64 = ((uint64_t)h<<32) | l; \ 151 } 152 #define sparc_st_swap64(s64, d64) \ 153 { \ 154 uint32_t *s4, h, l; \ 155 l = (s64) & 0xfffffffful, h = (s64) >> 32; \ 156 __asm__ volatile("add %3,4,%0\n\tsta %1,[%3]0x88\n\tsta %2,[%0]0x88"\ 157 : "+r"(s4) : "r"(l), "r"(h), "r"(d64)); \ 158 } 159 #endif /* sparc64 */ 160 #define sparc_ld_swap32(s32, d32)\ 161 __asm__("lda [%1]0x88,%0" : "=r"(d32) : "r"(s32)) 162 #define sparc_st_swap32(s32, d32)\ 163 __asm__ volatile("sta %0,[%1]0x88" : : "r"(s32), "r"(d32)) 164 #define ld_swap32(s, d) sparc_ld_swap32(s, d) 165 #define st_swap32(s, d) sparc_st_swap32(s, d) 166 #define ld_swap64(s, d) sparc_ld_swap64(s, d) 167 #define st_swap64(s, d) sparc_st_swap64(s, d) 168 #endif /* GCC || Sun Studio C > 5.9 */ 169 #endif /* sparc */ 170 171 /* GCC fallback */ 172 #if ((__GNUC__ >= 4) || defined(__PGIC__)) && !defined(ld_swap32) 173 #define ld_swap32(s, d) (d = __builtin_bswap32(*(s))) 174 #define st_swap32(s, d) (*(d) = __builtin_bswap32(s)) 175 #endif /* GCC4/PGIC && !swap32 */ 176 #if ((__GNUC__ >= 4) || defined(__PGIC__)) && !defined(ld_swap64) 177 #define ld_swap64(s, d) (d = __builtin_bswap64(*(s))) 178 #define st_swap64(s, d) (*(d) = __builtin_bswap64(s)) 179 #endif /* GCC4/PGIC && !swap64 */ 180 181 /* generic fallback */ 182 #if !defined(ld_swap32) 183 #define ld_swap32(s, d) \ 184 (d = (*(s) >> 24) | (*(s) >> 8 & 0xff00) | \ 185 (*(s) << 8 & 0xff0000) | (*(s) << 24)) 186 #define st_swap32(s, d) \ 187 (*(d) = ((s) >> 24) | ((s) >> 8 & 0xff00) | \ 188 ((s) << 8 & 0xff0000) | ((s) << 24)) 189 #endif 190 #if !defined(ld_swap64) 191 #define ld_swap64(s, d) \ 192 (d = (*(s) >> 56) | (*(s) >> 40 & 0xff00) | \ 193 (*(s) >> 24 & 0xff0000) | (*(s) >> 8 & 0xff000000) | \ 194 (*(s) & 0xff000000) << 8 | (*(s) & 0xff0000) << 24 | \ 195 (*(s) & 0xff00) << 40 | *(s) << 56) 196 #define st_swap64(s, d) \ 197 (*(d) = ((s) >> 56) | ((s) >> 40 & 0xff00) | \ 198 ((s) >> 24 & 0xff0000) | ((s) >> 8 & 0xff000000) | \ 199 ((s) & 0xff000000) << 8 | ((s) & 0xff0000) << 24 | \ 200 ((s) & 0xff00) << 40 | (s) << 56) 201 #endif 202 203 #endif /* MACHINE_IS_BIG_ENDIAN */ 204 205 206 #if defined(MACHINE_IS_LITTLE_ENDIAN) 207 /* replace swaps with simple assignments on little endian systems */ 208 #undef ld_swap32 209 #undef st_swap32 210 #define ld_swap32(s, d) (d = *(s)) 211 #define st_swap32(s, d) (*(d) = s) 212 #undef ld_swap64 213 #undef st_swap64 214 #define ld_swap64(s, d) (d = *(s)) 215 #define st_swap64(s, d) (*(d) = s) 216 #endif /* MACHINE_IS_LITTLE_ENDIAN */ 217 218 #endif /* BYTEORDER_INCLUDED */