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 */ 29 /* 30 * Portions copyright (c) 2013, Saso Kiselkov, All rights reserved 31 * Copyright 2016 Gary Mills 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 !defined(__lint)) 139 /* need Sun Studio C 5.10 and above for GNU inline assembly, but not lint */ 140 #if defined(__arch64__) 141 #define sparc_ld_swap64(s64, d64) \ 142 __asm__("ldxa [%1]0x88,%0" : "=r"(d64) : "r"(s64)) 143 #define sparc_st_swap64(s64, d64) \ 144 __asm__ volatile("stxa %0,[%1]0x88" : : "r"(s64), "r"(d64)) 145 #define st_swap64(s, d) sparc_st_swap64(s, d) 146 #else 147 #define sparc_ld_swap64(s64, d64) \ 148 { \ 149 uint32_t *s4, h, l; \ 150 __asm__("add %3,4,%0\n\tlda [%3]0x88,%1\n\tlda [%0]0x88,%2" \ 151 : "+r"(s4), "=r"(l), "=r"(h) : "r"(s64)); \ 152 d64 = ((uint64_t)h<<32) | l; \ 153 } 154 #define sparc_st_swap64(s64, d64) \ 155 { \ 156 uint32_t *s4, h, l; \ 157 l = (s64) & 0xfffffffful, h = (s64) >> 32; \ 158 __asm__ volatile("add %3,4,%0\n\tsta %1,[%3]0x88\n\tsta %2,[%0]0x88"\ 159 : "+r"(s4) : "r"(l), "r"(h), "r"(d64)); \ 160 } 161 #endif /* sparc64 */ 162 #define sparc_ld_swap32(s32, d32)\ 163 __asm__("lda [%1]0x88,%0" : "=r"(d32) : "r"(s32)) 164 #define sparc_st_swap32(s32, d32)\ 165 __asm__ volatile("sta %0,[%1]0x88" : : "r"(s32), "r"(d32)) 166 #define ld_swap32(s, d) sparc_ld_swap32(s, d) 167 #define st_swap32(s, d) sparc_st_swap32(s, d) 168 #define ld_swap64(s, d) sparc_ld_swap64(s, d) 169 #define st_swap64(s, d) sparc_st_swap64(s, d) 170 #endif /* GCC || Sun Studio C > 5.9 */ 171 #endif /* sparc */ 172 173 /* GCC fallback */ 174 #if ((__GNUC__ >= 4) || defined(__PGIC__)) && !defined(ld_swap32) 175 #define ld_swap32(s, d) (d = __builtin_bswap32(*(s))) 176 #define st_swap32(s, d) (*(d) = __builtin_bswap32(s)) 177 #endif /* GCC4/PGIC && !swap32 */ 178 #if ((__GNUC__ >= 4) || defined(__PGIC__)) && !defined(ld_swap64) 179 #define ld_swap64(s, d) (d = __builtin_bswap64(*(s))) 180 #define st_swap64(s, d) (*(d) = __builtin_bswap64(s)) 181 #endif /* GCC4/PGIC && !swap64 */ 182 183 /* generic fallback */ 184 #if !defined(ld_swap32) 185 #define ld_swap32(s, d) \ 186 (d = (*(s) >> 24) | (*(s) >> 8 & 0xff00) | \ 187 (*(s) << 8 & 0xff0000) | (*(s) << 24)) 188 #define st_swap32(s, d) \ 189 (*(d) = ((s) >> 24) | ((s) >> 8 & 0xff00) | \ 190 ((s) << 8 & 0xff0000) | ((s) << 24)) 191 #endif 192 #if !defined(ld_swap64) 193 #define ld_swap64(s, d) \ 194 (d = (*(s) >> 56) | (*(s) >> 40 & 0xff00) | \ 195 (*(s) >> 24 & 0xff0000) | (*(s) >> 8 & 0xff000000) | \ 196 (*(s) & 0xff000000) << 8 | (*(s) & 0xff0000) << 24 | \ 197 (*(s) & 0xff00) << 40 | *(s) << 56) 198 #define st_swap64(s, d) \ 199 (*(d) = ((s) >> 56) | ((s) >> 40 & 0xff00) | \ 200 ((s) >> 24 & 0xff0000) | ((s) >> 8 & 0xff000000) | \ 201 ((s) & 0xff000000) << 8 | ((s) & 0xff0000) << 24 | \ 202 ((s) & 0xff00) << 40 | (s) << 56) 203 #endif 204 205 #endif /* MACHINE_IS_BIG_ENDIAN */ 206 207 208 #if defined(MACHINE_IS_LITTLE_ENDIAN) 209 /* replace swaps with simple assignments on little endian systems */ 210 #undef ld_swap32 211 #undef st_swap32 212 #define ld_swap32(s, d) (d = *(s)) 213 #define st_swap32(s, d) (*(d) = s) 214 #undef ld_swap64 215 #undef st_swap64 216 #define ld_swap64(s, d) (d = *(s)) 217 #define st_swap64(s, d) (*(d) = s) 218 #endif /* MACHINE_IS_LITTLE_ENDIAN */ 219 220 #endif /* _CRYPTO_EDONR_BYTEORDER_H */