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