1919namespace LIBC_NAMESPACE_DECL {
2020namespace internal {
2121
22- // Returns the max amount of bytes deemed reasonable - based on the target
23- // properties - for use in local stack arrays.
24- constexpr size_t max_stack_array_size () {
25- // Uses target pointer size as heuristic how much memory is available and
26- // unlikely to run into stack overflow and perf problems.
27- constexpr size_t ptr_diff_size = sizeof (ptrdiff_t );
28-
29- if constexpr (ptr_diff_size >= 8 ) {
30- return 4096 ;
31- }
32-
33- if constexpr (ptr_diff_size == 4 ) {
34- return 512 ;
35- }
36-
37- // 8-bit platforms are just not gonna work well with libc, qsort
38- // won't be the problem.
39- // 16-bit platforms ought to be able to store 64 bytes on the stack.
40- return 64 ;
41- }
42-
4322class ArrayGenericSize {
44- uint8_t *array_base;
23+ cpp::byte *array_base;
4524 size_t array_len;
4625 size_t elem_size;
4726
48- uint8_t *get_internal (size_t i) const noexcept {
27+ LIBC_INLINE cpp::byte *get_internal (size_t i) const {
4928 return array_base + (i * elem_size);
5029 }
5130
5231public:
53- ArrayGenericSize (uint8_t *a, size_t s, size_t e) noexcept
54- : array_base(a), array_len(s), elem_size(e) {}
32+ ArrayGenericSize (void *a, size_t s, size_t e)
33+ : array_base(reinterpret_cast <cpp::byte *>(a)), array_len(s),
34+ elem_size (e) {}
5535
5636 static constexpr bool has_fixed_size () { return false ; }
5737
58- void *get (size_t i) const noexcept {
59- return reinterpret_cast <void *>(get_internal (i));
60- }
38+ LIBC_INLINE void *get (size_t i) const { return get_internal (i); }
6139
62- void swap (size_t i, size_t j) const noexcept {
40+ void swap (size_t i, size_t j) const {
6341 // It's possible to use 8 byte blocks with `uint64_t`, but that
6442 // generates more machine code as the remainder loop gets
6543 // unrolled, plus 4 byte operations are more likely to be
@@ -70,8 +48,8 @@ class ArrayGenericSize {
7048 using block_t = uint32_t ;
7149 constexpr size_t BLOCK_SIZE = sizeof (block_t );
7250
73- uint8_t *elem_i = get_internal (i);
74- uint8_t *elem_j = get_internal (j);
51+ cpp::byte *elem_i = get_internal (i);
52+ cpp::byte *elem_j = get_internal (j);
7553
7654 const size_t elem_size_rem = elem_size % BLOCK_SIZE;
7755 const block_t *elem_i_block_end =
@@ -88,74 +66,73 @@ class ArrayGenericSize {
8866 elem_j_block += 1 ;
8967 }
9068
91- elem_i = reinterpret_cast <uint8_t *>(elem_i_block);
92- elem_j = reinterpret_cast <uint8_t *>(elem_j_block);
69+ elem_i = reinterpret_cast <cpp::byte *>(elem_i_block);
70+ elem_j = reinterpret_cast <cpp::byte *>(elem_j_block);
9371 for (size_t n = 0 ; n < elem_size_rem; ++n) {
94- uint8_t tmp = elem_i[n];
72+ cpp::byte tmp = elem_i[n];
9573 elem_i[n] = elem_j[n];
9674 elem_j[n] = tmp;
9775 }
9876 }
9977
100- size_t len () const noexcept { return array_len; }
78+ LIBC_INLINE size_t len () const { return array_len; }
10179
10280 // Make an Array starting at index |i| and length |s|.
103- ArrayGenericSize make_array (size_t i, size_t s) const noexcept {
81+ LIBC_INLINE ArrayGenericSize make_array (size_t i, size_t s) const {
10482 return ArrayGenericSize (get_internal (i), s, elem_size);
10583 }
10684
10785 // Reset this Array to point at a different interval of the same
10886 // items starting at index |i|.
109- void reset_bounds (size_t i, size_t s) noexcept {
87+ LIBC_INLINE void reset_bounds (size_t i, size_t s) {
11088 array_base = get_internal (i);
11189 array_len = s;
11290 }
11391};
11492
115- // Having a specialized Array type for sorting that knowns at
93+ // Having a specialized Array type for sorting that knows at
11694// compile-time what the size of the element is, allows for much more
11795// efficient swapping and for cheaper offset calculations.
11896template <size_t ELEM_SIZE> class ArrayFixedSize {
119- uint8_t *array_base;
97+ cpp::byte *array_base;
12098 size_t array_len;
12199
122- uint8_t *get_internal (size_t i) const noexcept {
100+ LIBC_INLINE cpp::byte *get_internal (size_t i) const {
123101 return array_base + (i * ELEM_SIZE);
124102 }
125103
126104public:
127- ArrayFixedSize (uint8_t *a, size_t s) noexcept : array_base(a), array_len(s) {}
105+ ArrayFixedSize (void *a, size_t s)
106+ : array_base(reinterpret_cast <cpp::byte *>(a)), array_len(s) {}
128107
129108 // Beware this function is used a heuristic for cheap to swap types, so
130109 // instantiating `ArrayFixedSize` with `ELEM_SIZE > 100` is probably a bad
131110 // idea perf wise.
132111 static constexpr bool has_fixed_size () { return true ; }
133112
134- void *get (size_t i) const noexcept {
135- return reinterpret_cast <void *>(get_internal (i));
136- }
113+ LIBC_INLINE void *get (size_t i) const { return get_internal (i); }
137114
138- void swap (size_t i, size_t j) const noexcept {
139- alignas (32 ) uint8_t tmp[ELEM_SIZE];
115+ LIBC_INLINE void swap (size_t i, size_t j) const {
116+ alignas (32 ) cpp::byte tmp[ELEM_SIZE];
140117
141- uint8_t *elem_i = get_internal (i);
142- uint8_t *elem_j = get_internal (j);
118+ cpp::byte *elem_i = get_internal (i);
119+ cpp::byte *elem_j = get_internal (j);
143120
144121 inline_memcpy (tmp, elem_i, ELEM_SIZE);
145122 inline_memmove (elem_i, elem_j, ELEM_SIZE);
146123 inline_memcpy (elem_j, tmp, ELEM_SIZE);
147124 }
148125
149- size_t len () const noexcept { return array_len; }
126+ LIBC_INLINE size_t len () const { return array_len; }
150127
151128 // Make an Array starting at index |i| and length |s|.
152- ArrayFixedSize<ELEM_SIZE> make_array (size_t i, size_t s) const noexcept {
129+ LIBC_INLINE ArrayFixedSize<ELEM_SIZE> make_array (size_t i, size_t s) const {
153130 return ArrayFixedSize<ELEM_SIZE>(get_internal (i), s);
154131 }
155132
156133 // Reset this Array to point at a different interval of the same
157134 // items starting at index |i|.
158- void reset_bounds (size_t i, size_t s) noexcept {
135+ LIBC_INLINE void reset_bounds (size_t i, size_t s) {
159136 array_base = get_internal (i);
160137 array_len = s;
161138 }
0 commit comments