-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsystem.h
244 lines (201 loc) · 4.94 KB
/
system.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
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
/*
* Copyright (C) 2015, Simon Fuhrmann
* TU Darmstadt - Graphics, Capture and Massively Parallel Computing
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD 3-Clause license. See the LICENSE.txt file for details.
*/
#ifndef UTIL_SYSTEM_HEADER
#define UTIL_SYSTEM_HEADER
#include <ctime>
#include <cstdlib>
#include <thread>
#include <chrono>
#include "util/defines.h"
UTIL_NAMESPACE_BEGIN
UTIL_SYSTEM_NAMESPACE_BEGIN
/*
* ------------------------- Sleep functions--------------------------
*/
/** Sleeps the given amount of milli seconds. */
void sleep (std::size_t msec);
/** Sleeps the given amount of seconds. */
void sleep_sec (float secs);
/*
* ------------------------- Random functions-------------------------
*/
/** Initializes the random number generator. */
void rand_init (void);
/** Initializes the random number generator with a given seed. */
void rand_seed (int seed);
/** Returns a floating point random number in [0, 1]. */
float rand_float (void);
/** Returns a random number in [0, 2^31]. */
int rand_int (void);
/*
* ---------------------- Signals / Application ----------------------
*/
/** Prints the application name and date and time of the build. */
void
print_build_timestamp (char const* application_name);
/** Prints the application name and the given date and time strings. */
void
print_build_timestamp (char const* application_name,
char const* date, char const* time);
/** Registers signal SIGSEGV (segmentation fault) handler. */
void register_segfault_handler (void);
/** Handles signal SIGSEGV (segmentation fault) printing a stack trace. */
void signal_segfault_handler (int code);
/** Prints a stack trace. */
void print_stack_trace (void);
/*
* ----------------------- Endian conversions ------------------------
*/
/** Swaps little/big endianess of the operand. */
template <int N>
inline void
byte_swap (char* data);
/** Little endian to host order conversion. */
template <typename T>
inline T
letoh (T const& x);
/** Big endian to host order conversion. */
template <typename T>
inline T
betoh (T const& x);
/* ---------------------------------------------------------------- */
inline void
sleep (std::size_t msec)
{
std::this_thread::sleep_for(std::chrono::milliseconds(msec));
}
inline void
sleep_sec (float secs)
{
sleep((std::size_t)(secs * 1000.0f));
}
inline void
rand_init (void)
{
std::srand(std::time(nullptr));
}
inline void
rand_seed (int seed)
{
std::srand(seed);
}
inline float
rand_float (void)
{
return (float)std::rand() / (float)RAND_MAX;
}
inline int
rand_int (void)
{
return std::rand();
}
inline void
print_build_timestamp (char const* application_name)
{
print_build_timestamp(application_name, __DATE__, __TIME__);
}
/*
* Determine host byte-order.
* Windows: "All versions of windows run little-endian, period."
* http://social.msdn.microsoft.com/Forums/en-US/windowsmobiledev/thread/04c92ef9-e38e-415f-8958-ec9f7c196fd3
*/
#if defined(_WIN32) || defined(__CYGWIN__)
# define HOST_BYTEORDER_LE
#endif
#if defined(__APPLE__)
# if defined(__ppc__) || defined(__ppc64__)
# define HOST_BYTEORDER_BE
# elif defined(__i386__) || defined(__x86_64__)
# define HOST_BYTEORDER_LE
# endif
#endif
#if defined(__linux__)
# include <endian.h> // part of glibc 2.9
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define HOST_BYTEORDER_LE
# else
# define HOST_BYTEORDER_BE
# endif
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
# include <machine/endian.h>
# if BYTE_ORDER == LITTLE_ENDIAN
# define HOST_BYTEORDER_LE
# else
# define HOST_BYTEORDER_BE
# endif
#endif
#if defined(__SunOS)
# include <sys/byteorder.h>
# if defined(_LITTLE_ENDIAN)
# define HOST_BYTEORDER_LE
# else
# define HOST_BYTEORDER_BE
# endif
#endif
template <>
inline void
byte_swap<2> (char* data)
{
std::swap(data[0], data[1]);
}
template <>
inline void
byte_swap<4> (char* data)
{
std::swap(data[0], data[3]);
std::swap(data[1], data[2]);
}
template <>
inline void
byte_swap<8> (char* data)
{
std::swap(data[0], data[7]);
std::swap(data[1], data[6]);
std::swap(data[2], data[5]);
std::swap(data[3], data[4]);
}
#if defined(HOST_BYTEORDER_LE) && defined(HOST_BYTEORDER_BE)
# error "Host endianess can not be both LE and BE!"
#elif defined(HOST_BYTEORDER_LE)
template <typename T>
inline T
letoh (T const& x)
{
return x;
}
template <typename T>
inline T
betoh (T const& x)
{
T copy(x);
byte_swap<sizeof(T)>(reinterpret_cast<char*>(©));
return copy;
}
#elif defined(HOST_BYTEORDER_BE)
template <typename T>
inline T
letoh (T const& x)
{
T copy(x);
byte_swap<sizeof(T)>(reinterpret_cast<char*>(©));
return copy;
}
template <typename T>
inline T
betoh (T const& x)
{
return x;
}
#else
# error "Couldn't determine host endianess!"
#endif
UTIL_SYSTEM_NAMESPACE_END
UTIL_NAMESPACE_END
#endif /* UTIL_SYSTEM_HEADER */