From 1e92d5999790d58be255d4051c41471c2fd036b9 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Wed, 11 Nov 2020 20:43:36 -0500 Subject: [PATCH] Sign conversion pedantry. --- include/fast_float/ascii_number.h | 4 ++-- include/fast_float/decimal_to_binary.h | 4 ++-- include/fast_float/float_common.h | 2 +- include/fast_float/parse_number.h | 2 +- tests/CMakeLists.txt | 2 +- tests/basictest.cpp | 8 ++++---- tests/random_string.cpp | 6 +++--- tests/short_random_string.cpp | 6 +++--- tests/string_test.cpp | 2 +- 9 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/fast_float/ascii_number.h b/include/fast_float/ascii_number.h index d571fcb..689c908 100644 --- a/include/fast_float/ascii_number.h +++ b/include/fast_float/ascii_number.h @@ -71,7 +71,7 @@ parsed_number_string parse_number_string(const char *p, const char *pend, chars_ // a multiplication by 10 is cheaper than an arbitrary integer // multiplication i = 10 * i + - (*p - '0'); // might overflow, we will handle the overflow later + uint64_t(*p - '0'); // might overflow, we will handle the overflow later ++p; } int64_t exponent = 0; @@ -236,7 +236,7 @@ fastfloat_really_inline decimal parse_decimal(const char *p, const char *pend) n } answer.decimal_point += (neg_exp ? -exp_number : exp_number); } - answer.decimal_point += answer.num_digits; + answer.decimal_point += int32_t(answer.num_digits); if(answer.num_digits > max_digits) { answer.truncated = true; answer.num_digits = max_digits; diff --git a/include/fast_float/decimal_to_binary.h b/include/fast_float/decimal_to_binary.h index 23cf169..68ab079 100644 --- a/include/fast_float/decimal_to_binary.h +++ b/include/fast_float/decimal_to_binary.h @@ -104,7 +104,7 @@ adjusted_mantissa compute_float(int64_t q, uint64_t w) noexcept { // value128 product = compute_product(q, w); // but in practice, we can win big with the compute_product_approximation if its additional branch // is easily predicted. Which is best is data specific. - uint64_t upperbit = product.high >> 63; + int upperbit = int(product.high >> 63); answer.mantissa = product.high >> (upperbit + 64 - binary::mantissa_explicit_bits() - 3); @@ -143,7 +143,7 @@ adjusted_mantissa compute_float(int64_t q, uint64_t w) noexcept { // answer.mantissa = product.high >> (upperbit + 64 - binary::mantissa_explicit_bits() - 3); // ... we dropped out only zeroes. But if this happened, then we can go back!!! if((answer.mantissa << (upperbit + 64 - binary::mantissa_explicit_bits() - 3)) == product.high) { - answer.mantissa &= ~1; // flip it so that we do not round up + answer.mantissa &= ~uint64_t(1); // flip it so that we do not round up } } diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index 27a3c72..09917ad 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -175,7 +175,7 @@ struct decimal { } // Generate san exponent matching to_truncated_mantissa() inline int32_t to_truncated_exponent() { - return decimal_point - max_digit_without_overflow; + return decimal_point - int32_t(max_digit_without_overflow); } }; diff --git a/include/fast_float/parse_number.h b/include/fast_float/parse_number.h index 3e246d9..4652cc8 100644 --- a/include/fast_float/parse_number.h +++ b/include/fast_float/parse_number.h @@ -77,7 +77,7 @@ from_chars_result from_chars(const char *first, const char *last, from_chars_result answer; - while ((first != last) && fast_float::is_space(*first)) { + while ((first != last) && fast_float::is_space(uint8_t(*first))) { first++; } if (first == last) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index db02991..0b32b52 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,7 +4,7 @@ function(fast_float_add_cpp_test TEST_NAME) add_test(${TEST_NAME} ${TEST_NAME}) if(NOT WIN32) target_compile_options(${TEST_NAME} PUBLIC -Werror -Wall -Wextra -Weffc++) - target_compile_options(${TEST_NAME} PUBLIC -Wsign-compare -Wshadow -Wwrite-strings -Wpointer-arith -Winit-self -Wconversion -Wno-sign-conversion) + target_compile_options(${TEST_NAME} PUBLIC -Wsign-compare -Wshadow -Wwrite-strings -Wpointer-arith -Winit-self -Wconversion -Wsign-conversion) endif() target_link_libraries(${TEST_NAME} PUBLIC fast_float) endfunction(fast_float_add_cpp_test) diff --git a/tests/basictest.cpp b/tests/basictest.cpp index d01bd95..64dadd4 100644 --- a/tests/basictest.cpp +++ b/tests/basictest.cpp @@ -10,7 +10,7 @@ template std::string to_string(T d) { std::string s(64, '\0'); auto written = std::snprintf(&s[0], s.size(), "%.*e", std::numeric_limits::max_digits10 - 1, d); - s.resize(written); + s.resize(size_t(written)); return s; } @@ -18,7 +18,7 @@ template std::string to_long_string(T d) { std::string s(4096, '\0'); auto written = std::snprintf(&s[0], s.size(), "%.*e", std::numeric_limits::max_digits10 * 10, d); - s.resize(written); + s.resize(size_t(written)); return s; } @@ -134,7 +134,7 @@ bool issue8() { auto answer = fast_float::from_chars(s, s + strlen(s) - i, d); if(answer.ec != std::errc()) { std::cerr << "parsing failure\n"; return false; } if(d != 0x1.921fb54442d18p+1) { - printf("%.*s\n", int(strlen(s) - i), s); + printf("%.*s\n", int(strlen(s) - size_t(i)), s); std::cout << std::hexfloat << d << std::endl; std::cout << std::defaultfloat << d << std::endl; return false; @@ -308,7 +308,7 @@ bool test_fixed_only() { for (int i = start_point; i <= 308; ++i) {// large negative values should be zero. std::cout << "."; std::cout.flush(); - size_t n = snprintf(buf, sizeof(buf), "1e%d", i); + size_t n = size_t(snprintf(buf, sizeof(buf), "1e%d", i)); if (n >= sizeof(buf)) { abort(); } double actual; auto result = fast_float::from_chars(buf, buf + 1000, actual); diff --git a/tests/random_string.cpp b/tests/random_string.cpp index 9f8732b..8663691 100644 --- a/tests/random_string.cpp +++ b/tests/random_string.cpp @@ -24,7 +24,7 @@ double cygwin_strtod_l(const char* start, char** end) { class RandomEngine { public: RandomEngine() = delete; - RandomEngine(int new_seed) : wyhash64_x_(new_seed) {}; + RandomEngine(uint64_t new_seed) : wyhash64_x_(new_seed) {}; uint64_t next() { // Adapted from https://github.com/wangyi-fudan/wyhash/blob/master/wyhash.h // Inspired from @@ -59,7 +59,7 @@ public: l = m.low; } } - return int(m.high + min); + return int(m.high) + min; } int next_digit() { return next_ranged_int(0, 9); } @@ -146,7 +146,7 @@ std::pair strtof_from_string(char *st) { * We generate random strings and we try to parse them with both strtod/strtof, * and we verify that we get the same answer with with fast_float::from_chars. */ -bool tester(int seed, size_t volume) { +bool tester(uint64_t seed, size_t volume) { char buffer[4096]; // large buffer (can't overflow) RandomEngine rand(seed); for (size_t i = 0; i < volume; i++) { diff --git a/tests/short_random_string.cpp b/tests/short_random_string.cpp index 42389bc..50503cc 100644 --- a/tests/short_random_string.cpp +++ b/tests/short_random_string.cpp @@ -24,7 +24,7 @@ double cygwin_strtod_l(const char* start, char** end) { class RandomEngine { public: RandomEngine() = delete; - RandomEngine(int new_seed) : wyhash64_x_(new_seed) { }; + RandomEngine(uint64_t new_seed) : wyhash64_x_(new_seed) { }; uint64_t next() { // Adapted from https://github.com/wangyi-fudan/wyhash/blob/master/wyhash.h // Inspired from @@ -59,7 +59,7 @@ public: l = m.low; } } - return int(m.high + min); + return int(m.high) + min; } int next_digit() { return next_ranged_int(0, 9); } @@ -142,7 +142,7 @@ std::pair strtof_from_string(char *st) { * We generate random strings and we try to parse them with both strtod/strtof, * and we verify that we get the same answer with with fast_float::from_chars. */ -bool tester(int seed, size_t volume) { +bool tester(uint64_t seed, size_t volume) { char buffer[4096]; // large buffer (can't overflow) RandomEngine rand(seed); for (size_t i = 0; i < volume; i++) { diff --git a/tests/string_test.cpp b/tests/string_test.cpp index e2d6b96..7da2f7b 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -30,7 +30,7 @@ template std::string to_string(T d) { std::string s(64, '\0'); auto written = std::snprintf(&s[0], s.size(), "%.*e", std::numeric_limits::max_digits10 - 1, d); - s.resize(written); + s.resize(size_t(written)); return s; }