|
| 1 | +// Copyright 2024 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_DECIMAL_BID_CONVERSION_HPP |
| 6 | +#define BOOST_DECIMAL_BID_CONVERSION_HPP |
| 7 | + |
| 8 | +#include <boost/decimal/decimal32_t.hpp> |
| 9 | +#include <boost/decimal/decimal64_t.hpp> |
| 10 | +#include <boost/decimal/decimal128_t.hpp> |
| 11 | +#include <boost/decimal/decimal_fast32_t.hpp> |
| 12 | +#include <boost/decimal/decimal_fast64_t.hpp> |
| 13 | +#include <boost/decimal/decimal_fast128_t.hpp> |
| 14 | +#include <boost/decimal/charconv.hpp> |
| 15 | +#include <boost/decimal/detail/concepts.hpp> |
| 16 | +#include "detail/int128.hpp" |
| 17 | + |
| 18 | +namespace boost { |
| 19 | +namespace decimal { |
| 20 | + |
| 21 | +#if defined(__GNUC__) && __GNUC__ == 7 |
| 22 | +# pragma GCC diagnostic push |
| 23 | +# pragma GCC diagnostic ignored "-Wconversion" |
| 24 | +#endif |
| 25 | + |
| 26 | +BOOST_DECIMAL_EXPORT constexpr auto to_bid_d32(const decimal32_t val) noexcept -> std::uint32_t |
| 27 | +{ |
| 28 | + return val.bits_; |
| 29 | +} |
| 30 | + |
| 31 | +BOOST_DECIMAL_EXPORT constexpr auto from_bid_d32(const std::uint32_t bits) noexcept -> decimal32_t |
| 32 | +{ |
| 33 | + return from_bits(bits); |
| 34 | +} |
| 35 | + |
| 36 | +BOOST_DECIMAL_EXPORT constexpr auto to_bid_d32f(const decimal_fast32_t val) noexcept -> std::uint32_t |
| 37 | +{ |
| 38 | + const decimal32_t compliant_val {val}; |
| 39 | + return to_bid_d32(compliant_val); |
| 40 | +} |
| 41 | + |
| 42 | +BOOST_DECIMAL_EXPORT constexpr auto from_bid_d32f(const std::uint32_t bits) noexcept -> decimal_fast32_t |
| 43 | +{ |
| 44 | + const auto compliant_val {from_bid_d32(bits)}; |
| 45 | + const decimal_fast32_t val {compliant_val}; |
| 46 | + return val; |
| 47 | +} |
| 48 | + |
| 49 | +BOOST_DECIMAL_EXPORT constexpr auto to_bid_d64(const decimal64_t val) noexcept -> std::uint64_t |
| 50 | +{ |
| 51 | + return val.bits_; |
| 52 | +} |
| 53 | + |
| 54 | +BOOST_DECIMAL_EXPORT constexpr auto from_bid_d64(const std::uint64_t bits) noexcept -> decimal64_t |
| 55 | +{ |
| 56 | + return from_bits(bits); |
| 57 | +} |
| 58 | + |
| 59 | +BOOST_DECIMAL_EXPORT constexpr auto to_bid_d64f(const decimal_fast64_t val) noexcept -> std::uint64_t |
| 60 | +{ |
| 61 | + const decimal64_t compliant_val {val}; |
| 62 | + return to_bid_d64(compliant_val); |
| 63 | +} |
| 64 | + |
| 65 | +BOOST_DECIMAL_EXPORT constexpr auto from_bid_d64f(const std::uint64_t bits) noexcept -> decimal_fast64_t |
| 66 | +{ |
| 67 | + const auto compliant_val {from_bid_d64(bits)}; |
| 68 | + const decimal_fast64_t val {compliant_val}; |
| 69 | + return val; |
| 70 | +} |
| 71 | + |
| 72 | +BOOST_DECIMAL_EXPORT constexpr auto to_bid_d128(const decimal128_t val) noexcept -> int128::uint128_t |
| 73 | +{ |
| 74 | + return val.bits_; |
| 75 | +} |
| 76 | + |
| 77 | +BOOST_DECIMAL_EXPORT constexpr auto from_bid_d128(const int128::uint128_t bits) noexcept -> decimal128_t |
| 78 | +{ |
| 79 | + return from_bits(bits); |
| 80 | +} |
| 81 | + |
| 82 | +#ifdef BOOST_DECIMAL_HAS_INT128 |
| 83 | +BOOST_DECIMAL_EXPORT constexpr auto from_bid_d128(const detail::builtin_uint128_t bits) noexcept -> decimal128_t |
| 84 | +{ |
| 85 | + return from_bits(bits); |
| 86 | +} |
| 87 | +#endif |
| 88 | + |
| 89 | +BOOST_DECIMAL_EXPORT constexpr auto to_bid_d128f(const decimal_fast128_t& val) noexcept -> int128::uint128_t |
| 90 | +{ |
| 91 | + const decimal128_t compliant_val {val}; |
| 92 | + return to_bid_d128(compliant_val); |
| 93 | +} |
| 94 | + |
| 95 | +BOOST_DECIMAL_EXPORT constexpr auto from_bid_d128f(const int128::uint128_t bits) noexcept -> decimal_fast128_t |
| 96 | +{ |
| 97 | + const auto compliant_val {from_bid_d128(bits)}; |
| 98 | + const decimal_fast128_t val {compliant_val}; |
| 99 | + return val; |
| 100 | +} |
| 101 | + |
| 102 | +#ifdef BOOST_DECIMAL_HAS_INT128 |
| 103 | +BOOST_DECIMAL_EXPORT constexpr auto from_bid_d128f(const detail::builtin_uint128_t bits) noexcept -> decimal_fast128_t |
| 104 | +{ |
| 105 | + const auto compliant_val {from_bid_d128(bits)}; |
| 106 | + const decimal_fast128_t val {compliant_val}; |
| 107 | + return val; |
| 108 | +} |
| 109 | +#endif |
| 110 | + |
| 111 | +BOOST_DECIMAL_EXPORT constexpr auto to_bid(const decimal32_t val) noexcept -> std::uint32_t |
| 112 | +{ |
| 113 | + return to_bid_d32(val); |
| 114 | +} |
| 115 | + |
| 116 | +BOOST_DECIMAL_EXPORT constexpr auto to_bid(const decimal_fast32_t val) noexcept -> std::uint32_t |
| 117 | +{ |
| 118 | + return to_bid_d32f(val); |
| 119 | +} |
| 120 | + |
| 121 | +BOOST_DECIMAL_EXPORT constexpr auto to_bid(const decimal64_t val) noexcept -> std::uint64_t |
| 122 | +{ |
| 123 | + return to_bid_d64(val); |
| 124 | +} |
| 125 | + |
| 126 | +BOOST_DECIMAL_EXPORT constexpr auto to_bid(const decimal_fast64_t val) noexcept -> std::uint64_t |
| 127 | +{ |
| 128 | + return to_bid_d64f(val); |
| 129 | +} |
| 130 | + |
| 131 | +BOOST_DECIMAL_EXPORT constexpr auto to_bid(const decimal128_t val) noexcept -> int128::uint128_t |
| 132 | +{ |
| 133 | + return to_bid_d128(val); |
| 134 | +} |
| 135 | + |
| 136 | +BOOST_DECIMAL_EXPORT constexpr auto to_bid(const decimal_fast128_t& val) noexcept -> int128::uint128_t |
| 137 | +{ |
| 138 | + return to_bid_d128f(val); |
| 139 | +} |
| 140 | + |
| 141 | +BOOST_DECIMAL_EXPORT template <typename T> |
| 142 | +constexpr auto to_bid(const T val) noexcept |
| 143 | +{ |
| 144 | + return to_bid(val); |
| 145 | +} |
| 146 | + |
| 147 | +BOOST_DECIMAL_EXPORT template <typename T = decimal32_t> |
| 148 | +constexpr auto from_bid(const std::uint32_t bits) noexcept |
| 149 | + BOOST_DECIMAL_REQUIRES(detail::is_decimal_floating_point_v, T) |
| 150 | +{ |
| 151 | + return from_bid_d32(bits); |
| 152 | +} |
| 153 | + |
| 154 | +template <> |
| 155 | +constexpr auto from_bid<decimal_fast32_t>(const std::uint32_t bits) noexcept -> decimal_fast32_t |
| 156 | +{ |
| 157 | + return from_bid_d32f(bits); |
| 158 | +} |
| 159 | + |
| 160 | +BOOST_DECIMAL_EXPORT template <typename T = decimal64_t> |
| 161 | +constexpr auto from_bid(const std::uint64_t bits) noexcept |
| 162 | + BOOST_DECIMAL_REQUIRES(detail::is_decimal_floating_point_v, T) |
| 163 | +{ |
| 164 | + return from_bid_d64(bits); |
| 165 | +} |
| 166 | + |
| 167 | +template <> |
| 168 | +constexpr auto from_bid<decimal_fast64_t>(const std::uint64_t bits) noexcept -> decimal_fast64_t |
| 169 | +{ |
| 170 | + return from_bid_d64f(bits); |
| 171 | +} |
| 172 | + |
| 173 | +BOOST_DECIMAL_EXPORT template <typename T = decimal128_t> |
| 174 | +constexpr auto from_bid(const int128::uint128_t bits) noexcept |
| 175 | + BOOST_DECIMAL_REQUIRES(detail::is_decimal_floating_point_v, T) |
| 176 | +{ |
| 177 | + return from_bid_d128(bits); |
| 178 | +} |
| 179 | + |
| 180 | +template <> |
| 181 | +constexpr auto from_bid<decimal_fast128_t>(const int128::uint128_t bits) noexcept -> decimal_fast128_t |
| 182 | +{ |
| 183 | + return from_bid_d128f(bits); |
| 184 | +} |
| 185 | + |
| 186 | +#if defined(__GNUC__) && __GNUC__ == 7 |
| 187 | +# pragma GCC diagnostic pop |
| 188 | +#endif |
| 189 | + |
| 190 | +} // namespace decimal |
| 191 | +} // namespace boost |
| 192 | + |
| 193 | +#endif //BOOST_BID_CONVERSION_HPP |
0 commit comments