Path: blob/main/contrib/llvm-project/libcxx/include/__string/constexpr_c_functions.h
35233 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___STRING_CONSTEXPR_C_FUNCTIONS_H9#define _LIBCPP___STRING_CONSTEXPR_C_FUNCTIONS_H1011#include <__config>12#include <__memory/addressof.h>13#include <__memory/construct_at.h>14#include <__type_traits/datasizeof.h>15#include <__type_traits/is_always_bitcastable.h>16#include <__type_traits/is_assignable.h>17#include <__type_traits/is_constant_evaluated.h>18#include <__type_traits/is_constructible.h>19#include <__type_traits/is_equality_comparable.h>20#include <__type_traits/is_same.h>21#include <__type_traits/is_trivially_copyable.h>22#include <__type_traits/is_trivially_lexicographically_comparable.h>23#include <__type_traits/remove_cv.h>24#include <__utility/is_pointer_in_range.h>25#include <cstddef>2627#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)28# pragma GCC system_header29#endif3031_LIBCPP_BEGIN_NAMESPACE_STD3233// Type used to encode that a function takes an integer that represents a number34// of elements as opposed to a number of bytes.35enum class __element_count : size_t {};3637template <class _Tp>38inline const bool __is_char_type = false;3940template <>41inline const bool __is_char_type<char> = true;4243#ifndef _LIBCPP_HAS_NO_CHAR8_T44template <>45inline const bool __is_char_type<char8_t> = true;46#endif4748template <class _Tp>49inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 size_t __constexpr_strlen(const _Tp* __str) _NOEXCEPT {50static_assert(__is_char_type<_Tp>, "__constexpr_strlen only works with char and char8_t");51// GCC currently doesn't support __builtin_strlen for heap-allocated memory during constant evaluation.52// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=7081653if (__libcpp_is_constant_evaluated()) {54#if _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_COMPILER_CLANG_BASED)55if constexpr (is_same_v<_Tp, char>)56return __builtin_strlen(__str);57#endif58size_t __i = 0;59for (; __str[__i] != '\0'; ++__i)60;61return __i;62}63return __builtin_strlen(reinterpret_cast<const char*>(__str));64}6566// Because of __libcpp_is_trivially_lexicographically_comparable we know that comparing the object representations is67// equivalent to a std::memcmp. Since we have multiple objects contiguously in memory, we can call memcmp once instead68// of invoking it on every object individually.69template <class _Tp, class _Up>70_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int71__constexpr_memcmp(const _Tp* __lhs, const _Up* __rhs, __element_count __n) {72static_assert(__libcpp_is_trivially_lexicographically_comparable<_Tp, _Up>::value,73"_Tp and _Up have to be trivially lexicographically comparable");7475auto __count = static_cast<size_t>(__n);7677if (__libcpp_is_constant_evaluated()) {78#ifdef _LIBCPP_COMPILER_CLANG_BASED79if (sizeof(_Tp) == 1 && !is_same<_Tp, bool>::value)80return __builtin_memcmp(__lhs, __rhs, __count * sizeof(_Tp));81#endif8283while (__count != 0) {84if (*__lhs < *__rhs)85return -1;86if (*__rhs < *__lhs)87return 1;8889--__count;90++__lhs;91++__rhs;92}93return 0;94} else {95return __builtin_memcmp(__lhs, __rhs, __count * sizeof(_Tp));96}97}9899// Because of __libcpp_is_trivially_equality_comparable we know that comparing the object representations is equivalent100// to a std::memcmp(...) == 0. Since we have multiple objects contiguously in memory, we can call memcmp once instead101// of invoking it on every object individually.102template <class _Tp, class _Up>103_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool104__constexpr_memcmp_equal(const _Tp* __lhs, const _Up* __rhs, __element_count __n) {105static_assert(__libcpp_is_trivially_equality_comparable<_Tp, _Up>::value,106"_Tp and _Up have to be trivially equality comparable");107108auto __count = static_cast<size_t>(__n);109110if (__libcpp_is_constant_evaluated()) {111#ifdef _LIBCPP_COMPILER_CLANG_BASED112if (sizeof(_Tp) == 1 && is_integral<_Tp>::value && !is_same<_Tp, bool>::value)113return __builtin_memcmp(__lhs, __rhs, __count * sizeof(_Tp)) == 0;114#endif115while (__count != 0) {116if (*__lhs != *__rhs)117return false;118119--__count;120++__lhs;121++__rhs;122}123return true;124} else {125return ::__builtin_memcmp(__lhs, __rhs, __count * sizeof(_Tp)) == 0;126}127}128129template <class _Tp, class _Up>130_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __constexpr_memchr(_Tp* __str, _Up __value, size_t __count) {131static_assert(sizeof(_Tp) == 1 && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value,132"Calling memchr on non-trivially equality comparable types is unsafe.");133134if (__libcpp_is_constant_evaluated()) {135// use __builtin_char_memchr to optimize constexpr evaluation if we can136#if _LIBCPP_STD_VER >= 17 && __has_builtin(__builtin_char_memchr)137if constexpr (is_same_v<remove_cv_t<_Tp>, char> && is_same_v<remove_cv_t<_Up>, char>)138return __builtin_char_memchr(__str, __value, __count);139#endif140141for (; __count; --__count) {142if (*__str == __value)143return __str;144++__str;145}146return nullptr;147} else {148char __value_buffer = 0;149__builtin_memcpy(&__value_buffer, &__value, sizeof(char));150return static_cast<_Tp*>(__builtin_memchr(__str, __value_buffer, __count));151}152}153154// This function performs an assignment to an existing, already alive TriviallyCopyable object155// from another TriviallyCopyable object.156//157// It basically works around the fact that TriviallyCopyable objects are not required to be158// syntactically copy/move constructible or copy/move assignable. Technically, only one of the159// four operations is required to be syntactically valid -- but at least one definitely has to160// be valid.161//162// This is necessary in order to implement __constexpr_memmove below in a way that mirrors as163// closely as possible what the compiler's __builtin_memmove is able to do.164template <class _Tp, class _Up, __enable_if_t<is_assignable<_Tp&, _Up const&>::value, int> = 0>165_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp& __assign_trivially_copyable(_Tp& __dest, _Up const& __src) {166__dest = __src;167return __dest;168}169170// clang-format off171template <class _Tp, class _Up, __enable_if_t<!is_assignable<_Tp&, _Up const&>::value &&172is_assignable<_Tp&, _Up&&>::value, int> = 0>173// clang-format on174_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp& __assign_trivially_copyable(_Tp& __dest, _Up& __src) {175__dest =176static_cast<_Up&&>(__src); // this is safe, we're not actually moving anything since the assignment is trivial177return __dest;178}179180// clang-format off181template <class _Tp, class _Up, __enable_if_t<!is_assignable<_Tp&, _Up const&>::value &&182!is_assignable<_Tp&, _Up&&>::value &&183is_constructible<_Tp, _Up const&>::value, int> = 0>184// clang-format on185_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& __assign_trivially_copyable(_Tp& __dest, _Up const& __src) {186// _Tp is trivially destructible, so we don't need to call its destructor to end the lifetime of the object187// that was there previously188std::__construct_at(std::addressof(__dest), __src);189return __dest;190}191192// clang-format off193template <class _Tp, class _Up, __enable_if_t<!is_assignable<_Tp&, _Up const&>::value &&194!is_assignable<_Tp&, _Up&&>::value &&195!is_constructible<_Tp, _Up const&>::value &&196is_constructible<_Tp, _Up&&>::value, int> = 0>197// clang-format on198_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& __assign_trivially_copyable(_Tp& __dest, _Up& __src) {199// _Tp is trivially destructible, so we don't need to call its destructor to end the lifetime of the object200// that was there previously201std::__construct_at(202std::addressof(__dest),203static_cast<_Up&&>(__src)); // this is safe, we're not actually moving anything since the constructor is trivial204return __dest;205}206207template <class _Tp, class _Up, __enable_if_t<__is_always_bitcastable<_Up, _Tp>::value, int> = 0>208_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*209__constexpr_memmove(_Tp* __dest, _Up* __src, __element_count __n) {210size_t __count = static_cast<size_t>(__n);211if (__libcpp_is_constant_evaluated()) {212#ifdef _LIBCPP_COMPILER_CLANG_BASED213if (is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up> >::value) {214::__builtin_memmove(__dest, __src, __count * sizeof(_Tp));215return __dest;216}217#endif218if (std::__is_pointer_in_range(__src, __src + __count, __dest)) {219for (; __count > 0; --__count)220std::__assign_trivially_copyable(__dest[__count - 1], __src[__count - 1]);221} else {222for (size_t __i = 0; __i != __count; ++__i)223std::__assign_trivially_copyable(__dest[__i], __src[__i]);224}225} else if (__count > 0) {226::__builtin_memmove(__dest, __src, (__count - 1) * sizeof(_Tp) + __datasizeof_v<_Tp>);227}228return __dest;229}230231_LIBCPP_END_NAMESPACE_STD232233#endif // _LIBCPP___STRING_CONSTEXPR_C_FUNCTIONS_H234235236