forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimf_impl_utils.hpp
406 lines (347 loc) · 10.2 KB
/
imf_impl_utils.hpp
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//==------- imf_impl_utils.hpp - utils definitions used by half and bfloat16 imf
// functions -----==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//==------------------------------------------------------------------------==//
#ifndef __LIBDEVICE_IMF_IMPL_UTILS_H__
#define __LIBDEVICE_IMF_IMPL_UTILS_H__
#include <cstddef>
#include <cstdint>
// Rounding mode are used internally by type convert functions in imf libdevice
// and we don't want to include system's fenv.h, so we define ourselves'.
typedef enum {
__IML_RTE, // round to nearest-even
__IML_RTZ, // round to zero
__IML_RTP, // round to +inf
__IML_RTN, // round to -inf
} __iml_rounding_mode;
template <typename Ty> struct __iml_get_unsigned {};
template <> struct __iml_get_unsigned<short> {
using utype = uint16_t;
};
template <> struct __iml_get_unsigned<int> {
using utype = uint32_t;
};
template <> struct __iml_get_unsigned<long long> {
using utype = uint64_t;
};
// pre assumes input value is not 0.
template <typename Ty> size_t get_msb_pos(const Ty &x) {
size_t idx = 0;
Ty mask = ((Ty)1 << (sizeof(Ty) * 8 - 1));
for (idx = 0; idx < (sizeof(Ty) * 8); ++idx) {
if ((x & mask) == mask)
break;
mask >>= 1;
}
return (sizeof(Ty) * 8 - 1 - idx);
}
class __iml_ui128 {
public:
__iml_ui128() = default;
__iml_ui128(const __iml_ui128 &) = default;
explicit __iml_ui128(uint64_t x) {
bits[0] = x;
bits[1] = 0;
}
__iml_ui128 &operator=(const __iml_ui128 &x) {
if (this != &x) {
this->bits[0] = x.bits[0];
this->bits[1] = x.bits[1];
}
return *this;
}
__iml_ui128 &operator=(const uint64_t &x) {
this->bits[0] = x;
this->bits[1] = 0;
return *this;
}
explicit operator uint64_t() { return bits[0]; }
explicit operator uint32_t() { return static_cast<uint32_t>(bits[0]); }
__iml_ui128 operator<<(size_t n) {
if (n == 0)
return *this;
if (n >= 128)
return static_cast<__iml_ui128>(0x0U);
__iml_ui128 x = *this;
if (n >= 64) {
x.bits[1] = x.bits[0] << (n - 64);
x.bits[0] = 0x0;
} else {
x.bits[1] = x.bits[1] << n;
x.bits[1] =
x.bits[1] |
((x.bits[0] & ~((static_cast<uint64_t>(0x1) << (64 - n)) - 1)) >>
(64 - n));
x.bits[0] = x.bits[0] << n;
}
return x;
}
__iml_ui128 operator>>(size_t n) {
if (n == 0)
return *this;
if (n >= 128)
return static_cast<__iml_ui128>(0x0U);
__iml_ui128 x = *this;
if (n >= 64) {
x.bits[0] = x.bits[1] >> (n - 64);
x.bits[1] = 0x0;
} else {
x.bits[0] = x.bits[0] >> n;
x.bits[0] =
x.bits[0] |
((x.bits[1] & ((static_cast<uint64_t>(0x1) << n) - 1)) << (64 - n));
x.bits[1] = x.bits[1] >> n;
}
return x;
}
__iml_ui128 operator+(const __iml_ui128 &x) {
__iml_ui128 res;
res.bits[0] = this->bits[0] + x.bits[0];
res.bits[1] = this->bits[1] + x.bits[1];
if (res.bits[0] < this->bits[0] || res.bits[0] < x.bits[0])
res.bits[1] += 1;
return res;
}
__iml_ui128 operator+(const uint64_t &x) {
__iml_ui128 res;
res.bits[0] = this->bits[0] + x;
res.bits[1] = this->bits[1];
if (res.bits[0] < this->bits[0] || res.bits[0] < x)
res.bits[1] += 1;
return res;
}
__iml_ui128 operator+=(const __iml_ui128 &x) {
uint64_t temp = this->bits[0] + x.bits[0];
this->bits[1] += x.bits[1];
if ((temp < this->bits[0]) || (temp < x.bits[0]))
this->bits[1] += 1;
this->bits[0] = temp;
return *this;
}
__iml_ui128 operator+(int x) {
return this->operator+(static_cast<uint64_t>(x));
}
__iml_ui128 operator-(const uint64_t &x) {
__iml_ui128 res;
res.bits[0] = this->bits[0] - x;
res.bits[1] = this->bits[1];
if (res.bits[0] > this->bits[0])
res.bits[1]--;
return res;
}
__iml_ui128 operator-(int x) {
return this->operator-(static_cast<uint64_t>(x));
}
__iml_ui128 operator-(const __iml_ui128 &x) {
__iml_ui128 res;
res.bits[0] = this->bits[0] - x.bits[0];
res.bits[1] = this->bits[1];
if (res.bits[0] > this->bits[0])
res.bits[1]--;
return res;
}
__iml_ui128 operator-=(const __iml_ui128 &x) {
uint64_t temp = this->bits[0];
this->bits[0] -= x.bits[0];
if (this->bits[0] > temp)
this->bits[1] -= 1;
this->bits[1] -= x.bits[1];
return *this;
}
__iml_ui128 operator-=(uint64_t x) {
uint64_t temp = this->bits[0];
this->bits[0] -= x;
if (this->bits[0] > temp)
this->bits[1] -= 1;
return *this;
}
__iml_ui128 operator-=(int x) {
return this->operator-=(static_cast<uint64_t>(x));
}
bool operator==(const __iml_ui128 &x) {
if (this == &x)
return true;
return (this->bits[0] != x.bits[0]) ? false : (this->bits[1] == x.bits[1]);
}
bool operator!=(const __iml_ui128 &x) { return !operator==(x); }
bool operator==(const uint64_t &x) {
return (this->bits[1] != 0) ? false : (this->bits[0] == x);
}
bool operator!=(const uint64_t &x) { return !operator==(x); }
bool operator!=(int x) { return !operator==(static_cast<uint64_t>(x)); }
bool operator>(const __iml_ui128 &x) {
if (this->bits[1] > x.bits[1])
return true;
else if (this->bits[1] < x.bits[1])
return false;
else
return (this->bits[0] > x.bits[0]);
}
bool operator>=(const __iml_ui128 &x) {
return operator==(x) || operator>(x);
}
bool operator>(const uint64_t &x) {
if (this->bits[1] > 0)
return true;
return this->bits[0] > x;
}
__iml_ui128 operator&(const __iml_ui128 &x) {
__iml_ui128 res;
res.bits[0] = this->bits[0] & x.bits[0];
res.bits[1] = this->bits[1] & x.bits[1];
return res;
}
__iml_ui128 operator&(const uint64_t &x) {
__iml_ui128 res;
res.bits[0] = this->bits[0] & x;
res.bits[1] = 0x0;
return res;
}
__iml_ui128 operator&(const int64_t &x) {
__iml_ui128 res;
uint64_t ux = static_cast<uint64_t>(x);
res.bits[0] = this->bits[0] & ux;
res.bits[1] = 0x0;
return res;
}
__iml_ui128 operator&(const uint32_t &x) {
__iml_ui128 res;
res.bits[0] = this->bits[0] & x;
res.bits[1] = 0x0;
return res;
}
__iml_ui128 operator&(const int32_t &x) {
__iml_ui128 res;
uint32_t ux = static_cast<uint32_t>(x);
res.bits[0] = this->bits[0] & ux;
res.bits[1] = 0x0;
return res;
}
__iml_ui128 operator&=(const __iml_ui128 &x) {
this->bits[0] &= x.bits[0];
this->bits[1] &= x.bits[1];
return *this;
}
__iml_ui128 operator|(const __iml_ui128 &x) {
__iml_ui128 res;
res.bits[0] = this->bits[0] | x.bits[0];
res.bits[1] = this->bits[1] | x.bits[1];
return res;
}
__iml_ui128 operator|(const uint64_t &x) {
__iml_ui128 res;
res.bits[0] = this->bits[0] | x;
res.bits[1] = 0x0;
return res;
}
__iml_ui128 operator|=(const __iml_ui128 &x) {
this->bits[0] |= x.bits[0];
this->bits[1] |= x.bits[1];
return *this;
}
__iml_ui128 operator~() {
__iml_ui128 res;
res.bits[0] = ~this->bits[0];
res.bits[1] = ~this->bits[1];
return res;
}
size_t ui128_msb_pos() const {
if (this->bits[1] == 0)
return get_msb_pos<uint64_t>(this->bits[0]);
else
return get_msb_pos<uint64_t>(this->bits[1]) + 64;
}
// overflow is not considered here.
__iml_ui128 operator*(const __iml_ui128 &x) {
__iml_ui128 res{0x0}, tmp1, tmp2, b1;
size_t msb1 = this->ui128_msb_pos();
size_t msb2 = x.ui128_msb_pos();
size_t min_msb;
if (msb1 < msb2) {
min_msb = msb1;
tmp1 = x;
tmp2 = *this;
} else {
min_msb = msb2;
tmp1 = *this;
tmp2 = x;
}
for (size_t idx = 0; idx <= min_msb; ++idx) {
b1 = static_cast<__iml_ui128>(0x1);
b1 = b1 << idx;
__iml_ui128 t3 = tmp2 & b1;
if (t3 == b1) {
res = res + tmp1;
}
tmp1 = tmp1 << 1;
}
return res;
}
uint64_t bits[2];
};
template <typename Ty> struct __iml_get_double_size_unsigned {};
template <> struct __iml_get_double_size_unsigned<uint16_t> {
using utype = uint32_t;
};
template <> struct __iml_get_double_size_unsigned<uint32_t> {
using utype = uint64_t;
};
template <> struct __iml_get_double_size_unsigned<uint64_t> {
using utype = __iml_ui128;
};
template <typename Ty> struct __iml_fp_config {};
template <> struct __iml_fp_config<float> {
// signed/unsigned integral type with same size
using utype = uint32_t;
using stype = int32_t;
const static int32_t bias = 127;
const static uint32_t exp_mask = 0xFF;
const static uint32_t fra_mask = 0x7FFFFF;
const static uint32_t nan_bits = 0x7FC00000;
const static uint32_t pos_inf_bits = 0x7F800000;
const static uint32_t neg_inf_bits = 0xFF800000;
const static uint32_t max_fin_bits = 0x7F7FFFFF;
const static uint32_t min_fin_bits = 0xFF7FFFFF;
};
template <> struct __iml_fp_config<double> {
using utype = uint64_t;
using stype = int64_t;
const static int32_t bias = 1023;
const static uint64_t exp_mask = 0x7FF;
const static uint64_t fra_mask = 0xFFFFFFFFFFFFF;
const static uint64_t nan_bits = 0x7FF8000000000000;
const static uint64_t pos_inf_bits = 0x7FF0000000000000;
const static uint64_t neg_inf_bits = 0xFFF0000000000000;
const static uint64_t max_fin_bits = 0x7FEFFFFFFFFFFFFF;
const static uint64_t min_fin_bits = 0xFFEFFFFFFFFFFFFF;
};
// Pre-assumption, fra is not all zero bit from bit pos idx - 1 to 0
template <typename Ty> static int get_leading_zeros_from(Ty fra, int idx) {
Ty y = static_cast<Ty>(0x1) << (idx - 1);
for (size_t i = 0; i < idx; ++i) {
if ((fra & y) == y)
return i;
y >>= 1;
}
// FATAL error;
return -1;
}
static int get_leading_zeros_from(__iml_ui128 fra, int idx) {
if (idx <= 64)
return get_leading_zeros_from<uint64_t>(fra.bits[0], idx);
else if (idx <= 128) {
size_t tmp_idx = idx - 64;
uint64_t b1(1);
if (fra.bits[1] & ((b1 << tmp_idx) - 1)) {
return get_leading_zeros_from<uint64_t>(fra.bits[1], tmp_idx);
} else {
return tmp_idx + get_leading_zeros_from<uint64_t>(fra.bits[0], 64);
}
} else
return -1;
}
#endif // __LIBDEVICE_IMF_IMPL_UTILS_H__