1// Copyright 2023 Matt Borland
2// Distributed under the Boost Software License, Version 1.0.
3// https://www.boost.org/LICENSE_1_0.txt
4
5#ifndef BOOST_CHARCONV_DETAIL_COMPUTE_FLOAT32_HPP
6#define BOOST_CHARCONV_DETAIL_COMPUTE_FLOAT32_HPP
7
8#include <boost/charconv/detail/compute_float64.hpp>
9#include <limits>
10#include <cstdint>
11#include <cmath>
12
13namespace boost { namespace charconv { namespace detail {
14
15inline float compute_float32(std::int64_t power, std::uint64_t i, bool negative, bool& success) noexcept
16{
17 const double d = compute_float64(power, i, negative, success);
18 float return_val;
19
20 if (success)
21 {
22 // Some compilers (e.g. Intel) will optimize std::isinf to always false depending on compiler flags
23 //
24 // From Intel(R) oneAPI DPC++/C++ Compiler 2023.0.0 (2023.0.0.20221201)
25 // warning: comparison with infinity always evaluates to false in fast floating point modes [-Wtautological-constant-compare]
26 // if (std::isinf(return_val))
27 if (d > static_cast<double>((std::numeric_limits<float>::max)()) ||
28 d < static_cast<double>((std::numeric_limits<float>::lowest)()))
29 {
30 return_val = negative ? -HUGE_VALF : HUGE_VALF;
31 success = false;
32 }
33 else
34 {
35 return_val = static_cast<float>(d);
36 }
37 }
38 else
39 {
40 if (power > 38)
41 {
42 return_val = negative ? -HUGE_VALF : HUGE_VALF;
43 }
44 else
45 {
46 return_val = negative ? -0.0F : 0.0F;
47 }
48 }
49
50 return return_val;
51}
52
53}}} // Namespaces
54
55#endif // BOOST_CHARCONV_DETAIL_COMPUTE_FLOAT32_HPP
56

source code of boost/libs/charconv/include/boost/charconv/detail/compute_float32.hpp