fix warnings.

This commit is contained in:
IRainman 2025-04-10 18:23:01 +03:00
parent 6cacae0782
commit f8625b6416
11 changed files with 197 additions and 165 deletions

View File

@ -146,8 +146,8 @@ time_it_ns(std::vector<std::basic_string<CharT>> &lines, T const &function,
return std::make_pair(min_value, average); return std::make_pair(min_value, average);
} }
void pretty_print(size_t volume, size_t number_of_floats, std::string const &name, void pretty_print(size_t volume, size_t number_of_floats,
std::pair<double, double> result) { std::string const &name, std::pair<double, double> result) {
double volumeMB = volume / (1024. * 1024.); double volumeMB = volume / (1024. * 1024.);
printf("%-40s: %8.2f MB/s (+/- %.1f %%) ", name.data(), printf("%-40s: %8.2f MB/s (+/- %.1f %%) ", name.data(),
volumeMB * 1000000000 / result.first, volumeMB * 1000000000 / result.first,
@ -223,7 +223,8 @@ void fileload(std::string filename) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
#ifdef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN #ifdef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
std::cout << "# FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN is enabled" << std::endl; std::cout << "# FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN is enabled"
<< std::endl;
#endif #endif
#ifdef USING_COUNTERS #ifdef USING_COUNTERS
if (collector.has_events()) { if (collector.has_events()) {

View File

@ -37,7 +37,8 @@ struct event_count {
event_count() : elapsed(0), event_counts{0, 0, 0, 0} {} event_count() : elapsed(0), event_counts{0, 0, 0, 0} {}
event_count(const std::chrono::duration<double> &_elapsed, event_count(const std::chrono::duration<double> &_elapsed,
const std::array<unsigned long long, event_counter_types_size> &_event_counts) const std::array<unsigned long long, event_counter_types_size>
&_event_counts)
: elapsed(_elapsed), event_counts(_event_counts) {} : elapsed(_elapsed), event_counts(_event_counts) {}
event_count(const event_count &other) event_count(const event_count &other)

View File

@ -22,7 +22,8 @@ template <int TYPE = PERF_TYPE_HARDWARE> class LinuxEvents {
std::vector<uint64_t> ids{}; std::vector<uint64_t> ids{};
public: public:
explicit LinuxEvents(std::array<unsigned long long, 4> config_vec) : fd(0), working(true) { explicit LinuxEvents(std::array<unsigned long long, 4> config_vec)
: fd(0), working(true) {
memset(&attribs, 0, sizeof(attribs)); memset(&attribs, 0, sizeof(attribs));
attribs.type = TYPE; attribs.type = TYPE;
attribs.size = sizeof(attribs); attribs.size = sizeof(attribs);

View File

@ -301,7 +301,8 @@ parse_number_string(UC const *p, UC const *pend,
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
answer.negative = (*p == UC('-')); answer.negative = (*p == UC('-'));
// C++17 20.19.3.(7.1) explicitly forbids '+' sign here // C++17 20.19.3.(7.1) explicitly forbids '+' sign here
if ((*p == UC('-')) || (uint8_t(options.format & chars_format::allow_leading_plus) && if ((*p == UC('-')) ||
(uint8_t(options.format & chars_format::allow_leading_plus) &&
!basic_json_fmt && *p == UC('+'))) { !basic_json_fmt && *p == UC('+'))) {
++p; ++p;
if (p == pend) { if (p == pend) {
@ -316,8 +317,8 @@ parse_number_string(UC const *p, UC const *pend,
} }
else { else {
if (!is_integer(*p) && if (!is_integer(*p) &&
(*p != (*p != options.decimal_point)) { // a sign must be followed by an
options.decimal_point)) { // a sign must be followed by an integer or the dot // integer or the dot
return report_parse_error<UC>( return report_parse_error<UC>(
p, parse_error::missing_integer_or_dot_after_sign); p, parse_error::missing_integer_or_dot_after_sign);
} }
@ -331,13 +332,15 @@ parse_number_string(UC const *p, UC const *pend,
while ((p != pend) && is_integer(*p)) { while ((p != pend) && is_integer(*p)) {
// a multiplication by 10 is cheaper than an arbitrary integer // a multiplication by 10 is cheaper than an arbitrary integer
// multiplication // multiplication
answer.mantissa = 10 * answer.mantissa + answer.mantissa =
10 * answer.mantissa +
uint64_t(*p - uint64_t(*p -
UC('0')); // might overflow, we will handle the overflow later UC('0')); // might overflow, we will handle the overflow later
++p; ++p;
} }
UC const *const end_of_integer_part = p; UC const *const end_of_integer_part = p;
uint16_t digit_count = uint16_t(end_of_integer_part - start_digits); uint16_t digit_count =
static_cast<uint16_t>(end_of_integer_part - start_digits);
answer.integer = span<UC const>(start_digits, digit_count); answer.integer = span<UC const>(start_digits, digit_count);
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
FASTFLOAT_IF_CONSTEXPR17(basic_json_fmt) { FASTFLOAT_IF_CONSTEXPR17(basic_json_fmt) {
@ -363,35 +366,37 @@ parse_number_string(UC const *p, UC const *pend,
while ((p != pend) && is_integer(*p)) { while ((p != pend) && is_integer(*p)) {
uint8_t const digit = uint8_t(*p - UC('0')); uint8_t const digit = uint8_t(*p - UC('0'));
answer.mantissa = answer.mantissa * 10 + digit; // in rare cases, this will overflow, but that's ok answer.mantissa =
answer.mantissa * 10 +
digit; // in rare cases, this will overflow, but that's ok
++p; ++p;
} }
fraction = uint16_t(before - p); fraction = static_cast<uint16_t>(before - p);
answer.fraction = span<UC const>(before, uint16_t(p - before)); answer.fraction = span<UC const>(before, static_cast<uint16_t>(p - before));
digit_count -= fraction; digit_count -= fraction;
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
FASTFLOAT_IF_CONSTEXPR17(basic_json_fmt) { FASTFLOAT_IF_CONSTEXPR17(basic_json_fmt) {
// at least 1 digit in fractional part // at least 1 digit in fractional part
if (has_decimal_point && fraction == 0) { if (has_decimal_point && fraction == 0) {
return report_parse_error<UC>(p, return report_parse_error<UC>(
parse_error::no_digits_in_fractional_part); p, parse_error::no_digits_in_fractional_part);
} }
} }
#endif #endif
} } else if (digit_count ==
else if (digit_count == 0) { // we must have encountered at least one integer! 0) { // we must have encountered at least one integer!
return report_parse_error<UC>(p, parse_error::no_digits_in_mantissa); return report_parse_error<UC>(p, parse_error::no_digits_in_mantissa);
} }
// We have now parsed the integer and the fraction part of the mantissa. // We have now parsed the integer and the fraction part of the mantissa.
// Now we can parse the exponent part. // Now we can parse the exponent part.
if (p != pend && if ((p != pend) && (uint8_t(options.format & chars_format::scientific) &&
(uint8_t(options.format & chars_format::scientific) && (UC('e') == *p) ||
(UC('e') == *p) || (UC('E') == *p)) (UC('E') == *p))
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|| (uint8_t(options.format & detail::basic_fortran_fmt) && || (uint8_t(options.format & detail::basic_fortran_fmt) &&
((UC('+') == *p) || (UC('-') == *p) || ((UC('+') == *p) || (UC('-') == *p) || (UC('d') == *p) ||
(UC('d') == *p) || (UC('D') == *p))) (UC('D') == *p)))
#endif #endif
) { ) {
UC const *location_of_e = p; UC const *location_of_e = p;
@ -467,16 +472,19 @@ parse_number_string(UC const *p, UC const *pend,
p = answer.integer.ptr; p = answer.integer.ptr;
UC const *int_end = p + answer.integer.len(); UC const *int_end = p + answer.integer.len();
uint64_t const minimal_nineteen_digit_integer{1000000000000000000}; uint64_t const minimal_nineteen_digit_integer{1000000000000000000};
while ((answer.mantissa < minimal_nineteen_digit_integer) && (p != int_end)) { while ((answer.mantissa < minimal_nineteen_digit_integer) &&
(p != int_end)) {
answer.mantissa = answer.mantissa * 10 + uint64_t(*p - UC('0')); answer.mantissa = answer.mantissa * 10 + uint64_t(*p - UC('0'));
++p; ++p;
} }
if (answer.mantissa >= minimal_nineteen_digit_integer) { // We have a big integers if (answer.mantissa >=
minimal_nineteen_digit_integer) { // We have a big integers
answer.exponent += int16_t(end_of_integer_part - p); answer.exponent += int16_t(end_of_integer_part - p);
} else { // We have a value with a fractional component. } else { // We have a value with a fractional component.
p = answer.fraction.ptr; p = answer.fraction.ptr;
UC const *frac_end = p + answer.fraction.len(); UC const *frac_end = p + answer.fraction.len();
while ((answer.mantissa < minimal_nineteen_digit_integer) && (p != frac_end)) { while ((answer.mantissa < minimal_nineteen_digit_integer) &&
(p != frac_end)) {
answer.mantissa = answer.mantissa * 10 + uint64_t(*p - UC('0')); answer.mantissa = answer.mantissa * 10 + uint64_t(*p - UC('0'));
++p; ++p;
} }
@ -537,12 +545,11 @@ parse_int_string(UC const *p, UC const *pend, T &value,
if (digit >= options.base) { if (digit >= options.base) {
break; break;
} }
i = uint64_t(options.base) * i + i = uint64_t(options.base) * i + digit; // might overflow, check this later
digit; // might overflow, check this later
p++; p++;
} }
uint16_t const digit_count = uint16_t(p - start_digits); uint16_t const digit_count = static_cast<uint16_t>(p - start_digits);
if (digit_count == 0) { if (digit_count == 0) {
if (has_leading_zeros) { if (has_leading_zeros) {

View File

@ -71,9 +71,7 @@ template <uint8_t size> struct stackvec {
} }
// set the length, without bounds checking. // set the length, without bounds checking.
FASTFLOAT_CONSTEXPR14 void set_len(uint8_t len) noexcept { FASTFLOAT_CONSTEXPR14 void set_len(uint8_t len) noexcept { length = len; }
length = len;
}
constexpr uint8_t len() const noexcept { return length; } constexpr uint8_t len() const noexcept { return length; }
@ -101,7 +99,7 @@ template <uint8_t size> struct stackvec {
FASTFLOAT_CONSTEXPR20 void extend_unchecked(limb_span s) noexcept { FASTFLOAT_CONSTEXPR20 void extend_unchecked(limb_span s) noexcept {
limb *ptr = data + length; limb *ptr = data + length;
std::copy_n(s.ptr, s.len(), ptr); std::copy_n(s.ptr, s.len(), ptr);
set_len(len() + s.len()); set_len(uint8_t(len() + s.len()));
} }
// try to add items to the vector, returning if items were added // try to add items to the vector, returning if items were added
@ -304,7 +302,7 @@ FASTFLOAT_CONSTEXPR20 bool large_add_from(stackvec<size> &x, limb_span y,
// the effective x buffer is from `xstart..x.len()`, so exit early // the effective x buffer is from `xstart..x.len()`, so exit early
// if we can't get that current range. // if we can't get that current range.
if (x.len() < start || y.len() > x.len() - start) { if (x.len() < start || y.len() > x.len() - start) {
FASTFLOAT_TRY(x.try_resize(y.len() + start, 0)); FASTFLOAT_TRY(x.try_resize(uint8_t(y.len() + start), 0));
} }
bool carry = false; bool carry = false;
@ -547,7 +545,7 @@ struct bigint : pow5_tables<> {
limb *first = vec.data; limb *first = vec.data;
limb *last = first + n; limb *last = first + n;
::std::fill(first, last, 0); ::std::fill(first, last, 0);
vec.set_len(n + vec.len()); vec.set_len(uint8_t(n + vec.len()));
return true; return true;
} else { } else {
return true; return true;
@ -584,8 +582,8 @@ struct bigint : pow5_tables<> {
// get the number of bits in the bigint. // get the number of bits in the bigint.
FASTFLOAT_CONSTEXPR20 uint16_t bit_length() const noexcept { FASTFLOAT_CONSTEXPR20 uint16_t bit_length() const noexcept {
uint16_t lz = ctlz(); uint8_t lz = ctlz();
return uint16_t(limb_bits * vec.len()) - lz; return limb_bits * vec.len() - lz;
} }
FASTFLOAT_CONSTEXPR20 bool mul(limb y) noexcept { return small_mul(vec, y); } FASTFLOAT_CONSTEXPR20 bool mul(limb y) noexcept { return small_mul(vec, y); }

View File

@ -58,7 +58,6 @@
#define FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE 1 #define FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE 1
#endif #endif
// For support attribute [[assume]] is declared in P1774 // For support attribute [[assume]] is declared in P1774
#if defined(__cpp_attrubute_assume) #if defined(__cpp_attrubute_assume)
#define FASTFLOAT_ASSUME(expr) [[assume(expr)]] #define FASTFLOAT_ASSUME(expr) [[assume(expr)]]

View File

@ -262,7 +262,7 @@ parse_mantissa(bigint &result, const parsed_number_string_t<UC> &num) noexcept {
// try to minimize the number of big integer and scalar multiplication. // try to minimize the number of big integer and scalar multiplication.
// therefore, try to parse 8 digits at a time, and multiply by the largest // therefore, try to parse 8 digits at a time, and multiply by the largest
// scalar value (9 or 19 digits) for each step. // scalar value (9 or 19 digits) for each step.
uint16_t const max_digits = uint16_t(binary_format<T>::max_digits()); uint16_t const max_digits = binary_format<T>::max_digits();
uint16_t counter = 0; uint16_t counter = 0;
uint16_t digits = 0; uint16_t digits = 0;
limb value = 0; limb value = 0;
@ -342,9 +342,8 @@ parse_mantissa(bigint &result, const parsed_number_string_t<UC> &num) noexcept {
} }
template <typename T> template <typename T>
inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa positive_digit_comp(
positive_digit_comp(bigint &bigmant, adjusted_mantissa am, bigint &bigmant, adjusted_mantissa am, int16_t const exponent) noexcept {
int16_t const exponent) noexcept {
FASTFLOAT_ASSERT(bigmant.pow10(exponent)); FASTFLOAT_ASSERT(bigmant.pow10(exponent));
bool truncated; bool truncated;
am.mantissa = bigmant.hi64(truncated); am.mantissa = bigmant.hi64(truncated);
@ -370,15 +369,15 @@ positive_digit_comp(bigint &bigmant, adjusted_mantissa am,
// we then need to scale by `2^(f- e)`, and then the two significant digits // we then need to scale by `2^(f- e)`, and then the two significant digits
// are of the same magnitude. // are of the same magnitude.
template <typename T> template <typename T>
inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa negative_digit_comp(
negative_digit_comp(bigint &bigmant, adjusted_mantissa am, bigint &bigmant, adjusted_mantissa am, int16_t const exponent) noexcept {
int16_t const exponent) noexcept {
bigint &real_digits = bigmant; bigint &real_digits = bigmant;
int16_t const &real_exp = exponent; int16_t const &real_exp = exponent;
T b; T b;
{ {
// get the value of `b`, rounded down, and get a bigint representation of b+h // get the value of `b`, rounded down, and get a bigint representation of
// b+h
adjusted_mantissa am_b = am; adjusted_mantissa am_b = am;
// gcc7 bug: use a lambda to remove the noexcept qualifier bug with // gcc7 bug: use a lambda to remove the noexcept qualifier bug with
// -Wnoexcept-type. // -Wnoexcept-type.
@ -396,7 +395,7 @@ negative_digit_comp(bigint &bigmant, adjusted_mantissa am,
// scale real digits and theor digits to be same power. // scale real digits and theor digits to be same power.
int16_t pow2_exp = theor_exp - real_exp; int16_t pow2_exp = theor_exp - real_exp;
uint16_t pow5_exp = uint16_t(-real_exp); uint16_t pow5_exp = -real_exp;
if (pow5_exp != 0) { if (pow5_exp != 0) {
FASTFLOAT_ASSERT(theor_digits.pow5(pow5_exp)); FASTFLOAT_ASSERT(theor_digits.pow5(pow5_exp));
} }

View File

@ -202,12 +202,18 @@ using parse_options = parse_options_t<char>;
#ifndef FASTFLOAT_ASSERT #ifndef FASTFLOAT_ASSERT
#define FASTFLOAT_ASSERT(x) \ #define FASTFLOAT_ASSERT(x) \
{ ((void)(x)); FASTFLOAT_ASSUME(x); } { \
((void)(x)); \
FASTFLOAT_ASSUME(x); \
}
#endif #endif
#ifndef FASTFLOAT_DEBUG_ASSERT #ifndef FASTFLOAT_DEBUG_ASSERT
#define FASTFLOAT_DEBUG_ASSERT(x) \ #define FASTFLOAT_DEBUG_ASSERT(x) \
{ ((void)(x)); FASTFLOAT_ASSUME(x); } { \
((void)(x)); \
FASTFLOAT_ASSUME(x); \
}
#endif #endif
// rust style `try!()` macro, or `?` operator // rust style `try!()` macro, or `?` operator
@ -295,7 +301,8 @@ template <typename T> struct span {
T const *ptr; T const *ptr;
uint16_t length; uint16_t length;
constexpr span(T const *_ptr, uint16_t _length) : ptr(_ptr), length(_length) {} constexpr span(T const *_ptr, uint16_t _length)
: ptr(_ptr), length(_length) {}
constexpr span() : ptr(nullptr), length(0) {} constexpr span() : ptr(nullptr), length(0) {}
@ -451,23 +458,25 @@ template <typename T, typename U = void> struct binary_format_lookup_tables;
template <typename T> struct binary_format : binary_format_lookup_tables<T> { template <typename T> struct binary_format : binary_format_lookup_tables<T> {
using equiv_uint = equiv_uint_t<T>; using equiv_uint = equiv_uint_t<T>;
// TODO add type for bit shift operations and use it.
// TODO add type for exponent operations and use it.
static constexpr int mantissa_explicit_bits(); static constexpr uint8_t mantissa_explicit_bits();
static constexpr int minimum_exponent(); static constexpr int16_t minimum_exponent();
static constexpr int infinite_power(); static constexpr int16_t infinite_power();
static constexpr int sign_index(); static constexpr uint8_t sign_index();
static constexpr int static constexpr int8_t
min_exponent_fast_path(); // used when fegetround() == FE_TONEAREST min_exponent_fast_path(); // used when fegetround() == FE_TONEAREST
static constexpr int max_exponent_fast_path(); static constexpr int8_t max_exponent_fast_path();
static constexpr int max_exponent_round_to_even(); static constexpr int16_t max_exponent_round_to_even();
static constexpr int min_exponent_round_to_even(); static constexpr int16_t min_exponent_round_to_even();
static constexpr uint64_t max_mantissa_fast_path(int64_t power); static constexpr equiv_uint max_mantissa_fast_path(int64_t power);
static constexpr uint64_t static constexpr equiv_uint
max_mantissa_fast_path(); // used when fegetround() == FE_TONEAREST max_mantissa_fast_path(); // used when fegetround() == FE_TONEAREST
static constexpr int largest_power_of_ten(); static constexpr int16_t largest_power_of_ten();
static constexpr int smallest_power_of_ten(); static constexpr int16_t smallest_power_of_ten();
static constexpr T exact_power_of_ten(int64_t power); static constexpr T exact_power_of_ten(int64_t power);
static constexpr size_t max_digits(); static constexpr uint16_t max_digits();
static constexpr equiv_uint exponent_mask(); static constexpr equiv_uint exponent_mask();
static constexpr equiv_uint mantissa_mask(); static constexpr equiv_uint mantissa_mask();
static constexpr equiv_uint hidden_bit_mask(); static constexpr equiv_uint hidden_bit_mask();
@ -531,7 +540,7 @@ template <typename U> struct binary_format_lookup_tables<float, U> {
// Largest integer value v so that (5**index * v) <= 1<<24. // Largest integer value v so that (5**index * v) <= 1<<24.
// 0x1000000 == 1<<24 // 0x1000000 == 1<<24
static constexpr uint64_t max_mantissa[] = { static constexpr uint32_t max_mantissa[] = {
0x1000000, 0x1000000,
0x1000000 / 5, 0x1000000 / 5,
0x1000000 / (5 * 5), 0x1000000 / (5 * 5),
@ -557,7 +566,7 @@ constexpr uint64_t binary_format_lookup_tables<float, U>::max_mantissa[];
#endif #endif
template <> template <>
inline constexpr int binary_format<double>::min_exponent_fast_path() { inline constexpr int8_t binary_format<double>::min_exponent_fast_path() {
#if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0) #if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
return 0; return 0;
#else #else
@ -566,7 +575,7 @@ inline constexpr int binary_format<double>::min_exponent_fast_path() {
} }
template <> template <>
inline constexpr int binary_format<float>::min_exponent_fast_path() { inline constexpr int8_t binary_format<float>::min_exponent_fast_path() {
#if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0) #if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
return 0; return 0;
#else #else
@ -575,70 +584,70 @@ inline constexpr int binary_format<float>::min_exponent_fast_path() {
} }
template <> template <>
inline constexpr int binary_format<double>::mantissa_explicit_bits() { inline constexpr uint8_t binary_format<double>::mantissa_explicit_bits() {
return 52; return 52;
} }
template <> template <>
inline constexpr int binary_format<float>::mantissa_explicit_bits() { inline constexpr uint8_t binary_format<float>::mantissa_explicit_bits() {
return 23; return 23;
} }
template <> template <>
inline constexpr int binary_format<double>::max_exponent_round_to_even() { inline constexpr int16_t binary_format<double>::max_exponent_round_to_even() {
return 23; return 23;
} }
template <> template <>
inline constexpr int binary_format<float>::max_exponent_round_to_even() { inline constexpr int16_t binary_format<float>::max_exponent_round_to_even() {
return 10; return 10;
} }
template <> template <>
inline constexpr int binary_format<double>::min_exponent_round_to_even() { inline constexpr int16_t binary_format<double>::min_exponent_round_to_even() {
return -4; return -4;
} }
template <> template <>
inline constexpr int binary_format<float>::min_exponent_round_to_even() { inline constexpr int16_t binary_format<float>::min_exponent_round_to_even() {
return -17; return -17;
} }
template <> inline constexpr int binary_format<double>::minimum_exponent() { template <> inline constexpr int16_t binary_format<double>::minimum_exponent() {
return -1023; return -1023;
} }
template <> inline constexpr int binary_format<float>::minimum_exponent() { template <> inline constexpr int16_t binary_format<float>::minimum_exponent() {
return -127; return -127;
} }
template <> inline constexpr int binary_format<double>::infinite_power() { template <> inline constexpr int16_t binary_format<double>::infinite_power() {
return 0x7FF; return 0x7FF;
} }
template <> inline constexpr int binary_format<float>::infinite_power() { template <> inline constexpr int16_t binary_format<float>::infinite_power() {
return 0xFF; return 0xFF;
} }
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
template <> inline constexpr int binary_format<double>::sign_index() { template <> inline constexpr uint8_t binary_format<double>::sign_index() {
return 63; return 63;
} }
template <> inline constexpr int binary_format<float>::sign_index() { template <> inline constexpr uint8_t binary_format<float>::sign_index() {
return 31; return 31;
} }
#endif #endif
template <> template <>
inline constexpr int binary_format<double>::max_exponent_fast_path() { inline constexpr int8_t binary_format<double>::max_exponent_fast_path() {
return 22; return 22;
} }
template <> template <>
inline constexpr int binary_format<float>::max_exponent_fast_path() { inline constexpr int8_t binary_format<float>::max_exponent_fast_path() {
return 10; return 10;
} }
@ -648,8 +657,8 @@ inline constexpr uint64_t binary_format<double>::max_mantissa_fast_path() {
} }
template <> template <>
inline constexpr uint64_t binary_format<float>::max_mantissa_fast_path() { inline constexpr uint32_t binary_format<float>::max_mantissa_fast_path() {
return uint64_t(2) << mantissa_explicit_bits(); return uint32_t(2) << mantissa_explicit_bits();
} }
// credit: Jakub Jelínek // credit: Jakub Jelínek
@ -660,7 +669,7 @@ template <typename U> struct binary_format_lookup_tables<std::float16_t, U> {
// Largest integer value v so that (5**index * v) <= 1<<11. // Largest integer value v so that (5**index * v) <= 1<<11.
// 0x800 == 1<<11 // 0x800 == 1<<11
static constexpr uint64_t max_mantissa[] = {0x800, static constexpr uint16_t max_mantissa[] = {0x800,
0x800 / 5, 0x800 / 5,
0x800 / (5 * 5), 0x800 / (5 * 5),
0x800 / (5 * 5 * 5), 0x800 / (5 * 5 * 5),
@ -675,7 +684,7 @@ constexpr std::float16_t
binary_format_lookup_tables<std::float16_t, U>::powers_of_ten[]; binary_format_lookup_tables<std::float16_t, U>::powers_of_ten[];
template <typename U> template <typename U>
constexpr uint64_t constexpr uint16_t
binary_format_lookup_tables<std::float16_t, U>::max_mantissa[]; binary_format_lookup_tables<std::float16_t, U>::max_mantissa[];
#endif #endif
@ -706,19 +715,21 @@ binary_format<std::float16_t>::hidden_bit_mask() {
} }
template <> template <>
inline constexpr int binary_format<std::float16_t>::max_exponent_fast_path() { inline constexpr int8_t
binary_format<std::float16_t>::max_exponent_fast_path() {
return 4; return 4;
} }
template <> template <>
inline constexpr int binary_format<std::float16_t>::mantissa_explicit_bits() { inline constexpr uint8_t
binary_format<std::float16_t>::mantissa_explicit_bits() {
return 10; return 10;
} }
template <> template <>
inline constexpr uint64_t inline constexpr int8_t
binary_format<std::float16_t>::max_mantissa_fast_path() { binary_format<std::float16_t>::max_mantissa_fast_path() {
return uint64_t(2) << mantissa_explicit_bits(); return uint16_t(2) << mantissa_explicit_bits();
} }
template <> template <>
@ -732,52 +743,55 @@ binary_format<std::float16_t>::max_mantissa_fast_path(int64_t power) {
} }
template <> template <>
inline constexpr int binary_format<std::float16_t>::min_exponent_fast_path() { inline constexpr int8_t
binary_format<std::float16_t>::min_exponent_fast_path() {
return 0; return 0;
} }
template <> template <>
inline constexpr int inline constexpr int16_t
binary_format<std::float16_t>::max_exponent_round_to_even() { binary_format<std::float16_t>::max_exponent_round_to_even() {
return 5; return 5;
} }
template <> template <>
inline constexpr int inline constexpr int16_t
binary_format<std::float16_t>::min_exponent_round_to_even() { binary_format<std::float16_t>::min_exponent_round_to_even() {
return -22; return -22;
} }
template <> template <>
inline constexpr int binary_format<std::float16_t>::minimum_exponent() { inline constexpr int16_t binary_format<std::float16_t>::minimum_exponent() {
return -15; return -15;
} }
template <> template <>
inline constexpr int binary_format<std::float16_t>::infinite_power() { inline constexpr int16_t binary_format<std::float16_t>::infinite_power() {
return 0x1F; return 0x1F;
} }
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
template <> inline constexpr unsigned int binary_format<std::float16_t>::sign_index() { template <>
inline constexpr uint8_t binary_format<std::float16_t>::sign_index() {
return 15; return 15;
} }
#endif #endif
template <> template <>
inline constexpr int binary_format<std::float16_t>::largest_power_of_ten() { inline constexpr int16_t binary_format<std::float16_t>::largest_power_of_ten() {
return 4; return 4;
} }
template <> template <>
inline constexpr int binary_format<std::float16_t>::smallest_power_of_ten() { inline constexpr int16_t
binary_format<std::float16_t>::smallest_power_of_ten() {
return -27; return -27;
} }
template <> template <>
inline constexpr size_t binary_format<std::float16_t>::max_digits() { inline constexpr uint8_t binary_format<std::float16_t>::max_digits() {
return 22; return 22;
} }
#endif // __STDCPP_FLOAT16_T__ #endif // __STDCPP_FLOAT16_T__
@ -815,7 +829,8 @@ binary_format<std::bfloat16_t>::exact_power_of_ten(int64_t power) {
} }
template <> template <>
inline constexpr int binary_format<std::bfloat16_t>::max_exponent_fast_path() { inline constexpr int8_t
binary_format<std::bfloat16_t>::max_exponent_fast_path() {
return 3; return 3;
} }
@ -838,14 +853,16 @@ binary_format<std::bfloat16_t>::hidden_bit_mask() {
} }
template <> template <>
inline constexpr int binary_format<std::bfloat16_t>::mantissa_explicit_bits() { inline constexpr uint8_t
binary_format<std::bfloat16_t>::mantissa_explicit_bits() {
return 7; return 7;
} }
template <> template <>
inline constexpr uint64_t inline constexpr binary_format<std::bfloat16_t>::equiv_uint
binary_format<std::bfloat16_t>::max_mantissa_fast_path() { binary_format<std::bfloat16_t>::max_mantissa_fast_path() {
return uint64_t(2) << mantissa_explicit_bits(); return binary_format<std::bfloat16_t>::equiv_uint(2)
<< mantissa_explicit_bits();
} }
template <> template <>
@ -859,52 +876,56 @@ binary_format<std::bfloat16_t>::max_mantissa_fast_path(int64_t power) {
} }
template <> template <>
inline constexpr int binary_format<std::bfloat16_t>::min_exponent_fast_path() { inline constexpr int8_t
binary_format<std::bfloat16_t>::min_exponent_fast_path() {
return 0; return 0;
} }
template <> template <>
inline constexpr int inline constexpr int16_t
binary_format<std::bfloat16_t>::max_exponent_round_to_even() { binary_format<std::bfloat16_t>::max_exponent_round_to_even() {
return 3; return 3;
} }
template <> template <>
inline constexpr int inline constexpr int16_t
binary_format<std::bfloat16_t>::min_exponent_round_to_even() { binary_format<std::bfloat16_t>::min_exponent_round_to_even() {
return -24; return -24;
} }
template <> template <>
inline constexpr int binary_format<std::bfloat16_t>::minimum_exponent() { inline constexpr int16_t binary_format<std::bfloat16_t>::minimum_exponent() {
return -127; return -127;
} }
template <> template <>
inline constexpr int binary_format<std::bfloat16_t>::infinite_power() { inline constexpr int16_t binary_format<std::bfloat16_t>::infinite_power() {
return 0xFF; return 0xFF;
} }
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
template <> inline constexpr int binary_format<std::bfloat16_t>::sign_index() { template <>
inline constexpr uint8_t binary_format<std::bfloat16_t>::sign_index() {
return 15; return 15;
} }
#endif #endif
template <> template <>
inline constexpr int binary_format<std::bfloat16_t>::largest_power_of_ten() { inline constexpr int16_t
binary_format<std::bfloat16_t>::largest_power_of_ten() {
return 38; return 38;
} }
template <> template <>
inline constexpr int binary_format<std::bfloat16_t>::smallest_power_of_ten() { inline constexpr int16_t
binary_format<std::bfloat16_t>::smallest_power_of_ten() {
return -60; return -60;
} }
template <> template <>
inline constexpr size_t binary_format<std::bfloat16_t>::max_digits() { inline constexpr uint16_t binary_format<std::bfloat16_t>::max_digits() {
return 98; return 98;
} }
#endif // __STDCPP_BFLOAT16_T__ #endif // __STDCPP_BFLOAT16_T__
@ -920,7 +941,7 @@ binary_format<double>::max_mantissa_fast_path(int64_t power) {
} }
template <> template <>
inline constexpr uint64_t inline constexpr uint32_t
binary_format<float>::max_mantissa_fast_path(int64_t power) { binary_format<float>::max_mantissa_fast_path(int64_t power) {
// caller is responsible to ensure that // caller is responsible to ensure that
FASTFLOAT_ASSUME(power >= 0 && power <= 10); FASTFLOAT_ASSUME(power >= 0 && power <= 10);
@ -948,28 +969,31 @@ inline constexpr float binary_format<float>::exact_power_of_ten(int64_t power) {
return (void)powers_of_ten[0], powers_of_ten[power]; return (void)powers_of_ten[0], powers_of_ten[power];
} }
template <> inline constexpr int binary_format<double>::largest_power_of_ten() { template <>
inline constexpr int16_t binary_format<double>::largest_power_of_ten() {
return 308; return 308;
} }
template <> inline constexpr int binary_format<float>::largest_power_of_ten() { template <>
inline constexpr int16_t binary_format<float>::largest_power_of_ten() {
return 38; return 38;
} }
template <> template <>
inline constexpr int binary_format<double>::smallest_power_of_ten() { inline constexpr int16_t binary_format<double>::smallest_power_of_ten() {
return -342; return -342;
} }
template <> inline constexpr int binary_format<float>::smallest_power_of_ten() { template <>
inline constexpr int16_t binary_format<float>::smallest_power_of_ten() {
return -64; return -64;
} }
template <> inline constexpr size_t binary_format<double>::max_digits() { template <> inline constexpr uint16_t binary_format<double>::max_digits() {
return 769; return 769;
} }
template <> inline constexpr size_t binary_format<float>::max_digits() { template <> inline constexpr uint16_t binary_format<float>::max_digits() {
return 114; return 114;
} }

View File

@ -656,7 +656,8 @@ TEST_CASE("decimal_point_parsing") {
answer = fast_float::from_chars_advanced( answer = fast_float::from_chars_advanced(
input.data(), input.data() + input.size(), result, input.data(), input.data() + input.size(), result,
fast_float::parse_options({fast_float::chars_format::general, ',', 10})); fast_float::parse_options(
{fast_float::chars_format::general, ',', 10}));
CHECK_MESSAGE(answer.ec == std::errc(), "expected parse success"); CHECK_MESSAGE(answer.ec == std::errc(), "expected parse success");
CHECK_MESSAGE(answer.ptr == input.data() + input.size(), CHECK_MESSAGE(answer.ptr == input.data() + input.size(),
"Parsing should have stopped at end"); "Parsing should have stopped at end");
@ -666,8 +667,8 @@ TEST_CASE("decimal_point_parsing") {
std::string const input = "1.25"; std::string const input = "1.25";
auto answer = fast_float::from_chars_advanced( auto answer = fast_float::from_chars_advanced(
input.data(), input.data() + input.size(), result, input.data(), input.data() + input.size(), result,
fast_float::parse_options({fast_float::chars_format::general, ',', fast_float::parse_options(
10})); {fast_float::chars_format::general, ',', 10}));
CHECK_MESSAGE(answer.ec == std::errc(), "expected parse success"); CHECK_MESSAGE(answer.ec == std::errc(), "expected parse success");
CHECK_MESSAGE(answer.ptr == input.data() + 1, CHECK_MESSAGE(answer.ptr == input.data() + 1,
"Parsing should have stopped at dot"); "Parsing should have stopped at dot");
@ -1324,8 +1325,8 @@ TEST_CASE("double.general") {
TEST_CASE("double.decimal_point") { TEST_CASE("double.decimal_point") {
constexpr auto options = [] { constexpr auto options = [] {
return fast_float::parse_options({fast_float::chars_format::general, ',', return fast_float::parse_options(
10}); {fast_float::chars_format::general, ',', 10});
}(); }();
// infinities // infinities
@ -1642,8 +1643,8 @@ TEST_CASE("float.general") {
TEST_CASE("float.decimal_point") { TEST_CASE("float.decimal_point") {
constexpr auto options = [] { constexpr auto options = [] {
return fast_float::parse_options({fast_float::chars_format::general, ',', return fast_float::parse_options(
10}); {fast_float::chars_format::general, ',', 10});
}(); }();
// infinity // infinity

View File

@ -11,8 +11,8 @@ int main_readme() {
double result; double result;
auto answer = fast_float::from_chars_advanced( auto answer = fast_float::from_chars_advanced(
input.data(), input.data() + input.size(), result, input.data(), input.data() + input.size(), result,
fast_float::parse_options ({ fast_float::parse_options(
fast_float::chars_format::fortran | {fast_float::chars_format::fortran |
fast_float::chars_format::allow_leading_plus})); fast_float::chars_format::allow_leading_plus}));
if ((answer.ec != std::errc()) || ((result != 10000))) { if ((answer.ec != std::errc()) || ((result != 10000))) {
std::cerr << "parsing failure\n" << result << "\n"; std::cerr << "parsing failure\n" << result << "\n";
@ -35,10 +35,10 @@ int main() {
for (auto const &f : fmt1) { for (auto const &f : fmt1) {
auto d{std::distance(&fmt1[0], &f)}; auto d{std::distance(&fmt1[0], &f)};
double result; double result;
auto answer{fast_float::from_chars_advanced(f.data(), f.data() + f.size(), auto answer{fast_float::from_chars_advanced(
result, f.data(), f.data() + f.size(), result,
fast_float::parse_options ({ fast_float::parse_options(
fast_float::chars_format::fortran | {fast_float::chars_format::fortran |
fast_float::chars_format::allow_leading_plus}))}; fast_float::chars_format::allow_leading_plus}))};
if (answer.ec != std::errc() || result != expected[std::size_t(d)]) { if (answer.ec != std::errc() || result != expected[std::size_t(d)]) {
std::cerr << "parsing failure on " << f << std::endl; std::cerr << "parsing failure on " << f << std::endl;
@ -49,10 +49,10 @@ int main() {
for (auto const &f : fmt2) { for (auto const &f : fmt2) {
auto d{std::distance(&fmt2[0], &f)}; auto d{std::distance(&fmt2[0], &f)};
double result; double result;
auto answer{fast_float::from_chars_advanced(f.data(), f.data() + f.size(), auto answer{fast_float::from_chars_advanced(
result, f.data(), f.data() + f.size(), result,
fast_float::parse_options ({ fast_float::parse_options(
fast_float::chars_format::fortran | {fast_float::chars_format::fortran |
fast_float::chars_format::allow_leading_plus}))}; fast_float::chars_format::allow_leading_plus}))};
if (answer.ec != std::errc() || result != expected[std::size_t(d)]) { if (answer.ec != std::errc() || result != expected[std::size_t(d)]) {
std::cerr << "parsing failure on " << f << std::endl; std::cerr << "parsing failure on " << f << std::endl;
@ -63,10 +63,10 @@ int main() {
for (auto const &f : fmt3) { for (auto const &f : fmt3) {
auto d{std::distance(&fmt3[0], &f)}; auto d{std::distance(&fmt3[0], &f)};
double result; double result;
auto answer{fast_float::from_chars_advanced(f.data(), f.data() + f.size(), auto answer{fast_float::from_chars_advanced(
result, f.data(), f.data() + f.size(), result,
fast_float::parse_options ({ fast_float::parse_options(
fast_float::chars_format::fortran | {fast_float::chars_format::fortran |
fast_float::chars_format::allow_leading_plus}))}; fast_float::chars_format::allow_leading_plus}))};
if (answer.ec != std::errc() || result != expected[std::size_t(d)]) { if (answer.ec != std::errc() || result != expected[std::size_t(d)]) {
std::cerr << "parsing failure on " << f << std::endl; std::cerr << "parsing failure on " << f << std::endl;

View File

@ -9,8 +9,8 @@ int main_readme() {
double result; double result;
auto answer = fast_float::from_chars_advanced( auto answer = fast_float::from_chars_advanced(
input.data(), input.data() + input.size(), result, input.data(), input.data() + input.size(), result,
fast_float::parse_options options({ fast_float::parse_options options(
fast_float::chars_format::json | {fast_float::chars_format::json |
fast_float::chars_format::allow_leading_plus}) // should be ignored fast_float::chars_format::allow_leading_plus}) // should be ignored
); );
if (answer.ec == std::errc()) { if (answer.ec == std::errc()) {
@ -25,8 +25,8 @@ int main_readme2() {
double result; double result;
auto answer = fast_float::from_chars_advanced( auto answer = fast_float::from_chars_advanced(
input.data(), input.data() + input.size(), result, input.data(), input.data() + input.size(), result,
fast_float::parse_options options({ fast_float::parse_options options(
fast_float::chars_format::json | {fast_float::chars_format::json |
fast_float::chars_format::allow_leading_plus}) // should be ignored fast_float::chars_format::allow_leading_plus}) // should be ignored
); );
if (answer.ec == std::errc()) { if (answer.ec == std::errc()) {
@ -42,8 +42,8 @@ int main_readme3() {
double result; double result;
auto answer = fast_float::from_chars_advanced( auto answer = fast_float::from_chars_advanced(
input.data(), input.data() + input.size(), result, input.data(), input.data() + input.size(), result,
fast_float::parse_options options({ fast_float::parse_options options(
fast_float::chars_format::json_or_infnan | {fast_float::chars_format::json_or_infnan |
fast_float::chars_format::allow_leading_plus}); // should be ignored fast_float::chars_format::allow_leading_plus}); // should be ignored
); );
if (answer.ec != std::errc() || (!std::isinf(result))) { if (answer.ec != std::errc() || (!std::isinf(result))) {
@ -136,9 +136,10 @@ int main() {
auto const &expected_reason = reject[i].reason; auto const &expected_reason = reject[i].reason;
auto answer = fast_float::parse_number_string<true>( auto answer = fast_float::parse_number_string<true>(
f.data(), f.data() + f.size(), f.data(), f.data() + f.size(),
fast_float::parse_options({ fast_float::parse_options(
fast_float::chars_format::json | {fast_float::chars_format::json |
fast_float::chars_format::allow_leading_plus})); // should be ignored fast_float::chars_format::allow_leading_plus})); // should be
// ignored
if (answer.valid) { if (answer.valid) {
std::cerr << "json parse accepted invalid json " << f << std::endl; std::cerr << "json parse accepted invalid json " << f << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;