| 1 |
#include "utf8.h" |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <assert.h> |
| 5 |
|
| 6 |
#include "utf8-scalar.c" |
| 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)) { |
| 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 |
|
| 48 |
return fu8_count_utf8_codepoints_avx(utf8, len); |
| 49 |
} |
| 50 |
if (len >= 16 && (instruction_set == ISET_SSE4) != 0) { |
| 51 |
|
| 52 |
return fu8_count_utf8_codepoints_sse4(utf8, len); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 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 |
|
| 84 |
|
| 85 |
|
| 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 |
|
| 117 |
} |
| 118 |
|
| 119 |
while (utf8 < utf8_end_position) { |
| 120 |
|
| 121 |
if (code_point_index == cpidx) { |
| 122 |
|
| 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 |
|
| 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; |
| 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 |
|
| 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 |
|
| 176 |
return val; |
| 177 |
} |
| 178 |
tidx--; |
| 179 |
val = tab->byte_positions[tidx]; |
| 180 |
} |
| 181 |
|
| 182 |
return 0; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 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 |
|
| 239 |
return _fu8_build_idxtab(index, 0, cplen, utf8, bytelen, 0, tab); |
| 240 |
} |