-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.h
168 lines (124 loc) · 3.62 KB
/
debug.h
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
#pragma once
#include "util/config.h"
#include "util/strong_typedef.hpp"
namespace debug{
enum { enable=1 };
} //namespace debug
extern "C" void die();
//
//
// Helper function: Bits
//
template<bool algo, int N, typename T>
struct ones_t{
static T ones() {
static_assert(N<(sizeof(T)*8), "Invalid args");
return (T(1)<<N)-1;
}
};
template<int N, typename T>
struct ones_t<true,N,T>{
static T ones() {
static_assert(N==(sizeof(T)*8), "Invalid args");
return ~T(0);
}
};
static inline bool test_bit(uint32_t x,int k){ return x&(1<<k);}
template<int A, int B, typename T>
static inline T ones() {
return ones_t<A-B+1 == (sizeof(T)*8),A-B+1,T>::ones();
}
template<int A,int B,typename T>
static inline T get_bits(T x){
static_assert( ((sizeof(T)*8)>A) && (A>=B),"Invalid args");
return (x>>B)&ones<A,B,T>();
}
template<int A,int B,typename T,typename T2>
static inline T set_bits(T x,T2 y){
static_assert( ((sizeof(T)*8)>A) && (A>=B),"Invalid args");
return (x&~(ones<A,B,T>()<<B)) | (T(y&ones<A,B,T2>())<<B) ;
}
//template<typename T>
//static inline T get_bits(T x,int A,int B){ return (x>>B)&ones<T>(A,B);}
//
//
// hoh_debug and hoh_assert
//
//
//
#ifdef SIMULATOR
#include <iostream>
#define hoh_debug(x) do{ if(debug::enable){ std::cout<<x<<'\n';} }while(false)
#define hoh_debug_nl(x) do{ if(debug::enable){ std::cout<<x; } }while(false)
#define hoh_debug_if(c,x) do{ if(c){ std::cout<<x<<'\n';} }while(false)
#define hoh_assert(c,x) do{ if(c){}else{ std::cout<<"ASSERT failed: "<<x<<"\nCondition:"<<#c<<"\nLocation: "<<__FILE__<<":"<<__LINE__<<":"<<__PRETTY_FUNCTION__<<"\n"; die();} }while(false)
typedef std::ostream dout_t;
static inline dout_t& operator<<(dout_t& os,constaddr_t x){
return os<<(uintptr_t)x;
}
#else
namespace serial{ void print(char x); }
#define hoh_debug(x) do{ if(debug::enable){ dout_t dout; dout<<x<<'\n';} }while(false)
#define hoh_debug_nl(x) do{ if(debug::enable){ dout_t dout; dout<<x; } }while(false)
#define hoh_debug_if(c,x) do{ if(c){ dout_t dout; dout<<x<<'\n';} }while(false)
#define hoh_assert(c,x) do{ if(c){}else{ dout_t dout; dout<<"ASSERT failed: "<<x<<"\nCondition:"<<#c<<"\nLocation: "<<__FILE__<<":"<<__LINE__<<":"<<__PRETTY_FUNCTION__<<"\n"; die();} }while(false)
struct dout_t{};
static inline dout_t& operator <<(dout_t& os,char x){
if(debug::enable){
serial::print(x);
}
return os;
}
static inline dout_t& operator<<(dout_t& os,const char* s){
for(int i=0;s[i]!=0;i++){
os<<s[i];
}
return os;
}
static inline char hex2char(unsigned int i){
return "0123456789abcdef"[i%16];
}
static inline dout_t& operator<<(dout_t& os,uint8_t x){
enum {max=sizeof(uint8_t)*2+1};
char a[max];
for(int i=0;i<max-1;i++){
a[max-1-i-1]=hex2char(x%16);
x=x/16;
}
a[max-1]='\0';
return os<<a;
}
static inline dout_t& operator<<(dout_t& os,uint16_t x){
enum {max=sizeof(uint16_t)*2+1};
char a[max];
for(int i=0;i<max-1;i++){
a[max-1-i-1]=hex2char(x%16);
x=x/16;
}
a[max-1]='\0';
return os<<a;
}
static inline dout_t& operator<<(dout_t& os,uint32_t x){
enum {max=sizeof(uint32_t)*2+1};
char a[max];
for(int i=0;i<max-1;i++){
a[max-1-i-1]=hex2char(x%16);
x=x/16;
}
a[max-1]='\0';
return os<<a;
}
static inline dout_t& operator<<(dout_t& os,uint64_t x){
return os<<uint32_t(get_bits<63,32>(x))<<uint32_t(get_bits<31,0>(x));
}
static inline dout_t& operator<<(dout_t& os,constaddr_t x){
return os<<(uintptr_t)x;
}
static inline dout_t& operator<<(dout_t& os,int32_t x){
if(x<0){
os<<"-";
x=-x;
}
return os<<uint32_t(x);
}
#endif