1 /*-------------------------------------------------------------------------
4 * Functions for the SQL datatypes BIT() and BIT VARYING().
6 * Code originally contributed by Adriaan Joubert.
8 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/include/utils/varbit.h,v 1.22 2005/12/25 02:14:18 momjian Exp $
13 *-------------------------------------------------------------------------
21 * Modeled on struct varlena from postgres.h, but data type is bits8.
25 int32 vl_len; /* standard varlena header (total size in
27 int32 bit_len; /* number of valid bits */
28 bits8 bit_dat[1]; /* bit string, most sig. byte first */
32 * fmgr interface macros
34 * BIT and BIT VARYING are toastable varlena types. They are the same
35 * as far as representation goes, so we just have one set of macros.
37 #define DatumGetVarBitP(X) ((VarBit *) PG_DETOAST_DATUM(X))
38 #define DatumGetVarBitPCopy(X) ((VarBit *) PG_DETOAST_DATUM_COPY(X))
39 #define VarBitPGetDatum(X) PointerGetDatum(X)
40 #define PG_GETARG_VARBIT_P(n) DatumGetVarBitP(PG_GETARG_DATUM(n))
41 #define PG_GETARG_VARBIT_P_COPY(n) DatumGetVarBitPCopy(PG_GETARG_DATUM(n))
42 #define PG_RETURN_VARBIT_P(x) return VarBitPGetDatum(x)
44 /* Header overhead *in addition to* VARHDRSZ */
45 #define VARBITHDRSZ sizeof(int32)
46 /* Number of bits in this bit string */
47 #define VARBITLEN(PTR) (((VarBit *) (PTR))->bit_len)
48 /* Pointer to the first byte containing bit string data */
49 #define VARBITS(PTR) (((VarBit *) (PTR))->bit_dat)
50 /* Number of bytes in the data section of a bit string */
51 #define VARBITBYTES(PTR) (VARSIZE(PTR) - VARHDRSZ - VARBITHDRSZ)
52 /* Padding of the bit string at the end (in bits) */
53 #define VARBITPAD(PTR) (VARBITBYTES(PTR)*BITS_PER_BYTE - VARBITLEN(PTR))
54 /* Number of bytes needed to store a bit string of a given length */
55 #define VARBITTOTALLEN(BITLEN) (((BITLEN) + BITS_PER_BYTE-1)/BITS_PER_BYTE + \
56 VARHDRSZ + VARBITHDRSZ)
57 /* pointer beyond the end of the bit string (like end() in STL containers) */
58 #define VARBITEND(PTR) (((bits8 *) (PTR)) + VARSIZE(PTR))
59 /* Mask that will cover exactly one byte, i.e. BITS_PER_BYTE bits */
63 extern Datum bit_in(PG_FUNCTION_ARGS);
64 extern Datum bit_out(PG_FUNCTION_ARGS);
65 extern Datum bit_recv(PG_FUNCTION_ARGS);
66 extern Datum bit_send(PG_FUNCTION_ARGS);
67 extern Datum varbit_in(PG_FUNCTION_ARGS);
68 extern Datum varbit_out(PG_FUNCTION_ARGS);
69 extern Datum varbit_recv(PG_FUNCTION_ARGS);
70 extern Datum varbit_send(PG_FUNCTION_ARGS);
71 extern Datum bit(PG_FUNCTION_ARGS);
72 extern Datum varbit(PG_FUNCTION_ARGS);
73 extern Datum biteq(PG_FUNCTION_ARGS);
74 extern Datum bitne(PG_FUNCTION_ARGS);
75 extern Datum bitlt(PG_FUNCTION_ARGS);
76 extern Datum bitle(PG_FUNCTION_ARGS);
77 extern Datum bitgt(PG_FUNCTION_ARGS);
78 extern Datum bitge(PG_FUNCTION_ARGS);
79 extern Datum bitcmp(PG_FUNCTION_ARGS);
80 extern Datum bitand(PG_FUNCTION_ARGS);
81 extern Datum bitor(PG_FUNCTION_ARGS);
82 extern Datum bitxor(PG_FUNCTION_ARGS);
83 extern Datum bitnot(PG_FUNCTION_ARGS);
84 extern Datum bitshiftleft(PG_FUNCTION_ARGS);
85 extern Datum bitshiftright(PG_FUNCTION_ARGS);
86 extern Datum bitcat(PG_FUNCTION_ARGS);
87 extern Datum bitsubstr(PG_FUNCTION_ARGS);
88 extern Datum bitlength(PG_FUNCTION_ARGS);
89 extern Datum bitoctetlength(PG_FUNCTION_ARGS);
90 extern Datum bitfromint4(PG_FUNCTION_ARGS);
91 extern Datum bittoint4(PG_FUNCTION_ARGS);
92 extern Datum bitfromint8(PG_FUNCTION_ARGS);
93 extern Datum bittoint8(PG_FUNCTION_ARGS);
94 extern Datum bitposition(PG_FUNCTION_ARGS);