xref: /aosp_15_r20/external/abseil-cpp/absl/numeric/internal/representation.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2021 The Abseil Authors
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_NUMERIC_INTERNAL_REPRESENTATION_H_
16*9356374aSAndroid Build Coastguard Worker #define ABSL_NUMERIC_INTERNAL_REPRESENTATION_H_
17*9356374aSAndroid Build Coastguard Worker 
18*9356374aSAndroid Build Coastguard Worker #include <limits>
19*9356374aSAndroid Build Coastguard Worker 
20*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
21*9356374aSAndroid Build Coastguard Worker 
22*9356374aSAndroid Build Coastguard Worker namespace absl {
23*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
24*9356374aSAndroid Build Coastguard Worker namespace numeric_internal {
25*9356374aSAndroid Build Coastguard Worker 
26*9356374aSAndroid Build Coastguard Worker // Returns true iff long double is represented as a pair of doubles added
27*9356374aSAndroid Build Coastguard Worker // together.
IsDoubleDouble()28*9356374aSAndroid Build Coastguard Worker inline constexpr bool IsDoubleDouble() {
29*9356374aSAndroid Build Coastguard Worker   // A double-double value always has exactly twice the precision of a double
30*9356374aSAndroid Build Coastguard Worker   // value--one double carries the high digits and one double carries the low
31*9356374aSAndroid Build Coastguard Worker   // digits. This property is not shared with any other common floating-point
32*9356374aSAndroid Build Coastguard Worker   // representation, so this test won't trigger false positives. For reference,
33*9356374aSAndroid Build Coastguard Worker   // this table gives the number of bits of precision of each common
34*9356374aSAndroid Build Coastguard Worker   // floating-point representation:
35*9356374aSAndroid Build Coastguard Worker   //
36*9356374aSAndroid Build Coastguard Worker   //                type     precision
37*9356374aSAndroid Build Coastguard Worker   //         IEEE single          24 b
38*9356374aSAndroid Build Coastguard Worker   //         IEEE double          53
39*9356374aSAndroid Build Coastguard Worker   //     x86 long double          64
40*9356374aSAndroid Build Coastguard Worker   //       double-double         106
41*9356374aSAndroid Build Coastguard Worker   //      IEEE quadruple         113
42*9356374aSAndroid Build Coastguard Worker   //
43*9356374aSAndroid Build Coastguard Worker   // Note in particular that a quadruple-precision float has greater precision
44*9356374aSAndroid Build Coastguard Worker   // than a double-double float despite taking up the same amount of memory; the
45*9356374aSAndroid Build Coastguard Worker   // quad has more of its bits allocated to the mantissa than the double-double
46*9356374aSAndroid Build Coastguard Worker   // has.
47*9356374aSAndroid Build Coastguard Worker   return std::numeric_limits<long double>::digits ==
48*9356374aSAndroid Build Coastguard Worker          2 * std::numeric_limits<double>::digits;
49*9356374aSAndroid Build Coastguard Worker }
50*9356374aSAndroid Build Coastguard Worker 
51*9356374aSAndroid Build Coastguard Worker }  // namespace numeric_internal
52*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
53*9356374aSAndroid Build Coastguard Worker }  // namespace absl
54*9356374aSAndroid Build Coastguard Worker 
55*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_NUMERIC_INTERNAL_REPRESENTATION_H_
56