-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathAlgorithms.cpp
145 lines (116 loc) · 3.41 KB
/
Algorithms.cpp
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
//==============================================================================
//
// Algorithms.cpp
//
// Copyright (C) 2013-2025 Greg Utas
//
// This file is part of the Robust Services Core (RSC).
//
// RSC is free software: you can redistribute it and/or modify it under the
// terms of the Lesser GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// RSC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the Lesser GNU General Public License
// along with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#include "Algorithms.h"
#include <cstdlib>
#include <cstring>
//------------------------------------------------------------------------------
namespace NodeBase
{
size_t find_first_one(uword n)
{
if(n == 0) return BITS_PER_WORD;
int i = 0;
for(NO_OP; ((n & 0x01) == 0); ++i) n >>= 1;
return i;
}
//------------------------------------------------------------------------------
void* getptr1(const void* ptr2, ptrdiff_t diff)
{
return (void*) ((const_ptr_t) ptr2 - diff);
}
//------------------------------------------------------------------------------
void* getptr2(const void* ptr1, ptrdiff_t diff)
{
return (void*) ((const_ptr_t) ptr1 + diff);
}
//------------------------------------------------------------------------------
size_t log2(size_t n, bool up)
{
if(n == 0) return 0;
size_t i = 0;
if(up) --n;
for(NO_OP; n > 0; n >>= 1) ++i;
if(!up) --i;
return i;
}
//------------------------------------------------------------------------------
uint64_t pack2(uint32_t a, uint32_t b)
{
uint64_t result = a;
result <<= 32;
result += b;
return result;
}
//------------------------------------------------------------------------------
uint64_t pack3(uint16_t a, uint16_t b, uint16_t c)
{
uint64_t result = a;
result <<= 16;
result += b;
result <<= 16;
result += c;
return result;
}
//------------------------------------------------------------------------------
uint64_t pack4(uint16_t a, uint16_t b, uint16_t c, uint16_t d)
{
uint64_t result = a;
result <<= 16;
result += b;
result <<= 16;
result += c;
result <<= 16;
result += d;
return result;
}
//------------------------------------------------------------------------------
ptrdiff_t ptrdiff(const void* ptr1, const void* ptr2)
{
return ((const_ptr_t) ptr1 - (const_ptr_t) ptr2);
}
//------------------------------------------------------------------------------
uint32_t rand(uint32_t min, uint32_t max)
{
// Return a rand value, min <= value <= max.
//
return (double(::rand()) / (uint32_t(RAND_MAX) + 1.0)) * (max - min) + min;
}
//------------------------------------------------------------------------------
size_t round_to_2_exp_n(size_t n, size_t e, bool up)
{
auto incr = (1 << e);
if(up) n += (incr - 1);
n = (n >> e) << e;
return n;
}
//------------------------------------------------------------------------------
uint32_t string_hash(c_string s)
{
uint64_t hash = 0;
auto size = strlen(s);
for(size_t i = 0; i < size; ++i)
{
hash = s[i] + (hash << 16) + (hash << 6) - hash;
}
return hash;
}
}