summaryrefslogtreecommitdiffstats
path: root/src/kvilib/core/kvi_strasm.h
blob: 11e3b2dfb386de582429554975eda6043ea8efd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef _KVI_STRASM_H_
#define _KVI_STRASM_H_

//=============================================================================
//
//   File : kvi_strasm.h
//   Creation date : Sun Jun 18 2000 18:38:26 by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 1999-2000 Szymon Stefanek (pragma at kvirc dot net)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

//=============================================================================
//
//   Inline assembly implementations of the commonly used string functions
//   These will work only on i386 based machines and can be compiled
//   only by gcc
//
//=============================================================================

extern inline bool kvi_strEqualCS(const char * str1,const char * str2)
{
	// An instruction pattern is really useful in this case.
	// When inlining, GCC can optimize to load esi and edi
	// directly with the strings , without pushing and getting it
	// from the stack...
	bool eax;
	__asm__ __volatile__ (
		"	cld\n"
		"1:\n"
		"	lodsb %%ds:(%%esi),%%al\n"
		"	scasb %%es:(%%edi),%%al\n"
		"	jne 2f\n"
		"	testb %%al,%%al\n"
		"	jne 1b\n"
		"	movl $0x1,%%eax\n"
		"	jmp 3f\n"
		"2:\n"
		"	xorl %%eax,%%eax\n"
		"3:"
		: "=a" (eax), "=&S" (str1), "=&D" (str2)
		:             "1"   (str1), "2"   (str2)
	);
	return eax;
}

extern inline bool kvi_strEqualCSN(const char * str1,const char * str2,int len)	
{
	bool eax;
	__asm__ __volatile__ (
		"1:\n"
		"	decl %3\n"
		"	js 2f\n"
		"	movb (%1),%%al\n"
		"	incl %1\n"
		"	cmpb %%al,(%2)\n"
		"	jne 3f\n"
		"	incl %2\n"
		"	testb %%al,%%al\n"
		"	jne 1b\n"
		"2:\n"
		"	movl $0x1,%%eax\n"
		"	jmp 4f\n"
		"3:\n"
		"	xorl %%eax,%%eax\n"
		"4:\n"
		: "=a" (eax),  "=r" (str1), "=r" (str2), "=r" (len)
		:              "1"  (str1), "2"  (str2), "3"  (len)
	);
	return eax;
}

// OPTIMIZATION
// The following two functions are used to compare a variable string with one in that
// only A-Z<->a-z case insensivity is significant.
// For example 
// kvi_strEqualNoLocalCI("a string that does not contain any strange char",str2)
// will always give the correct result
// These will NOT work with localizable characters:
// 'a' with umlaut will be not equal to 'A' with umlaut

extern inline bool kvi_strEqualNoLocaleCI(const char *str1,const char *str2)
{
	// Trivial implementation
	// Ignores completely locales....only A-Z chars are transformed to a-z
	// Anyway...it will work for IRC :)
	int reg;
	bool eax;
	__asm__ __volatile__ (
		"1:\n"
		"	movb (%2),%%al\n"
		"	cmpb $65,%%al\n"
		"	jb 2f\n"
		"	cmpb $90,%%al\n"
		"	ja 2f\n"
		"	addb $32,%%al\n"
		"2:\n"
		"	movb (%3),%b1\n"
		"	cmpb $65,%b1\n"
		"	jb 3f\n"
		"	cmpb $90,%b1\n"
		"	ja 3f\n"
		"	addb $32,%b1\n"
		"3:\n"
		"	cmpb %%al,%b1\n"
		"	jne 4f\n"
		"	incl %2\n"
		"	incl %3\n"
		"	testb %%al,%%al\n"
		"	jne 1b\n"
		"	movl $1,%%eax\n"
		"	jmp 5f\n"
		"4:\n"
		"	xorl %%eax,%%eax\n"
		"5:\n"
		: "=a" (eax), "=q" (reg), "=r" (str1), "=r" (str2)
		:                         "2"  (str1), "3"  (str2)
	);
	return eax;
}

extern inline bool kvi_strEqualNoLocaleCIN(const char *str1,const char *str2,int len)
{

	int reg;
	bool eax;
	__asm__ __volatile__ (
		"1:\n"
		"	decl %4\n"
		"	js 4f\n"
		"	movb (%2),%%al\n"
		"	cmpb $65,%%al\n"
		"	jb 2f\n"
		"	cmpb $90,%%al\n"
		"	ja 2f\n"
		"	addb $32,%%al\n"
		"2:\n"
		"	movb (%3),%b1\n"
		"	cmpb $65,%b1\n"
		"	jb 3f\n"
		"	cmpb $90,%b1\n"
		"	ja 3f\n"
		"	addb $32,%b1\n"
		"3:\n"
		"	cmpb %%al,%b1\n"
		"	jne 5f\n"
		"	incl %2\n"
		"	incl %3\n"
		"	testb %%al,%%al\n"
		"	jne 1b\n"
		"4:\n"
		"	movl $1,%%eax\n"
		"	jmp 6f\n"
		"5:\n"
		"	xorl %%eax,%%eax\n"
		"6:\n"
		: "=a" (eax), "=q" (reg), "=r" (str1), "=r" (str2), "=r" (len)
		:                         "2"  (str1), "3"  (str2), "4"  (len)
	);
	return eax;
}


extern inline int kvi_strLen(const char * str)
{
	int ecx;
	__asm__ __volatile__(
		"	cld\n"
		"	repne\n"
		"	scasb\n"
		"	notl %0\n"
		"	decl %0"
		: "=c" (ecx),        "=&D" (str)
		: "0"  (0xffffffff), "1"   (str), "a" (0)
	);
	return ecx;
}

#endif //_KVI_STRASM_H_