aboutsummaryrefslogtreecommitdiff
path: root/newlib/libc/machine/i386/memchr.S
blob: d9b0bf2b7ca5697688c0297d0198b9cf9d0da04f (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
/*
 * ====================================================
 * Copyright (C) 1998, 2002, 2008 by Red Hat Inc. All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this
 * software is freely granted, provided that this notice
 * is preserved.
 * ====================================================
 */

	#include "i386mach.h"

	.global	SYM (memchr)
       SOTYPE_FUNCTION(memchr)

SYM (memchr):
#ifdef __iamcu__
	pushl	edi
	movl	eax,edi
	movl	edx,eax
	xorl	edx,edx
	testl	ecx,ecx
	jz	L20

	repnz
	scasb

	setnz	dl
	decl	edi

	decl	edx
	andl	edi,edx
L20:
	movl	edx,eax

	popl	edi
#else
	pushl	ebp
	movl	esp,ebp
	pushl	edi
	movzbl	12(ebp),eax
	movl	16(ebp),ecx
	movl	8(ebp),edi
	xorl	edx,edx
	testl	ecx,ecx
	jz	L20

#ifdef __OPTIMIZE_SIZE__

	cld
	repnz
	scasb

	setnz	dl
	decl	edi

#else /* !__OPTIMIZE_SIZE__ */
/* Do byte-wise checks until string is aligned.  */
	testl	$3,edi
	je	L5
	cmpb	(edi),al
	je	L15
	incl	edi
	decl	ecx
	je	L20

	testl	$3,edi
	je	L5
	cmpb	(edi),al
	je	L15
	incl	edi
	decl	ecx
	je	L20

	testl	$3,edi
	je	L5
	cmpb	(edi),al
	je	L15
	incl	edi
	decl	ecx
	je	L20

/* Create a mask, then check a word at a time.  */
L5:
	movb	al,ah
	movl	eax,edx
	sall	$16,edx
	orl	edx,eax
	pushl	ebx

	.p2align 4,,7
L8:
	subl	$4,ecx
	jc	L9
	movl	(edi),edx
	addl	$4,edi
	xorl	eax,edx
	leal	-16843009(edx),ebx
	notl	edx
	andl	edx,ebx
	testl	$-2139062144,ebx
	je	L8

	subl	$4,edi

L9:
	popl	ebx
	xorl	edx,edx
	addl	$4,ecx
	je	L20

/* Final byte-wise checks.  */
	.p2align 4,,7
L10:
	cmpb	(edi),al
	je	L15
	incl	edi
	decl	ecx
	jne	L10

	xorl	edi,edi

#endif /* !__OPTIMIZE_SIZE__ */

L15:
	decl	edx
	andl	edi,edx
L20:
	movl	edx,eax

	leal	-4(ebp),esp
	popl	edi
	leave
#endif
	ret