-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathatypes.h
More file actions
564 lines (479 loc) · 15.3 KB
/
atypes.h
File metadata and controls
564 lines (479 loc) · 15.3 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*****************************************************************************
This is part of Alterlib - the free code collection under the MIT License
------------------------------------------------------------------------------
Copyright (C) 2006-2023 Maxim L. Grishin (altmer@arts-union.ru)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*****************************************************************************/
#ifndef ATYPES_H
#define ATYPES_H
#include <atomic>
//////////////////////////////////////////////////////////////////
//определение типов
//////////////////////////////////////////////////////////////////
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;
typedef signed char int8;
typedef short int16;
typedef int int32;
typedef signed long long int64;
typedef float real32;
typedef double real64;
#ifndef ANDROID_NDK
typedef long double real80;
typedef long double realx;
#else
typedef double realx;
#endif
typedef double real;
#if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__) ) \
|| defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
typedef signed long long intz;
typedef unsigned long long uintz;
#else
typedef int intz;
typedef unsigned int uintz;
#endif
typedef signed long long intx;
typedef unsigned long long uintx;
typedef unsigned int uint;
typedef unsigned short charx;
#ifndef NULL
#define NULL 0
#endif
namespace alt {
constexpr uint64 makeSTATE(const char* str)
{ //based on fnv1a hash
const uint64 prime = 0x100000001b3;
uint64 hash = 0xcbf29ce484222325;
if (!str) {
return hash;
}
while (*str != '\0') {
hash ^= *str++;
hash *= prime;
}
return hash;
}
constexpr uint64 makeID64(const char *str)
{
uint64 rv = 0;
if(str)
{
for(int i=0;i<sizeof(uint64) && str[i];i++)
rv |= ((uint64)(unsigned char)str[i])<<(i*8);
}
return rv;
}
template<class T>
__inline T roundup(T size, T align)
{
return ((size+align-1)/align)*align;
}
template<class T>
__inline T roundup_div(T size, T align)
{
return (size+align-1)/align;
}
namespace utils {
template<class T>
__inline bool signed_type()
{
T a = 0;
T b = a-1;
return b<a;
}
__inline uintz upsize(uintz x)
{
const uintz MINSIZE = (sizeof(uintz)*8);
return ( ((x+1)+((x)>>1))<MINSIZE ? MINSIZE : ((x+1)+((x)>>1)) );
}
template <class T>
__inline uint strlen(const T *Str)
{
if(!Str)return 0;
uint i=0;
while(Str[i]){i++;};
return i;
}
template <class T>
__inline void memcpy(T *dst, const T *src, intz num)
{
intz i;
if(dst<src) //в случае перекрытий следует копировать с нужной стороны
{
for(i=0;i<num;i++)dst[i]=src[i];
}
else if(dst>src)
{
for(i=num-1;i>=0;i--)dst[i]=src[i];
}
}
template <class T>
__inline int memcmp(const T *dst, const T *src, intz num)
{
intz i;
for(i=0;i<num;i++)
{
if(dst[i]<src[i])return -1;
if(dst[i]>src[i])return 1;
}
return 0;
}
template <class T>
__inline int strcmp(const T *dst, const T *src)
{
int i=0;
while(dst[i] && src[i])
{
if(dst[i]<src[i])return -1;
if(dst[i]>src[i])return 1;
i++;
}
if(dst[i]<src[i])return -1;
if(dst[i]>src[i])return 1;
return 0;
}
template <class T>
__inline void memset(T *dst, T c, intz num)
{
for(intz i=0;i<num;i++)dst[i]=c;
}
//Float16 code origin: https://github.com/ankan-ban/GemmTest/blob/master/utils.h (MIT License)
__inline uint16 fp32_to_fp16(float f32)
{
uint32 f = *(uint32*)& f32;
uint16 f16 = 0;
f16 |= (f >> 16) & 0x8000; // copy sign bit
uint32 e = (f >> 23) & 0xff; // extract exponent
uint32 m = f & 0x7fffff; // extract mantissa
if (e == 255) {
// dealing with a special here
if (m == 0) {
// infinity
return (f16 | 0x7c00); // e=31, m=0, preserve sign
}
else {
// NaN
return 0x7e00; // e=31, m=0x200, s=0
}
}
else if ((e >= 143) || ((e == 142) && (m > 0x7fe000))) {
// not representable in FP16, so return infinity
return (f16 | 0x7c00); // e=31, m=0, preserve sign
}
else if ((e <= 101) || ((e == 102) && (m < 0x2000))) {
// underflow to 0
return f16;
}
else if (e <= 112) {
// denorm situation
m |= 0x800000; // add leading 1
// the 24-bit mantissa needs to shift 14 bits over to
// fit into 10 bits, and then as many bits as the exponent
// is below our denorm exponent
// 127 (fp32 bias)
// - e (actual fp32 exponent)
// + 24 (fp32 mantissa bits including leading 1)
// - 10 (fp16 mantissa bits not including leading 1)
// - 15 (fp16 denorm exponent)
// = 126 - e
m >>= (126 - e);
return (uint16)(f16 | m); // e=0, preserve sign
}
else {
// can convert directly to fp16
e -= 112; // 127 - 15 exponent bias
m >>= 13; // 23 - 10 mantissa bits
return (uint16)(f16 | (e << 10) | m);
}
}
//Float16 code origin: https://github.com/ankan-ban/GemmTest/blob/master/utils.h (MIT License)
__inline float fp16_to_fp32(uint16 f16)
{
uint32 f = f16;
uint32 f32 = 0;
f32 |= (f << 16) & 0x80000000; // copy sign bit
uint32 e = (f >> 10) & 0x1f; // extract exponent
uint32 m = f & 0x3ff; // extract mantissa
if (e == 0) {
if (m == 0) {
// nothing to do; it's already +/- 0
}
else {
// denorm
e = 113;
m <<= 13;
// shift mantissa until the top bit is 1<<23
// note that we've alrady guaranteed that the
// mantissa is non-zero and that the top bit is
// at or below 1<<23
while (!(m & 0x800000)) {
e--;
m <<= 1;
}
m &= 0x7fffff;
f32 |= (e << 23) | m;
}
}
else if (e == 31) {
// FP special
if (m == 0) {
// Inf
f32 |= 0x7f800000; // e=255, m=0, preserve sign
}
else {
// NaN
f32 = 0x7fc00000; // e=255, m=0x800000, s=0
}
}
else {
e += 112; // 127-15 exponent bias
m <<= 13; // 23-10 mantissa bits
f32 |= (e << 23) | m;
}
return *(float*)& f32;
}
} // namespace utils
__inline uintz ptr2int(void *val)
{
return reinterpret_cast<uintz>(val);
}
template<class T>
__inline T* int2ptr(uintz val)
{
return reinterpret_cast<T*>(val);
}
template<class T>
__inline T unaligned_read(void *buff)
{
T rv;
for(uint i=0;i<sizeof(T) && i<sizeof(void*);i++)
{
((uint8*)&rv)[i]=((uint8*)buff)[i];
}
return rv;
}
template<class T>
__inline void unaligned_write(void *buff, T val)
{
for(uint i=0;i<sizeof(T) && i<sizeof(void*);i++)
{
((uint8*)buff)[i]=((uint8*)&val)[i];
}
}
template<class DST, class SRC>
__inline DST reintrpret(const SRC &val)
{
if constexpr (std::is_integral_v<std::remove_cvref_t<SRC>>
&& std::is_integral_v<std::remove_cvref_t<DST>>)
{
return val;
}
else
{
return reinterpret_cast<DST>(val);
}
}
class retCode
{
public:
retCode(){ state=0; }
retCode(const retCode &val){ state=val.state; }
retCode(const int &val){ state=val; }
virtual ~retCode(){}
virtual retCode& operator=(const retCode &val)
{
state=val.state;
return *this;
}
virtual retCode& operator=(int val)
{
state=val;
return *this;
}
virtual retCode& set(int val){state=val;return *this;}
int get(){return state;}
uint value() //значение
{
if(state<0)return 0;
return state;
}
uint error() //номер ошибки
{
if(state>=0)return 0;
return -state;
}
void clear()
{
state = 0;
}
operator int(){return state;}
protected:
int state;
};
template <typename L, typename R>
class pair
{
public:
pair(){vl = L(); vr = R();}
~pair(){}
pair(L left_val, R right_val){vl=left_val;vr=right_val;}
pair(const pair &val){*this=val;}
pair& operator=(const pair &val){vl=val.vl;vr=val.vr;return *this;}
L& left(){return vl;}
R& right(){return vr;}
const L& left() const {return vl;}
const R& right() const {return vr;}
private:
L vl;
R vr;
};
template <typename L, typename M, typename R>
class trio
{
public:
trio(){}
~trio(){}
trio(L left_val, M middle_val, R right_val){vl=left_val;vm=middle_val;vr=right_val;}
trio(const pair<L,R> &val){vl=val.left();vr=val.right();}
trio(const trio &val){*this=val;}
trio& operator=(const trio &val){vl=val.vl;vm=val.vm;vr=val.vr;return *this;}
L& left(){return vl;}
M& middle(){return vm;}
R& right(){return vr;}
const L& left() const {return vl;}
const M& middle() const {return vm;}
const R& right() const {return vr;}
private:
L vl;
M vm;
R vr;
};
struct threadSafeRefCounter {
using value_type = std::atomic<uint>;
static void inc(value_type& c) noexcept {
c.fetch_add(1, std::memory_order_relaxed);
}
static bool dec_is_last(value_type& c) noexcept {
return c.fetch_sub(1, std::memory_order_release) == 1;
}
static void after_last() noexcept {
std::atomic_thread_fence(std::memory_order_acquire);
}
static unsigned load(const value_type& c) noexcept {
return c.load(std::memory_order_relaxed);
}
static bool is_unique(const value_type& c) noexcept {
return load(c) == 1;
}
};
struct singleThreadRefCounter {
using value_type = uint;
static void inc(value_type& c) noexcept { ++c; }
static bool dec_is_last(value_type& c) noexcept { return --c == 0; }
static void after_last() noexcept {}
static unsigned load(const value_type& c) noexcept { return c; }
static bool is_unique(const value_type& c) noexcept { return c == 1; }
};
template <class T, class REF = threadSafeRefCounter>
class shared
{
public:
shared()
{
}
explicit shared(T *val)
{
data = new Internal();
data->object = val;
REF::inc(data->refcount);
}
shared(const shared<T> &val)
{
if(val.data)
{
data = val.data;
REF::inc(data->refcount);
}
}
shared& operator = (const shared<T> &val)
{
if(data == val.data)
return *this;
removeInternal();
if(val.data)
{
data = val.data;
REF::inc(data->refcount);
}
return *this;
}
~shared()
{
removeInternal();
}
void free()
{
removeInternal();
}
T* get()
{
if(!data)
return nullptr;
return data->object;
}
T* operator->() const
{
return data->object;
}
T& operator*() const
{
return *(data->object);
}
uint use_count() const noexcept {
return data ? REF::load(data->refcount) : 0u;
}
bool unique() const noexcept {
return data ? REF::is_unique(data->refcount) : false;
}
private:
struct Internal
{
T *object = nullptr;
typename REF::value_type refcount = 0;
};
Internal *data = nullptr;
void removeInternal()
{
Internal* d = data;
data = nullptr;
if (!d) return;
if (REF::dec_is_last(d->refcount))
{
REF::after_last();
delete d->object;
delete d;
}
}
};
} //namespace alt
#ifdef __GNUC__
#define __fastcall __attribute__((__fastcall__))
#endif
#endif // ATYPES_H