Blame view

Pods/Realm/include/core/realm/array_direct.hpp 13.3 KB
75d24c15   yangbin   123
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
  /*************************************************************************
   *
   * Copyright 2016 Realm Inc.
   *
   * 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 REALM_ARRAY_DIRECT_HPP
  #define REALM_ARRAY_DIRECT_HPP
  
  #include <realm/utilities.hpp>
  #include <realm/alloc.hpp>
  
  // clang-format off
  /* wid == 16/32 likely when accessing offsets in B tree */
  #define REALM_TEMPEX(fun, wid, arg) \
      if (wid == 16) {fun<16> arg;} \
      else if (wid == 32) {fun<32> arg;} \
      else if (wid == 0) {fun<0> arg;} \
      else if (wid == 1) {fun<1> arg;} \
      else if (wid == 2) {fun<2> arg;} \
      else if (wid == 4) {fun<4> arg;} \
      else if (wid == 8) {fun<8> arg;} \
      else if (wid == 64) {fun<64> arg;} \
      else {REALM_ASSERT_DEBUG(false); fun<0> arg;}
  
  #define REALM_TEMPEX2(fun, targ, wid, arg) \
      if (wid == 16) {fun<targ, 16> arg;} \
      else if (wid == 32) {fun<targ, 32> arg;} \
      else if (wid == 0) {fun<targ, 0> arg;} \
      else if (wid == 1) {fun<targ, 1> arg;} \
      else if (wid == 2) {fun<targ, 2> arg;} \
      else if (wid == 4) {fun<targ, 4> arg;} \
      else if (wid == 8) {fun<targ, 8> arg;} \
      else if (wid == 64) {fun<targ, 64> arg;} \
      else {REALM_ASSERT_DEBUG(false); fun<targ, 0> arg;}
  
  #define REALM_TEMPEX3(fun, targ1, wid, targ3, arg) \
      if (wid == 16) {fun<targ1, 16, targ3> arg;} \
      else if (wid == 32) {fun<targ1, 32, targ3> arg;} \
      else if (wid == 0) {fun<targ1, 0, targ3> arg;} \
      else if (wid == 1) {fun<targ1, 1, targ3> arg;} \
      else if (wid == 2) {fun<targ1, 2, targ3> arg;} \
      else if (wid == 4) {fun<targ1, 4, targ3> arg;} \
      else if (wid == 8) {fun<targ1, 8, targ3> arg;} \
      else if (wid == 64) {fun<targ1, 64, targ3> arg;} \
      else {REALM_ASSERT_DEBUG(false); fun<targ1, 0, targ3> arg;}
  
  #define REALM_TEMPEX4(fun, targ1, targ3, targ4, wid, arg) \
      if (wid == 16) {fun<targ1, targ3, targ4, 16> arg;} \
      else if (wid == 32) {fun<targ1, targ3, targ4, 32> arg;} \
      else if (wid == 0) {fun<targ1, targ3, targ4, 0> arg;} \
      else if (wid == 1) {fun<targ1, targ3, targ4, 1> arg;} \
      else if (wid == 2) {fun<targ1, targ3, targ4, 2> arg;} \
      else if (wid == 4) {fun<targ1, targ3, targ4, 4> arg;} \
      else if (wid == 8) {fun<targ1, targ3, targ4, 8> arg;} \
      else if (wid == 64) {fun<targ1, targ3, targ4, 64> arg;} \
      else {REALM_ASSERT_DEBUG(false); fun<targ1, targ3, targ4, 0> arg;}
  // clang-format on
  
  namespace realm {
  
  /// Takes a 64-bit value and returns the minimum number of bits needed
  /// to fit the value. For alignment this is rounded up to nearest
  /// log2. Posssible results {0, 1, 2, 4, 8, 16, 32, 64}
  size_t bit_width(int64_t value);
  
  // Direct access methods
  
  template <size_t width>
  void set_direct(char* data, size_t ndx, int_fast64_t value) noexcept
  {
      if (width == 0) {
          REALM_ASSERT_DEBUG(value == 0);
          return;
      }
      else if (width == 1) {
          REALM_ASSERT_DEBUG(0 <= value && value <= 0x01);
          size_t byte_ndx = ndx / 8;
          size_t bit_ndx = ndx % 8;
          typedef unsigned char uchar;
          uchar* p = reinterpret_cast<uchar*>(data) + byte_ndx;
          *p = uchar((*p & ~(0x01 << bit_ndx)) | (int(value) & 0x01) << bit_ndx);
      }
      else if (width == 2) {
          REALM_ASSERT_DEBUG(0 <= value && value <= 0x03);
          size_t byte_ndx = ndx / 4;
          size_t bit_ndx = ndx % 4 * 2;
          typedef unsigned char uchar;
          uchar* p = reinterpret_cast<uchar*>(data) + byte_ndx;
          *p = uchar((*p & ~(0x03 << bit_ndx)) | (int(value) & 0x03) << bit_ndx);
      }
      else if (width == 4) {
          REALM_ASSERT_DEBUG(0 <= value && value <= 0x0F);
          size_t byte_ndx = ndx / 2;
          size_t bit_ndx = ndx % 2 * 4;
          typedef unsigned char uchar;
          uchar* p = reinterpret_cast<uchar*>(data) + byte_ndx;
          *p = uchar((*p & ~(0x0F << bit_ndx)) | (int(value) & 0x0F) << bit_ndx);
      }
      else if (width == 8) {
          REALM_ASSERT_DEBUG(std::numeric_limits<int8_t>::min() <= value &&
                             value <= std::numeric_limits<int8_t>::max());
          *(reinterpret_cast<int8_t*>(data) + ndx) = int8_t(value);
      }
      else if (width == 16) {
          REALM_ASSERT_DEBUG(std::numeric_limits<int16_t>::min() <= value &&
                             value <= std::numeric_limits<int16_t>::max());
          *(reinterpret_cast<int16_t*>(data) + ndx) = int16_t(value);
      }
      else if (width == 32) {
          REALM_ASSERT_DEBUG(std::numeric_limits<int32_t>::min() <= value &&
                             value <= std::numeric_limits<int32_t>::max());
          *(reinterpret_cast<int32_t*>(data) + ndx) = int32_t(value);
      }
      else if (width == 64) {
          REALM_ASSERT_DEBUG(std::numeric_limits<int64_t>::min() <= value &&
                             value <= std::numeric_limits<int64_t>::max());
          *(reinterpret_cast<int64_t*>(data) + ndx) = int64_t(value);
      }
      else {
          REALM_ASSERT_DEBUG(false);
      }
  }
  
  inline void set_direct(char* data, size_t width, size_t ndx, int_fast64_t value) noexcept
  {
      REALM_TEMPEX(set_direct, width, (data, ndx, value));
  }
  
  template <size_t width>
  void fill_direct(char* data, size_t begin, size_t end, int_fast64_t value) noexcept
  {
      for (size_t i = begin; i != end; ++i)
          set_direct<width>(data, i, value);
  }
  
  template <int w>
  int64_t get_direct(const char* data, size_t ndx) noexcept
  {
      if (w == 0) {
          return 0;
      }
      if (w == 1) {
          size_t offset = ndx >> 3;
          return (data[offset] >> (ndx & 7)) & 0x01;
      }
      if (w == 2) {
          size_t offset = ndx >> 2;
          return (data[offset] >> ((ndx & 3) << 1)) & 0x03;
      }
      if (w == 4) {
          size_t offset = ndx >> 1;
          return (data[offset] >> ((ndx & 1) << 2)) & 0x0F;
      }
      if (w == 8) {
          return *reinterpret_cast<const signed char*>(data + ndx);
      }
      if (w == 16) {
          size_t offset = ndx * 2;
          return *reinterpret_cast<const int16_t*>(data + offset);
      }
      if (w == 32) {
          size_t offset = ndx * 4;
          return *reinterpret_cast<const int32_t*>(data + offset);
      }
      if (w == 64) {
          size_t offset = ndx * 8;
          return *reinterpret_cast<const int64_t*>(data + offset);
      }
      REALM_ASSERT_DEBUG(false);
      return int64_t(-1);
  }
  
  inline int64_t get_direct(const char* data, size_t width, size_t ndx) noexcept
  {
      REALM_TEMPEX(return get_direct, width, (data, ndx));
  }
  
  
  template <int width>
  inline std::pair<int64_t, int64_t> get_two(const char* data, size_t ndx) noexcept
  {
      return std::make_pair(to_size_t(get_direct<width>(data, ndx + 0)), to_size_t(get_direct<width>(data, ndx + 1)));
  }
  
  inline std::pair<int64_t, int64_t> get_two(const char* data, size_t width, size_t ndx) noexcept
  {
      REALM_TEMPEX(return get_two, width, (data, ndx));
  }
  
  
  template <int width>
  inline void get_three(const char* data, size_t ndx, ref_type& v0, ref_type& v1, ref_type& v2) noexcept
  {
      v0 = to_ref(get_direct<width>(data, ndx + 0));
      v1 = to_ref(get_direct<width>(data, ndx + 1));
      v2 = to_ref(get_direct<width>(data, ndx + 2));
  }
  
  inline void get_three(const char* data, size_t width, size_t ndx, ref_type& v0, ref_type& v1, ref_type& v2) noexcept
  {
      REALM_TEMPEX(get_three, width, (data, ndx, v0, v1, v2));
  }
  
  
  // Lower/upper bound in sorted sequence
  // ------------------------------------
  //
  //   3 3 3 4 4 4 5 6 7 9 9 9
  //   ^     ^     ^     ^     ^
  //   |     |     |     |     |
  //   |     |     |     |      -- Lower and upper bound of 15
  //   |     |     |     |
  //   |     |     |      -- Lower and upper bound of 8
  //   |     |     |
  //   |     |      -- Upper bound of 4
  //   |     |
  //   |      -- Lower bound of 4
  //   |
  //    -- Lower and upper bound of 1
  //
  // These functions are semantically identical to std::lower_bound() and
  // std::upper_bound().
  //
  // We currently use binary search. See for example
  // http://www.tbray.org/ongoing/When/200x/2003/03/22/Binary.
  template <int width>
  inline size_t lower_bound(const char* data, size_t size, int64_t value) noexcept
  {
      // The binary search used here is carefully optimized. Key trick is to use a single
      // loop controlling variable (size) instead of high/low pair, and to keep updates
      // to size done inside the loop independent of comparisons. Further key to speed
      // is to avoid branching inside the loop, using conditional moves instead. This
      // provides robust performance for random searches, though predictable searches
      // might be slightly faster if we used branches instead. The loop unrolling yields
      // a final 5-20% speedup depending on circumstances.
  
      size_t low = 0;
  
      while (size >= 8) {
          // The following code (at X, Y and Z) is 3 times manually unrolled instances of (A) below.
          // These code blocks must be kept in sync. Meassurements indicate 3 times unrolling to give
          // the best performance. See (A) for comments on the loop body.
          // (X)
          size_t half = size / 2;
          size_t other_half = size - half;
          size_t probe = low + half;
          size_t other_low = low + other_half;
          int64_t v = get_direct<width>(data, probe);
          size = half;
          low = (v < value) ? other_low : low;
  
          // (Y)
          half = size / 2;
          other_half = size - half;
          probe = low + half;
          other_low = low + other_half;
          v = get_direct<width>(data, probe);
          size = half;
          low = (v < value) ? other_low : low;
  
          // (Z)
          half = size / 2;
          other_half = size - half;
          probe = low + half;
          other_low = low + other_half;
          v = get_direct<width>(data, probe);
          size = half;
          low = (v < value) ? other_low : low;
      }
      while (size > 0) {
          // (A)
          // To understand the idea in this code, please note that
          // for performance, computation of size for the next iteration
          // MUST be INDEPENDENT of the conditional. This allows the
          // processor to unroll the loop as fast as possible, and it
          // minimizes the length of dependence chains leading up to branches.
          // Making the unfolding of the loop independent of the data being
          // searched, also minimizes the delays incurred by branch
          // mispredictions, because they can be determined earlier
          // and the speculation corrected earlier.
  
          // Counterintuitive:
          // To make size independent of data, we cannot always split the
          // range at the theoretical optimal point. When we determine that
          // the key is larger than the probe at some index K, and prepare
          // to search the upper part of the range, you would normally start
          // the search at the next index, K+1, to get the shortest range.
          // We can only do this when splitting a range with odd number of entries.
          // If there is an even number of entries we search from K instead of K+1.
          // This potentially leads to redundant comparisons, but in practice we
          // gain more performance by making the changes to size predictable.
  
          // if size is even, half and other_half are the same.
          // if size is odd, half is one less than other_half.
          size_t half = size / 2;
          size_t other_half = size - half;
          size_t probe = low + half;
          size_t other_low = low + other_half;
          int64_t v = get_direct<width>(data, probe);
          size = half;
          // for max performance, the line below should compile into a conditional
          // move instruction. Not all compilers do this. To maximize chance
          // of succes, no computation should be done in the branches of the
          // conditional.
          low = (v < value) ? other_low : low;
      };
  
      return low;
  }
  
  // See lower_bound()
  template <int width>
  inline size_t upper_bound(const char* data, size_t size, int64_t value) noexcept
  {
      size_t low = 0;
      while (size >= 8) {
          size_t half = size / 2;
          size_t other_half = size - half;
          size_t probe = low + half;
          size_t other_low = low + other_half;
          int64_t v = get_direct<width>(data, probe);
          size = half;
          low = (value >= v) ? other_low : low;
  
          half = size / 2;
          other_half = size - half;
          probe = low + half;
          other_low = low + other_half;
          v = get_direct<width>(data, probe);
          size = half;
          low = (value >= v) ? other_low : low;
  
          half = size / 2;
          other_half = size - half;
          probe = low + half;
          other_low = low + other_half;
          v = get_direct<width>(data, probe);
          size = half;
          low = (value >= v) ? other_low : low;
      }
  
      while (size > 0) {
          size_t half = size / 2;
          size_t other_half = size - half;
          size_t probe = low + half;
          size_t other_low = low + other_half;
          int64_t v = get_direct<width>(data, probe);
          size = half;
          low = (value >= v) ? other_low : low;
      };
  
      return low;
  }
  }
  
  #endif /* ARRAY_TPL_HPP_ */