|
6 | 6 | using System.Globalization; |
7 | 7 | using System.Numerics; |
8 | 8 | using System.Runtime.InteropServices; |
9 | | -using System.Runtime.Intrinsics; |
10 | | -using System.Runtime.Intrinsics.X86; |
11 | 9 | using System.Text; |
12 | 10 | using Internal.Runtime.CompilerServices; |
13 | 11 |
|
@@ -1493,137 +1491,78 @@ private string[] SplitWithPostProcessing(ReadOnlySpan<int> sepList, ReadOnlySpan |
1493 | 1491 | /// <param name="sepListBuilder"><see cref="ValueListBuilder{T}"/> to store indexes</param> |
1494 | 1492 | private void MakeSeparatorList(ReadOnlySpan<char> separators, ref ValueListBuilder<int> sepListBuilder) |
1495 | 1493 | { |
1496 | | - // Special-case no separators to mean any whitespace is a separator. |
1497 | | - if (separators.Length == 0) |
| 1494 | + char sep0, sep1, sep2; |
| 1495 | + |
| 1496 | + switch (separators.Length) |
1498 | 1497 | { |
1499 | | - for (int i = 0; i < Length; i++) |
1500 | | - { |
1501 | | - if (char.IsWhiteSpace(this[i])) |
| 1498 | + // Special-case no separators to mean any whitespace is a separator. |
| 1499 | + case 0: |
| 1500 | + for (int i = 0; i < Length; i++) |
1502 | 1501 | { |
1503 | | - sepListBuilder.Append(i); |
| 1502 | + if (char.IsWhiteSpace(this[i])) |
| 1503 | + { |
| 1504 | + sepListBuilder.Append(i); |
| 1505 | + } |
1504 | 1506 | } |
1505 | | - } |
1506 | | - } |
1507 | | - |
1508 | | - // Special-case the common cases of 1, 2, and 3 separators, with manual comparisons against each separator. |
1509 | | - else if (separators.Length <= 3) |
1510 | | - { |
1511 | | - char sep0, sep1, sep2; |
1512 | | - sep0 = separators[0]; |
1513 | | - sep1 = separators.Length > 1 ? separators[1] : sep0; |
1514 | | - sep2 = separators.Length > 2 ? separators[2] : sep1; |
1515 | | - |
1516 | | - if (Length >= 16 && Sse41.IsSupported) |
1517 | | - { |
1518 | | - MakeSeparatorListVectorized(ref sepListBuilder, sep0, sep1, sep2); |
1519 | | - return; |
1520 | | - } |
| 1507 | + break; |
1521 | 1508 |
|
1522 | | - for (int i = 0; i < Length; i++) |
1523 | | - { |
1524 | | - char c = this[i]; |
1525 | | - if (c == sep0 || c == sep1 || c == sep2) |
| 1509 | + // Special-case the common cases of 1, 2, and 3 separators, with manual comparisons against each separator. |
| 1510 | + case 1: |
| 1511 | + sep0 = separators[0]; |
| 1512 | + for (int i = 0; i < Length; i++) |
1526 | 1513 | { |
1527 | | - sepListBuilder.Append(i); |
| 1514 | + if (this[i] == sep0) |
| 1515 | + { |
| 1516 | + sepListBuilder.Append(i); |
| 1517 | + } |
1528 | 1518 | } |
1529 | | - } |
1530 | | - } |
1531 | | - |
1532 | | - // Handle > 3 separators with a probabilistic map, ala IndexOfAny. |
1533 | | - // This optimizes for chars being unlikely to match a separator. |
1534 | | - else |
1535 | | - { |
1536 | | - unsafe |
1537 | | - { |
1538 | | - ProbabilisticMap map = default; |
1539 | | - uint* charMap = (uint*)↦ |
1540 | | - InitializeProbabilisticMap(charMap, separators); |
1541 | | - |
| 1519 | + break; |
| 1520 | + case 2: |
| 1521 | + sep0 = separators[0]; |
| 1522 | + sep1 = separators[1]; |
1542 | 1523 | for (int i = 0; i < Length; i++) |
1543 | 1524 | { |
1544 | 1525 | char c = this[i]; |
1545 | | - if (IsCharBitSet(charMap, (byte)c) && IsCharBitSet(charMap, (byte)(c >> 8)) && |
1546 | | - separators.Contains(c)) |
| 1526 | + if (c == sep0 || c == sep1) |
1547 | 1527 | { |
1548 | 1528 | sepListBuilder.Append(i); |
1549 | 1529 | } |
1550 | 1530 | } |
1551 | | - } |
1552 | | - } |
1553 | | - } |
1554 | | - |
1555 | | - private void MakeSeparatorListVectorized(ref ValueListBuilder<int> sepListBuilder, char c, char c2, char c3) |
1556 | | - { |
1557 | | - // Redundant test so we won't prejit remainder of this method |
1558 | | - // on platforms without SSE. |
1559 | | - if (!Sse41.IsSupported) |
1560 | | - { |
1561 | | - throw new PlatformNotSupportedException(); |
1562 | | - } |
1563 | | - |
1564 | | - // Constant that allows for the truncation of 16-bit (FFFF/0000) values within a register to 4-bit (F/0) |
1565 | | - Vector128<byte> shuffleConstant = Vector128.Create(0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF); |
1566 | | - |
1567 | | - Vector128<ushort> v1 = Vector128.Create(c); |
1568 | | - Vector128<ushort> v2 = Vector128.Create(c2); |
1569 | | - Vector128<ushort> v3 = Vector128.Create(c3); |
1570 | | - |
1571 | | - ref char c0 = ref MemoryMarshal.GetReference(this.AsSpan()); |
1572 | | - int cond = Length & -Vector128<ushort>.Count; |
1573 | | - int i = 0; |
1574 | | - |
1575 | | - for (; i < cond; i += Vector128<ushort>.Count) |
1576 | | - { |
1577 | | - Vector128<ushort> charVector = ReadVector(ref c0, i); |
1578 | | - Vector128<ushort> cmp = Sse2.CompareEqual(charVector, v1); |
1579 | | - |
1580 | | - cmp = Sse2.Or(Sse2.CompareEqual(charVector, v2), cmp); |
1581 | | - cmp = Sse2.Or(Sse2.CompareEqual(charVector, v3), cmp); |
1582 | | - |
1583 | | - if (Sse41.TestZ(cmp, cmp)) { continue; } |
1584 | | - |
1585 | | - Vector128<byte> mask = Sse2.ShiftRightLogical(cmp.AsUInt64(), 4).AsByte(); |
1586 | | - mask = Ssse3.Shuffle(mask, shuffleConstant); |
1587 | | - |
1588 | | - uint lowBits = Sse2.ConvertToUInt32(mask.AsUInt32()); |
1589 | | - mask = Sse2.ShiftRightLogical(mask.AsUInt64(), 32).AsByte(); |
1590 | | - uint highBits = Sse2.ConvertToUInt32(mask.AsUInt32()); |
1591 | | - |
1592 | | - for (int idx = i; lowBits != 0; idx++) |
1593 | | - { |
1594 | | - if ((lowBits & 0xF) != 0) |
| 1531 | + break; |
| 1532 | + case 3: |
| 1533 | + sep0 = separators[0]; |
| 1534 | + sep1 = separators[1]; |
| 1535 | + sep2 = separators[2]; |
| 1536 | + for (int i = 0; i < Length; i++) |
1595 | 1537 | { |
1596 | | - sepListBuilder.Append(idx); |
| 1538 | + char c = this[i]; |
| 1539 | + if (c == sep0 || c == sep1 || c == sep2) |
| 1540 | + { |
| 1541 | + sepListBuilder.Append(i); |
| 1542 | + } |
1597 | 1543 | } |
| 1544 | + break; |
1598 | 1545 |
|
1599 | | - lowBits >>= 8; |
1600 | | - } |
1601 | | - |
1602 | | - for (int idx = i + 4; highBits != 0; idx++) |
1603 | | - { |
1604 | | - if ((highBits & 0xF) != 0) |
| 1546 | + // Handle > 3 separators with a probabilistic map, ala IndexOfAny. |
| 1547 | + // This optimizes for chars being unlikely to match a separator. |
| 1548 | + default: |
| 1549 | + unsafe |
1605 | 1550 | { |
1606 | | - sepListBuilder.Append(idx); |
1607 | | - } |
1608 | | - |
1609 | | - highBits >>= 8; |
1610 | | - } |
1611 | | - } |
1612 | | - |
1613 | | - for (; i < Length; i++) |
1614 | | - { |
1615 | | - char curr = Unsafe.Add(ref c0, (IntPtr)(uint)i); |
1616 | | - if (curr == c || curr == c2 || curr == c3) |
1617 | | - { |
1618 | | - sepListBuilder.Append(i); |
1619 | | - } |
1620 | | - } |
| 1551 | + ProbabilisticMap map = default; |
| 1552 | + uint* charMap = (uint*)↦ |
| 1553 | + InitializeProbabilisticMap(charMap, separators); |
1621 | 1554 |
|
1622 | | - static Vector128<ushort> ReadVector(ref char c0, int offset) |
1623 | | - { |
1624 | | - ref char ci = ref Unsafe.Add(ref c0, (IntPtr)(uint)offset); |
1625 | | - ref byte b = ref Unsafe.As<char, byte>(ref ci); |
1626 | | - return Unsafe.ReadUnaligned<Vector128<ushort>>(ref b); |
| 1555 | + for (int i = 0; i < Length; i++) |
| 1556 | + { |
| 1557 | + char c = this[i]; |
| 1558 | + if (IsCharBitSet(charMap, (byte)c) && IsCharBitSet(charMap, (byte)(c >> 8)) && |
| 1559 | + separators.Contains(c)) |
| 1560 | + { |
| 1561 | + sepListBuilder.Append(i); |
| 1562 | + } |
| 1563 | + } |
| 1564 | + } |
| 1565 | + break; |
1627 | 1566 | } |
1628 | 1567 | } |
1629 | 1568 |
|
|
0 commit comments