-
Notifications
You must be signed in to change notification settings - Fork 0
/
FF.h
89 lines (70 loc) · 1.73 KB
/
FF.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma once
#include <cstdint>
#include <iosfwd>
#include <string>
namespace FF {
typedef uint16_t hex_half;
typedef uint32_t hex_single;
typedef uint64_t hex_double;
template <class T>
struct NativeFloat;
template <>
struct NativeFloat<hex_double> {
using NativeType = double;
};
template <>
struct NativeFloat<hex_single> {
using NativeType = float;
};
template <>
struct NativeFloat<hex_half> {
using NativeType = float;
};
template <class T>
struct FloatInfo final {
public:
FloatInfo() = delete;
explicit FloatInfo(T x) {
hex_val.hex = x;
};
explicit FloatInfo(typename NativeFloat<T>::NativeType x) {
hex_val.f = x;
};
static const unsigned mantissa_bits;
static const unsigned exponent_bits;
static const unsigned sign_shift;
static const T mantissa_mask;
static const T exponent_mask;
static const T sign_mask;
static const int exp_bias;
bool isDenorm() const;
bool isInf() const;
bool isNan() const;
bool isZero() const;
private:
template <class X>
friend std::ostream& operator<<(std::ostream&, const FloatInfo<X>&);
union Converter {
typename NativeFloat<T>::NativeType f;
T hex;
};
union Converter hex_val;
T getSignBits() const {
return (hex_val.hex & sign_mask) >> ((sizeof(T) * 8) - 1);
}
T getExponentBits() const {
return (hex_val.hex & exponent_mask) >> mantissa_bits;
}
T getMantissaBits() const {
return hex_val.hex & mantissa_mask;
}
};
template <class T>
std::ostream& operator<<(std::ostream& os, const FloatInfo<T>& fi);
template <class T>
typename NativeFloat<T>::NativeType parseString(const std::string&& str);
void printAll(const double);
void parseHalf(const char*);
void parseSingle(const char*);
void parseDouble(const char*);
}