Properly use $(AWK) in Makefile, not 'awk'
[pgsql.git] / contrib / btree_gist / btree_float4.c
blobd138aa94bf2de30c3dc0160b8cf9992c438679ca
1 /*
2 * contrib/btree_gist/btree_float4.c
3 */
4 #include "postgres.h"
6 #include "btree_gist.h"
7 #include "btree_utils_num.h"
8 #include "utils/float.h"
10 typedef struct float4key
12 float4 lower;
13 float4 upper;
14 } float4KEY;
17 ** float4 ops
19 PG_FUNCTION_INFO_V1(gbt_float4_compress);
20 PG_FUNCTION_INFO_V1(gbt_float4_fetch);
21 PG_FUNCTION_INFO_V1(gbt_float4_union);
22 PG_FUNCTION_INFO_V1(gbt_float4_picksplit);
23 PG_FUNCTION_INFO_V1(gbt_float4_consistent);
24 PG_FUNCTION_INFO_V1(gbt_float4_distance);
25 PG_FUNCTION_INFO_V1(gbt_float4_penalty);
26 PG_FUNCTION_INFO_V1(gbt_float4_same);
28 static bool
29 gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
31 return (*((const float4 *) a) > *((const float4 *) b));
33 static bool
34 gbt_float4ge(const void *a, const void *b, FmgrInfo *flinfo)
36 return (*((const float4 *) a) >= *((const float4 *) b));
38 static bool
39 gbt_float4eq(const void *a, const void *b, FmgrInfo *flinfo)
41 return (*((const float4 *) a) == *((const float4 *) b));
43 static bool
44 gbt_float4le(const void *a, const void *b, FmgrInfo *flinfo)
46 return (*((const float4 *) a) <= *((const float4 *) b));
48 static bool
49 gbt_float4lt(const void *a, const void *b, FmgrInfo *flinfo)
51 return (*((const float4 *) a) < *((const float4 *) b));
54 static int
55 gbt_float4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
57 float4KEY *ia = (float4KEY *) (((const Nsrt *) a)->t);
58 float4KEY *ib = (float4KEY *) (((const Nsrt *) b)->t);
60 if (ia->lower == ib->lower)
62 if (ia->upper == ib->upper)
63 return 0;
65 return (ia->upper > ib->upper) ? 1 : -1;
68 return (ia->lower > ib->lower) ? 1 : -1;
71 static float8
72 gbt_float4_dist(const void *a, const void *b, FmgrInfo *flinfo)
74 return GET_FLOAT_DISTANCE(float4, a, b);
78 static const gbtree_ninfo tinfo =
80 gbt_t_float4,
81 sizeof(float4),
82 8, /* sizeof(gbtreekey8) */
83 gbt_float4gt,
84 gbt_float4ge,
85 gbt_float4eq,
86 gbt_float4le,
87 gbt_float4lt,
88 gbt_float4key_cmp,
89 gbt_float4_dist
93 PG_FUNCTION_INFO_V1(float4_dist);
94 Datum
95 float4_dist(PG_FUNCTION_ARGS)
97 float4 a = PG_GETARG_FLOAT4(0);
98 float4 b = PG_GETARG_FLOAT4(1);
99 float4 r;
101 r = a - b;
102 if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
103 float_overflow_error();
105 PG_RETURN_FLOAT4(fabsf(r));
109 /**************************************************
110 * float4 ops
111 **************************************************/
114 Datum
115 gbt_float4_compress(PG_FUNCTION_ARGS)
117 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
119 PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
122 Datum
123 gbt_float4_fetch(PG_FUNCTION_ARGS)
125 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
127 PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
130 Datum
131 gbt_float4_consistent(PG_FUNCTION_ARGS)
133 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
134 float4 query = PG_GETARG_FLOAT4(1);
135 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
137 /* Oid subtype = PG_GETARG_OID(3); */
138 bool *recheck = (bool *) PG_GETARG_POINTER(4);
139 float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
140 GBT_NUMKEY_R key;
142 /* All cases served by this function are exact */
143 *recheck = false;
145 key.lower = (GBT_NUMKEY *) &kkk->lower;
146 key.upper = (GBT_NUMKEY *) &kkk->upper;
148 PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
149 GIST_LEAF(entry), &tinfo,
150 fcinfo->flinfo));
154 Datum
155 gbt_float4_distance(PG_FUNCTION_ARGS)
157 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
158 float4 query = PG_GETARG_FLOAT4(1);
160 /* Oid subtype = PG_GETARG_OID(3); */
161 float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
162 GBT_NUMKEY_R key;
164 key.lower = (GBT_NUMKEY *) &kkk->lower;
165 key.upper = (GBT_NUMKEY *) &kkk->upper;
167 PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
168 &tinfo, fcinfo->flinfo));
172 Datum
173 gbt_float4_union(PG_FUNCTION_ARGS)
175 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
176 void *out = palloc(sizeof(float4KEY));
178 *(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
179 PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
183 Datum
184 gbt_float4_penalty(PG_FUNCTION_ARGS)
186 float4KEY *origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
187 float4KEY *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
188 float *result = (float *) PG_GETARG_POINTER(2);
190 penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
192 PG_RETURN_POINTER(result);
195 Datum
196 gbt_float4_picksplit(PG_FUNCTION_ARGS)
198 PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
199 (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
200 &tinfo, fcinfo->flinfo));
203 Datum
204 gbt_float4_same(PG_FUNCTION_ARGS)
206 float4KEY *b1 = (float4KEY *) PG_GETARG_POINTER(0);
207 float4KEY *b2 = (float4KEY *) PG_GETARG_POINTER(1);
208 bool *result = (bool *) PG_GETARG_POINTER(2);
210 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
211 PG_RETURN_POINTER(result);