Welcome to mirror list, hosted at ThFree Co, Russian Federation.

memchr.c « xscale « machine « libc « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9bf18268bde8477355021592ac9ed2032fe86070 (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
#if defined __thumb__

#include "../../string/memchr.c"

#else

#include <string.h>
#include "xscale.h"

void *
memchr (const void *start, int c, size_t len)
{
  const char *str = start;

  if (len == 0)
    return 0;

  asm (PRELOADSTR ("%0") : : "r" (start));

  c &= 0xff;

#ifndef __OPTIMIZE_SIZE__
  /* Skip unaligned part.  */
  if ((long)str & 3)
    {
      str--;
      do
	{
	  if (*++str == c)
	    return (void *)str;
	}
      while (((long)str & 3) != 0 && --len > 0);
    }

  if (len > 3)
    {
      unsigned int c2 = c + (c << 8);
      c2 += c2 << 16;

      /* Load two constants:
         R7 = 0xfefefeff [ == ~(0x80808080 << 1) ]
         R6 = 0x80808080  */

      asm (
       "mov	r6, #0x80\n\
	add	r6, r6, #0x8000\n\
	add	r6, r6, r6, lsl #16\n\
	mvn	r7, r6, lsl #1\n\
\n\
0:\n\
	cmp	%1, #0x7\n\
	bls	1f\n\
\n\
	ldmia	%0!, { r3, r9 }\n\
"	PRELOADSTR ("%0") "\n\
	sub	%1, %1, #8\n\
	eor	r3, r3, %2\n\
	eor	r9, r9, %2\n\
	add	r2, r3, r7\n\
	add	r8, r9, r7\n\
	bic	r2, r2, r3\n\
	bic	r8, r8,	r9\n\
	and	r1, r2, r6\n\
	and	r9, r8, r6\n\
	orrs	r1, r1, r9\n\
	beq	0b\n\
\n\
	add	%1, %1, #8\n\
	sub	%0, %0, #8\n\
1:\n\
	cmp	%1, #0x3\n\
	bls	2f\n\
\n\
	ldr	r3, [%0], #4\n\
"	PRELOADSTR ("%0") "\n\
	sub	%1, %1, #4\n\
	eor	r3, r3, %2\n\
	add	r2, r3, r7\n\
	bic	r2, r2, r3\n\
	ands	r1, r2, r6\n\
	beq	1b\n\
\n\
	sub	%0, %0, #4\n\
	add	%1, %1, #4\n\
2:\n\
"
       : "=&r" (str), "=&r" (len)
       : "r" (c2), "0" (str), "1" (len)
       : "r1", "r2", "r3", "r6", "r7", "r8", "r9", "cc");
    }
#endif

  while (len-- > 0)
    { 
      if (*str == c)
        return (void *)str;
      str++;
    } 

  return 0;
}
#endif