45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * CDDL HEADER START
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * The contents of this file are subject to the terms of the
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Common Development and Distribution License (the "License").
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * You may not use this file except in compliance with the License.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * or http://opensource.org/licenses/CDDL-1.0.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * See the License for the specific language governing permissions
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * and limitations under the License.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * When distributing Covered Code, include this CDDL HEADER in each
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * If applicable, add the following below this CDDL HEADER, with the
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * fields enclosed by brackets "[]" replaced with your own identifying
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * CDDL HEADER END
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Copyright (C) 2009, 2010, Jorn Amundsen <jorn.amundsen@ntnu.no>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * C header file to determine compile machine byte order. Take care when cross
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Portions copyright (c) 2013, Saso Kiselkov, All rights reserved
8b2aab4fb2ee21a9076eb25068178246a12470b7Gary Mills * Copyright 2016 Gary Mills
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* __BYTE_ORDER || BYTE_ORDER */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#if !defined(MACHINE_IS_BIG_ENDIAN) && !defined(MACHINE_IS_LITTLE_ENDIAN)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#if defined(_LITTLE_ENDIAN) || defined(_MIPSEL)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* !MACHINE_IS_BIG_ENDIAN && !MACHINE_IS_LITTLE_ENDIAN */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#if !defined(MACHINE_IS_BIG_ENDIAN) && !defined(MACHINE_IS_LITTLE_ENDIAN)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Byte swapping macros for big endian architectures and compilers,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * add as appropriate for other architectures and/or compilers.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * ld_swap64(src,dst) : uint64_t dst = *(src)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * st_swap64(src,dst) : *(dst) = uint64_t src
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__("ldbrx %0,0,%1" : "=r"(d64) : "r"(s64))
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__ volatile("stdbrx %1,0,%0" : : "r"(d64), "r"(s64))
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__("addi %0,%3,4;lwbrx %1,0,%3;lwbrx %2,0,%0;rldimi %1,%2,32,0"\
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens : "+r"(s4), "=r"(d64), "=r"(h) : "b"(s64)); \
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__ volatile("addi %0,%3,4;stwbrx %1,0,%3;stwbrx %2,0,%0" \
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* 64BIT && PWR7 */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__("addi %0,%3,4;lwbrx %1,0,%3;lwbrx %2,0,%0" \
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__ volatile("addi %0,%3,4;stwbrx %1,0,%3;stwbrx %2,0,%0" \
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* __64BIT__ */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__("lwbrx %0,0,%1" : "=r"(d32) : "r"(s32))
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__ volatile("stwbrx %1,0,%0" : : "r"(d32), "r"(s32))
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* __PPC__ || _ARCH_PPC */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#if !defined(__arch64__) && !defined(__sparcv8) && defined(__sparcv9)
8b2aab4fb2ee21a9076eb25068178246a12470b7Gary Mills#if defined(__GNUC__) || (defined(__SUNPRO_C) && __SUNPRO_C > 0x590 && \
8b2aab4fb2ee21a9076eb25068178246a12470b7Gary Mills/* need Sun Studio C 5.10 and above for GNU inline assembly, but not lint */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__("ldxa [%1]0x88,%0" : "=r"(d64) : "r"(s64))
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__ volatile("stxa %0,[%1]0x88" : : "r"(s64), "r"(d64))
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__("add %3,4,%0\n\tlda [%3]0x88,%1\n\tlda [%0]0x88,%2" \
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__ volatile("add %3,4,%0\n\tsta %1,[%3]0x88\n\tsta %2,[%0]0x88"\
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* sparc64 */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__("lda [%1]0x88,%0" : "=r"(d32) : "r"(s32))
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm__ volatile("sta %0,[%1]0x88" : : "r"(s32), "r"(d32))
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* GCC || Sun Studio C > 5.9 */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* sparc */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* GCC fallback */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#if ((__GNUC__ >= 4) || defined(__PGIC__)) && !defined(ld_swap32)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#define ld_swap32(s, d) (d = __builtin_bswap32(*(s)))
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#define st_swap32(s, d) (*(d) = __builtin_bswap32(s))
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* GCC4/PGIC && !swap32 */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#if ((__GNUC__ >= 4) || defined(__PGIC__)) && !defined(ld_swap64)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#define ld_swap64(s, d) (d = __builtin_bswap64(*(s)))
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#define st_swap64(s, d) (*(d) = __builtin_bswap64(s))
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* GCC4/PGIC && !swap64 */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* generic fallback */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (*(d) = ((s) >> 24) | ((s) >> 8 & 0xff00) | \
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (d = (*(s) >> 56) | (*(s) >> 40 & 0xff00) | \
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (*(s) >> 24 & 0xff0000) | (*(s) >> 8 & 0xff000000) | \
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (*(s) & 0xff000000) << 8 | (*(s) & 0xff0000) << 24 | \
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (*(d) = ((s) >> 56) | ((s) >> 40 & 0xff00) | \
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ((s) >> 24 & 0xff0000) | ((s) >> 8 & 0xff000000) | \
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ((s) & 0xff000000) << 8 | ((s) & 0xff0000) << 24 | \
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* MACHINE_IS_BIG_ENDIAN */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* replace swaps with simple assignments on little endian systems */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* MACHINE_IS_LITTLE_ENDIAN */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* _CRYPTO_EDONR_BYTEORDER_H */