File indexing completed on 2024-05-12 17:22:21

0001 /*
0002     SPDX-FileCopyrightText: 2002 Shie Erlich <erlich@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2002 Rafi Yanai <yanai@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0005 
0006     From the mplayer project (www.mplayerhq.hu)
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #ifndef BSWAP_H
0012 #define BSWAP_H
0013 
0014 /* this file doesn't exist
0015 #ifdef HAVE_CONFIG_H
0016 #include "config.h"
0017 #endif
0018 */
0019 
0020 #ifdef HAVE_BYTESWAP_H
0021 #include <byteswap.h>
0022 #else
0023 
0024 #ifdef ARCH_X86
0025 inline static unsigned short ByteSwap16(unsigned short x)
0026 {
0027     __asm("xchgb %b0,%h0" : "=q"(x) : "0"(x));
0028     return x;
0029 }
0030 #define bswap_16(x) ByteSwap16(x)
0031 
0032 inline static unsigned int ByteSwap32(unsigned int x)
0033 {
0034 #if __CPU__ > 386
0035     __asm("bswap %0"
0036           : "=r"(x)
0037           :
0038 #else
0039     __asm(
0040         "xchgb %b0,%h0\n"
0041         " rorl $16,%0\n"
0042         " xchgb %b0,%h0"
0043         : "=q"(x)
0044         :
0045 #endif
0046           "0"(x));
0047     return x;
0048 }
0049 #define bswap_32(x) ByteSwap32(x)
0050 
0051 inline static unsigned long long int ByteSwap64(unsigned long long int x)
0052 {
0053     register union {
0054         __extension__ unsigned long long int __ll;
0055         unsigned int __l[2];
0056     } __x;
0057     asm("xchgl %0,%1" : "=r"(__x.__l[0]), "=r"(__x.__l[1]) : "0"(bswap_32((unsigned long)x)), "1"(bswap_32((unsigned long)(x >> 32))));
0058     return __x.__ll;
0059 }
0060 #define bswap_64(x) ByteSwap64(x)
0061 
0062 #else
0063 
0064 #define bswap_16(x) ((unsigned short)(((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8))
0065 
0066 /* code from bits/byteswap.h SPDX-FileCopyrightText: 1997, 1998 Free Software Foundation Inc. */
0067 #define bswap_32(x) ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
0068 
0069 #define bswap_64(x)                                                                                                                                            \
0070     (__extension__({                                                                                                                                           \
0071         union {                                                                                                                                                \
0072             __extension__ unsigned long long int __ll;                                                                                                         \
0073             unsigned int __l[2];                                                                                                                               \
0074         } __w, __r;                                                                                                                                            \
0075         __w.__ll = (x);                                                                                                                                        \
0076         __r.__l[0] = bswap_32(__w.__l[1]);                                                                                                                     \
0077         __r.__l[1] = bswap_32(__w.__l[0]);                                                                                                                     \
0078         __r.__ll;                                                                                                                                              \
0079     }))
0080 #endif /* !ARCH_X86 */
0081 
0082 #endif /* !HAVE_BYTESWAP_H */
0083 
0084 /*
0085  be2me ... BigEndian to MachineEndian
0086  le2me ... LittleEndian to MachineEndian
0087 */
0088 
0089 #ifdef WORDS_BIGENDIAN
0090 #define be2me_16(x) (x)
0091 #define be2me_32(x) (x)
0092 #define be2me_64(x) (x)
0093 #define le2me_16(x) bswap_16(x)
0094 #define le2me_32(x) bswap_32(x)
0095 #define le2me_64(x) bswap_64(x)
0096 #else
0097 #define be2me_16(x) bswap_16(x)
0098 #define be2me_32(x) bswap_32(x)
0099 #define be2me_64(x) bswap_64(x)
0100 #define le2me_16(x) (x)
0101 #define le2me_32(x) (x)
0102 #define le2me_64(x) (x)
0103 #endif
0104 
0105 #endif