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