~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to mysys/ptr_cmp.c

  • Committer: bk at mysql
  • Date: 2000-07-31 19:29:14 UTC
  • Revision ID: sp1r-bk@work.mysql.com-20000731192914-08846
Import changeset

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
 
2
   
 
3
   This library is free software; you can redistribute it and/or
 
4
   modify it under the terms of the GNU Library General Public
 
5
   License as published by the Free Software Foundation; either
 
6
   version 2 of the License, or (at your option) any later version.
 
7
   
 
8
   This library is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
   Library General Public License for more details.
 
12
   
 
13
   You should have received a copy of the GNU Library General Public
 
14
   License along with this library; if not, write to the Free
 
15
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
16
   MA 02111-1307, USA */
 
17
 
 
18
/*
 
19
  get_ptr_compare(len) returns a pointer to a optimal byte-compare function
 
20
  for a array of stringpointer where all strings have size len.
 
21
  The bytes are compare as unsigned chars.
 
22
  Because the size is saved in a static variable.
 
23
  When using threads the program must have called my_init and the thread
 
24
  my_init_thread()
 
25
  */
 
26
 
 
27
#include "mysys_priv.h"
 
28
 
 
29
static int ptr_compare(uint *compare_length, uchar **a, uchar **b);
 
30
static int ptr_compare_0(uint *compare_length, uchar **a, uchar **b);
 
31
static int ptr_compare_1(uint *compare_length, uchar **a, uchar **b);
 
32
static int ptr_compare_2(uint *compare_length, uchar **a, uchar **b);
 
33
static int ptr_compare_3(uint *compare_length, uchar **a, uchar **b);
 
34
 
 
35
        /* Get a pointer to a optimal byte-compare function for a given size */
 
36
 
 
37
qsort2_cmp get_ptr_compare (uint size)
 
38
{
 
39
  if (size < 4)
 
40
    return (qsort2_cmp) ptr_compare;
 
41
  switch (size & 3) {
 
42
    case 0: return (qsort2_cmp) ptr_compare_0;
 
43
    case 1: return (qsort2_cmp) ptr_compare_1;
 
44
    case 2: return (qsort2_cmp) ptr_compare_2;
 
45
    case 3: return (qsort2_cmp) ptr_compare_3;
 
46
    }
 
47
  return 0;                                     /* Impossible */
 
48
}
 
49
 
 
50
 
 
51
        /*
 
52
          Compare to keys to see witch is smaller.
 
53
          Loop unrolled to make it quick !!
 
54
        */
 
55
 
 
56
#define cmp(N) if (first[N] != last[N]) return (int) first[N] - (int) last[N]
 
57
 
 
58
static int ptr_compare(uint *compare_length, uchar **a, uchar **b)
 
59
{
 
60
  reg3 int length= *compare_length;
 
61
  reg1 uchar *first,*last;
 
62
 
 
63
  first= *a; last= *b;
 
64
  while (--length)
 
65
  {
 
66
    if (*first++ != *last++)
 
67
      return (int) first[-1] - (int) last[-1];
 
68
  }
 
69
  return (int) first[0] - (int) last[0];
 
70
}
 
71
 
 
72
 
 
73
static int ptr_compare_0(uint *compare_length,uchar **a, uchar **b)
 
74
{
 
75
  reg3 int length= *compare_length;
 
76
  reg1 uchar *first,*last;
 
77
 
 
78
  first= *a; last= *b;
 
79
 loop:
 
80
  cmp(0);
 
81
  cmp(1);
 
82
  cmp(2);
 
83
  cmp(3);
 
84
  if ((length-=4))
 
85
  {
 
86
    first+=4;
 
87
    last+=4;
 
88
    goto loop;
 
89
  }
 
90
  return (0);
 
91
}
 
92
 
 
93
 
 
94
static int ptr_compare_1(uint *compare_length,uchar **a, uchar **b)
 
95
{
 
96
  reg3 int length= *compare_length-1;
 
97
  reg1 uchar *first,*last;
 
98
 
 
99
  first= *a+1; last= *b+1;
 
100
  cmp(-1);
 
101
 loop:
 
102
  cmp(0);
 
103
  cmp(1);
 
104
  cmp(2);
 
105
  cmp(3);
 
106
  if ((length-=4))
 
107
  {
 
108
    first+=4;
 
109
    last+=4;
 
110
    goto loop;
 
111
  }
 
112
  return (0);
 
113
}
 
114
 
 
115
static int ptr_compare_2(uint *compare_length,uchar **a, uchar **b)
 
116
{
 
117
  reg3 int length= *compare_length-2;
 
118
  reg1 uchar *first,*last;
 
119
 
 
120
  first= *a +2 ; last= *b +2;
 
121
  cmp(-2);
 
122
  cmp(-1);
 
123
 loop:
 
124
  cmp(0);
 
125
  cmp(1);
 
126
  cmp(2);
 
127
  cmp(3);
 
128
  if ((length-=4))
 
129
  {
 
130
    first+=4;
 
131
    last+=4;
 
132
    goto loop;
 
133
  }
 
134
  return (0);
 
135
}
 
136
 
 
137
static int ptr_compare_3(uint *compare_length,uchar **a, uchar **b)
 
138
{
 
139
  reg3 int length= *compare_length-3;
 
140
  reg1 uchar *first,*last;
 
141
 
 
142
  first= *a +3 ; last= *b +3;
 
143
  cmp(-3);
 
144
  cmp(-2);
 
145
  cmp(-1);
 
146
 loop:
 
147
  cmp(0);
 
148
  cmp(1);
 
149
  cmp(2);
 
150
  cmp(3);
 
151
  if ((length-=4))
 
152
  {
 
153
    first+=4;
 
154
    last+=4;
 
155
    goto loop;
 
156
  }
 
157
  return (0);
 
158
}