-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdprintf.h
More file actions
43 lines (32 loc) · 872 Bytes
/
dprintf.h
File metadata and controls
43 lines (32 loc) · 872 Bytes
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
#ifndef DPRINTF_H_
#define DPRINTF_H_
#include <stdint.h>
typedef int32_t dcell;
struct dbuf {
dcell* curr;
dcell* finish;
};
extern dbuf g_dbuf;
inline dcell tocell(int x) { return x; }
inline dcell tocell(const char* p) { return (dcell)p; }
inline dcell tocell(float x) { union { int n; float f; } nf; nf.f = x; return nf.n; }
inline dcell tocell(double x) { return tocell((float)x); }
inline void dwrite(dcell* p) {}
template<class T, class... Rest>
inline void dwrite(dcell* p, T first, Rest... rest) {
*p++ = tocell(first);
dwrite(p, rest...);
}
template<class... Rest>
void dprintf(const char* fmt, Rest... args) {
int n = sizeof...(args);
int sz = n + 2;
dcell* p = __sync_fetch_and_add(&g_dbuf.curr, sz*sizeof(dcell));
if(p + sz < g_dbuf.finish) {
*p++ = (dcell)fmt;
*p++ = n;
dwrite(p, args...);
}
}
void dflush();
#endif