forked from TimesGraph/TimesGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
228 lines (209 loc) · 6.19 KB
/
util.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
/*******************************************************************************
*
* Copyright (c) 2019-2022 TimesGraph
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef UTIL_H
#define UTIL_H
#include <cmath>
#include <cstdint>
#include "jni.h"
#if (defined(__GNUC__) && !defined(__clang__))
#define ATTR_UNUSED
#else
#define ATTR_UNUSED __attribute__((unused))
#endif
#if (defined(__GNUC__) && !defined(__clang__))
#define ATTRIBUTE_NEVER_INLINE __attribute__((noinline))
#elif defined(_MSC_VER)
#define ATTRIBUTE_NEVER_INLINE __declspec(noinline)
#else
#define ATTRIBUTE_NEVER_INLINE
#endif
#define PREDICT_FALSE(x) (__builtin_expect(x, 0))
#define PREDICT_TRUE(x) (__builtin_expect(false || (x), true))
#define JAVA_STATIC ATTR_UNUSED JNIEnv *e, ATTR_UNUSED jclass cl
#if __GNUC__
// Fetch into all levels of the cache hierarchy.
#define MM_PREFETCH_T0(address) __builtin_prefetch((address), 0, 3)
// Fetch into L2 and higher.
#define MM_PREFETCH_T1(address) __builtin_prefetch((address), 0, 2)
// Fetch into L3 and higher or an implementation-specific choice (e.g., L2 if there is no L3).
#define MM_PREFETCH_T2(address) __builtin_prefetch((address), 0, 1)
// Fetch data using the non-temporal access (NTA) hint.
#define MM_PREFETCH_NTA(address) __builtin_prefetch((address), 0, 0)
#else
// Fetch into all levels of the cache hierarchy.
#define MM_PREFETCH_T0(address) _mm_prefetch((address), _MM_HINT_T0)
// Fetch into L2 and higher.
#define MM_PREFETCH_T1(address) _mm_prefetch((address), _MM_HINT_T1)
// Fetch into L3 and higher or an implementation-specific choice (e.g., L2 if there is no L3).
#define MM_PREFETCH_T2(address) _mm_prefetch((address), _MM_HINT_T2)
// Fetch data using the non-temporal access (NTA) hint.
#define MM_PREFETCH_NTA(address) _mm_prefetch((address), _MM_HINT_NTA)
#endif
constexpr jdouble D_MAX = std::numeric_limits<jdouble>::infinity();
constexpr jdouble D_MIN = -std::numeric_limits<jdouble>::infinity();
constexpr jint I_MAX = std::numeric_limits<jint>::max();
constexpr jint I_MIN = std::numeric_limits<jint>::min();
constexpr jlong L_MIN = std::numeric_limits<jlong>::min();
constexpr jlong L_MAX = std::numeric_limits<jlong>::max();
constexpr jdouble D_NAN = std::numeric_limits<jdouble>::quiet_NaN();
inline uint32_t ceil_pow_2(uint32_t v)
{
v--;
v |= v >> 1u;
v |= v >> 2u;
v |= v >> 4u;
v |= v >> 8u;
v |= v >> 16u;
return v + 1;
}
template <class T, class V>
inline int64_t scroll_up(T data, int64_t high, V value)
{
do
{
if (high > 0)
{
high--;
}
else
{
return 0;
}
} while (data[high] == value);
return high + 1;
}
template <class T, class V>
inline int64_t scroll_down(T data, int64_t low, int64_t high, V value)
{
do
{
if (low < high)
{
low++;
}
else
{
return low;
}
} while (data[low] == value);
return low - 1;
}
template <class T, class V>
inline int64_t scan_up(T *data, V value, int64_t low, int64_t high)
{
for (int64_t i = low; i < high; i++)
{
T that = data[i];
if (that == value)
{
return i;
}
if (that > value)
{
return -(i + 1);
}
}
return -(high + 1);
}
template <class T, class V>
inline int64_t scan_down(T *data, V value, int64_t low, int64_t high)
{
for (int64_t i = high - 1; i >= low; i--)
{
T that = data[i];
if (that == value)
{
return i;
}
if (that < value)
{
return -(i + 2);
}
}
return -(low + 1);
}
// the "high" boundary is inclusive
template <class T, class V>
inline int64_t binary_search(T *data, V value, int64_t low, int64_t high, int32_t scan_dir)
{
while (high - low > 65)
{
const int64_t mid = (low + high) / 2;
const T midVal = data[mid];
if (midVal < value)
{
low = mid;
}
else if (midVal > value)
{
high = mid - 1;
}
else
{
// In case of multiple equal values, find the first
return scan_dir == -1 ? scroll_up(data, mid, midVal) : scroll_down(data, mid, high, midVal);
}
}
return scan_dir == -1 ? scan_up(data, value, low, high + 1) : scan_down(data, value, low, high + 1);
}
template <typename T>
int64_t branch_free_search_lower(const T *array, const int64_t count, T x)
{
const T *base = array;
int64_t n = count;
while (n > 1)
{
int64_t half = n / 2;
MM_PREFETCH_T0(base + half / 2);
MM_PREFETCH_T0(base + half + half / 2);
base = (base[half] < x) ? base + half : base;
n -= half;
}
return (*base < x) + base - array;
}
template <typename T>
int64_t branch_free_linked_search_lower(const int64_t *index, const T *values, const int64_t count, T x)
{
const T *base = index;
int64_t n = count;
while (n > 1)
{
int64_t half = n / 2;
MM_PREFETCH_T0(base + half / 2);
MM_PREFETCH_T0(base + half + half / 2);
base = (values[base[half]] < x) ? base + half : base;
n -= half;
}
return (values[*base] < x) + base - index;
}
template <typename T>
int64_t branch_free_search_upper(const T *array, const int64_t count, T x)
{
const T *base = array;
int64_t n = count;
while (n > 1)
{
int64_t half = n / 2;
MM_PREFETCH_T0(base + half / 2);
MM_PREFETCH_T0(base + half + half / 2);
base = (base[half] <= x) ? base + half : base;
n -= half;
}
return (*base <= x) + base - array;
}
#endif //UTIL_H