hexfloat.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. // Define a hexfloat literal emulator since we can't depend on being able to
  10. // for hexfloat literals
  11. // 0x10.F5p-10 == hexfloat<double>(0x10, 0xF5, -10)
  12. #ifndef HEXFLOAT_H
  13. #define HEXFLOAT_H
  14. #include <algorithm>
  15. #include <cmath>
  16. #include <climits>
  17. template <class T>
  18. class hexfloat
  19. {
  20. T value_;
  21. public:
  22. hexfloat(long long m1, unsigned long long m0, int exp)
  23. {
  24. const std::size_t n = sizeof(unsigned long long) * CHAR_BIT;
  25. int s = m1 < 0 ? -1 : 1;
  26. value_ = std::ldexp(m1 + s * std::ldexp(T(m0), -static_cast<int>(n -
  27. std::__clz(m0)/4*4)), exp);
  28. }
  29. operator T() const {return value_;}
  30. };
  31. #endif