xref: /minix3/external/bsd/libc++/dist/libcxx/test/support/hexfloat.h (revision 4684ddb6aab0b36791c8099bc705d6140b3d05d0)
1*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
2*4684ddb6SLionel Sambuc //
3*4684ddb6SLionel Sambuc //                     The LLVM Compiler Infrastructure
4*4684ddb6SLionel Sambuc //
5*4684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*4684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*4684ddb6SLionel Sambuc //
8*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
9*4684ddb6SLionel Sambuc 
10*4684ddb6SLionel Sambuc // Define a hexfloat literal emulator since we can't depend on being able to
11*4684ddb6SLionel Sambuc //   for hexfloat literals
12*4684ddb6SLionel Sambuc 
13*4684ddb6SLionel Sambuc // 0x10.F5p-10 == hexfloat<double>(0x10, 0xF5, -10)
14*4684ddb6SLionel Sambuc 
15*4684ddb6SLionel Sambuc #ifndef HEXFLOAT_H
16*4684ddb6SLionel Sambuc #define HEXFLOAT_H
17*4684ddb6SLionel Sambuc 
18*4684ddb6SLionel Sambuc #include <algorithm>
19*4684ddb6SLionel Sambuc #include <cmath>
20*4684ddb6SLionel Sambuc #include <climits>
21*4684ddb6SLionel Sambuc 
22*4684ddb6SLionel Sambuc template <class T>
23*4684ddb6SLionel Sambuc class hexfloat
24*4684ddb6SLionel Sambuc {
25*4684ddb6SLionel Sambuc     T value_;
26*4684ddb6SLionel Sambuc public:
hexfloat(long long m1,unsigned long long m0,int exp)27*4684ddb6SLionel Sambuc     hexfloat(long long m1, unsigned long long m0, int exp)
28*4684ddb6SLionel Sambuc     {
29*4684ddb6SLionel Sambuc         const std::size_t n = sizeof(unsigned long long) * CHAR_BIT;
30*4684ddb6SLionel Sambuc         int s = m1 < 0 ? -1 : 1;
31*4684ddb6SLionel Sambuc         value_ = std::ldexp(m1 + s * std::ldexp(T(m0), -static_cast<int>(n -
32*4684ddb6SLionel Sambuc                                                      std::__clz(m0)/4*4)), exp);
33*4684ddb6SLionel Sambuc     }
34*4684ddb6SLionel Sambuc 
T()35*4684ddb6SLionel Sambuc     operator T() const {return value_;}
36*4684ddb6SLionel Sambuc };
37*4684ddb6SLionel Sambuc 
38*4684ddb6SLionel Sambuc #endif
39