xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__string/constexpr_c_functions.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___CXX03___STRING_CONSTEXPR_C_FUNCTIONS_H
10 #define _LIBCPP___CXX03___STRING_CONSTEXPR_C_FUNCTIONS_H
11 
12 #include <__cxx03/__config>
13 #include <__cxx03/__memory/addressof.h>
14 #include <__cxx03/__memory/construct_at.h>
15 #include <__cxx03/__type_traits/datasizeof.h>
16 #include <__cxx03/__type_traits/is_always_bitcastable.h>
17 #include <__cxx03/__type_traits/is_assignable.h>
18 #include <__cxx03/__type_traits/is_constant_evaluated.h>
19 #include <__cxx03/__type_traits/is_constructible.h>
20 #include <__cxx03/__type_traits/is_equality_comparable.h>
21 #include <__cxx03/__type_traits/is_same.h>
22 #include <__cxx03/__type_traits/is_trivially_copyable.h>
23 #include <__cxx03/__type_traits/is_trivially_lexicographically_comparable.h>
24 #include <__cxx03/__type_traits/remove_cv.h>
25 #include <__cxx03/__utility/is_pointer_in_range.h>
26 #include <__cxx03/cstddef>
27 
28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29 #  pragma GCC system_header
30 #endif
31 
32 _LIBCPP_BEGIN_NAMESPACE_STD
33 
34 // Type used to encode that a function takes an integer that represents a number
35 // of elements as opposed to a number of bytes.
36 enum class __element_count : size_t {};
37 
38 template <class _Tp>
39 inline const bool __is_char_type = false;
40 
41 template <>
42 inline const bool __is_char_type<char> = true;
43 
44 #ifndef _LIBCPP_HAS_NO_CHAR8_T
45 template <>
46 inline const bool __is_char_type<char8_t> = true;
47 #endif
48 
49 template <class _Tp>
__constexpr_strlen(const _Tp * __str)50 inline _LIBCPP_HIDE_FROM_ABI size_t __constexpr_strlen(const _Tp* __str) _NOEXCEPT {
51   static_assert(__is_char_type<_Tp>, "__constexpr_strlen only works with char and char8_t");
52   // GCC currently doesn't support __builtin_strlen for heap-allocated memory during constant evaluation.
53   // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70816
54   if (__libcpp_is_constant_evaluated()) {
55     size_t __i = 0;
56     for (; __str[__i] != '\0'; ++__i)
57       ;
58     return __i;
59   }
60   return __builtin_strlen(reinterpret_cast<const char*>(__str));
61 }
62 
63 // Because of __libcpp_is_trivially_lexicographically_comparable we know that comparing the object representations is
64 // equivalent to a std::memcmp. Since we have multiple objects contiguously in memory, we can call memcmp once instead
65 // of invoking it on every object individually.
66 template <class _Tp, class _Up>
__constexpr_memcmp(const _Tp * __lhs,const _Up * __rhs,__element_count __n)67 _LIBCPP_HIDE_FROM_ABI int __constexpr_memcmp(const _Tp* __lhs, const _Up* __rhs, __element_count __n) {
68   static_assert(__libcpp_is_trivially_lexicographically_comparable<_Tp, _Up>::value,
69                 "_Tp and _Up have to be trivially lexicographically comparable");
70 
71   auto __count = static_cast<size_t>(__n);
72 
73   if (__libcpp_is_constant_evaluated()) {
74 #ifdef _LIBCPP_COMPILER_CLANG_BASED
75     if (sizeof(_Tp) == 1 && !is_same<_Tp, bool>::value)
76       return __builtin_memcmp(__lhs, __rhs, __count * sizeof(_Tp));
77 #endif
78 
79     while (__count != 0) {
80       if (*__lhs < *__rhs)
81         return -1;
82       if (*__rhs < *__lhs)
83         return 1;
84 
85       --__count;
86       ++__lhs;
87       ++__rhs;
88     }
89     return 0;
90   } else {
91     return __builtin_memcmp(__lhs, __rhs, __count * sizeof(_Tp));
92   }
93 }
94 
95 // Because of __libcpp_is_trivially_equality_comparable we know that comparing the object representations is equivalent
96 // to a std::memcmp(...) == 0. Since we have multiple objects contiguously in memory, we can call memcmp once instead
97 // of invoking it on every object individually.
98 template <class _Tp, class _Up>
__constexpr_memcmp_equal(const _Tp * __lhs,const _Up * __rhs,__element_count __n)99 _LIBCPP_HIDE_FROM_ABI bool __constexpr_memcmp_equal(const _Tp* __lhs, const _Up* __rhs, __element_count __n) {
100   static_assert(__libcpp_is_trivially_equality_comparable<_Tp, _Up>::value,
101                 "_Tp and _Up have to be trivially equality comparable");
102 
103   auto __count = static_cast<size_t>(__n);
104 
105   if (__libcpp_is_constant_evaluated()) {
106 #ifdef _LIBCPP_COMPILER_CLANG_BASED
107     if (sizeof(_Tp) == 1 && is_integral<_Tp>::value && !is_same<_Tp, bool>::value)
108       return __builtin_memcmp(__lhs, __rhs, __count * sizeof(_Tp)) == 0;
109 #endif
110     while (__count != 0) {
111       if (*__lhs != *__rhs)
112         return false;
113 
114       --__count;
115       ++__lhs;
116       ++__rhs;
117     }
118     return true;
119   } else {
120     return ::__builtin_memcmp(__lhs, __rhs, __count * sizeof(_Tp)) == 0;
121   }
122 }
123 
124 template <class _Tp, class _Up>
__constexpr_memchr(_Tp * __str,_Up __value,size_t __count)125 _LIBCPP_HIDE_FROM_ABI _Tp* __constexpr_memchr(_Tp* __str, _Up __value, size_t __count) {
126   static_assert(sizeof(_Tp) == 1 && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value,
127                 "Calling memchr on non-trivially equality comparable types is unsafe.");
128 
129   if (__libcpp_is_constant_evaluated()) {
130     // use __builtin_char_memchr to optimize constexpr evaluation if we can
131 
132     for (; __count; --__count) {
133       if (*__str == __value)
134         return __str;
135       ++__str;
136     }
137     return nullptr;
138   } else {
139     char __value_buffer = 0;
140     __builtin_memcpy(&__value_buffer, &__value, sizeof(char));
141     return static_cast<_Tp*>(__builtin_memchr(__str, __value_buffer, __count));
142   }
143 }
144 
145 // This function performs an assignment to an existing, already alive TriviallyCopyable object
146 // from another TriviallyCopyable object.
147 //
148 // It basically works around the fact that TriviallyCopyable objects are not required to be
149 // syntactically copy/move constructible or copy/move assignable. Technically, only one of the
150 // four operations is required to be syntactically valid -- but at least one definitely has to
151 // be valid.
152 //
153 // This is necessary in order to implement __constexpr_memmove below in a way that mirrors as
154 // closely as possible what the compiler's __builtin_memmove is able to do.
155 template <class _Tp, class _Up, __enable_if_t<is_assignable<_Tp&, _Up const&>::value, int> = 0>
__assign_trivially_copyable(_Tp & __dest,_Up const & __src)156 _LIBCPP_HIDE_FROM_ABI _Tp& __assign_trivially_copyable(_Tp& __dest, _Up const& __src) {
157   __dest = __src;
158   return __dest;
159 }
160 
161 // clang-format off
162 template <class _Tp, class _Up, __enable_if_t<!is_assignable<_Tp&, _Up const&>::value &&
163                                                is_assignable<_Tp&, _Up&&>::value, int> = 0>
164 // clang-format on
__assign_trivially_copyable(_Tp & __dest,_Up & __src)165 _LIBCPP_HIDE_FROM_ABI _Tp& __assign_trivially_copyable(_Tp& __dest, _Up& __src) {
166   __dest =
167       static_cast<_Up&&>(__src); // this is safe, we're not actually moving anything since the assignment is trivial
168   return __dest;
169 }
170 
171 // clang-format off
172 template <class _Tp, class _Up, __enable_if_t<!is_assignable<_Tp&, _Up const&>::value &&
173                                               !is_assignable<_Tp&, _Up&&>::value &&
174                                                is_constructible<_Tp, _Up const&>::value, int> = 0>
175 // clang-format on
__assign_trivially_copyable(_Tp & __dest,_Up const & __src)176 _LIBCPP_HIDE_FROM_ABI _Tp& __assign_trivially_copyable(_Tp& __dest, _Up const& __src) {
177   // _Tp is trivially destructible, so we don't need to call its destructor to end the lifetime of the object
178   // that was there previously
179   std::__construct_at(std::addressof(__dest), __src);
180   return __dest;
181 }
182 
183 // clang-format off
184 template <class _Tp, class _Up, __enable_if_t<!is_assignable<_Tp&, _Up const&>::value &&
185                                               !is_assignable<_Tp&, _Up&&>::value &&
186                                               !is_constructible<_Tp, _Up const&>::value &&
187                                                is_constructible<_Tp, _Up&&>::value, int> = 0>
188 // clang-format on
__assign_trivially_copyable(_Tp & __dest,_Up & __src)189 _LIBCPP_HIDE_FROM_ABI _Tp& __assign_trivially_copyable(_Tp& __dest, _Up& __src) {
190   // _Tp is trivially destructible, so we don't need to call its destructor to end the lifetime of the object
191   // that was there previously
192   std::__construct_at(
193       std::addressof(__dest),
194       static_cast<_Up&&>(__src)); // this is safe, we're not actually moving anything since the constructor is trivial
195   return __dest;
196 }
197 
198 template <class _Tp, class _Up, __enable_if_t<__is_always_bitcastable<_Up, _Tp>::value, int> = 0>
__constexpr_memmove(_Tp * __dest,_Up * __src,__element_count __n)199 _LIBCPP_HIDE_FROM_ABI _Tp* __constexpr_memmove(_Tp* __dest, _Up* __src, __element_count __n) {
200   size_t __count = static_cast<size_t>(__n);
201   if (__libcpp_is_constant_evaluated()) {
202 #ifdef _LIBCPP_COMPILER_CLANG_BASED
203     if (is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up> >::value) {
204       ::__builtin_memmove(__dest, __src, __count * sizeof(_Tp));
205       return __dest;
206     }
207 #endif
208     if (std::__is_pointer_in_range(__src, __src + __count, __dest)) {
209       for (; __count > 0; --__count)
210         std::__assign_trivially_copyable(__dest[__count - 1], __src[__count - 1]);
211     } else {
212       for (size_t __i = 0; __i != __count; ++__i)
213         std::__assign_trivially_copyable(__dest[__i], __src[__i]);
214     }
215   } else if (__count > 0) {
216     ::__builtin_memmove(__dest, __src, (__count - 1) * sizeof(_Tp) + __datasizeof_v<_Tp>);
217   }
218   return __dest;
219 }
220 
221 _LIBCPP_END_NAMESPACE_STD
222 
223 #endif // _LIBCPP___CXX03___STRING_CONSTEXPR_C_FUNCTIONS_H
224