-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdefs.cpp
More file actions
executable file
·84 lines (68 loc) · 1.58 KB
/
defs.cpp
File metadata and controls
executable file
·84 lines (68 loc) · 1.58 KB
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
#include <stdlib.h>
#include <stdio.h>
#include "defs.h"
// -----------------------------------------------------------------
Trace::Trace (bool const trace, char const * const function,
char const * const file, int const line)
: _output (trace), _function (function), _file (file), _line (line)
{
_trace (">>>>>");
} // Trace::Trace
Trace::~Trace ()
{
_trace ("<<<<<");
} // Trace::~Trace
void Trace::_trace (char const lead [])
{
if (_output)
printf ("%s %s (%s:%d)\n", lead, _function, _file, _line);
} // Trace::_trace
// -----------------------------------------------------------------
size_t Random (size_t const range)
{
return (size_t) rand () % range;
} // Random
size_t Random (size_t const low_incl, size_t const high_incl)
{
return low_incl + (size_t) rand () % (high_incl - low_incl + 1);
} // Random
size_t RoundDown (size_t const x, size_t const y)
{
return x - (x % y);
} // RoundDown
size_t RoundUp (size_t const x, size_t const y)
{
size_t const z = x % y;
return (z == 0 ? x : x + y - z);
} // RoundUp
bool IsPowerOf2 (size_t const x)
{
return x > 0 && (x & (x - 1)) == 0;
} // IsPowerOf2
size_t lsb (size_t const x)
{
size_t const y = x & (x - 1);
return x ^ y;
} // lsb
size_t msb (size_t const x)
{
size_t y = x;
for (size_t z; z = y & (y - 1), z != 0; y = z)
; // nothing
return y;
} // msb
int msbi (size_t const x)
{
int i = 0;
for (size_t z = 2; z <= x; ++ i, z <<= 1)
; // nothing
return i;
} // msbi
char const * YesNo (bool const b)
{
return b ? "Yes" : "No";
} // YesNo
char const * OkBad (bool const b)
{
return b ? "Ok" : "Bad";
} // OkBad