-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathutf.cc
More file actions
601 lines (515 loc) · 13 KB
/
Copy pathutf.cc
File metadata and controls
601 lines (515 loc) · 13 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// The Construct
//
// Copyright (C) The Construct Developers, Authors & Contributors
// Copyright (C) 2016-2020 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
//
// utf16
//
/// Decodes one or two escaped surrogates (surrogate pair) aligned to the
/// front of the input block. If the surrogates are a pair which decode into
/// a single codepoint, only the first element of the return vector is used;
/// otherwise each surrogate decodes into each element. Three surrogates
/// cannot be decoded at once, so the last two elements are never used.
ircd::u32x4
ircd::utf16::decode_surrogate_aligned_next(const u8x16 input)
noexcept
{
const u8x16 is_hex[3]
{
input >= '0' && input <= '9',
input >= 'A' && input <= 'F',
input >= 'a' && input <= 'f',
};
const u8x16 hex_nibble
{
((input - 0x30) & is_hex[0])
| ((input - 0x41 + 0x0a) & is_hex[1])
| ((input - 0x61 + 0x0a) & is_hex[2])
};
const u8x16 is_hex_nibble
{
is_hex[0] | is_hex[1] | is_hex[2]
};
// Masks the starting byte (the '\' char) of each valid surrogate.
const u8x16 is_surrogate
{
(input == '\\') &
shr<8>(input == 'u') &
shr<16>(is_hex_nibble) &
shr<24>(is_hex_nibble) &
shr<32>(is_hex_nibble) &
shr<40>(is_hex_nibble)
};
// is_surrogate may leave byte[0] and byte[6] (and possibly byte[12] which
// we don't care about here) as 0xff. Our result will be 4 byte codepoints
// matching those 6 byte inputs, so we shift the byte[6] over to byte[4]
// and stiffen the mask about to be generated.
const u32x4 surrogate_mask
(
((u32x4(is_surrogate) & 0xff) | (u32x4(is_surrogate) >> 16)) == 0xffU
);
// Decide if one or two surrogates were actually input and assert that
// between both lanes if so.
const u32x4 surrogate_deuce
{
(surrogate_mask & shr<32>(surrogate_mask)) |
(surrogate_mask & shl<32>(surrogate_mask))
};
// ASCII to integral converion of the upper nibbles
const u8x16 hex_upper
{
shr<16>(hex_nibble)
};
// ASCII to integral converion of the lower nibbles
const u8x16 hex_lower
{
shr<24>(hex_nibble)
};
// pack upper and lower nibbles into bytes, though these have a space
// between them when 4 nibbles becomes 2 bytes
const u8x16 hex_byte
{
(hex_upper << 4) | hex_lower
};
// Result for one or two unpaired surrogates
const u32x4 codepoint_unpaired
(
u8x16
{
hex_byte[2], hex_byte[0], 0, 0,
hex_byte[8], hex_byte[6], 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
}
);
// Determine if the unpaired codepoints can make a surrogate pair
const u32x4 surrogate_pair_range
(
codepoint_unpaired >= 0xd800U && codepoint_unpaired <= 0xdfffU
);
// Mask lane[0] if the codepoints are actually a surrogate pair
const u32x4 surrogate_paired
(
surrogate_pair_range & shr<32>(surrogate_pair_range)
);
// Pre-processing shuffle for surrogate pair decode
const u32x4 codepoint_pre_paired
{
shr<16>(codepoint_unpaired) | codepoint_unpaired
};
// Decode surrogate pair
const u32x4 codepoint_paired
{
0x10000U +
((codepoint_pre_paired & 0x000003ffU) << 10) +
((codepoint_pre_paired & 0x03ff0000U) >> 16)
};
// Decide if the codepoint is in the supplementary plane (3+ bytes)
const u32x4 codepoint_high
(
(codepoint_paired > 0xffffU) & surrogate_paired
);
// Decide if the codepoint is in the BMP (2- bytes)
const u32x4 codepoint_low
{
(codepoint_paired <= 0xffffU) & ~(shl<32>(codepoint_high))
};
// When one surrogate is input, only lane[0]
const u32x4 ret_codepoint_single
{
codepoint_unpaired & ~surrogate_pair_range & ~surrogate_deuce
};
// When two surrogates in a pair are input, lane[0] only
const u32x4 ret_codepoint_paired
{
codepoint_paired & (surrogate_paired & surrogate_deuce)
};
// When two unrelated surrogates are input, lane[0] and lane[1]
const u32x4 ret_codepoint_unpaired
{
codepoint_unpaired & ~surrogate_pair_range & surrogate_deuce
};
static const u32x4
mask_one { -1U, 0U, 0U, 0U, },
mask_two { -1U, -1U, 0U, 0U, };
return 0
| (ret_codepoint_single & mask_one)
| (ret_codepoint_paired & mask_one)
| (ret_codepoint_unpaired & mask_two)
;
}
namespace ircd::utf16
{
static const u128x1 full_mask {~u128x1{0}};
extern const u8x16 truncation_table[6];
}
decltype(ircd::utf16::truncation_table)
ircd::utf16::truncation_table
{
~shl<0x30>(~full_mask),
~shl<0x28>(~full_mask),
~shl<0x20>(~full_mask),
~shl<0x18>(~full_mask),
~shl<0x10>(~full_mask),
~shl<0x08>(~full_mask),
};
/// scan for utf-16 surrogates including incomplete sequences truncated
/// by the end of the input; also matches a single trailing slash.
ircd::u8x16
ircd::utf16::find_surrogate_partial(const u8x16 input)
noexcept
{
const u8x16 is_esc
(
input == '\\'
);
const u8x16 is_u
(
input == 'u'
);
const u8x16 hex_nibble[3]
{
input >= '0' && input <= '9',
input >= 'A' && input <= 'F',
input >= 'a' && input <= 'f',
};
const u8x16 is_hex_nibble
{
hex_nibble[0] | hex_nibble[1] | hex_nibble[2]
};
const u8x16 surrogate_sans[6]
{
// complete
is_esc
& shr<8>(is_u)
& shr<16>(is_hex_nibble) & shr<24>(is_hex_nibble)
& shr<32>(is_hex_nibble) & shr<40>(is_hex_nibble),
// sans 1
is_esc
& shr<8>(is_u)
& shr<16>(is_hex_nibble) & shr<24>(is_hex_nibble)
& shr<32>(is_hex_nibble),
// sans 2
is_esc
& shr<8>(is_u)
& shr<16>(is_hex_nibble) & shr<24>(is_hex_nibble),
// sans 3
is_esc
& shr<8>(is_u)
& shr<16>(is_hex_nibble),
// sans 4
is_esc
& shr<8>(is_u),
// sans 5
is_esc,
};
const u8x16 ret
{
(surrogate_sans[0] & truncation_table[0]) |
(surrogate_sans[1] & truncation_table[1]) |
(surrogate_sans[2] & truncation_table[2]) |
(surrogate_sans[3] & truncation_table[3]) |
(surrogate_sans[4] & truncation_table[4]) |
(surrogate_sans[5] & truncation_table[5])
};
return ret;
}
namespace ircd::utf16
{
template u8x16 utf16::find_surrogate<u8x16>(const u8x16) noexcept;
template u8x32 utf16::find_surrogate<u8x32>(const u8x32) noexcept;
template u8x64 utf16::find_surrogate<u8x64>(const u8x64) noexcept;
}
template<class u8xN>
u8xN
ircd::utf16::find_surrogate(const u8xN input)
noexcept
{
const u8xN hex_nibble[3]
{
input >= '0' && input <= '9',
input >= 'A' && input <= 'F',
input >= 'a' && input <= 'f',
};
const u8xN is_hex_nibble
{
hex_nibble[0] | hex_nibble[1] | hex_nibble[2]
};
const u8xN is_surrogate
{
(input == '\\') &
shr<8>(input == 'u') &
shr<16>(is_hex_nibble) &
shr<24>(is_hex_nibble) &
shr<32>(is_hex_nibble) &
shr<40>(is_hex_nibble)
};
return is_surrogate;
}
//
// utf8
//
ircd::u32x16
ircd::utf8::decode(const u8x16 string)
noexcept
{
const u32x16 in
(
simd::lane_cast<u32x16, u8x16>(string)
);
const u32x16 is_single
(
(in & 0x80) == 0
);
const u32x16 is_lead
(
(in - 0xc2) <= 0x32
);
const u32x16 is_trail
(
in >= 0x80 && in < 0xbf
);
const u32x16 expect_trail
{
(((in >= 0xe0) & 1) + ((in >= 0xf0) & 1) + 1) & is_lead
};
const u32x16 expect_length
{
expect_trail + 1
};
const u32x16 shift[4]
{
in << 0,
in << 8,
in << 16,
in << 24,
};
const u32x16 multibyte_packs
{
in[0x00] | shift[0x01][0x01] | shift[0x02][0x02] | shift[0x03][0x03],
in[0x01] | shift[0x01][0x02] | shift[0x02][0x03] | shift[0x03][0x04],
in[0x02] | shift[0x01][0x03] | shift[0x02][0x04] | shift[0x03][0x05],
in[0x03] | shift[0x01][0x04] | shift[0x02][0x05] | shift[0x03][0x06],
in[0x04] | shift[0x01][0x05] | shift[0x02][0x06] | shift[0x03][0x07],
in[0x05] | shift[0x01][0x06] | shift[0x02][0x07] | shift[0x03][0x08],
in[0x06] | shift[0x01][0x07] | shift[0x02][0x08] | shift[0x03][0x09],
in[0x07] | shift[0x01][0x08] | shift[0x02][0x09] | shift[0x03][0x0a],
in[0x08] | shift[0x01][0x09] | shift[0x02][0x0a] | shift[0x03][0x0b],
in[0x09] | shift[0x01][0x0a] | shift[0x02][0x0b] | shift[0x03][0x0c],
in[0x0a] | shift[0x01][0x0b] | shift[0x02][0x0c] | shift[0x03][0x0d],
in[0x0b] | shift[0x01][0x0c] | shift[0x02][0x0d] | shift[0x03][0x0e],
in[0x0c] | shift[0x01][0x0d] | shift[0x02][0x0e] | shift[0x03][0x0f],
in[0x0d] | shift[0x01][0x0e] | shift[0x02][0x0f] | shift[0x03][0x0f],
in[0x0e] | shift[0x01][0x0f] | shift[0x02][0x0f] | shift[0x03][0x0f],
in[0x0f] | shift[0x01][0x0f] | shift[0x02][0x0f] | shift[0x03][0x0f],
};
const u32x16 multibyte
{
0
| (multibyte_packs & (expect_length == 1) & 0x000000ffU)
| (multibyte_packs & (expect_length == 2) & 0x0000ffffU)
| (multibyte_packs & (expect_length == 3) & 0x00ffffffU)
| (multibyte_packs & (expect_length == 4) & 0xffffffffU)
};
const u32x16 integers
{
(in & is_single) | (multibyte & is_lead)
};
return integers;
}
namespace ircd::utf8
{
template<class u32xN> static u32xN _encode(const u32xN codepoint) noexcept;
}
template<>
ircd::u32x4
ircd::utf8::encode(const u32x4 codepoint)
noexcept
{
return _encode(codepoint);
}
template<>
ircd::u32x8
ircd::utf8::encode(const u32x8 codepoint)
noexcept
#ifdef __AVX2__
{
return _encode(codepoint);
}
#else // This block is only effective for GCC. Clang performs this automatically.
{
u32x4 cp[2];
for(size_t i(0); i < 2; ++i)
for(size_t j(0); j < 4; ++j)
cp[i][j] = codepoint[(i + 1) * j];
cp[0] = _encode(cp[0]);
cp[1] = _encode(cp[1]);
u32x8 ret;
for(size_t i(0); i < 2; ++i)
for(size_t j(0); j < 4; ++j)
ret[(i + 1) * j] = cp[i][j];
return ret;
}
#endif
template<>
ircd::u32x16
ircd::utf8::encode(const u32x16 codepoint)
noexcept
#ifdef __AVX512F__
{
return _encode(codepoint);
}
#else // This block is only effective for GCC. Clang performs this automatically.
{
u32x8 cp[2];
for(size_t i(0); i < 2; ++i)
for(size_t j(0); j < 8; ++j)
cp[i][j] = codepoint[(i + 1) * j];
cp[0] = encode(cp[0]);
cp[1] = encode(cp[1]);
u32x16 ret;
for(size_t i(0); i < 2; ++i)
for(size_t j(0); j < 8; ++j)
ret[(i + 1) * j] = cp[i][j];
return ret;
}
#endif
/// Transform multiple char32_t codepoints to their utf-8 encodings in
/// parallel, returning a sparse result in each char32_t (this does not
/// compress the result down).
template<class u32xN>
inline u32xN
ircd::utf8::_encode(const u32xN codepoint)
noexcept
{
const u32xN len
{
length(codepoint)
};
const u32xN enc_2
{
(((codepoint >> 6) | 0xc0) & 0xff) // byte[0]
| ((((codepoint & 0x3f) | 0x80) &0xff) << 8) // byte[1]
};
const u32xN enc_3
{
(((codepoint >> 12) | 0xe0) & 0xff) | // byte[0]
(((((codepoint >> 6) & 0x3f) | 0x80) & 0xff) << 8) | // byte[1]
((((codepoint & 0x3f) | 0x80) & 0xff) << 16) // byte[3]
};
const u32xN enc_4
{
(((codepoint >> 18) | 0xf0) & 0xff) | // byte[0]
(((((codepoint >> 12) & 0x3f) | 0x80) & 0xff) << 8) | // byte[1]
(((((codepoint >> 6) & 0x3f) | 0x80) & 0xff) << 16) | // byte[2]
((((codepoint & 0x3f) | 0x80) & 0xff) << 24) // byte[3]
};
return 0
| ((len == 0) & 0xFFFD)
| ((len == 1) & codepoint)
| ((len == 2) & enc_2)
| ((len == 3) & enc_3)
| ((len == 4) & enc_4)
;
}
namespace ircd::utf8
{
template<class u32xN> static u32xN _length(const u32xN codepoint) noexcept;
}
template<>
ircd::u32x4
ircd::utf8::length(const u32x4 codepoint)
noexcept
{
return _length(codepoint);
}
template<>
ircd::u32x8
ircd::utf8::length(const u32x8 codepoint)
noexcept
#ifdef __AVX2__
{
return _length(codepoint);
}
#else // This block is only effective for GCC. Clang performs this automatically.
{
u32x4 cp[2];
for(size_t i(0); i < 2; ++i)
for(size_t j(0); j < 4; ++j)
cp[i][j] = codepoint[(i + 1) * j];
cp[0] = _length(cp[0]);
cp[1] = _length(cp[1]);
u32x8 ret;
for(size_t i(0); i < 2; ++i)
for(size_t j(0); j < 4; ++j)
ret[(i + 1) * j] = cp[i][j];
return ret;
}
#endif
template<>
ircd::u32x16
ircd::utf8::length(const u32x16 codepoint)
noexcept
#ifdef __AVX512F__
{
return _length(codepoint);
}
#else // This block is only effective for GCC. Clang performs this automatically.
{
u32x8 cp[2];
for(size_t i(0); i < 2; ++i)
for(size_t j(0); j < 8; ++j)
cp[i][j] = codepoint[(i + 1) * j];
cp[0] = length(cp[0]);
cp[1] = length(cp[1]);
u32x16 ret;
for(size_t i(0); i < 2; ++i)
for(size_t j(0); j < 8; ++j)
ret[(i + 1) * j] = cp[i][j];
return ret;
}
#endif
/// Determine the utf-8 encoding length of multiple codepoints in parallel.
/// The input vector char32_t codepoints and the output yields an integer
/// of 0-4 for each lane.
template<class u32xN>
inline u32xN
ircd::utf8::_length(const u32xN codepoint)
noexcept
{
const u32xN len[5]
{
// length 1
codepoint <= 0x7f,
// length 2
codepoint <= 0x7ff && codepoint > 0x7f,
// length 3 low
codepoint <= 0xd7ff && codepoint > 0x7ff,
// length 3 high
codepoint <= 0xffff && codepoint > 0xdfff,
// length 4
codepoint <= 0x10ffff && codepoint > 0xffff,
};
[[gnu::unused]] // Preserved here for future reference
const u32xN len_3_err
(
codepoint <= 0xdfff && codepoint > 0xd7ff
);
[[gnu::unused]] // Preserved here for future reference
const u32xN len_err
{
(codepoint > 0x10ffff) | len_3_err
};
return 0
| (len[0] & 1)
| (len[1] & 2)
| (len[2] & 3)
| (len[3] & 3)
| (len[4] & 4)
;
}