Shared Persistent Heap Data Environment Manual  1.1.0
sasindexkey.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005-2014 IBM Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation, Steven Munroe - initial API and implementation
10  */
11 
12 #ifndef __SAS_INDEXKEY_H
13 #define __SAS_INDEXKEY_H
14 
15 #include <string.h>
16 
31 #ifdef __WORDSIZE_64
32 
33 typedef unsigned long machine_uint_t;
35 typedef unsigned int machine_uhalf_t;
37 const unsigned long machine_sign_mask = (0x8000000000000000);
39 const unsigned long double_exp_mask = (0x7ff0000000000000);
41 const unsigned long double_mask = (0xffffffffffffffff);
42 #else
43 
44 typedef unsigned long machine_uint_t;
46 typedef unsigned short machine_uhalf_t;
48 const unsigned long machine_sign_mask = (0x80000000);
50 const unsigned long double_exp_mask = (0x7ff00000);
52 const unsigned long double_mask = (0xffffffff);
53 #endif
54 
59 typedef struct SASIndexKey_t
60 {
68 
82 static inline int
84 {
85  int rc = 0;
86  if (op_a->data[0] != op_b->data[0])
87  {
88  if (op_a->data[0] < op_b->data[0])
89  rc = -1;
90  else
91  rc = 1;
92 
93  }
94  else
95  {
96  size_t len = op_a->compare_size;
97 
98  if (op_a->compare_size != op_b->compare_size)
99  {
100  if (op_a->compare_size > op_b->compare_size)
101  len = op_b->compare_size;
102 
103  rc = memcmp (&op_a->data[0], &op_b->data[0], len);
104 
105  if ((rc == 0) && (op_a->compare_size != op_b->compare_size))
106  {
107  if (op_a->compare_size < op_b->compare_size)
108  rc = -1;
109  else
110  rc = 1;
111  }
112  }
113  else if (len > sizeof (machine_uint_t))
114  {
115  unsigned int i = 1;
116  for (i = 1; i < (len / sizeof (machine_uint_t)); i++)
117  {
118  if (op_a->data[i] != op_b->data[i])
119  {
120  if (op_a->data[i] > op_b->data[i])
121  rc = 1;
122  else
123  rc = -1;
124  break;
125  }
126  }
127  }
128  }
129  return rc;
130 }
131 
141 static inline void
143 {
144  memcpy (dest, src, src->copy_size);
145 }
146 
156 static inline size_t
158 {
159  return key->copy_size;
160 }
161 
168 static inline void
169 SASIndexKeyInitRef (SASIndexKey_t * dest, void *value)
170 {
171  dest->compare_size = sizeof (void *);
172  dest->copy_size = 2 * sizeof (void *);
173  dest->data[0] = (machine_uint_t) value;
174 }
175 
176 
179 typedef union {
181 #ifdef __WORDSIZE_64
182  machine_uint_t key_element;
183 #else
184  struct {
185 #if __BYTE_ORDER == __BIG_ENDIAN
186  machine_uint_t data_0;
187  machine_uint_t data_1;
188 #else
189  machine_uint_t data_1;
190  machine_uint_t data_0;
191 #endif
192  } key_element;
193 #endif
194 
195  unsigned long long uint64_key;
197  long long int64_key;
199  double double_key;
201 
209 static inline void
210 SASIndexKeyInitUInt64 (SASIndexKey_t * dest, unsigned long long value)
211 {
212  sasindexkeymap_t key_val;
213 
214  key_val.uint64_key = value;
215 #ifdef __WORDSIZE_64
216  dest->compare_size = sizeof (unsigned long long);
217  dest->copy_size = sizeof (void *) + sizeof (unsigned long long);
218  dest->data[0] = key_val.key_element;
219 #else
220  dest->compare_size = sizeof (unsigned long long);
221  dest->copy_size = sizeof (void *) + sizeof (unsigned long long);
222  dest->data[0] = key_val.key_element.data_0;
223  dest->data[1] = key_val.key_element.data_1;
224 #endif
225 }
226 
234 static inline unsigned long long
236 {
237  sasindexkeymap_t key_val;
238 
239 #ifdef __WORDSIZE_64
240  key_val.key_element = dest->data[0];
241 #else
242  key_val.key_element.data_0 = dest->data[0];
243  key_val.key_element.data_1 = dest->data[1];
244 #endif
245  return key_val.uint64_key;
246 }
247 
258 static inline void
259 SASIndexKeyInitInt64 (SASIndexKey_t * dest, signed long long value)
260 {
261  sasindexkeymap_t key_val;
262 
263  key_val.int64_key = value;
264 #ifdef __WORDSIZE_64
265  dest->compare_size = sizeof (unsigned long long);
266  dest->copy_size = sizeof (void *) + sizeof (unsigned long long);
267  dest->data[0] = key_val.key_element ^ machine_sign_mask;
268 #else
269  dest->compare_size = sizeof (unsigned long long);
270  dest->copy_size = sizeof (void *) + sizeof (unsigned long long);
271  dest->data[0] = key_val.key_element.data_0 ^ machine_sign_mask;
272  dest->data[1] = key_val.key_element.data_1;
273 #endif
274 }
275 
283 static inline long long
285 {
286  sasindexkeymap_t key_val;
287 
288 #ifdef __WORDSIZE_64
289  key_val.key_element = dest->data[0] ^ machine_sign_mask;
290 #else
291  key_val.key_element.data_0 = dest->data[0] ^ machine_sign_mask;
292  key_val.key_element.data_1 = dest->data[1];
293 #endif
294  return key_val.int64_key;
295 }
296 
307 static inline void
308 SASIndexKeyInitDouble (SASIndexKey_t * dest, double value)
309 {
310  sasindexkeymap_t key_val;
311 
312  key_val.double_key = value;
313 #ifdef __WORDSIZE_64
314  dest->compare_size = sizeof (unsigned long long);
315  dest->copy_size = sizeof (void *) + sizeof (unsigned long long);
316  if (value < 0.0)
317  dest->data[0] = key_val.key_element ^ double_mask;
318  else
319  dest->data[0] = key_val.key_element ^ machine_sign_mask;
320 #else
321  dest->compare_size = sizeof (unsigned long long);
322  dest->copy_size = sizeof (void *) + sizeof (unsigned long long);
323  if (value < 0.0)
324  {
325  dest->data[0] = key_val.key_element.data_0 ^ double_mask;
326  dest->data[1] = key_val.key_element.data_1 ^ double_mask;
327  }
328  else
329  {
330  dest->data[0] = key_val.key_element.data_0 ^ machine_sign_mask;
331  dest->data[1] = key_val.key_element.data_1;
332  }
333 
334 #endif
335 }
336 
344 static inline double
346 {
347  sasindexkeymap_t key_val;
348 
349 #ifdef __WORDSIZE_64
350  if (dest->data[0] & machine_sign_mask)
351  key_val.key_element = dest->data[0] ^ machine_sign_mask;
352  else
353  key_val.key_element = dest->data[0] ^ double_mask;
354 #else
355  if (dest->data[0] & machine_sign_mask)
356  {
357  key_val.key_element.data_0 = dest->data[0] ^ machine_sign_mask;
358  key_val.key_element.data_1 = dest->data[1];
359  }
360  else
361  {
362  key_val.key_element.data_0 = dest->data[0] ^ double_mask;
363  key_val.key_element.data_1 = dest->data[1] ^ double_mask;
364  }
365 #endif
366  return key_val.double_key;
367 }
368 
369 #endif /* __SAS_INDEXKEY_H */
struct SASIndexKey_t SASIndexKey_t
Index Key Handle structure for binary index B-trees.
const unsigned long double_exp_mask
mask use to invert the exponent.
Definition: sasindexkey.h:50
const unsigned long machine_sign_mask
mask use to invert the signbit.
Definition: sasindexkey.h:48
static void SASIndexKeyInitRef(SASIndexKey_t *dest, void *value)
Initial a binary key @ destination with a address value.
Definition: sasindexkey.h:169
machine_uint_t data[15]
Definition: sasindexkey.h:66
static void SASIndexKeyInitInt64(SASIndexKey_t *dest, signed long long value)
Initial a binary key @ destination with a signed 64-bit integer value.
Definition: sasindexkey.h:259
static double SASIndexKeyReturn1stDouble(SASIndexKey_t *dest)
Return the value of the 1st binary key as a signed 64-bit integer value.
Definition: sasindexkey.h:345
static int SASIndexKeyCompare(SASIndexKey_t *op_a, SASIndexKey_t *op_b)
binary key compare logic for Index B-Tree keys.
Definition: sasindexkey.h:83
double double_key
Definition: sasindexkey.h:199
unsigned long long uint64_key
Definition: sasindexkey.h:195
static void SASIndexKeyInitDouble(SASIndexKey_t *dest, double value)
Initial a binary key @ destination with a signed 64-bit integer value.
Definition: sasindexkey.h:308
machine_uhalf_t copy_size
Definition: sasindexkey.h:62
static void SASIndexKeyInitUInt64(SASIndexKey_t *dest, unsigned long long value)
Initial a binary key @ destination with a unsigned 64-bit integer value.
Definition: sasindexkey.h:210
static size_t SASIndexKeySize(SASIndexKey_t *key)
Return the binary index key copy_size.
Definition: sasindexkey.h:157
Index Key Handle structure for binary index B-trees.
Definition: sasindexkey.h:59
static long long SASIndexKeyReturn1stInt64(SASIndexKey_t *dest)
Return the value of the 1st binary key as a signed 64-bit integer value.
Definition: sasindexkey.h:284
struct sasindexkeymap_t::@0 key_element
unsigned short machine_uhalf_t
data unit used to store copy and compare lengths.
Definition: sasindexkey.h:46
long long int64_key
Definition: sasindexkey.h:197
machine_uhalf_t compare_size
Definition: sasindexkey.h:64
const unsigned long double_mask
mask use to invert the sign and exponent.
Definition: sasindexkey.h:52
unsigned long machine_uint_t
word size data unit used for binary keys.
Definition: sasindexkey.h:44
static unsigned long long SASIndexKeyReturn1stUInt64(SASIndexKey_t *dest)
Return the value of the 1st binary key as a unsigned 64-bit integer value.
Definition: sasindexkey.h:235
Union of value types and key machine integer types.
Definition: sasindexkey.h:179
static void SASIndexKeyCopy(SASIndexKey_t *dest, SASIndexKey_t *src)
Copy a binary key from source to destination.
Definition: sasindexkey.h:142