1 /* 2 * The PCI Library -- System-Dependent Stuff 3 * 4 * Copyright (c) 1997--2020 Martin Mares <[email protected]> 5 * 6 * Can be freely distributed and used under the terms of the GNU GPL v2+ 7 * 8 * SPDX-License-Identifier: GPL-2.0-or-later 9 */ 10 11 #ifdef __GNUC__ 12 #define UNUSED __attribute__((unused)) 13 #define NONRET __attribute__((noreturn)) 14 #define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z))) 15 #else 16 #define UNUSED 17 #define NONRET 18 #define FORMAT_CHECK(x,y,z) 19 #define inline 20 #endif 21 22 typedef u8 byte; 23 typedef u16 word; 24 25 #ifdef PCI_OS_WINDOWS 26 #define strcasecmp _strcmpi 27 #define strncasecmp _strnicmp 28 #if defined(_MSC_VER) && _MSC_VER < 1800 29 #if _MSC_VER < 1300 30 #define strtoull strtoul 31 #else 32 #define strtoull _strtoui64 33 #endif 34 #endif 35 #if defined(_MSC_VER) && _MSC_VER < 1900 36 #define snprintf _snprintf 37 #define vsnprintf _vsnprintf 38 #endif 39 #endif 40 41 #ifdef PCI_HAVE_LINUX_BYTEORDER_H 42 43 #include <asm/byteorder.h> 44 #define cpu_to_le16 __cpu_to_le16 45 #define cpu_to_le32 __cpu_to_le32 46 #define le16_to_cpu __le16_to_cpu 47 #define le32_to_cpu __le32_to_cpu 48 49 #else 50 51 #ifdef PCI_OS_LINUX 52 #include <endian.h> 53 #define BYTE_ORDER __BYTE_ORDER 54 #define BIG_ENDIAN __BIG_ENDIAN 55 #endif 56 57 #ifdef PCI_OS_SUNOS 58 #include <sys/byteorder.h> 59 #if defined(__i386) && defined(LITTLE_ENDIAN) 60 # define BYTE_ORDER LITTLE_ENDIAN 61 #elif defined(__sparc) && defined(BIG_ENDIAN) 62 # define BYTE_ORDER BIG_ENDIAN 63 #else 64 #define BIG_ENDIAN 4321 65 #endif 66 #ifndef BYTE_ORDER 67 #ifdef _LITTLE_ENDIAN 68 #define BYTE_ORDER 1234 69 #else 70 #define BYTE_ORDER 4321 71 #endif 72 #endif /* BYTE_ORDER */ 73 #endif /* PCI_OS_SUNOS */ 74 75 #ifdef PCI_OS_WINDOWS 76 #ifdef __MINGW32__ 77 #include <sys/param.h> 78 #else 79 #include <io.h> 80 #define BIG_ENDIAN 4321 81 #define LITTLE_ENDIAN 1234 82 #define BYTE_ORDER LITTLE_ENDIAN 83 #endif 84 #endif 85 86 #ifdef PCI_OS_HAIKU 87 #include <endian.h> 88 #endif 89 90 #ifdef PCI_OS_SYLIXOS 91 #include <endian.h> 92 #endif 93 94 #ifdef PCI_OS_DJGPP 95 #define BIG_ENDIAN 4321 96 #define LITTLE_ENDIAN 1234 97 #define BYTE_ORDER LITTLE_ENDIAN 98 #endif 99 100 #ifdef PCI_OS_AMIGAOS 101 #include <machine/endian.h> 102 #endif 103 104 #if !defined(BYTE_ORDER) 105 #error "BYTE_ORDER not defined for your platform" 106 #endif 107 108 #if BYTE_ORDER == BIG_ENDIAN 109 #define cpu_to_le16 swab16 110 #define cpu_to_le32 swab32 111 #define le16_to_cpu swab16 112 #define le32_to_cpu swab32 113 swab16(word w)114static inline word swab16(word w) 115 { 116 return (w << 8) | ((w >> 8) & 0xff); 117 } 118 swab32(u32 w)119static inline u32 swab32(u32 w) 120 { 121 return ((w & 0xff000000) >> 24) | 122 ((w & 0x00ff0000) >> 8) | 123 ((w & 0x0000ff00) << 8) | 124 ((w & 0x000000ff) << 24); 125 } 126 #else 127 #define cpu_to_le16(x) (x) 128 #define cpu_to_le32(x) (x) 129 #define le16_to_cpu(x) (x) 130 #define le32_to_cpu(x) (x) 131 #endif 132 133 #endif 134