Debian Codegraph @

rpython/rlib/fastutf8/src/utf8.c

raw symbols SCIP
1 #include "utf8.h"
2
3 #include <stdio.h>
4 #include <assert.h>
5
6 #include "utf8-scalar.c" // copy code for scalar operations
7
8
9 int instruction_set = -1;
10 #define ISET_SSE4 0x1
11 #define ISET_AVX 0x2
12 #define ISET_AVX2 0x4
13
14 void detect_instructionset(void)
15 {
16 long eax;
17 long ebx;
18 long ecx;
19 long edx;
20 long op = 1;
21 asm ("cpuid"
22 : "=a" (eax),
23 "=b" (ebx),
24 "=c" (ecx),
25 "=d" (edx)
26 : "a" (op));
27
28 instruction_set = 0;
29 if (ecx & (1<<19)) { // sse4.1
30 instruction_set |= ISET_SSE4;
31 }
32 if(__builtin_cpu_supports("avx")) {
33 instruction_set |= ISET_AVX;
34 }
35 if(__builtin_cpu_supports("avx2")) {
36 instruction_set |= ISET_AVX2;
37 }
38 }
39
40 ssize_t fu8_count_utf8_codepoints(const char * utf8, size_t len)
41 {
42 if (instruction_set == -1) {
43 detect_instructionset();
44 }
45
46 if (len >= 32 && (instruction_set & ISET_AVX2) != 0) {
47 // to the MOON!
48 return fu8_count_utf8_codepoints_avx(utf8, len);
49 }
50 if (len >= 16 && (instruction_set == ISET_SSE4) != 0) {
51 // speed!!
52 return fu8_count_utf8_codepoints_sse4(utf8, len);
53 }
54
55 // oh no, just do it sequentially!
56 return fu8_count_utf8_codepoints_seq(utf8, len);
57 }
58
59 typedef struct fu8_idxtab {
60 int character_step;
61 size_t * byte_positions;
62 size_t bytepos_table_length;
63 } fu8_idxtab_t;
64
65 #include <stdlib.h>
66
67 fu8_idxtab_t * _fu8_alloc_idxtab(int cp_count, int character_step)
68 {
69 if (cp_count <= character_step) {
70 return NULL;
71 }
72 long s = (cp_count/character_step) * sizeof(size_t);
73 char * c = calloc(1, sizeof(fu8_idxtab_t)+s);
74 fu8_idxtab_t * i = (fu8_idxtab_t*)c;
75 i->character_step = character_step;
76 i->byte_positions = (size_t*)(c + sizeof(fu8_idxtab_t));
77 i->bytepos_table_length = cp_count/character_step;
78 return i;
79 }
80
81 void fu8_free_idxtab(struct fu8_idxtab * t)
82 {
83 // why manage this in C?
84 // it might at some point have a different data structure,
85 // then we can handle this easily here without modifying the API
86 free(t); t = NULL;
87 }
88
89 void _fu8_itab_set_bucket(struct fu8_idxtab * tab, int bucket, size_t off, size_t cpidx)
90 {
91 size_t oldval = tab->byte_positions[bucket];
92 if (oldval != 0) {
93 assert(oldval != off && "table mismatch");
94 }
95 assert(bucket >= 0 && bucket < tab->bytepos_table_length && "index out of bounds");
96 tab->byte_positions[bucket] = off;
97 }
98
99 ssize_t _fu8_build_idxtab(size_t cpidx, size_t cpidx_off, size_t cplen,
100 const uint8_t * utf8, size_t bytelen, size_t byteoff,
101 struct fu8_idxtab ** tab) {
102 size_t code_point_index = cpidx_off;
103 const uint8_t * utf8_start_position = utf8 + byteoff;
104 const uint8_t * utf8_end_position = utf8 + bytelen - byteoff;
105
106 struct fu8_idxtab * itab = tab[0];
107 if (itab == NULL) {
108 tab[0] = itab = _fu8_alloc_idxtab(cplen, 1000);
109 }
110
111 int bucket_step = -1;
112 int bucket = -1;
113 if (itab) {
114 bucket_step = itab->character_step;
115 bucket = cpidx_off / bucket_step;
116 //printf("bucket %d step %d iindex_off %ld\n", bucket, bucket_step, cpidx_off);
117 }
118
119 while (utf8 < utf8_end_position) {
120 //printf("%d %llx ok\n", code_point_index, utf8);
121 if (code_point_index == cpidx) {
122 //printf("return %llx %llx %llx\n", utf8_start_position, utf8, utf8_end_position);
123 return utf8 - utf8_start_position;
124 }
125
126 if (bucket_step != -1 && code_point_index != 0 && (code_point_index % bucket_step) == 0) {
127 _fu8_itab_set_bucket(itab, bucket++, byteoff + utf8 - utf8_start_position, code_point_index);
128 }
129
130 uint8_t c = *utf8++;
131 //printf("%x\n", c);
132 code_point_index += 1;
133 if ((c & 0xc0) == 0) {
134 continue;
135 }
136 if ((c & 0xe0) == 0xc0) {
137 utf8 += 1;
138 continue;
139 }
140 if ((c & 0xf0) == 0xe0) {
141 utf8 += 2;
142 continue;
143 }
144 if ((c & 0xf8) == 0xf0) {
145 utf8 += 3;
146 continue;
147 }
148 }
149
150 return -1; // out of bounds!!
151 }
152
153 size_t _fu8_idxtab_lookup_bytepos_i(struct fu8_idxtab * tab, size_t cpidx);
154
155 ssize_t _fu8_idx2bytepos(size_t index,
156 const uint8_t * utf8, size_t bytelen, size_t cplen,
157 struct fu8_idxtab ** tab)
158 {
159
160 assert(index != 0 && "index must not be 0");
161 // note that itab STILL can be NULL
162
163 }
164
165 size_t _fu8_idxtab_lookup_bytepos_i(struct fu8_idxtab * tab, size_t cpidx)
166 {
167 if (cpidx == 0 || tab == NULL) {
168 return 0;
169 }
170 int step = tab->character_step;
171 int tidx = cpidx / step;
172 size_t val = tab->byte_positions[tidx];
173 while (tidx > 0) {
174 if (val != 0) {
175 //printf("%llx at %d %d/%d\n", val, tidx, cpidx, step);
176 return val;
177 }
178 tidx--;
179 val = tab->byte_positions[tidx];
180 }
181 // no clue, start at the beginning!
182 return 0;
183
184 //int lp, rp; // left position, right position
185 //int mp; // middle position
186 //int count;
187 //lp = 0;
188 //rp = 16;
189
190 //if (cpidx == 0) {
191 // return -1;
192 //}
193
194 //size_t valid_left = -1;
195
196 //do {
197 // count = (rp - lp);
198 // mp = lp + count / 2;
199
200 // size_t lval = tab->codepoint_positions[lp];
201 // size_t mval = tab->codepoint_positions[mp];
202 // size_t rval = tab->codepoint_positions[rp];
203 // printf("l %d m %d r %d\nlv %d mv %d rv %d\n", lp, mp, rp, lval, mval, rval);
204 // if (lval != 0 && lval <= cpidx) {
205 // valid_left = lp;
206 // } else if (lval == 0) {
207 // // nothing is known about the left most value
208 // break;
209 // }
210
211 // if (mval == cpidx) {
212 // return mp;
213 // }
214
215 // if (mval == 0 || mval < cpidx) {
216 // // nothing is known about the middle value,
217 // // or mval is smaller the searched code point index
218 // rp = mp;
219 // continue;
220 // } else {
221 // lp = mp;
222 // continue;
223 // }
224
225 //} while (count > 1);
226
227 //return valid_left;
228 }
229
230 ssize_t fu8_idx2bytepos(size_t index,
231 const uint8_t * utf8, size_t bytelen,
232 size_t cplen,
233 struct fu8_idxtab ** tab)
234 {
235 if (index == 0) { return 0; }
236 if (index >= cplen) { return -1; }
237 size_t off = _fu8_idxtab_lookup_bytepos_i(tab[0], index);
238 //printf("found %llx\n", off);
239 return _fu8_build_idxtab(index, 0, cplen, utf8, bytelen, 0, tab);
240 }