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