~drizzle-trunk/drizzle/development

1 by brian
clean slate
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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"
212.4.2 by Monty Taylor
Fixed the includes in places to make the myisam header file move work.
23
#include <storage/myisam/myisampack.h>
1 by brian
clean slate
24
481 by Brian Aker
Remove all of uchar.
25
static int ptr_compare(size_t *compare_length, unsigned char **a, unsigned char **b);
26
static int ptr_compare_0(size_t *compare_length, unsigned char **a, unsigned char **b);
27
static int ptr_compare_1(size_t *compare_length, unsigned char **a, unsigned char **b);
28
static int ptr_compare_2(size_t *compare_length, unsigned char **a, unsigned char **b);
29
static int ptr_compare_3(size_t *compare_length, unsigned char **a, unsigned char **b);
1 by brian
clean slate
30
31
	/* Get a pointer to a optimal byte-compare function for a given size */
32
33
qsort2_cmp get_ptr_compare (size_t size)
34
{
35
  if (size < 4)
36
    return (qsort2_cmp) ptr_compare;
37
  switch (size & 3) {
38
    case 0: return (qsort2_cmp) ptr_compare_0;
39
    case 1: return (qsort2_cmp) ptr_compare_1;
40
    case 2: return (qsort2_cmp) ptr_compare_2;
41
    case 3: return (qsort2_cmp) ptr_compare_3;
42
    }
43
  return 0;					/* Impossible */
44
}
45
46
47
	/*
48
	  Compare to keys to see witch is smaller.
49
	  Loop unrolled to make it quick !!
50
	*/
51
52
#define cmp(N) if (first[N] != last[N]) return (int) first[N] - (int) last[N]
53
481 by Brian Aker
Remove all of uchar.
54
static int ptr_compare(size_t *compare_length, unsigned char **a, unsigned char **b)
1 by brian
clean slate
55
{
56
  register int length= *compare_length;
481 by Brian Aker
Remove all of uchar.
57
  register unsigned char *first,*last;
1 by brian
clean slate
58
59
  first= *a; last= *b;
60
  while (--length)
61
  {
62
    if (*first++ != *last++)
63
      return (int) first[-1] - (int) last[-1];
64
  }
65
  return (int) first[0] - (int) last[0];
66
}
67
68
481 by Brian Aker
Remove all of uchar.
69
static int ptr_compare_0(size_t *compare_length,unsigned char **a, unsigned char **b)
1 by brian
clean slate
70
{
71
  register int length= *compare_length;
481 by Brian Aker
Remove all of uchar.
72
  register unsigned char *first,*last;
1 by brian
clean slate
73
74
  first= *a; last= *b;
75
 loop:
76
  cmp(0);
77
  cmp(1);
78
  cmp(2);
79
  cmp(3);
80
  if ((length-=4))
81
  {
82
    first+=4;
83
    last+=4;
84
    goto loop;
85
  }
86
  return (0);
87
}
88
89
481 by Brian Aker
Remove all of uchar.
90
static int ptr_compare_1(size_t *compare_length,unsigned char **a, unsigned char **b)
1 by brian
clean slate
91
{
92
  register int length= *compare_length-1;
481 by Brian Aker
Remove all of uchar.
93
  register unsigned char *first,*last;
1 by brian
clean slate
94
95
  first= *a+1; last= *b+1;
96
  cmp(-1);
97
 loop:
98
  cmp(0);
99
  cmp(1);
100
  cmp(2);
101
  cmp(3);
102
  if ((length-=4))
103
  {
104
    first+=4;
105
    last+=4;
106
    goto loop;
107
  }
108
  return (0);
109
}
110
481 by Brian Aker
Remove all of uchar.
111
static int ptr_compare_2(size_t *compare_length,unsigned char **a, unsigned char **b)
1 by brian
clean slate
112
{
113
  register int length= *compare_length-2;
481 by Brian Aker
Remove all of uchar.
114
  register unsigned char *first,*last;
1 by brian
clean slate
115
116
  first= *a +2 ; last= *b +2;
117
  cmp(-2);
118
  cmp(-1);
119
 loop:
120
  cmp(0);
121
  cmp(1);
122
  cmp(2);
123
  cmp(3);
124
  if ((length-=4))
125
  {
126
    first+=4;
127
    last+=4;
128
    goto loop;
129
  }
130
  return (0);
131
}
132
481 by Brian Aker
Remove all of uchar.
133
static int ptr_compare_3(size_t *compare_length,unsigned char **a, unsigned char **b)
1 by brian
clean slate
134
{
135
  register int length= *compare_length-3;
481 by Brian Aker
Remove all of uchar.
136
  register unsigned char *first,*last;
1 by brian
clean slate
137
138
  first= *a +3 ; last= *b +3;
139
  cmp(-3);
140
  cmp(-2);
141
  cmp(-1);
142
 loop:
143
  cmp(0);
144
  cmp(1);
145
  cmp(2);
146
  cmp(3);
147
  if ((length-=4))
148
  {
149
    first+=4;
150
    last+=4;
151
    goto loop;
152
  }
153
  return (0);
154
}
155
481 by Brian Aker
Remove all of uchar.
156
void my_store_ptr(unsigned char *buff, size_t pack_length, my_off_t pos)
1 by brian
clean slate
157
{
158
  switch (pack_length) {
159
#if SIZEOF_OFF_T > 4
160
  case 8: mi_int8store(buff,pos); break;
161
  case 7: mi_int7store(buff,pos); break;
162
  case 6: mi_int6store(buff,pos); break;
163
  case 5: mi_int5store(buff,pos); break;
164
#endif
165
  case 4: mi_int4store(buff,pos); break;
166
  case 3: mi_int3store(buff,pos); break;
167
  case 2: mi_int2store(buff,pos); break;
481 by Brian Aker
Remove all of uchar.
168
  case 1: buff[0]= (unsigned char) pos; break;
51.3.15 by Jay Pipes
Phase 3 removal of DBUG in mysys
169
  default: assert(0);
1 by brian
clean slate
170
  }
171
  return;
172
}
173
481 by Brian Aker
Remove all of uchar.
174
my_off_t my_get_ptr(unsigned char *ptr, size_t pack_length)
1 by brian
clean slate
175
{
176
  my_off_t pos;
177
  switch (pack_length) {
178
#if SIZEOF_OFF_T > 4
179
  case 8: pos= (my_off_t) mi_uint8korr(ptr); break;
180
  case 7: pos= (my_off_t) mi_uint7korr(ptr); break;
181
  case 6: pos= (my_off_t) mi_uint6korr(ptr); break;
182
  case 5: pos= (my_off_t) mi_uint5korr(ptr); break;
183
#endif
184
  case 4: pos= (my_off_t) mi_uint4korr(ptr); break;
185
  case 3: pos= (my_off_t) mi_uint3korr(ptr); break;
186
  case 2: pos= (my_off_t) mi_uint2korr(ptr); break;
481 by Brian Aker
Remove all of uchar.
187
  case 1: pos= (my_off_t) *(unsigned char*) ptr; break;
51.3.15 by Jay Pipes
Phase 3 removal of DBUG in mysys
188
  default: assert(0); return 0;
1 by brian
clean slate
189
  }
190
 return pos;
191
}
192