-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.h
More file actions
98 lines (75 loc) · 2.46 KB
/
Copy pathutility.h
File metadata and controls
98 lines (75 loc) · 2.46 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// Created by ssln on 2019-12-02.
//
#ifndef UTILITY_H
#define UTILITY_H
#include <string>
namespace algorithm::utility
{
typedef int int32;
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;
static std::string uint2string(const uint32 value)
{
static const char zero = '0';
static const char one = '1';
static const char* avoid_logic[2] = { &zero, &one };
char retval[40] = {0};
int i = 31, j = 0;
for (; i >= 0; )
{
retval[j] = *(avoid_logic[(value & (1 << i)) >> i]); // no comparisons, very fast
--i;
++j;
retval[j] = *(avoid_logic[(value & (1 << i)) >> i]);
--i;
++j;
retval[j] = *(avoid_logic[(value & (1 << i)) >> i]);
--i;
++j;
retval[j] = *(avoid_logic[(value & (1 << i)) >> i]);
--i;
++j;
retval[j] = ' ';
++j;
}
retval[j - 1] = 0;
return retval;
}
static bool getbitsuint(const uint32 startbit, const uint32 numbits, const uint32 src, uint32& value)
{
uint32 base = (startbit + numbits - 1);
if (startbit < 1 || numbits < 1 || base>32) return false;
const uint64& one = 1;
const uint64& a = (one << base);
const uint64& b = (one << (startbit - 1));
const uint32& mask = static_cast<uint32>(a - b);
value = ((src & mask) >> (startbit - 1));
return true;
}
static bool setbits2c(const uint32 startbit, const uint32 numbits, const int64 scaledValue, uint32& src)
{
uint32 base = (startbit + numbits - 1);
// error if specified bit field is invalid
if (startbit < 1 || numbits < 1 || base > 32) return false;
const uint64& one = 1;
const uint64& a = (one << (numbits - 1));
// error if scaled value cannot fit in bits
const uint64& probe = (one << numbits);
if (scaledValue >= probe) return false;
uint32 tmp = src;
// mask: numBits bits starting at startBit are set
const uint64& b = (one << base);
const uint64& c = (one << (startbit - 1));
const uint32& mask = static_cast<uint32>(b - c);
// Clear specified bits in word
tmp &= ~mask;
// move scaled value to start at bit startBit and set all bits
// outside of bit field to zero. Then add bit field to word
tmp |= (scaledValue << (startbit - 1) & mask);
src = tmp;
return true;
}
}
#endif //UTILITY_H