forked from uTensor/uTensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuTensor_util.hpp
108 lines (86 loc) · 2.18 KB
/
uTensor_util.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
#ifndef UTENSOR_UTIL
#define UTENSOR_UTIL
#include <stdint.h>
#include <stdio.h>
#include <vector>
// #define MAX(A, B) ((A > B)? A:B)
#if MBED_CONF_APP_DEBUG_MSG
void return_error(int ret_val) {
if (ret_val) {
printf(" [**Failure**] %d\r\n", ret_val);
printf("Exiting...\r\n");
fflush(stdout);
exit(-1);
} else {
printf(" [DONE]\r\n");
}
}
void errno_error(void* ret_val) {
if (ret_val == NULL) {
printf(" [**Failure**] %d \r\n", errno);
printf("Exiting...\r\n");
fflush(stdout);
exit(-1);
} else {
printf(" [DONE]\r\n");
}
}
#define ON_ERR(FUNC, MSG) \
{ \
printf(" * "); \
printf(MSG); \
return_error(FUNC); \
}
#define DEBUG(MSG, ...) \
{ \
printf(MSG, ##__VA_ARGS__); \
fflush(stdout); \
}
#else // MBED_CONF_APP_DEBUG_MSG
void errno_error(void* ret_val) { /*DOES NOTHING*/
}
#define ON_ERR(FUNC, MSG) FUNC
#define DEBUG(MSG, ...)
#endif
#define ERR_EXIT(MSG, ...) \
{ \
printf("[Error] %s:%d @%s ", __FILE__, __LINE__, __func__); \
printf(MSG, ##__VA_ARGS__); \
fflush(stdout); \
exit(-1); \
}
typedef std::vector<uint32_t> Shape;
void printVector(std::vector<uint32_t> vec) {
printf("vector: \r\n");
for (uint32_t i : vec) {
printf("%d ", (unsigned int)i);
}
printf("\r\n");
}
// little endian to big endian
uint32_t htonl(uint32_t& val) {
const uint32_t mask = 0b11111111;
uint32_t ret = 0;
ret |= val >> 24;
ret |= (val & (mask << 16)) >> 8;
ret |= (val & (mask << 8)) << 8;
ret |= val << 24;
return ret;
}
// big endian to little endian
uint16_t ntoh16(uint16_t val) {
uint16_t ret = 0;
ret |= val >> 8;
ret |= val << 8;
return ret;
}
uint32_t ntoh32(uint32_t val) {
const uint32_t mask = 0b11111111;
uint32_t ret = 0;
ret |= val >> 24;
ret |= (val & (mask << 16)) >> 8;
ret |= (val & (mask << 8)) << 8;
ret |= val << 24;
return ret;
}
#endif