1/*
2 * ====================================================
3 * Copyright (C) 1998, 2002, 2008 by Red Hat Inc. All rights reserved.
4 *
5 * Permission to use, copy, modify, and distribute this
6 * software is freely granted, provided that this notice
7 * is preserved.
8 * ====================================================
9 */
10
11#include <picolibc.h>
12
13	#include "i386mach.h"
14
15	.global	SYM (memchr)
16       SOTYPE_FUNCTION(memchr)
17
18SYM (memchr):
19#ifdef __iamcu__
20	pushl	edi
21	movl	eax,edi
22	movl	edx,eax
23	xorl	edx,edx
24	testl	ecx,ecx
25	jz	L20
26
27	repnz
28	scasb
29
30	setnz	dl
31	decl	edi
32
33	decl	edx
34	andl	edi,edx
35L20:
36	movl	edx,eax
37
38	popl	edi
39#else
40	pushl	ebp
41	movl	esp,ebp
42	pushl	edi
43	movzbl	12(ebp),eax
44	movl	16(ebp),ecx
45	movl	8(ebp),edi
46	xorl	edx,edx
47	testl	ecx,ecx
48	jz	L20
49
50#ifdef __OPTIMIZE_SIZE__
51
52	cld
53	repnz
54	scasb
55
56	setnz	dl
57	decl	edi
58
59#else /* !__OPTIMIZE_SIZE__ */
60/* Do byte-wise checks until string is aligned.  */
61	testl	$3,edi
62	je	L5
63	cmpb	(edi),al
64	je	L15
65	incl	edi
66	decl	ecx
67	je	L20
68
69	testl	$3,edi
70	je	L5
71	cmpb	(edi),al
72	je	L15
73	incl	edi
74	decl	ecx
75	je	L20
76
77	testl	$3,edi
78	je	L5
79	cmpb	(edi),al
80	je	L15
81	incl	edi
82	decl	ecx
83	je	L20
84
85/* Create a mask, then check a word at a time.  */
86L5:
87	movb	al,ah
88	movl	eax,edx
89	sall	$16,edx
90	orl	edx,eax
91	pushl	ebx
92
93	.p2align 4,,7
94L8:
95	subl	$4,ecx
96	jc	L9
97	movl	(edi),edx
98	addl	$4,edi
99	xorl	eax,edx
100	leal	-16843009(edx),ebx
101	notl	edx
102	andl	edx,ebx
103	testl	$-2139062144,ebx
104	je	L8
105
106	subl	$4,edi
107
108L9:
109	popl	ebx
110	xorl	edx,edx
111	addl	$4,ecx
112	je	L20
113
114/* Final byte-wise checks.  */
115	.p2align 4,,7
116L10:
117	cmpb	(edi),al
118	je	L15
119	incl	edi
120	decl	ecx
121	jne	L10
122
123	xorl	edi,edi
124
125#endif /* !__OPTIMIZE_SIZE__ */
126
127L15:
128	decl	edx
129	andl	edi,edx
130L20:
131	movl	edx,eax
132
133	leal	-4(ebp),esp
134	popl	edi
135	leave
136#endif
137	ret
138
139#if defined(__linux__) && defined(__ELF__)
140.section .note.GNU-stack,"",%progbits
141#endif
142