MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
system.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#ifndef UTIL_SYSTEM_HEADER
11#define UTIL_SYSTEM_HEADER
12
13#include <ctime>
14#include <cstdlib>
15#include <thread>
16#include <chrono>
17#include <vector>
18#include <istream>
19
20#include "util/defines.h"
21
24
25/*
26 * ------------------------- Sleep functions--------------------------
27 */
28
30void sleep (std::size_t msec);
32void sleep_sec (float secs);
33
34/*
35 * ------------------------- Random functions-------------------------
36 */
37
39void rand_init (void);
41void rand_seed (int seed);
43float rand_float (void);
45int rand_int (void);
46
47/*
48 * ---------------------- Signals / Application ----------------------
49 */
50
52void
53print_build_timestamp (char const* application_name);
54
56void
57print_build_timestamp (char const* application_name,
58 char const* date, char const* time);
59
62
64void signal_segfault_handler (int code);
65
67void print_stack_trace (void);
68
69/*
70 * ----------------------- Endian conversions ------------------------
71 */
72
74template <int N>
75inline void
76byte_swap (char* data);
77
79template <typename T>
80inline T
81letoh (T const& x);
82
84template <typename T>
85inline T
86betoh (T const& x);
87
89template <typename T>
90inline T
91read_binary_little_endian(std::istream* stream);
92
93/* ---------------------------------------------------------------- */
94
95inline void
96sleep (std::size_t msec)
97{
98 std::this_thread::sleep_for(std::chrono::milliseconds(msec));
99}
100
101inline void
102sleep_sec (float secs)
103{
104 sleep((std::size_t)(secs * 1000.0f));
105}
106
107inline void
109{
110 std::srand(std::time(nullptr));
111}
112
113inline void
114rand_seed (int seed)
115{
116 std::srand(seed);
117}
118
119inline float
121{
122 return (float)std::rand() / (float)RAND_MAX;
123}
124
125inline int
127{
128 return std::rand();
129}
130
131inline void
132print_build_timestamp (char const* application_name)
133{
134 print_build_timestamp(application_name, __DATE__, __TIME__);
135}
136
137/*
138 * Determine host byte-order.
139 * Windows: "All versions of windows run little-endian, period."
140 * http://social.msdn.microsoft.com/Forums/en-US/windowsmobiledev/thread/04c92ef9-e38e-415f-8958-ec9f7c196fd3
141 */
142#if defined(_WIN32) || defined(__CYGWIN__)
143# define HOST_BYTEORDER_LE
144#endif
145
146/*
147 * The only Apple architectures with big-endian are PowerPC 32 and 64 bit.
148 */
149#if defined(__APPLE__)
150# if defined(__ppc__) || defined(__ppc64__)
151# define HOST_BYTEORDER_BE
152# else
153# define HOST_BYTEORDER_LE
154# endif
155#endif
156
157#if defined(__linux__)
158# include <endian.h> // part of glibc 2.9
159# if __BYTE_ORDER == __LITTLE_ENDIAN
160# define HOST_BYTEORDER_LE
161# else
162# define HOST_BYTEORDER_BE
163# endif
164#endif
165
166#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
167# include <machine/endian.h>
168# if BYTE_ORDER == LITTLE_ENDIAN
169# define HOST_BYTEORDER_LE
170# else
171# define HOST_BYTEORDER_BE
172# endif
173#endif
174
175#if defined(__SunOS)
176# include <sys/byteorder.h>
177# if defined(_LITTLE_ENDIAN)
178# define HOST_BYTEORDER_LE
179# else
180# define HOST_BYTEORDER_BE
181# endif
182#endif
183
184template <>
185inline void
186byte_swap<2> (char* data)
187{
188 std::swap(data[0], data[1]);
189}
190
191template <>
192inline void
193byte_swap<4> (char* data)
194{
195 std::swap(data[0], data[3]);
196 std::swap(data[1], data[2]);
197}
198
199template <>
200inline void
201byte_swap<8> (char* data)
202{
203 std::swap(data[0], data[7]);
204 std::swap(data[1], data[6]);
205 std::swap(data[2], data[5]);
206 std::swap(data[3], data[4]);
207}
208
209#if defined(HOST_BYTEORDER_LE) && defined(HOST_BYTEORDER_BE)
210# error "Host endianess can not be both LE and BE!"
211#elif defined(HOST_BYTEORDER_LE)
212
213template <typename T>
214inline T
215letoh (T const& x)
216{
217 return x;
218}
219
220template <typename T>
221inline T
222betoh (T const& x)
223{
224 T copy(x);
225 byte_swap<sizeof(T)>(reinterpret_cast<char*>(&copy));
226 return copy;
227}
228
229#elif defined(HOST_BYTEORDER_BE)
230
231template <typename T>
232inline T
233letoh (T const& x)
234{
235 T copy(x);
236 byte_swap<sizeof(T)>(reinterpret_cast<char*>(&copy));
237 return copy;
238}
239
240template <typename T>
241inline T
242betoh (T const& x)
243{
244 return x;
245}
246
247#else
248# error "Couldn't determine host endianess!"
249#endif
250
251template <typename T>
252inline T
253read_binary_little_endian(std::istream* stream)
254{
255 T data_little_endian;
256 stream->read(reinterpret_cast<char*>(&data_little_endian), sizeof(T));
257 return letoh(data_little_endian);
258}
259
262
263#endif /* UTIL_SYSTEM_HEADER */
void swap(mve::Image< T > &a, mve::Image< T > &b)
Specialization of std::swap for efficient image swapping.
Definition image.h:478
void print_stack_trace(void)
Prints a stack trace.
Definition system.cc:52
T read_binary_little_endian(std::istream *stream)
Reads little endian according to host order conversion.
Definition system.h:253
void byte_swap(char *data)
Swaps little/big endianess of the operand.
void signal_segfault_handler(int code)
Handles signal SIGSEGV (segmentation fault) printing a stack trace.
Definition system.cc:41
void sleep_sec(float secs)
Sleeps the given amount of seconds.
Definition system.h:102
int rand_int(void)
Returns a random number in [0, 2^31].
Definition system.h:126
void print_build_timestamp(char const *application_name, char const *date, char const *time)
Prints the application name and the given date and time strings.
Definition system.cc:23
void rand_init(void)
Initializes the random number generator.
Definition system.h:108
void sleep(std::size_t msec)
Sleeps the given amount of milli seconds.
Definition system.h:96
void rand_seed(int seed)
Initializes the random number generator with a given seed.
Definition system.h:114
void register_segfault_handler(void)
Registers signal SIGSEGV (segmentation fault) handler.
Definition system.cc:33
float rand_float(void)
Returns a floating point random number in [0, 1].
Definition system.h:120
T betoh(T const &x)
Big endian to host order conversion.
T letoh(T const &x)
Little endian to host order conversion.
#define UTIL_NAMESPACE_BEGIN
Definition defines.h:13
#define UTIL_NAMESPACE_END
Definition defines.h:14
#define UTIL_SYSTEM_NAMESPACE_BEGIN
Definition defines.h:22
#define UTIL_SYSTEM_NAMESPACE_END
Definition defines.h:23