~ubuntu-branches/ubuntu/hoary/kvirc/hoary

« back to all changes in this revision

Viewing changes to src/kvilib/core/kvi_strasm.h

  • Committer: Bazaar Package Importer
  • Author(s): Robin Verduijn
  • Date: 2004-12-14 15:32:19 UTC
  • mfrom: (0.2.1 upstream) (1.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20041214153219-fdink3gyp2s20b6g
Tags: 2:2.1.3.1-2
* Change Recommends on xmms to a Suggests.
* Rebuild against KDE 3.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef _KVI_STRASM_H_INCLUDED_
 
2
#define _KVI_STRASM_H_INCLUDED_
 
3
 
 
4
// =============================================================================
 
5
//
 
6
//      --- kvi_strasm.h ---
 
7
//
 
8
//   This file is part of the KVIrc IRC client distribution
 
9
//   Copyright (C) 1999-2000 Szymon Stefanek (stefanek@tin.it)
 
10
//
 
11
//   This program is FREE software. You can redistribute it and/or
 
12
//   modify it under the terms of the GNU General Public License
 
13
//   as published by the Free Software Foundation; either version 2
 
14
//   of the License, or (at your opinion) any later version.
 
15
//
 
16
//   This program is distributed in the HOPE that it will be USEFUL,
 
17
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
19
//   See the GNU General Public License for more details.
 
20
//
 
21
//   You should have received a copy of the GNU General Public License
 
22
//   along with this program. If not, write to the Free Software Foundation,
 
23
//   Inc, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
24
//
 
25
// =============================================================================
 
26
 
 
27
//
 
28
//   Inline assembly implementations of the commonly used string functions
 
29
//   These will work only on i386 based machines and can be compiled
 
30
//   only by gcc
 
31
//
 
32
 
 
33
extern inline bool kvi_strEqualCS(const char *str1, const char *str2)
 
34
{
 
35
        // An instruction pattern is really useful in this case.
 
36
        // When inlining, GCC can optimize to load esi and edi
 
37
        // directly with the strings, without pushing and getting it
 
38
        // from the stack...
 
39
        register bool eax;
 
40
        __asm__ __volatile__ (
 
41
                "       cld\n"
 
42
                "1:\n"
 
43
                "       lodsb %%ds:(%%esi),%%al\n"
 
44
                "       scasb %%es:(%%edi),%%al\n"
 
45
                "       jne 2f\n"
 
46
                "       testb %%al,%%al\n"
 
47
                "       jne 1b\n"
 
48
                "       movl $0x1,%%eax\n"
 
49
                "       jmp 3f\n"
 
50
                "2:\n"
 
51
                "       xorl %%eax,%%eax\n"
 
52
                "3:"
 
53
                : "=a" (eax), "=&S" (str1), "=&D" (str2)
 
54
                :             "1"   (str1), "2"   (str2)
 
55
        );
 
56
        return eax;
 
57
}
 
58
 
 
59
extern inline bool kvi_strEqualCSN(const char *str1, const char *str2, int len)
 
60
{
 
61
        register bool eax;
 
62
        __asm__ __volatile__ (
 
63
                "1:\n"
 
64
                "       decl %3\n"
 
65
                "       js 2f\n"
 
66
                "       movb (%1),%%al\n"
 
67
                "       incl %1\n"
 
68
                "       cmpb %%al,(%2)\n"
 
69
                "       jne 3f\n"
 
70
                "       incl %2\n"
 
71
                "       testb %%al,%%al\n"
 
72
                "       jne 1b\n"
 
73
                "2:\n"
 
74
                "       movl $0x1,%%eax\n"
 
75
                "       jmp 4f\n"
 
76
                "3:\n"
 
77
                "       xorl %%eax,%%eax\n"
 
78
                "4:\n"
 
79
                : "=a" (eax),  "=r" (str1), "=r" (str2), "=r" (len)
 
80
                :              "1"  (str1), "2"  (str2), "3"  (len)
 
81
        );
 
82
        return eax;
 
83
}
 
84
 
 
85
// OPTIMIZATION
 
86
// The following two functions are used to compare a variable string with one in that
 
87
// only A-Z <-> a-z case insensivity is significant.
 
88
// For example:
 
89
//     kvi_strEqualNoLocalCI("a string that does not contain any strange char", str2)
 
90
// will always give the correct result
 
91
// These will NOT work with localizable characters:
 
92
// 'a' with umlaut will be not equal to 'A' with umlaut
 
93
extern inline bool kvi_strEqualNoLocaleCI(const char *str1, const char *str2)
 
94
{
 
95
        // Trivial implementation
 
96
        // Completely ignores locale. Only A-Z chars are transformed to a-z
 
97
        // Anyway, it will work for IRC :)
 
98
        register int reg;
 
99
        register bool eax;
 
100
        __asm__ __volatile__ (
 
101
                "1:\n"
 
102
                "       movb (%2),%%al\n"
 
103
                "       cmpb $65,%%al\n"
 
104
                "       jb 2f\n"
 
105
                "       cmpb $90,%%al\n"
 
106
                "       ja 2f\n"
 
107
                "       addb $32,%%al\n"
 
108
                "2:\n"
 
109
                "       movb (%3),%b1\n"
 
110
                "       cmpb $65,%b1\n"
 
111
                "       jb 3f\n"
 
112
                "       cmpb $90,%b1\n"
 
113
                "       ja 3f\n"
 
114
                "       addb $32,%b1\n"
 
115
                "3:\n"
 
116
                "       cmpb %%al,%b1\n"
 
117
                "       jne 4f\n"
 
118
                "       incl %2\n"
 
119
                "       incl %3\n"
 
120
                "       testb %%al,%%al\n"
 
121
                "       jne 1b\n"
 
122
                "       movl $1,%%eax\n"
 
123
                "       jmp 5f\n"
 
124
                "4:\n"
 
125
                "       xorl %%eax,%%eax\n"
 
126
                "5:\n"
 
127
                : "=a" (eax), "=q" (reg), "=r" (str1), "=r" (str2)
 
128
                :                         "2"  (str1), "3"  (str2)
 
129
        );
 
130
        return eax;
 
131
}
 
132
 
 
133
extern inline bool kvi_strEqualNoLocaleCIN(const char *str1, const char *str2, int len)
 
134
{
 
135
        register int reg;
 
136
        register bool eax;
 
137
        __asm__ __volatile__ (
 
138
                "1:\n"
 
139
                "       decl %4\n"
 
140
                "       js 4f\n"
 
141
                "       movb (%2),%%al\n"
 
142
                "       cmpb $65,%%al\n"
 
143
                "       jb 2f\n"
 
144
                "       cmpb $90,%%al\n"
 
145
                "       ja 2f\n"
 
146
                "       addb $32,%%al\n"
 
147
                "2:\n"
 
148
                "       movb (%3),%b1\n"
 
149
                "       cmpb $65,%b1\n"
 
150
                "       jb 3f\n"
 
151
                "       cmpb $90,%b1\n"
 
152
                "       ja 3f\n"
 
153
                "       addb $32,%b1\n"
 
154
                "3:\n"
 
155
                "       cmpb %%al,%b1\n"
 
156
                "       jne 5f\n"
 
157
                "       incl %2\n"
 
158
                "       incl %3\n"
 
159
                "       testb %%al,%%al\n"
 
160
                "       jne 1b\n"
 
161
                "4:\n"
 
162
                "       movl $1,%%eax\n"
 
163
                "       jmp 6f\n"
 
164
                "5:\n"
 
165
                "       xorl %%eax,%%eax\n"
 
166
                "6:\n"
 
167
                : "=a" (eax), "=q" (reg), "=r" (str1), "=r" (str2), "=r" (len)
 
168
                :                         "2"  (str1), "3"  (str2), "4"  (len)
 
169
        );
 
170
        return eax;
 
171
}
 
172
 
 
173
extern inline int kvi_strLen(const char *str)
 
174
{
 
175
        register int ecx;
 
176
        __asm__ __volatile__(
 
177
                "       cld\n"
 
178
                "       repne\n"
 
179
                "       scasb\n"
 
180
                "       notl %0\n"
 
181
                "       decl %0"
 
182
                : "=c" (ecx),        "=&D" (str)
 
183
                : "0"  (0xffffffff), "1"   (str), "a" (0)
 
184
        );
 
185
        return ecx;
 
186
}
 
187
 
 
188
#endif // _KVI_STRASM_H_INCLUDED_