representation.h 2.0 KB

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