~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to lib/ts/test_memchr.cc

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 
 
3
  A brief file description
 
4
 
 
5
  @section license License
 
6
 
 
7
  Licensed to the Apache Software Foundation (ASF) under one
 
8
  or more contributor license agreements.  See the NOTICE file
 
9
  distributed with this work for additional information
 
10
  regarding copyright ownership.  The ASF licenses this file
 
11
  to you under the Apache License, Version 2.0 (the
 
12
  "License"); you may not use this file except in compliance
 
13
  with the License.  You may obtain a copy of the License at
 
14
 
 
15
      http://www.apache.org/licenses/LICENSE-2.0
 
16
 
 
17
  Unless required by applicable law or agreed to in writing, software
 
18
  distributed under the License is distributed on an "AS IS" BASIS,
 
19
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
20
  See the License for the specific language governing permissions and
 
21
  limitations under the License.
 
22
 */
 
23
 
 
24
#include <stdio.h>
 
25
 
 
26
//
 
27
// fast memchr
 
28
//
 
29
void *
 
30
ink_memchr(const void *as, int ac, size_t an)
 
31
{
 
32
  unsigned char c = (unsigned char) ac;
 
33
  unsigned char *s = (unsigned char *) as;
 
34
 
 
35
  // initial segment
 
36
 
 
37
  int i_len = (int)(((uintptr_t) 8 - (uintptr_t) as) & 7);
 
38
 
 
39
  // too short to concern us
 
40
 
 
41
  if ((int) an < i_len) {
 
42
    for (int i = 0; i < (int) an; i++)
 
43
      if (s[i] == c)
 
44
        return &s[i];
 
45
    return 0;
 
46
  }
 
47
  // bytes 0-3
 
48
 
 
49
  switch (i_len & 3) {
 
50
  case 3:
 
51
    if (*s++ == c)
 
52
      return s - 1;
 
53
  case 2:
 
54
    if (*s++ == c)
 
55
      return s - 1;
 
56
  case 1:
 
57
    if (*s++ == c)
 
58
      return s - 1;
 
59
  case 0:
 
60
    break;
 
61
  }
 
62
 
 
63
  // bytes 4-8
 
64
 
 
65
  unsigned int ib = c;
 
66
  ib |= (ib << 8);
 
67
  ib |= (ib << 16);
 
68
  unsigned int im = 0x7efefeff;
 
69
  if (i_len & 4) {
 
70
    unsigned int ibp = *(unsigned int *) s;
 
71
    unsigned int ibb = ibp ^ ib;
 
72
    ibb = ((ibb + im) ^ ~ibb) & ~im;
 
73
    if (ibb) {
 
74
      if (s[0] == c)
 
75
        return &s[0];
 
76
      if (s[1] == c)
 
77
        return &s[1];
 
78
      if (s[2] == c)
 
79
        return &s[2];
 
80
      if (s[3] == c)
 
81
        return &s[3];
 
82
    }
 
83
    s += 4;
 
84
  }
 
85
  // next 8x bytes
 
86
  uint64_t m = 0x7efefefefefefeffLL;
 
87
  uint64_t b = ((uint64_t) ib);
 
88
  b |= (b << 32);
 
89
  uint64_t *p = (uint64_t *) s;
 
90
  unsigned int n = (((unsigned int) an) - (s - (unsigned char *) as)) >> 3;
 
91
  uint64_t *end = p + n;
 
92
  while (p < end) {
 
93
    uint64_t bp = *p;
 
94
    uint64_t bb = bp ^ b;
 
95
    bb = ((bb + m) ^ ~bb) & ~m;
 
96
    if (bb) {
 
97
      s = (unsigned char *) p;
 
98
      if (s[0] == c)
 
99
        return &s[0];
 
100
      if (s[1] == c)
 
101
        return &s[1];
 
102
      if (s[2] == c)
 
103
        return &s[2];
 
104
      if (s[3] == c)
 
105
        return &s[3];
 
106
      if (s[4] == c)
 
107
        return &s[4];
 
108
      if (s[5] == c)
 
109
        return &s[5];
 
110
      if (s[6] == c)
 
111
        return &s[6];
 
112
      if (s[7] == c)
 
113
        return &s[7];
 
114
    }
 
115
    p++;
 
116
  }
 
117
 
 
118
  // terminal segement
 
119
 
 
120
  i_len = an - (((unsigned char *) p) - ((unsigned char *) as));
 
121
  s = (unsigned char *) p;
 
122
 
 
123
  // n-(4..8)..n bytes
 
124
 
 
125
  if (i_len & 4) {
 
126
    unsigned int ibp = *(unsigned int *) s;
 
127
    unsigned int ibb = ibp ^ ib;
 
128
    ibb = ((ibb + im) ^ ~ibb) & ~im;
 
129
    if (ibb) {
 
130
      if (s[0] == c)
 
131
        return &s[0];
 
132
      if (s[1] == c)
 
133
        return &s[1];
 
134
      if (s[2] == c)
 
135
        return &s[2];
 
136
      if (s[3] == c)
 
137
        return &s[3];
 
138
    }
 
139
    s += 4;
 
140
  }
 
141
  // n-(0..3)..n bytes
 
142
 
 
143
  switch (i_len & 3) {
 
144
  case 3:
 
145
    if (*s++ == c)
 
146
      return s - 1;
 
147
  case 2:
 
148
    if (*s++ == c)
 
149
      return s - 1;
 
150
  case 1:
 
151
    if (*s++ == c)
 
152
      return s - 1;
 
153
  case 0:
 
154
    break;
 
155
  }
 
156
  return 0;
 
157
}
 
158
 
 
159
 
 
160
#define MEMCHR(_s,_c) ink_memchr(_s,_c,strlen(_s))?:""
 
161
main()
 
162
{
 
163
  int i = 0;
 
164
  printf("%d %s\n", i++, MEMCHR("a;ldkfjoiwenalkdufla asdfj3i", ' '));
 
165
  printf("%d %s\n", i++, MEMCHR("a;ldkfjoiwenalkdufla asdfj3i", '3'));
 
166
  printf("%d %s\n", i++, MEMCHR("a;ldkfjoiwenalkdufla asdfj3i", '\n'));
 
167
  printf("%d %s\n", i++, MEMCHR("a;ldkfjoiwenalk$uflaE$$dfj3i", '$'));
 
168
  printf("%d %s\n", i++, MEMCHR("a;ldkfjoiwenalkd#####asdfj3i", '#'));
 
169
  printf("%d %s\n", i++, MEMCHR("a;ldkfjoiwenalkdufla a^^sdfj3i", '^'));
 
170
  printf("%d %s\n", i++, MEMCHR("a;ldkfjoiwenalkdufla asd*************fj3i", '*'));
 
171
}