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