1
# Copyright (C) 2000 MySQL AB
2
# This program is free software; you can redistribute it and/or modify
3
# it under the terms of the GNU General Public License as published by
4
# the Free Software Foundation; version 2 of the License.
6
# This program is distributed in the hope that it will be useful,
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
# GNU General Public License for more details.
11
# You should have received a copy of the GNU General Public License
12
# along with this program; if not, write to the Free Software
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Optimized string functions Intel 80x86 (gcc/gas syntax)
22
# Move a alligned, not overlapped, by (long) divided memory area
23
# Args: to,from,length
26
.type bmove_align,@function
30
movl 4(%esp),%edi # to
31
movl 8(%esp),%esi # from
32
movl 12(%esp),%ecx # length
33
addw $3,%cx # fix if not divisible with long
38
movl -4(%esi,%ecx),%eax
39
movl %eax,-4(%edi,%ecx)
47
.size bmove_align,.bmove_align_end-bmove_align
49
# Move a string from higher to lower
50
# Arg from_end+1,to_end+1,length
53
.type bmove_upp,@function
55
movl %edi,%edx # Remember %edi
57
movl 8(%esp),%edi # dst
58
movl 16(%esp),%ecx # length
59
movl 12(%esp),%esi # source
62
subl %ecx,%esi # To start of strings
66
.bu_10: movb -1(%esi,%ecx),%al
67
movb %al,-1(%edi,%ecx)
75
.size bmove_upp,.bmove_upp_end-bmove_upp
77
# Append fillchars to string
81
.type strappend,@function
84
movl 8(%esp),%edi # Memory pointer
85
movl 12(%esp),%ecx # Length
86
clrl %eax # Find end of string
89
jnz sa_99 # String to long, shorten it
90
movzb 16(%esp),%eax # Fillchar
91
decl %edi # Point at end null
92
incl %ecx # rep made one dec for null-char
94
movb %al,%ah # (2) Set up a 32 bit pattern.
97
movw %dx,%ax # (2) %eax has the 32 bit pattern.
99
movl %ecx,%edx # (2) Save the count of bytes.
100
shrl $2,%ecx # (2) Number of dwords.
104
and %edx,%ecx # (2) Fill in the odd bytes
106
stosb # Move last bytes if any
108
sa_99: movb $0,(%edi) # End of string
112
.size strappend,.strappend_end-strappend
114
# Find if string contains any char in another string
116
# Ret: Pointer to first found char in str
119
.type strcont,@function
123
movl 8(%esp),%esi # str
124
movl 12(%esp),%ecx # set
125
clrb %ah # For endtest
129
jz sc_fo # Found char
130
sc_20: cmp (%edi),%ah # Test if null
131
jnz sc_10 # Not end of set yet
132
incl %esi # Next char in str
133
sc_60: movl %ecx,%edi # %edi = Set
134
movb (%esi),%al # Test if this char exist
136
jnz sc_20 # Not end of string
137
clrl %esi # Return Null
138
sc_fo: movl %esi,%eax # Char found here
139
movl %edx,%edi # Restore
143
.size strcont,.strcont_end-strcont
147
# ret: Pointer to end null
150
.type strend,@function
152
movl %edi,%edx # Save
153
movl 4(%esp),%edi # str
154
clrl %eax # Find end of string
160
decl %eax # End of string
161
movl %edx,%edi # Restore
164
.size strend,.strend_end-strend
166
# Make a string with len fill-chars and endnull
167
# Args: dest,len,fill
171
.type strfill,@function
174
movl 8(%esp),%edi # Memory pointer
175
movl 12(%esp),%ecx # Length
176
movzb 16(%esp),%eax # Fill
178
movb %al,%ah # (2) Set up a 32 bit pattern
181
movw %dx,%ax # (2) %eax has the 32 bit pattern.
183
movl %ecx,%edx # (2) Save the count of bytes.
184
shrl $2,%ecx # (2) Number of dwords.
188
and %edx,%ecx # (2) Fill in the odd bytes
190
stosb # Move last bytes if any
192
movb %cl,(%edi) # End NULL
193
movl %edi,%eax # End i %eax
197
.size strfill,.strfill_end-strfill
200
# Find a char in or end of a string
202
# Ret: pointer to found char or NullS
205
.type strcend,@function
208
movl 4(%esp),%edi # str
209
movb 8(%esp),%ah # search
210
clrb %al # for scasb to find end
212
se_10: cmpb (%edi),%ah
213
jz se_20 # Found char
216
dec %edi # Not found, point at end of string
217
se_20: movl %edi,%eax
218
movl %edx,%edi # Restore
221
.size strcend,.strcend_end-strcend
223
# Test if string has a given suffix
226
.type is_prefix,@function
228
movl %edi,%edx # Save %edi
229
pushl %esi # and %esi
230
movl 12(%esp),%esi # get suffix
231
movl 8(%esp),%edi # s1
232
movl $1,%eax # Ok and zero-test
233
ip_10: cmpb (%esi),%ah
234
jz suf_ok # End of string/ found suffix
235
cmpsb # Compare strings
236
jz ip_10 # Same, possible prefix
237
xor %eax,%eax # Not suffix
242
.size is_prefix,.is_prefix_end-is_prefix
244
# Find a substring in string
248
.type strstr,@function
253
movl 12(%esp),%esi # str
254
movl 16(%esp),%edi # search
256
incl %ecx # %ecx = search+1
257
movb (%edi),%ah # %ah = First char in search
260
sf_00: movl %edx,%esi # si = Current str-pos
261
sf_10: movb (%esi),%al # Test if this char exist
263
jz sf_90 # End of string, didn't find search
266
jnz sf_10 # Didn't find first char, continue
267
movl %esi,%edx # Save str-pos in %edx
269
sf_20: cmpb $0,(%edi)
270
jz sf_fo # Found substring
273
jmp sf_00 # Next str-pos
275
sf_90: movl $1,%edx # Return Null
276
sf_fo: movl %edx,%eax # Char found here
277
decl %eax # Pointed one after
282
.size strstr,.strstr_end-strstr
285
# Find a substring in string, return index
289
.type strinstr,@function
294
pushl 12(%ebp) # search
299
jz si_99 # Not found, return NULL
300
sub 8(%ebp),%eax # Pos from start
301
inc %eax # And first pos = 1
305
.size strinstr,.strinstr_end-strinstr
307
# Make a string of len length from another string
308
# Arg: dst,src,length
312
.type strmake,@function
317
mov 12(%esp),%edi # dst
319
movl 20(%esp),%ecx # length
320
movl 16(%esp),%esi # src
323
sm_00: movb (%esi,%edx),%al
330
sm_90: movb $0,(%edi,%edx)
331
sm_99: lea (%edi,%edx),%eax # Return pointer to end null
336
.size strmake,.strmake_end-strmake
338
# Move a string with max len chars
340
# ret: pos to first null or dst+len
343
.type strnmov,@function
347
movl 12(%esp),%edi # dst
348
movl 16(%esp),%esi # src
349
movl 20(%esp),%ecx # Length of memory-area
350
jecxz snm_99 # Nothing to do
351
clrb %al # For test of end-null
353
snm_10: cmpb (%esi),%al # Next char to move
355
jz snm_20 # last char, fill with null
356
loop snm_10 # Continue moving
357
incl %edi # Point two after last
358
snm_20: decl %edi # Point at first null (or last+1)
359
snm_99: movl %edi,%eax # Pointer at last char
364
.size strnmov,.strnmov_end-strnmov
368
.type strmov,@function
370
movl %esi,%ecx # Save old %esi and %edi
372
movl 8(%esp),%esi # get source pointer (s2)
373
movl 4(%esp),%edi # %edi -> s1
374
smo_10: movb (%esi),%al
377
jnz smo_10 # Not last
380
movl %ecx,%esi # Restore
384
.size strmov,.strmov_end-strmov
387
.type strxmov,@function
389
movl %ebx,%edx # Save %ebx, %esi and %edi
392
leal 8(%esp),%ebx # Get destination
395
jmp next_str # Handle source ebx+4
401
decl %edi # Don't copy last null
408
movb %al,0(%edi) # Force last to ASCII 0
410
movl %edi,%eax # Return ptr to ASCII 0
411
pop %edi # Restore registers
416
.size strxmov,.strxmov_end-strxmov