PostgreSQL Source Code: src/include/port/pg_bswap.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#ifndef PG_BSWAP_H
21#define PG_BSWAP_H
22
23
24
25
26
27
28
29
30
31#if defined(HAVE__BUILTIN_BSWAP16)
32
33#define pg_bswap16(x) __builtin_bswap16(x)
34
35#elif defined(_MSC_VER)
36
37#define pg_bswap16(x) _byteswap_ushort(x)
38
39#else
40
43{
44 return
45 ((x << 8) & 0xff00) |
46 ((x >> 8) & 0x00ff);
47}
48
49#endif
50
51
52
53#if defined(HAVE__BUILTIN_BSWAP32)
54
55#define pg_bswap32(x) __builtin_bswap32(x)
56
57#elif defined(_MSC_VER)
58
59#define pg_bswap32(x) _byteswap_ulong(x)
60
61#else
62
65{
66 return
67 ((x << 24) & 0xff000000) |
68 ((x << 8) & 0x00ff0000) |
69 ((x >> 8) & 0x0000ff00) |
70 ((x >> 24) & 0x000000ff);
71}
72
73#endif
74
75
76
77#if defined(HAVE__BUILTIN_BSWAP64)
78
79#define pg_bswap64(x) __builtin_bswap64(x)
80
81
82#elif defined(_MSC_VER)
83
84#define pg_bswap64(x) _byteswap_uint64(x)
85
86#else
87
90{
91 return
92 ((x << 56) & UINT64CONST(0xff00000000000000)) |
93 ((x << 40) & UINT64CONST(0x00ff000000000000)) |
94 ((x << 24) & UINT64CONST(0x0000ff0000000000)) |
95 ((x << 8) & UINT64CONST(0x000000ff00000000)) |
96 ((x >> 8) & UINT64CONST(0x00000000ff000000)) |
97 ((x >> 24) & UINT64CONST(0x0000000000ff0000)) |
98 ((x >> 40) & UINT64CONST(0x000000000000ff00)) |
99 ((x >> 56) & UINT64CONST(0x00000000000000ff));
100}
101#endif
102
103
104
105
106
107
108#ifdef WORDS_BIGENDIAN
109
110#define pg_hton16(x) (x)
111#define pg_hton32(x) (x)
112#define pg_hton64(x) (x)
113
114#define pg_ntoh16(x) (x)
115#define pg_ntoh32(x) (x)
116#define pg_ntoh64(x) (x)
117
118#else
119
120#define pg_hton16(x) pg_bswap16(x)
121#define pg_hton32(x) pg_bswap32(x)
122#define pg_hton64(x) pg_bswap64(x)
123
124#define pg_ntoh16(x) pg_bswap16(x)
125#define pg_ntoh32(x) pg_bswap32(x)
126#define pg_ntoh64(x) pg_bswap64(x)
127
128#endif
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149#ifdef SIZEOF_DATUM
150#ifdef WORDS_BIGENDIAN
151#define DatumBigEndianToNative(x) (x)
152#else
153#if SIZEOF_DATUM == 8
154#define DatumBigEndianToNative(x) pg_bswap64(x)
155#else
156#define DatumBigEndianToNative(x) pg_bswap32(x)
157#endif
158#endif
159#endif
160
161#endif
static uint16 pg_bswap16(uint16 x)
static uint64 pg_bswap64(uint64 x)
static uint32 pg_bswap32(uint32 x)