From c2daa8a614b2d412d12ae8d289b0ab7019d6989a Mon Sep 17 00:00:00 2001 From: IRainman Date: Sun, 9 Mar 2025 03:41:27 +0300 Subject: [PATCH] Added FASTFLOAT_ASSUME for support attribute [[assume]] is declared in P1774 --- include/fast_float/ascii_number.h | 2 +- include/fast_float/constexpr_feature_detect.h | 9 +++++++++ include/fast_float/float_common.h | 2 +- include/fast_float/parse_number.h | 6 +++--- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/fast_float/ascii_number.h b/include/fast_float/ascii_number.h index c5b93f4..ca47afd 100644 --- a/include/fast_float/ascii_number.h +++ b/include/fast_float/ascii_number.h @@ -291,7 +291,7 @@ parse_number_string(UC const *p, UC const *pend, parsed_number_string_t answer; answer.valid = false; answer.too_many_digits = false; - [[assume(p < pend)]]; // assume p < pend, so dereference without checks; + FASTFLOAT_ASSUME(p < pend); // assume p < pend, so dereference without checks; #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN answer.negative = (*p == UC('-')); // C++17 20.19.3.(7.1) explicitly forbids '+' sign here diff --git a/include/fast_float/constexpr_feature_detect.h b/include/fast_float/constexpr_feature_detect.h index 26752ee..b1d1a8a 100644 --- a/include/fast_float/constexpr_feature_detect.h +++ b/include/fast_float/constexpr_feature_detect.h @@ -46,4 +46,13 @@ #define FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE 1 #endif +// For support attribute [[assume]] is declared in P1774 +#if defined(__clang__) +#define FASTFLOAT_ASSUME(expr) __builtin_assume(expr) +#elif defined(__GNUC__) && !defined(__ICC) +#define ASSUME(expr) __attribute__((expr))) +#elif defined(_MSC_VER) || defined(__ICC) +#define FASTFLOAT_ASSUME(expr) __assume(expr) +#endif + #endif // FASTFLOAT_CONSTEXPR_FEATURE_DETECT_H diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index 7f2d002..28d8671 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -342,7 +342,7 @@ leading_zeroes_generic(uint64_t input_num, int last_bit = 0) { fastfloat_really_inline FASTFLOAT_CONSTEXPR20 int leading_zeroes(uint64_t input_num) noexcept { assert(input_num > 0); - [[assume(input_num > 0)]]; + FASTFLOAT_ASSUME(input_num > 0); if (cpp20_and_in_constexpr()) { return leading_zeroes_generic(input_num); } diff --git a/include/fast_float/parse_number.h b/include/fast_float/parse_number.h index 0798603..80eb364 100644 --- a/include/fast_float/parse_number.h +++ b/include/fast_float/parse_number.h @@ -27,7 +27,7 @@ from_chars_result_t from_chars_result_t answer{}; answer.ptr = first; answer.ec = std::errc(); // be optimistic - [[assume(first < last)]]; // so dereference without checks + FASTFLOAT_ASSUME(first < last); // so dereference without checks bool const minusSign = (*first == UC('-')); // C++17 20.19.3.(7.1) explicitly forbids '+' sign here @@ -321,7 +321,7 @@ from_chars_float_advanced(UC const *first, UC const *last, T &value, } #else // We are in parser code with external loop that checks bounds. - [[assume((first < last))]]; + FASTFLOAT_ASSUME(first < last); #endif parsed_number_string_t const pns = parse_number_string(first, last, options); @@ -374,7 +374,7 @@ from_chars_int_advanced(UC const *first, UC const *last, T &value, } #else // We are in parser code with external loop that checks bounds. - [[assume((first < last))]]; + FASTFLOAT_ASSUME(first < last); #endif if ( #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN