summaryrefslogtreecommitdiffstats
path: root/floatprops.h
blob: 8d4ddcf6be90cf4fb83640151f72ed01bdc03c2e (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// -*-C++-*-

#ifndef FLOATPROPS_H
#define FLOATPROPS_H

#include <cmath>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits>
#include <sstream>
#include <typeinfo>



namespace vecmathlib {
  
  // A structure describing various properties of a floating point
  // type. Most properties are already described in numeric_limits, so
  // we inherit it.
  template<typename real_t>
  struct floatprops {
    // Some interesting properties are:
    //    digits
    //    epsilon
    //    min_exponent
    //    max_exponent
  };
  
  template<typename int_t>
  struct intprops {
  };
  
  
  
  // Properties of float
  template<>
  struct floatprops<float>: std::numeric_limits<float> {
    typedef float real_t;
    typedef int32_t int_t;
    typedef uint32_t uint_t;
    
    // Ensure the internal representation is what we expect
    static_assert(is_signed, "real_t is not signed");
    static_assert(radix==2, "real_t is not binary");
    
    // Ensure the sizes match
    static_assert(sizeof(real_t) == sizeof(int_t), "int_t has wrong size");
    static_assert(sizeof(real_t) == sizeof(uint_t), "uint_t has wrong size");
    
    // Number of bits in internal representation
    static int const bits = 8 * sizeof(real_t);
    static int const mantissa_bits = digits - 1;
    static int const signbit_bits = 1;
    static int const exponent_bits = bits - mantissa_bits - signbit_bits;
    static int const exponent_offset = 2 - min_exponent;
    static_assert(mantissa_bits + exponent_bits + signbit_bits == bits,
                  "error in bit counts");
    static uint_t const mantissa_mask = (uint_t(1) << mantissa_bits) - 1;
    static uint_t const exponent_mask =
      ((uint_t(1) << exponent_bits) - 1) << mantissa_bits;
    static uint_t const signbit_mask = uint_t(1) << (bits-1);
    static_assert((mantissa_mask & exponent_mask & signbit_mask) == uint_t(0),
                  "error in masks");
    static_assert((mantissa_mask | exponent_mask | signbit_mask) == ~uint_t(0),
                  "error in masks");
    
    // Re-interpret bit patterns
    static inline real_t as_float(int_t x)
    {
      // return *(real_t*)&x;
      // union { int_t i; real_t r; } ir;
      // return ir.i=x, ir.r;
      real_t res;
      std::memcpy(&res, &x, sizeof res);
      return res;
    }
    static inline int_t as_int(real_t x)
    {
      // return *(int_t*)&x;
      // union { real_t r; int_t i; } ri;
      // return ri.r=x, ri.i;
      int_t res;
      std::memcpy(&res, &x, sizeof res);
      return res;
    }
    
    // Convert values
    static inline real_t convert_float(int_t x) { return real_t(x); }
    static inline int_t convert_int(real_t x)
    {
      static_assert(sizeof std::lrint(x) >= sizeof(int_t),
                    "lrint() has wrong return type");
      long res = std::lrint(x);
      if (sizeof std::lrint(x) > sizeof(int_t)) {
        if (res < std::numeric_limits<int_t>::min() ||
            res > std::numeric_limits<int_t>::max())
        {
          return std::numeric_limits<int_t>::min();
        }
      }
      return res;
    }
  };
  
  template<>
  struct intprops<floatprops<float>::int_t> {
    typedef float real_t;
  };
  
  
  
  // Properties of double
  template<>
  struct floatprops<double>: std::numeric_limits<double> {
    typedef double real_t;
    typedef int64_t int_t;
    typedef uint64_t uint_t;
    
    // Ensure the internal representation is what we expect
    static_assert(is_signed, "real_t is not signed");
    static_assert(radix==2, "real_t is not binary");
    
    // Ensure the sizes match
    static_assert(sizeof(real_t) == sizeof(int_t), "int_t has wrong size");
    static_assert(sizeof(real_t) == sizeof(uint_t), "uint_t has wrong size");
    
    // Number of bits in internal representation
    static int const bits = 8 * sizeof(real_t);
    static int const mantissa_bits = digits - 1;
    static int const signbit_bits = 1;
    static int const exponent_bits = bits - mantissa_bits - signbit_bits;
    static int const exponent_offset = 2 - min_exponent;
    static_assert(mantissa_bits + exponent_bits + signbit_bits == bits,
                  "error in bit counts");
    static uint_t const mantissa_mask = (uint_t(1) << mantissa_bits) - 1;
    static uint_t const exponent_mask =
      ((uint_t(1) << exponent_bits) - 1) << mantissa_bits;
    static uint_t const signbit_mask = uint_t(1) << (bits-1);
    static_assert((mantissa_mask & exponent_mask & signbit_mask) == uint_t(0),
                  "error in masks");
    static_assert((mantissa_mask | exponent_mask | signbit_mask) == ~uint_t(0),
                  "error in masks");
    
    // Re-interpret bit patterns
    static inline real_t as_float(int_t x)
    {
      // return *(real_t*)&x;
      // union { int_t i; real_t r; } ir;
      // return ir.i=x, ir.r;
      real_t res;
      std::memcpy(&res, &x, sizeof res);
      return res;
    }
    static inline int_t as_int(real_t x)
    {
      // return *(int_t*)&x;
      // union { real_t r; int_t i; } ri;
      // return ri.r=x, ri.i;
      int_t res;
      std::memcpy(&res, &x, sizeof res);
      return res;
    }
    
    // Convert values
    static inline real_t convert_float(int_t x) { return real_t(x); }
    static inline int_t convert_int(real_t x)
    {
      static_assert(sizeof std::lrint(x) >= sizeof(int_t),
                    "lrint() has wrong return type");
      long res = std::lrint(x);
      if (sizeof std::lrint(x) > sizeof(int_t)) {
        if (res < std::numeric_limits<int_t>::min() ||
            res > std::numeric_limits<int_t>::max())
        {
          return std::numeric_limits<int_t>::min();
        }
      }
      return res;
    }
  };
  
  template<>
  struct intprops<floatprops<double>::int_t> {
    typedef double real_t;
  };
  
  
  
} // namespace vecmathlib

#endif  // #ifndef FLOATPROPS_H
OpenPOWER on IntegriCloud