Drizzled Public API Documentation

ptr_cmp.cc
1 /* Copyright (C) 2000 MySQL AB
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /*
17  get_ptr_compare(len) returns a pointer to a optimal byte-compare function
18  for a array of stringpointer where all strings have size len.
19  The bytes are compare as unsigned chars.
20  */
21 
22 #include <config.h>
23 #include <drizzled/internal/my_sys.h>
24 
25 #include <assert.h>
26 
27 #include <plugin/myisam/myisampack.h>
28 
29 namespace drizzled {
30 namespace internal {
31 
32 static int ptr_compare(size_t *compare_length, unsigned char **a, unsigned char **b);
33 static int ptr_compare_0(size_t *compare_length, unsigned char **a, unsigned char **b);
34 static int ptr_compare_1(size_t *compare_length, unsigned char **a, unsigned char **b);
35 static int ptr_compare_2(size_t *compare_length, unsigned char **a, unsigned char **b);
36 static int ptr_compare_3(size_t *compare_length, unsigned char **a, unsigned char **b);
37 
38  /* Get a pointer to a optimal byte-compare function for a given size */
39 
40 qsort2_cmp get_ptr_compare (size_t size)
41 {
42  if (size < 4)
43  return (qsort2_cmp) ptr_compare;
44  switch (size & 3) {
45  case 0: return (qsort2_cmp) ptr_compare_0;
46  case 1: return (qsort2_cmp) ptr_compare_1;
47  case 2: return (qsort2_cmp) ptr_compare_2;
48  case 3: return (qsort2_cmp) ptr_compare_3;
49  }
50  return 0; /* Impossible */
51 }
52 
53 
54  /*
55  Compare two keys to see which is smaller.
56  Loop unrolled to make it quick !!
57  */
58 
59 #define cmp(N) if (first[N] != last[N]) return (int) first[N] - (int) last[N]
60 
61 static int ptr_compare(size_t *compare_length, unsigned char **a, unsigned char **b)
62 {
63  int length= *compare_length;
64  unsigned char *first,*last;
65 
66  first= *a; last= *b;
67  while (--length)
68  {
69  if (*first++ != *last++)
70  return (int) first[-1] - (int) last[-1];
71  }
72  return (int) first[0] - (int) last[0];
73 }
74 
75 
76 static int ptr_compare_0(size_t *compare_length,unsigned char **a, unsigned char **b)
77 {
78  int length= *compare_length;
79  unsigned char *first,*last;
80 
81  first= *a; last= *b;
82  loop:
83  cmp(0);
84  cmp(1);
85  cmp(2);
86  cmp(3);
87  if ((length-=4))
88  {
89  first+=4;
90  last+=4;
91  goto loop;
92  }
93  return (0);
94 }
95 
96 
97 static int ptr_compare_1(size_t *compare_length,unsigned char **a, unsigned char **b)
98 {
99  int length= *compare_length-1;
100  unsigned char *first,*last;
101 
102  first= *a+1; last= *b+1;
103  cmp(-1);
104  loop:
105  cmp(0);
106  cmp(1);
107  cmp(2);
108  cmp(3);
109  if ((length-=4))
110  {
111  first+=4;
112  last+=4;
113  goto loop;
114  }
115  return (0);
116 }
117 
118 static int ptr_compare_2(size_t *compare_length,unsigned char **a, unsigned char **b)
119 {
120  int length= *compare_length-2;
121  unsigned char *first,*last;
122 
123  first= *a +2 ; last= *b +2;
124  cmp(-2);
125  cmp(-1);
126  loop:
127  cmp(0);
128  cmp(1);
129  cmp(2);
130  cmp(3);
131  if ((length-=4))
132  {
133  first+=4;
134  last+=4;
135  goto loop;
136  }
137  return (0);
138 }
139 
140 static int ptr_compare_3(size_t *compare_length,unsigned char **a, unsigned char **b)
141 {
142  int length= *compare_length-3;
143  unsigned char *first,*last;
144 
145  first= *a +3 ; last= *b +3;
146  cmp(-3);
147  cmp(-2);
148  cmp(-1);
149  loop:
150  cmp(0);
151  cmp(1);
152  cmp(2);
153  cmp(3);
154  if ((length-=4))
155  {
156  first+=4;
157  last+=4;
158  goto loop;
159  }
160  return (0);
161 }
162 
163 void my_store_ptr(unsigned char *buff, size_t pack_length, my_off_t pos)
164 {
165  switch (pack_length) {
166 #if SIZEOF_OFF_T > 4
167  case 8: mi_int8store(buff,pos); break;
168  case 7: mi_int7store(buff,pos); break;
169  case 6: mi_int6store(buff,pos); break;
170  case 5: mi_int5store(buff,pos); break;
171 #endif
172  case 4: mi_int4store(buff,pos); break;
173  case 3: mi_int3store(buff,pos); break;
174  case 2: mi_int2store(buff,pos); break;
175  case 1: buff[0]= (unsigned char) pos; break;
176  default: assert(0);
177  }
178  return;
179 }
180 
181 my_off_t my_get_ptr(unsigned char *ptr, size_t pack_length)
182 {
183  my_off_t pos;
184  switch (pack_length) {
185 #if SIZEOF_OFF_T > 4
186  case 8: pos= (my_off_t) mi_uint8korr(ptr); break;
187  case 7: pos= (my_off_t) mi_uint7korr(ptr); break;
188  case 6: pos= (my_off_t) mi_uint6korr(ptr); break;
189  case 5: pos= (my_off_t) mi_uint5korr(ptr); break;
190 #endif
191  case 4: pos= (my_off_t) mi_uint4korr(ptr); break;
192  case 3: pos= (my_off_t) mi_uint3korr(ptr); break;
193  case 2: pos= (my_off_t) mi_uint2korr(ptr); break;
194  case 1: pos= (my_off_t) *(unsigned char*) ptr; break;
195  default: assert(0); return 0;
196  }
197  return pos;
198 }
199 
200 } /* namespace internal */
201 } /* namespace drizzled */