Howard Hinnant | 0a11111 | 2011-05-13 21:52:40 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // Define a hexfloat literal emulator since we can't depend on being able to |
| 11 | // for hexfloat literals |
| 12 | |
| 13 | // 0x10.F5p-10 == hexfloat<double>(0x10, 0xF5, -10) |
| 14 | |
| 15 | #ifndef HEXFLOAT_H |
| 16 | #define HEXFLOAT_H |
| 17 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 18 | #include <algorithm> |
Howard Hinnant | 0a11111 | 2011-05-13 21:52:40 +0000 | [diff] [blame] | 19 | #include <cmath> |
| 20 | #include <climits> |
| 21 | |
| 22 | template <class T> |
| 23 | class hexfloat |
| 24 | { |
| 25 | T value_; |
| 26 | public: |
Howard Hinnant | e80c36e | 2011-05-14 14:33:56 +0000 | [diff] [blame] | 27 | hexfloat(long long m1, unsigned long long m0, int exp) |
Howard Hinnant | 0a11111 | 2011-05-13 21:52:40 +0000 | [diff] [blame] | 28 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 29 | const std::size_t n = sizeof(unsigned long long) * CHAR_BIT; |
Howard Hinnant | e80c36e | 2011-05-14 14:33:56 +0000 | [diff] [blame] | 30 | int s = m1 < 0 ? -1 : 1; |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 31 | value_ = std::ldexp(m1 + s * std::ldexp(T(m0), -static_cast<int>(n - |
| 32 | std::__clz(m0)/4*4)), exp); |
Howard Hinnant | 0a11111 | 2011-05-13 21:52:40 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | operator T() const {return value_;} |
| 36 | }; |
| 37 | |
| 38 | #endif |