summaryrefslogtreecommitdiffstats
path: root/floatprops.h
blob: c7a3b7fbc2e41f70310b2c121943388a7dc31cab (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// -*-C++-*-

#ifndef FLOATPROPS_H
#define FLOATPROPS_H

#include "floattypes.h"

#include <cassert>
#include <cmath>
#include <cstring>
#include <limits>

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:
  //    min
  //    max
  //    digits
  //    epsilon
  //    min_exponent
  //    max_exponent
  //    infinity
  //    quiet_NaN
};

// Properties of fp8
template <> struct floatprops<fp8> {
  typedef fp8 real_t;
  typedef vml_std::int8_t int_t;
  typedef vml_std::uint8_t uint_t;

  static char const *name() { return "fp8"; }

  // Definitions that might come from numeric_limits<> instead:
  static real_t min() { __builtin_unreachable(); }
  static real_t max() { __builtin_unreachable(); }
  static int const digits = 4;
  static real_t epsilon() { __builtin_unreachable(); }
  static int const min_exponent = -6;
  static int const max_exponent = 7;
  static real_t infinity() { __builtin_unreachable(); }
  static real_t quiet_NaN() { __builtin_unreachable(); }

  // 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(~uint_t(0)),
                "error in masks");

  // Re-interpret bit patterns
  static real_t as_float(int_t x) {
    real_t res;
    std::memcpy(&res, &x, sizeof res);
    return res;
  }
  static int_t as_int(real_t x) {
    int_t res;
    std::memcpy(&res, &x, sizeof res);
    return res;
  }
  static int_t replicate_byte(unsigned char byte) {
    int_t res;
    std::memset(&res, byte, sizeof res);
    return res;
  }

  // Convert values (truncate)
  static real_t convert_float(int_t x) { __builtin_unreachable(); }
  static int_t convert_int(real_t x) { __builtin_unreachable(); }
};

// Properties of fp16
template <> struct floatprops<fp16> {
  typedef fp16 real_t;
  typedef vml_std::int16_t int_t;
  typedef vml_std::uint16_t uint_t;

  static char const *name() { return "fp16"; }

  // Definitions that might come from numeric_limits<> instead:
  static real_t min() { __builtin_unreachable(); }
  static real_t max() { __builtin_unreachable(); }
  static int const digits = 11;
  static real_t epsilon() { __builtin_unreachable(); }
  static int const min_exponent = -14;
  static int const max_exponent = 15;
  static real_t infinity() { __builtin_unreachable(); }
  static real_t quiet_NaN() { __builtin_unreachable(); }

  // 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(~uint_t(0)),
                "error in masks");

  // Re-interpret bit patterns
  static real_t as_float(int_t x) {
    real_t res;
    std::memcpy(&res, &x, sizeof res);
    return res;
  }
  static int_t as_int(real_t x) {
    int_t res;
    std::memcpy(&res, &x, sizeof res);
    return res;
  }
  static int_t replicate_byte(unsigned char byte) {
    int_t res;
    std::memset(&res, byte, sizeof res);
    return res;
  }

  // Convert values (truncate)
  static real_t convert_float(int_t x) { __builtin_unreachable(); }
  static int_t convert_int(real_t x) { __builtin_unreachable(); }
};

// Properties of float
template <> struct floatprops<float> : std::numeric_limits<float> {
  typedef float real_t;
  typedef vml_std::int32_t int_t;
  typedef vml_std::uint32_t uint_t;

  static char const *name() { return "float"; }

  // 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(~uint_t(0)),
                "error in masks");

  // Re-interpret bit patterns
  static real_t as_float(int_t x) {
    real_t res;
    std::memcpy(&res, &x, sizeof res);
    return res;
  }
  static int_t as_int(real_t x) {
    int_t res;
    std::memcpy(&res, &x, sizeof res);
    return res;
  }
  static int_t replicate_byte(unsigned char byte) {
    int_t res;
    std::memset(&res, byte, sizeof res);
    return res;
  }

  // Convert values (truncate)
  static real_t convert_float(int_t x) { return real_t(x); }
  static int_t convert_int(real_t x) { return int_t(x); }
};

// Properties of double
template <> struct floatprops<double> : std::numeric_limits<double> {
  typedef double real_t;
  typedef vml_std::int64_t int_t;
  typedef vml_std::uint64_t uint_t;

  static char const *name() { return "double"; }

  // 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(~uint_t(0)),
                "error in masks");

  // Re-interpret bit patterns
  static real_t as_float(int_t x) {
    real_t res;
    std::memcpy(&res, &x, sizeof res);
    return res;
  }
  static int_t as_int(real_t x) {
    int_t res;
    std::memcpy(&res, &x, sizeof res);
    return res;
  }
  static int_t replicate_byte(unsigned char byte) {
    int_t res;
    std::memset(&res, byte, sizeof res);
    return res;
  }

  // Convert values (truncate)
  static real_t convert_float(int_t x) { return real_t(x); }
  static int_t convert_int(real_t x) { return int_t(x); }
};

// We are adding the (unused) type RV here to avoid name mangling
// problems. On some systems, the vector size does not enter into
// the mangled name (!), leading to duplicate function definitions.
template <typename RV, typename V, typename E>
E get_elt(const V &v, const int n) {
  const size_t s = sizeof(E);
  E e;
  // assert(n>=0 and s*n<sizeof(V));
  std::memcpy(&e, &((const char *)&v)[s * n], s);
  return e;
}

template <typename RV, typename V, typename E>
V &set_elt(V &v, const int n, const E e) {
  const size_t s = sizeof(E);
  // assert(n>=0 and s*n<sizeof(V));
  std::memcpy(&((char *)&v)[s * n], &e, s);
  return v;
}

} // namespace vecmathlib

#endif // #ifndef FLOATPROPS_H
OpenPOWER on IntegriCloud