]> granicus.if.org Git - postgresql/blob - src/include/utils/pg_crc.h
Add btree and hash opclasses for pg_lsn.
[postgresql] / src / include / utils / pg_crc.h
1 /*
2  * pg_crc.h
3  *
4  * PostgreSQL CRC support
5  *
6  * See Ross Williams' excellent introduction
7  * A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS, available from
8  * http://www.ross.net/crc/ or several other net sites.
9  *
10  * We use a normal (not "reflected", in Williams' terms) CRC, using initial
11  * all-ones register contents and a final bit inversion.
12  *
13  * The 64-bit variant is not used as of PostgreSQL 8.1, but we retain the
14  * code for possible future use.
15  *
16  *
17  * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
18  * Portions Copyright (c) 1994, Regents of the University of California
19  *
20  * src/include/utils/pg_crc.h
21  */
22 #ifndef PG_CRC_H
23 #define PG_CRC_H
24
25 /* ugly hack to let this be used in frontend and backend code on Cygwin */
26 #ifdef FRONTEND
27 #define CRCDLLIMPORT
28 #else
29 #define CRCDLLIMPORT PGDLLIMPORT
30 #endif
31
32 typedef uint32 pg_crc32;
33
34 /* Initialize a CRC accumulator */
35 #define INIT_CRC32(crc) ((crc) = 0xFFFFFFFF)
36
37 /* Finish a CRC calculation */
38 #define FIN_CRC32(crc)  ((crc) ^= 0xFFFFFFFF)
39
40 /* Accumulate some (more) bytes into a CRC */
41 #define COMP_CRC32(crc, data, len)      \
42 do { \
43         const unsigned char *__data = (const unsigned char *) (data); \
44         uint32          __len = (len); \
45 \
46         while (__len-- > 0) \
47         { \
48                 int             __tab_index = ((int) ((crc) >> 24) ^ *__data++) & 0xFF; \
49                 (crc) = pg_crc32_table[__tab_index] ^ ((crc) << 8); \
50         } \
51 } while (0)
52
53 /* Check for equality of two CRCs */
54 #define EQ_CRC32(c1,c2)  ((c1) == (c2))
55
56 /* Constant table for CRC calculation */
57 extern CRCDLLIMPORT const uint32 pg_crc32_table[];
58
59
60 #ifdef PROVIDE_64BIT_CRC
61
62 /*
63  * If we use a 64-bit integer type, then a 64-bit CRC looks just like the
64  * usual sort of implementation.  However, we can also fake it with two
65  * 32-bit registers.  Experience has shown that the two-32-bit-registers code
66  * is as fast as, or even much faster than, the 64-bit code on all but true
67  * 64-bit machines.  We use SIZEOF_VOID_P to check the native word width.
68  */
69
70 #if SIZEOF_VOID_P < 8
71
72 /*
73  * crc0 represents the LSBs of the 64-bit value, crc1 the MSBs.  Note that
74  * with crc0 placed first, the output of 32-bit and 64-bit implementations
75  * will be bit-compatible only on little-endian architectures.  If it were
76  * important to make the two possible implementations bit-compatible on
77  * all machines, we could do a configure test to decide how to order the
78  * two fields, but it seems not worth the trouble.
79  */
80 typedef struct pg_crc64
81 {
82         uint32          crc0;
83         uint32          crc1;
84 }       pg_crc64;
85
86 /* Initialize a CRC accumulator */
87 #define INIT_CRC64(crc) ((crc).crc0 = 0xffffffff, (crc).crc1 = 0xffffffff)
88
89 /* Finish a CRC calculation */
90 #define FIN_CRC64(crc)  ((crc).crc0 ^= 0xffffffff, (crc).crc1 ^= 0xffffffff)
91
92 /* Accumulate some (more) bytes into a CRC */
93 #define COMP_CRC64(crc, data, len)      \
94 do { \
95         uint32          __crc0 = (crc).crc0; \
96         uint32          __crc1 = (crc).crc1; \
97         unsigned char *__data = (unsigned char *) (data); \
98         uint32          __len = (len); \
99 \
100         while (__len-- > 0) \
101         { \
102                 int             __tab_index = ((int) (__crc1 >> 24) ^ *__data++) & 0xFF; \
103                 __crc1 = pg_crc64_table1[__tab_index] ^ ((__crc1 << 8) | (__crc0 >> 24)); \
104                 __crc0 = pg_crc64_table0[__tab_index] ^ (__crc0 << 8); \
105         } \
106         (crc).crc0 = __crc0; \
107         (crc).crc1 = __crc1; \
108 } while (0)
109
110 /* Check for equality of two CRCs */
111 #define EQ_CRC64(c1,c2)  ((c1).crc0 == (c2).crc0 && (c1).crc1 == (c2).crc1)
112
113 /* Constant table for CRC calculation */
114 extern CRCDLLIMPORT const uint32 pg_crc64_table0[];
115 extern CRCDLLIMPORT const uint32 pg_crc64_table1[];
116 #else                                                   /* use int64 implementation */
117
118 typedef struct pg_crc64
119 {
120         uint64          crc0;
121 }       pg_crc64;
122
123 /* Initialize a CRC accumulator */
124 #define INIT_CRC64(crc) ((crc).crc0 = UINT64CONST(0xffffffffffffffff))
125
126 /* Finish a CRC calculation */
127 #define FIN_CRC64(crc)  ((crc).crc0 ^= UINT64CONST(0xffffffffffffffff))
128
129 /* Accumulate some (more) bytes into a CRC */
130 #define COMP_CRC64(crc, data, len)      \
131 do { \
132         uint64          __crc0 = (crc).crc0; \
133         unsigned char *__data = (unsigned char *) (data); \
134         uint32          __len = (len); \
135 \
136         while (__len-- > 0) \
137         { \
138                 int             __tab_index = ((int) (__crc0 >> 56) ^ *__data++) & 0xFF; \
139                 __crc0 = pg_crc64_table[__tab_index] ^ (__crc0 << 8); \
140         } \
141         (crc).crc0 = __crc0; \
142 } while (0)
143
144 /* Check for equality of two CRCs */
145 #define EQ_CRC64(c1,c2)  ((c1).crc0 == (c2).crc0)
146
147 /* Constant table for CRC calculation */
148 extern CRCDLLIMPORT const uint64 pg_crc64_table[];
149 #endif   /* SIZEOF_VOID_P < 8 */
150 #endif   /* PROVIDE_64BIT_CRC */
151
152 #endif   /* PG_CRC_H */