/src/cpython/Objects/stringlib/find_max_char.h
Line | Count | Source |
1 | | /* Finding the optimal width of unicode characters in a buffer */ |
2 | | |
3 | | /* find_max_char for one-byte will work for bytes objects as well. */ |
4 | | #if !STRINGLIB_IS_UNICODE && STRINGLIB_SIZEOF_CHAR > 1 |
5 | | # error "find_max_char.h is specific to Unicode" |
6 | | #endif |
7 | | |
8 | 47.7M | #define MAX_CHAR_ASCII 0x7f |
9 | 12.4M | #define MAX_CHAR_UCS1 0xff |
10 | 21.3M | #define MAX_CHAR_UCS2 0xffff |
11 | 1.73M | #define MAX_CHAR_UCS4 0x10ffff |
12 | | |
13 | | /* Masks selecting the bits that are set in a C 'size_t' holding several |
14 | | characters when at least one of them is wider than ASCII, UCS1 or UCS2. */ |
15 | | #if STRINGLIB_SIZEOF_CHAR == 1 |
16 | | # if SIZEOF_SIZE_T == 8 |
17 | 66.6M | # define MASK_ASCII 0x8080808080808080ULL |
18 | | # elif SIZEOF_SIZE_T == 4 |
19 | | # define MASK_ASCII 0x80808080U |
20 | | # else |
21 | | # error C 'size_t' size should be either 4 or 8! |
22 | | # endif |
23 | 27.0M | # define MASK_MAX_CHAR MASK_ASCII |
24 | 1.64M | # define MAX_CHAR MAX_CHAR_UCS1 |
25 | | #elif STRINGLIB_SIZEOF_CHAR == 2 |
26 | | # if SIZEOF_SIZE_T == 8 |
27 | 5.81M | # define MASK_ASCII 0xFF80FF80FF80FF80ULL |
28 | 115M | # define MASK_UCS1 0xFF00FF00FF00FF00ULL |
29 | | # elif SIZEOF_SIZE_T == 4 |
30 | | # define MASK_ASCII 0xFF80FF80U |
31 | | # define MASK_UCS1 0xFF00FF00U |
32 | | # else |
33 | | # error C 'size_t' size should be either 4 or 8! |
34 | | # endif |
35 | 103M | # define MASK_MAX_CHAR MASK_UCS1 |
36 | 9.77M | # define MAX_CHAR MAX_CHAR_UCS2 |
37 | | #elif STRINGLIB_SIZEOF_CHAR == 4 |
38 | | # if SIZEOF_SIZE_T == 8 |
39 | 13.1M | # define MASK_ASCII 0xFFFFFF80FFFFFF80ULL |
40 | 18.5M | # define MASK_UCS1 0xFFFFFF00FFFFFF00ULL |
41 | 116M | # define MASK_UCS2 0xFFFF0000FFFF0000ULL |
42 | | # elif SIZEOF_SIZE_T == 4 |
43 | | # define MASK_ASCII 0xFFFFFF80U |
44 | | # define MASK_UCS1 0xFFFFFF00U |
45 | | # define MASK_UCS2 0xFFFF0000U |
46 | | # else |
47 | | # error C 'size_t' size should be either 4 or 8! |
48 | | # endif |
49 | 97.7M | # define MASK_MAX_CHAR MASK_UCS2 |
50 | 1.38M | # define MAX_CHAR MAX_CHAR_UCS4 |
51 | | #else |
52 | | # error Invalid STRINGLIB_SIZEOF_CHAR (must be 1, 2 or 4) |
53 | | #endif |
54 | | |
55 | | /* OR all characters into a single size_t accumulator and test the high bits |
56 | | at the end, instead of comparing each character. The accumulation loop has |
57 | | no branches, so compilers vectorize it. The start is first read in pieces |
58 | | until the pointer is aligned, so the loops can use plain word loads on |
59 | | every platform. Every 16 words, check whether the widest kind is already |
60 | | known and exit early. */ |
61 | | Py_LOCAL_INLINE(Py_UCS4) |
62 | | STRINGLIB(find_max_char)(const STRINGLIB_CHAR *begin, const STRINGLIB_CHAR *end) |
63 | 83.3M | { |
64 | 83.3M | const unsigned char *p = (const unsigned char *)begin; |
65 | 83.3M | const unsigned char *_end = (const unsigned char *)end; |
66 | 83.3M | size_t value = 0; |
67 | | |
68 | 83.3M | if (p + SIZEOF_SIZE_T <= _end) { |
69 | 68.0M | if (!_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)) { |
70 | | #if STRINGLIB_SIZEOF_CHAR == 1 |
71 | 10.8M | if (!_Py_IS_ALIGNED(p, sizeof(uint16_t))) { |
72 | 6.23M | value |= *p++; |
73 | 6.23M | } |
74 | | #endif |
75 | | #if STRINGLIB_SIZEOF_CHAR <= 2 |
76 | 22.9M | if (!_Py_IS_ALIGNED(p, sizeof(uint32_t))) { |
77 | 17.7M | value |= *(const uint16_t *)p; |
78 | 17.7M | p += sizeof(uint16_t); |
79 | 17.7M | } |
80 | | #endif |
81 | 31.2M | #if SIZEOF_SIZE_T == 8 |
82 | 31.2M | if (!_Py_IS_ALIGNED(p, SIZEOF_SIZE_T)) { |
83 | 27.0M | value |= *(const uint32_t *)p; |
84 | 27.0M | p += sizeof(uint32_t); |
85 | 27.0M | } |
86 | 31.2M | #endif |
87 | 31.2M | if (value & MASK_MAX_CHAR) { |
88 | 2.42M | return MAX_CHAR; |
89 | 2.42M | } |
90 | 31.2M | assert(_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)); |
91 | 28.8M | } |
92 | | |
93 | 251M | for (; p + SIZEOF_SIZE_T * 16 <= _end; p += SIZEOF_SIZE_T * 16) { |
94 | | /* Four independent accumulators so the loop does not serialize on |
95 | | one register when the compiler does not vectorize it. */ |
96 | 196M | size_t v0 = 0, v1 = 0, v2 = 0, v3 = 0; |
97 | 982M | for (int i = 0; i < 16 * SIZEOF_SIZE_T; i += 4 * SIZEOF_SIZE_T) { |
98 | 786M | const size_t *w = (const size_t *)(p + i); |
99 | 786M | v0 |= w[0]; |
100 | 786M | v1 |= w[1]; |
101 | 786M | v2 |= w[2]; |
102 | 786M | v3 |= w[3]; |
103 | 786M | } |
104 | 196M | size_t block = v0 | v1 | v2 | v3; |
105 | 196M | if (block & MASK_MAX_CHAR) { |
106 | 10.3M | return MAX_CHAR; |
107 | 10.3M | } |
108 | 186M | value |= block; |
109 | 186M | } |
110 | | |
111 | 235M | for (; p + SIZEOF_SIZE_T <= _end; p += SIZEOF_SIZE_T) { |
112 | 179M | value |= *(const size_t *)p; |
113 | 179M | } |
114 | 55.2M | } |
115 | | |
116 | | /* Fewer than SIZEOF_SIZE_T bytes are left. Read them as two overlapping |
117 | | pieces, one from each end. OR does not mind seeing a byte twice. */ |
118 | 70.5M | if (p < _end) { |
119 | 54.6M | size_t n = (size_t)(_end - p); |
120 | 54.6M | if (n >= 4) { |
121 | 32.8M | uint32_t a, b; |
122 | 32.8M | memcpy(&a, p, 4); |
123 | 32.8M | memcpy(&b, _end - 4, 4); |
124 | 32.8M | value |= a | b; |
125 | 32.8M | } |
126 | | #if STRINGLIB_SIZEOF_CHAR <= 2 |
127 | 21.7M | else if (n >= 2) { |
128 | 12.2M | uint16_t a, b; |
129 | 12.2M | memcpy(&a, p, 2); |
130 | 12.2M | memcpy(&b, _end - 2, 2); |
131 | 12.2M | value |= a | b; |
132 | 12.2M | } |
133 | 9.55M | #endif |
134 | | #if STRINGLIB_SIZEOF_CHAR == 1 |
135 | 9.55M | else { |
136 | 9.55M | value |= *p; |
137 | 9.55M | } |
138 | | #endif |
139 | 54.6M | } |
140 | | |
141 | | #ifdef MASK_UCS2 |
142 | 18.9M | if (value & MASK_UCS2) { |
143 | 357k | return MAX_CHAR_UCS4; |
144 | 357k | } |
145 | 18.5M | #endif |
146 | | #ifdef MASK_UCS1 |
147 | 30.5M | if (value & MASK_UCS1) { |
148 | 11.6M | return MAX_CHAR_UCS2; |
149 | 11.6M | } |
150 | 18.9M | #endif |
151 | 58.5M | if (value & MASK_ASCII) { |
152 | 10.7M | return MAX_CHAR_UCS1; |
153 | 10.7M | } |
154 | 47.7M | return MAX_CHAR_ASCII; |
155 | 18.9M | } Unexecuted instantiation: unicode_writer.c:ucs1lib_find_max_char unicodeobject.c:ucs4lib_find_max_char Line | Count | Source | 63 | 20.3M | { | 64 | 20.3M | const unsigned char *p = (const unsigned char *)begin; | 65 | 20.3M | const unsigned char *_end = (const unsigned char *)end; | 66 | 20.3M | size_t value = 0; | 67 | | | 68 | 20.3M | if (p + SIZEOF_SIZE_T <= _end) { | 69 | 20.2M | if (!_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)) { | 70 | | #if STRINGLIB_SIZEOF_CHAR == 1 | 71 | | if (!_Py_IS_ALIGNED(p, sizeof(uint16_t))) { | 72 | | value |= *p++; | 73 | | } | 74 | | #endif | 75 | | #if STRINGLIB_SIZEOF_CHAR <= 2 | 76 | | if (!_Py_IS_ALIGNED(p, sizeof(uint32_t))) { | 77 | | value |= *(const uint16_t *)p; | 78 | | p += sizeof(uint16_t); | 79 | | } | 80 | | #endif | 81 | 8.28M | #if SIZEOF_SIZE_T == 8 | 82 | 8.28M | if (!_Py_IS_ALIGNED(p, SIZEOF_SIZE_T)) { | 83 | 8.28M | value |= *(const uint32_t *)p; | 84 | 8.28M | p += sizeof(uint32_t); | 85 | 8.28M | } | 86 | 8.28M | #endif | 87 | 8.28M | if (value & MASK_MAX_CHAR) { | 88 | 20.1k | return MAX_CHAR; | 89 | 20.1k | } | 90 | 8.28M | assert(_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)); | 91 | 8.26M | } | 92 | | | 93 | 108M | for (; p + SIZEOF_SIZE_T * 16 <= _end; p += SIZEOF_SIZE_T * 16) { | 94 | | /* Four independent accumulators so the loop does not serialize on | 95 | | one register when the compiler does not vectorize it. */ | 96 | 89.4M | size_t v0 = 0, v1 = 0, v2 = 0, v3 = 0; | 97 | 447M | for (int i = 0; i < 16 * SIZEOF_SIZE_T; i += 4 * SIZEOF_SIZE_T) { | 98 | 357M | const size_t *w = (const size_t *)(p + i); | 99 | 357M | v0 |= w[0]; | 100 | 357M | v1 |= w[1]; | 101 | 357M | v2 |= w[2]; | 102 | 357M | v3 |= w[3]; | 103 | 357M | } | 104 | 89.4M | size_t block = v0 | v1 | v2 | v3; | 105 | 89.4M | if (block & MASK_MAX_CHAR) { | 106 | 1.36M | return MAX_CHAR; | 107 | 1.36M | } | 108 | 88.0M | value |= block; | 109 | 88.0M | } | 110 | | | 111 | 55.5M | for (; p + SIZEOF_SIZE_T <= _end; p += SIZEOF_SIZE_T) { | 112 | 36.6M | value |= *(const size_t *)p; | 113 | 36.6M | } | 114 | 18.9M | } | 115 | | | 116 | | /* Fewer than SIZEOF_SIZE_T bytes are left. Read them as two overlapping | 117 | | pieces, one from each end. OR does not mind seeing a byte twice. */ | 118 | 18.9M | if (p < _end) { | 119 | 8.88M | size_t n = (size_t)(_end - p); | 120 | 8.88M | if (n >= 4) { | 121 | 8.88M | uint32_t a, b; | 122 | 8.88M | memcpy(&a, p, 4); | 123 | 8.88M | memcpy(&b, _end - 4, 4); | 124 | 8.88M | value |= a | b; | 125 | 8.88M | } | 126 | | #if STRINGLIB_SIZEOF_CHAR <= 2 | 127 | | else if (n >= 2) { | 128 | | uint16_t a, b; | 129 | | memcpy(&a, p, 2); | 130 | | memcpy(&b, _end - 2, 2); | 131 | | value |= a | b; | 132 | | } | 133 | | #endif | 134 | | #if STRINGLIB_SIZEOF_CHAR == 1 | 135 | | else { | 136 | | value |= *p; | 137 | | } | 138 | | #endif | 139 | 8.88M | } | 140 | | | 141 | 18.9M | #ifdef MASK_UCS2 | 142 | 18.9M | if (value & MASK_UCS2) { | 143 | 357k | return MAX_CHAR_UCS4; | 144 | 357k | } | 145 | 18.5M | #endif | 146 | 18.5M | #ifdef MASK_UCS1 | 147 | 18.5M | if (value & MASK_UCS1) { | 148 | 5.45M | return MAX_CHAR_UCS2; | 149 | 5.45M | } | 150 | 13.1M | #endif | 151 | 13.1M | if (value & MASK_ASCII) { | 152 | 1.23M | return MAX_CHAR_UCS1; | 153 | 1.23M | } | 154 | 11.8M | return MAX_CHAR_ASCII; | 155 | 13.1M | } |
unicodeobject.c:ucs1lib_find_max_char Line | Count | Source | 63 | 41.2M | { | 64 | 41.2M | const unsigned char *p = (const unsigned char *)begin; | 65 | 41.2M | const unsigned char *_end = (const unsigned char *)end; | 66 | 41.2M | size_t value = 0; | 67 | | | 68 | 41.2M | if (p + SIZEOF_SIZE_T <= _end) { | 69 | 29.8M | if (!_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)) { | 70 | 10.8M | #if STRINGLIB_SIZEOF_CHAR == 1 | 71 | 10.8M | if (!_Py_IS_ALIGNED(p, sizeof(uint16_t))) { | 72 | 6.23M | value |= *p++; | 73 | 6.23M | } | 74 | 10.8M | #endif | 75 | 10.8M | #if STRINGLIB_SIZEOF_CHAR <= 2 | 76 | 10.8M | if (!_Py_IS_ALIGNED(p, sizeof(uint32_t))) { | 77 | 7.19M | value |= *(const uint16_t *)p; | 78 | 7.19M | p += sizeof(uint16_t); | 79 | 7.19M | } | 80 | 10.8M | #endif | 81 | 10.8M | #if SIZEOF_SIZE_T == 8 | 82 | 10.8M | if (!_Py_IS_ALIGNED(p, SIZEOF_SIZE_T)) { | 83 | 8.16M | value |= *(const uint32_t *)p; | 84 | 8.16M | p += sizeof(uint32_t); | 85 | 8.16M | } | 86 | 10.8M | #endif | 87 | 10.8M | if (value & MASK_MAX_CHAR) { | 88 | 1.07M | return MAX_CHAR; | 89 | 1.07M | } | 90 | 10.8M | assert(_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)); | 91 | 9.81M | } | 92 | | | 93 | 44.3M | for (; p + SIZEOF_SIZE_T * 16 <= _end; p += SIZEOF_SIZE_T * 16) { | 94 | | /* Four independent accumulators so the loop does not serialize on | 95 | | one register when the compiler does not vectorize it. */ | 96 | 16.1M | size_t v0 = 0, v1 = 0, v2 = 0, v3 = 0; | 97 | 80.8M | for (int i = 0; i < 16 * SIZEOF_SIZE_T; i += 4 * SIZEOF_SIZE_T) { | 98 | 64.6M | const size_t *w = (const size_t *)(p + i); | 99 | 64.6M | v0 |= w[0]; | 100 | 64.6M | v1 |= w[1]; | 101 | 64.6M | v2 |= w[2]; | 102 | 64.6M | v3 |= w[3]; | 103 | 64.6M | } | 104 | 16.1M | size_t block = v0 | v1 | v2 | v3; | 105 | 16.1M | if (block & MASK_MAX_CHAR) { | 106 | 570k | return MAX_CHAR; | 107 | 570k | } | 108 | 15.5M | value |= block; | 109 | 15.5M | } | 110 | | | 111 | 132M | for (; p + SIZEOF_SIZE_T <= _end; p += SIZEOF_SIZE_T) { | 112 | 104M | value |= *(const size_t *)p; | 113 | 104M | } | 114 | 28.1M | } | 115 | | | 116 | | /* Fewer than SIZEOF_SIZE_T bytes are left. Read them as two overlapping | 117 | | pieces, one from each end. OR does not mind seeing a byte twice. */ | 118 | 39.6M | if (p < _end) { | 119 | 35.5M | size_t n = (size_t)(_end - p); | 120 | 35.5M | if (n >= 4) { | 121 | 15.5M | uint32_t a, b; | 122 | 15.5M | memcpy(&a, p, 4); | 123 | 15.5M | memcpy(&b, _end - 4, 4); | 124 | 15.5M | value |= a | b; | 125 | 15.5M | } | 126 | 20.0M | #if STRINGLIB_SIZEOF_CHAR <= 2 | 127 | 20.0M | else if (n >= 2) { | 128 | 10.4M | uint16_t a, b; | 129 | 10.4M | memcpy(&a, p, 2); | 130 | 10.4M | memcpy(&b, _end - 2, 2); | 131 | 10.4M | value |= a | b; | 132 | 10.4M | } | 133 | 9.55M | #endif | 134 | 9.55M | #if STRINGLIB_SIZEOF_CHAR == 1 | 135 | 9.55M | else { | 136 | 9.55M | value |= *p; | 137 | 9.55M | } | 138 | 35.5M | #endif | 139 | 35.5M | } | 140 | | | 141 | | #ifdef MASK_UCS2 | 142 | | if (value & MASK_UCS2) { | 143 | | return MAX_CHAR_UCS4; | 144 | | } | 145 | | #endif | 146 | | #ifdef MASK_UCS1 | 147 | | if (value & MASK_UCS1) { | 148 | | return MAX_CHAR_UCS2; | 149 | | } | 150 | | #endif | 151 | 39.6M | if (value & MASK_ASCII) { | 152 | 9.41M | return MAX_CHAR_UCS1; | 153 | 9.41M | } | 154 | 30.1M | return MAX_CHAR_ASCII; | 155 | 39.6M | } |
unicodeobject.c:ucs2lib_find_max_char Line | Count | Source | 63 | 21.7M | { | 64 | 21.7M | const unsigned char *p = (const unsigned char *)begin; | 65 | 21.7M | const unsigned char *_end = (const unsigned char *)end; | 66 | 21.7M | size_t value = 0; | 67 | | | 68 | 21.7M | if (p + SIZEOF_SIZE_T <= _end) { | 69 | 17.9M | if (!_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)) { | 70 | | #if STRINGLIB_SIZEOF_CHAR == 1 | 71 | | if (!_Py_IS_ALIGNED(p, sizeof(uint16_t))) { | 72 | | value |= *p++; | 73 | | } | 74 | | #endif | 75 | 12.0M | #if STRINGLIB_SIZEOF_CHAR <= 2 | 76 | 12.0M | if (!_Py_IS_ALIGNED(p, sizeof(uint32_t))) { | 77 | 10.5M | value |= *(const uint16_t *)p; | 78 | 10.5M | p += sizeof(uint16_t); | 79 | 10.5M | } | 80 | 12.0M | #endif | 81 | 12.0M | #if SIZEOF_SIZE_T == 8 | 82 | 12.0M | if (!_Py_IS_ALIGNED(p, SIZEOF_SIZE_T)) { | 83 | 10.6M | value |= *(const uint32_t *)p; | 84 | 10.6M | p += sizeof(uint32_t); | 85 | 10.6M | } | 86 | 12.0M | #endif | 87 | 12.0M | if (value & MASK_MAX_CHAR) { | 88 | 1.33M | return MAX_CHAR; | 89 | 1.33M | } | 90 | 12.0M | assert(_Py_IS_ALIGNED(p, ALIGNOF_SIZE_T)); | 91 | 10.7M | } | 92 | | | 93 | 99.1M | for (; p + SIZEOF_SIZE_T * 16 <= _end; p += SIZEOF_SIZE_T * 16) { | 94 | | /* Four independent accumulators so the loop does not serialize on | 95 | | one register when the compiler does not vectorize it. */ | 96 | 90.9M | size_t v0 = 0, v1 = 0, v2 = 0, v3 = 0; | 97 | 454M | for (int i = 0; i < 16 * SIZEOF_SIZE_T; i += 4 * SIZEOF_SIZE_T) { | 98 | 363M | const size_t *w = (const size_t *)(p + i); | 99 | 363M | v0 |= w[0]; | 100 | 363M | v1 |= w[1]; | 101 | 363M | v2 |= w[2]; | 102 | 363M | v3 |= w[3]; | 103 | 363M | } | 104 | 90.9M | size_t block = v0 | v1 | v2 | v3; | 105 | 90.9M | if (block & MASK_MAX_CHAR) { | 106 | 8.44M | return MAX_CHAR; | 107 | 8.44M | } | 108 | 82.5M | value |= block; | 109 | 82.5M | } | 110 | | | 111 | 47.2M | for (; p + SIZEOF_SIZE_T <= _end; p += SIZEOF_SIZE_T) { | 112 | 39.0M | value |= *(const size_t *)p; | 113 | 39.0M | } | 114 | 8.18M | } | 115 | | | 116 | | /* Fewer than SIZEOF_SIZE_T bytes are left. Read them as two overlapping | 117 | | pieces, one from each end. OR does not mind seeing a byte twice. */ | 118 | 11.9M | if (p < _end) { | 119 | 10.2M | size_t n = (size_t)(_end - p); | 120 | 10.2M | if (n >= 4) { | 121 | 8.47M | uint32_t a, b; | 122 | 8.47M | memcpy(&a, p, 4); | 123 | 8.47M | memcpy(&b, _end - 4, 4); | 124 | 8.47M | value |= a | b; | 125 | 8.47M | } | 126 | 1.75M | #if STRINGLIB_SIZEOF_CHAR <= 2 | 127 | 1.75M | else if (n >= 2) { | 128 | 1.75M | uint16_t a, b; | 129 | 1.75M | memcpy(&a, p, 2); | 130 | 1.75M | memcpy(&b, _end - 2, 2); | 131 | 1.75M | value |= a | b; | 132 | 1.75M | } | 133 | 10.2M | #endif | 134 | | #if STRINGLIB_SIZEOF_CHAR == 1 | 135 | | else { | 136 | | value |= *p; | 137 | | } | 138 | | #endif | 139 | 10.2M | } | 140 | | | 141 | | #ifdef MASK_UCS2 | 142 | | if (value & MASK_UCS2) { | 143 | | return MAX_CHAR_UCS4; | 144 | | } | 145 | | #endif | 146 | 11.9M | #ifdef MASK_UCS1 | 147 | 11.9M | if (value & MASK_UCS1) { | 148 | 6.17M | return MAX_CHAR_UCS2; | 149 | 6.17M | } | 150 | 5.81M | #endif | 151 | 5.81M | if (value & MASK_ASCII) { | 152 | 122k | return MAX_CHAR_UCS1; | 153 | 122k | } | 154 | 5.69M | return MAX_CHAR_ASCII; | 155 | 5.81M | } |
Unexecuted instantiation: unicodeobject.c:asciilib_find_max_char Unexecuted instantiation: bytes_methods.c:stringlib_find_max_char |
156 | | |
157 | | #undef MASK_MAX_CHAR |
158 | | #undef MAX_CHAR |
159 | | #undef MASK_ASCII |
160 | | #undef MASK_UCS1 |
161 | | #undef MASK_UCS2 |
162 | | #undef MAX_CHAR_ASCII |
163 | | #undef MAX_CHAR_UCS1 |
164 | | #undef MAX_CHAR_UCS2 |
165 | | #undef MAX_CHAR_UCS4 |