MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ptr_cmp.c
1 /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
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 "mysys_priv.h"
23 #include <myisampack.h>
24 
25 #ifdef __sun
26 /*
27  * On Solaris, memcmp() is normally faster than the unrolled ptr_compare_N
28  * functions, as memcmp() is usually a platform-specific implementation
29  * written in assembler, provided in /usr/lib/libc/libc_hwcap*.so.1.
30  * This implementation is also usually faster than the built-in memcmp
31  * supplied by GCC, so it is recommended to build with "-fno-builtin-memcmp"
32  * in CFLAGS if building with GCC on Solaris.
33  */
34 
35 #include <string.h>
36 
37 static int native_compare(size_t *length, unsigned char **a, unsigned char **b)
38 {
39  return memcmp(*a, *b, *length);
40 }
41 
42 #else /* __sun */
43 
44 static int ptr_compare(size_t *compare_length, uchar **a, uchar **b);
45 static int ptr_compare_0(size_t *compare_length, uchar **a, uchar **b);
46 static int ptr_compare_1(size_t *compare_length, uchar **a, uchar **b);
47 static int ptr_compare_2(size_t *compare_length, uchar **a, uchar **b);
48 static int ptr_compare_3(size_t *compare_length, uchar **a, uchar **b);
49 #endif /* __sun */
50 
51  /* Get a pointer to a optimal byte-compare function for a given size */
52 
53 #ifdef __sun
54 qsort2_cmp get_ptr_compare (size_t size __attribute__((unused)))
55 {
56  return (qsort2_cmp) native_compare;
57 }
58 #else
59 qsort2_cmp get_ptr_compare (size_t size)
60 {
61  if (size < 4)
62  return (qsort2_cmp) ptr_compare;
63  switch (size & 3) {
64  case 0: return (qsort2_cmp) ptr_compare_0;
65  case 1: return (qsort2_cmp) ptr_compare_1;
66  case 2: return (qsort2_cmp) ptr_compare_2;
67  case 3: return (qsort2_cmp) ptr_compare_3;
68  }
69  return 0; /* Impossible */
70 }
71 #endif /* __sun */
72 
73 
74  /*
75  Compare to keys to see witch is smaller.
76  Loop unrolled to make it quick !!
77  */
78 
79 #define cmp(N) if (first[N] != last[N]) return (int) first[N] - (int) last[N]
80 
81 #ifndef __sun
82 
83 static int ptr_compare(size_t *compare_length, uchar **a, uchar **b)
84 {
85  reg3 int length= *compare_length;
86  reg1 uchar *first,*last;
87 
88  first= *a; last= *b;
89  while (--length)
90  {
91  if (*first++ != *last++)
92  return (int) first[-1] - (int) last[-1];
93  }
94  return (int) first[0] - (int) last[0];
95 }
96 
97 
98 static int ptr_compare_0(size_t *compare_length,uchar **a, uchar **b)
99 {
100  reg3 int length= *compare_length;
101  reg1 uchar *first,*last;
102 
103  first= *a; last= *b;
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 
119 static int ptr_compare_1(size_t *compare_length,uchar **a, uchar **b)
120 {
121  reg3 int length= *compare_length-1;
122  reg1 uchar *first,*last;
123 
124  first= *a+1; last= *b+1;
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_2(size_t *compare_length,uchar **a, uchar **b)
141 {
142  reg3 int length= *compare_length-2;
143  reg1 uchar *first,*last;
144 
145  first= *a +2 ; last= *b +2;
146  cmp(-2);
147  cmp(-1);
148  loop:
149  cmp(0);
150  cmp(1);
151  cmp(2);
152  cmp(3);
153  if ((length-=4))
154  {
155  first+=4;
156  last+=4;
157  goto loop;
158  }
159  return (0);
160 }
161 
162 static int ptr_compare_3(size_t *compare_length,uchar **a, uchar **b)
163 {
164  reg3 int length= *compare_length-3;
165  reg1 uchar *first,*last;
166 
167  first= *a +3 ; last= *b +3;
168  cmp(-3);
169  cmp(-2);
170  cmp(-1);
171  loop:
172  cmp(0);
173  cmp(1);
174  cmp(2);
175  cmp(3);
176  if ((length-=4))
177  {
178  first+=4;
179  last+=4;
180  goto loop;
181  }
182  return (0);
183 }
184 
185 #endif /* !__sun */
186 
187 void my_store_ptr(uchar *buff, size_t pack_length, my_off_t pos)
188 {
189  switch (pack_length) {
190 #if SIZEOF_OFF_T > 4
191  case 8: mi_int8store(buff,pos); break;
192  case 7: mi_int7store(buff,pos); break;
193  case 6: mi_int6store(buff,pos); break;
194  case 5: mi_int5store(buff,pos); break;
195 #endif
196  case 4: mi_int4store(buff,pos); break;
197  case 3: mi_int3store(buff,pos); break;
198  case 2: mi_int2store(buff,pos); break;
199  case 1: buff[0]= (uchar) pos; break;
200  default: DBUG_ASSERT(0);
201  }
202  return;
203 }
204 
205 my_off_t my_get_ptr(uchar *ptr, size_t pack_length)
206 {
207  my_off_t pos;
208  switch (pack_length) {
209 #if SIZEOF_OFF_T > 4
210  case 8: pos= (my_off_t) mi_uint8korr(ptr); break;
211  case 7: pos= (my_off_t) mi_uint7korr(ptr); break;
212  case 6: pos= (my_off_t) mi_uint6korr(ptr); break;
213  case 5: pos= (my_off_t) mi_uint5korr(ptr); break;
214 #endif
215  case 4: pos= (my_off_t) mi_uint4korr(ptr); break;
216  case 3: pos= (my_off_t) mi_uint3korr(ptr); break;
217  case 2: pos= (my_off_t) mi_uint2korr(ptr); break;
218  case 1: pos= (my_off_t) *(uchar*) ptr; break;
219  default: DBUG_ASSERT(0); return 0;
220  }
221  return pos;
222 }
223