Dominik Laskowski | 9f5e5db | 2021-05-25 12:52:24 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <limits> |
| 20 | #include <type_traits> |
| 21 | |
| 22 | namespace android::ftl::details { |
| 23 | |
| 24 | // Exponent whose power of 2 is the (exclusive) upper bound of T. |
| 25 | template <typename T, typename L = std::numeric_limits<T>> |
| 26 | constexpr int max_exponent = std::is_floating_point_v<T> ? L::max_exponent : L::digits; |
| 27 | |
| 28 | // Extension of std::numeric_limits<T> that reduces the maximum for integral types T such that it |
| 29 | // has an exact representation for floating-point types F. For example, the maximum int32_t value |
| 30 | // is 2'147'483'647, but casting it to float commonly rounds up to 2'147'483'650.f, which cannot |
| 31 | // be safely converted back lest the signed overflow invokes undefined behavior. This pitfall is |
| 32 | // avoided by clearing the lower (31 - 24 =) 7 bits of precision to 2'147'483'520. Note that the |
| 33 | // minimum is representable. |
| 34 | template <typename T, typename F> |
| 35 | struct safe_limits : std::numeric_limits<T> { |
| 36 | static constexpr T max() { |
| 37 | using Base = std::numeric_limits<T>; |
| 38 | |
| 39 | if constexpr (std::is_integral_v<T> && std::is_floating_point_v<F>) { |
| 40 | // Assume the mantissa is 24 bits for float, or 53 bits for double. |
| 41 | using Float = std::numeric_limits<F>; |
| 42 | static_assert(Float::is_iec559); |
| 43 | |
| 44 | // If the integer is wider than the mantissa, clear the excess bits of precision. |
| 45 | constexpr int kShift = Base::digits - Float::digits; |
| 46 | if constexpr (kShift > 0) { |
| 47 | using U = std::make_unsigned_t<T>; |
| 48 | constexpr U kOne = static_cast<U>(1); |
| 49 | return static_cast<U>(Base::max()) & ~((kOne << kShift) - kOne); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return Base::max(); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | } // namespace android::ftl::details |