morton_utils.cc 13.3 KB
Newer Older
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
 *  This file is part of libc_utils.
 *
 *  libc_utils is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  libc_utils 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 GNU General Public License
 *  along with libc_utils; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 *  libc_utils is being developed at the Max-Planck-Institut fuer Astrophysik
 *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
 *  (DLR).
 */

/*
 *  Utilities for conversion between coordinates, Morton, and Peano indices
 *
 *  Copyright (C) 2015-2020 Max-Planck-Society
 *  Author: Martin Reinecke
 */

#include "mr_util/morton_utils.h"

namespace mr {

#ifndef __BMI2__

#if 1

namespace {

const uint16_t utab[] = {
#define Z(a) 0x##a##0, 0x##a##1, 0x##a##4, 0x##a##5
#define Y(a) Z(a##0), Z(a##1), Z(a##4), Z(a##5)
#define X(a) Y(a##0), Y(a##1), Y(a##4), Y(a##5)
X(0),X(1),X(4),X(5)
#undef X
#undef Y
#undef Z
};
static const uint16_t ctab[] = {
#define Z(a) a,a+1,a+256,a+257
#define Y(a) Z(a),Z(a+2),Z(a+512),Z(a+514)
#define X(a) Y(a),Y(a+4),Y(a+1024),Y(a+1028)
X(0),X(8),X(2048),X(2056)
#undef X
#undef Y
#undef Z
};

}

uint32_t block2morton2D_32 (uint32_t v)
  {
  typedef uint32_t I;
  return  (I)(utab[ v     &0xff])     | ((I)(utab[(v>> 8)&0xff])<<16)
       | ((I)(utab[(v>>16)&0xff])<<1) | ((I)(utab[(v>>24)&0xff])<<17);
  }
uint32_t coord2morton2D_32 (uint32_t x, uint32_t y)
  {
  typedef uint32_t I;
  return  (I)(utab[x&0xff])     | ((I)(utab[(x>>8)&0xff])<<16)
       | ((I)(utab[y&0xff])<<1) | ((I)(utab[(y>>8)&0xff])<<17);
  }
uint32_t morton2block2D_32 (uint32_t v)
  {
  typedef uint32_t I;
  I raw1 = v&0x55555555, raw2 = (v>>1)&0x55555555;
  raw1|=raw1>>15;
  raw2|=raw2>>15;
  return ctab[raw1&0xff]      | (ctab[(raw1>>8)&0xff]<< 4)
      | (ctab[raw2&0xff]<<16) | (ctab[(raw2>>8)&0xff]<<20);
  }
void morton2coord2D_32 (uint32_t v, uint32_t *x, uint32_t *y)
  {
  typedef uint32_t I;
  I raw1 = v&0x55555555, raw2 = (v>>1)&0x55555555;
  raw1|=raw1>>15;
  raw2|=raw2>>15;
  *x =  ctab[raw1&0xff] | (ctab[(raw1>>8)&0xff]<<4);
  *y =  ctab[raw2&0xff] | (ctab[(raw2>>8)&0xff]<<4);
  }
uint64_t block2morton2D_64 (uint64_t v)
  {
  typedef uint64_t I;
  return  (I)(utab[ v     &0xff])      | ((I)(utab[(v>> 8)&0xff])<<16)
       | ((I)(utab[(v>>16)&0xff])<<32) | ((I)(utab[(v>>24)&0xff])<<48)
       | ((I)(utab[(v>>32)&0xff])<< 1) | ((I)(utab[(v>>40)&0xff])<<17)
       | ((I)(utab[(v>>48)&0xff])<<33) | ((I)(utab[(v>>56)&0xff])<<49);
  }
uint64_t coord2morton2D_64 (uint64_t x, uint64_t y)
  {
  typedef uint64_t I;
  return  (I)(utab[ x     &0xff])      | ((I)(utab[(x>> 8)&0xff])<<16)
       | ((I)(utab[(x>>16)&0xff])<<32) | ((I)(utab[(x>>24)&0xff])<<48)
       | ((I)(utab[ y     &0xff])<< 1) | ((I)(utab[(y>> 8)&0xff])<<17)
       | ((I)(utab[(y>>16)&0xff])<<33) | ((I)(utab[(y>>24)&0xff])<<49);
  }
uint64_t morton2block2D_64 (uint64_t v)
  {
  typedef uint64_t I;
  I raw1 = v&0x5555555555555555, raw2 = (v>>1)&0x5555555555555555;
  raw1|=raw1>>15;
  raw2|=raw2>>15;
  return (I)ctab[ raw1     &0xff]      | ((I)ctab[(raw1>> 8)&0xff]<< 4)
      | ((I)ctab[(raw1>>32)&0xff]<<16) | ((I)ctab[(raw1>>40)&0xff]<<20)
      | ((I)ctab[ raw2     &0xff]<<32) | ((I)ctab[(raw2>> 8)&0xff]<<36)
      | ((I)ctab[(raw2>>32)&0xff]<<48) | ((I)ctab[(raw2>>40)&0xff]<<52);
  }
void morton2coord2D_64 (uint64_t v, uint64_t *x, uint64_t *y)
  {
  typedef uint64_t I;
  I raw1 = v&0x5555555555555555, raw2 = (v>>1)&0x5555555555555555;
  raw1|=raw1>>15;
  raw2|=raw2>>15;
  *x =  (I)ctab[ raw1     &0xff]      | ((I)ctab[(raw1>> 8)&0xff]<< 4)
     | ((I)ctab[(raw1>>32)&0xff]<<16) | ((I)ctab[(raw1>>40)&0xff]<<20);
  *y =  (I)ctab[ raw2     &0xff]      | ((I)ctab[(raw2>> 8)&0xff]<< 4)
     | ((I)ctab[(raw2>>32)&0xff]<<16) | ((I)ctab[(raw2>>40)&0xff]<<20);
  }

#else

static inline uint64_t spread2D_64 (uint64_t v)
  {
  v&=0xffffffffu;
  v = (v|(v<<16)) & 0x0000ffff0000ffffu;
  v = (v|(v<< 8)) & 0x00ff00ff00ff00ffu;
  v = (v|(v<< 4)) & 0x0f0f0f0f0f0f0f0fu;
  v = (v|(v<< 2)) & 0x3333333333333333u;
  v = (v|(v<< 1)) & 0x5555555555555555u;
  return v;
  }
static inline uint64_t compress2D_64 (uint64_t v)
  {
  v&=0x5555555555555555u;
  v = (v|(v>> 1)) & 0x3333333333333333u;
  v = (v|(v>> 2)) & 0x0f0f0f0f0f0f0f0fu;
  v = (v|(v>> 4)) & 0x00ff00ff00ff00ffu;
  v = (v|(v>> 8)) & 0x0000ffff0000ffffu;
  v = (v|(v>>16)) & 0x00000000ffffffffu;
  return v;
  }

uint32_t block2morton2D_32 (uint32_t v)
  { uint64_t t=spread2D_64(v); return (t | (t>>31)) & 0xffffffffu; }
uint32_t coord2morton2D_32 (uint32_t x, uint32_t y)
  {
  uint64_t t=spread2D_64((x&0xffff)|(y<<16));
  return (t | (t>>31)) & 0xffffffffu;
  }
uint32_t morton2block2D_32 (uint32_t v)
  { uint64_t t=v; t|=t<<31; t=compress2D_64(t); return t; }
void morton2coord2D_32 (uint32_t v, uint32_t *x, uint32_t *y)
  {
  uint64_t t=v; t|=t<<31; t=compress2D_64(t);
  *x = t&0xffff;
  *y = t>>16;
  }
uint64_t block2morton2D_64 (uint64_t v)
  { return spread2D_64(v) | (spread2D_64(v>>32)<<1); }
uint64_t coord2morton2D_64 (uint64_t x, uint64_t y)
  { return spread2D_64(x) | (spread2D_64(y)<<1); }
uint64_t morton2block2D_64 (uint64_t v)
  { return compress2D_64(v) | (compress2D_64(v>>1)<<32); }
void morton2coord2D_64 (uint64_t v, uint64_t *x, uint64_t *y)
  { *x = compress2D_64(v); *y = compress2D_64(v>>1); }

#endif

namespace {

inline uint32_t spread3D_32 (uint32_t v)
  {
  v&=0x3ff;
  v = (v|(v<< 8)|(v<<16)) & 0x0f00f00fu;
  v = (v|(v<< 4)) & 0xc30c30c3u;
  v = (v|(v<< 2)) & 0x49249249u;
  return v;
  }
inline uint32_t compress3D_32 (uint32_t v)
  {
  v&=0x9249249u;
  v = (v|(v>> 2)) & 0xc30c30c3u;
  v = (v|(v>> 4)) & 0x0f00f00fu;
  v = (v|(v>> 8)|(v>>16)) & 0x3ffu;
  return v;
  }
inline uint64_t spread3D_64 (uint64_t v)
  {
  v&=0x1fffff;
  v = (v|(v<<16)|(v<<32)) & 0x00ff0000ff0000ffu;
  v = (v|(v<< 8)) & 0xf00f00f00f00f00fu;
  v = (v|(v<< 4)) & 0x30c30c30c30c30c3u;
  v = (v|(v<< 2)) & 0x9249249249249249u;
  return v;
  }
inline uint64_t compress3D_64 (uint64_t v)
  {
  v&=0x1249249249249249u;
  v=(v|(v>> 2)) & 0x30c30c30c30c30c3u;
  v=(v|(v>> 4)) & 0xf00f00f00f00f00fu;
  v=(v|(v>> 8)) & 0x00ff0000ff0000ffu;
  v=(v|(v>>16)|(v>>32)) & 0x1fffffu;
  return v;
  }

}

uint32_t block2morton3D_32 (uint32_t v)
  {
  uint32_t v2=v&0xfffff;
  uint64_t v3=spread3D_64(v2);
  v3=(v3|(v3>>29))&0x1fffffff;
  return v3|(spread3D_32(v>>20)<<2);
  }
uint32_t coord2morton3D_32 (uint32_t x, uint32_t y, uint32_t z)
  {
  uint32_t v2=(x&0x3ff)|((y&0x3ff)<<10);
  uint64_t v3=spread3D_64(v2);
  v3=(v3|(v3>>29))&0x1fffffff;
  return v3|(spread3D_32(z&0x3ff)<<2);
  }
uint32_t morton2block3D_32 (uint32_t v)
  {
  return compress3D_32(v)
      | (compress3D_32(v>>1)<<10)
      | (compress3D_32(v>>2)<<20);
  }
void morton2coord3D_32 (uint32_t v, uint32_t *x, uint32_t *y, uint32_t *z)
  {
  *x = compress3D_32(v);
  *y = compress3D_32(v>>1);
  *z = compress3D_32(v>>2);
  }

uint64_t block2morton3D_64 (uint64_t v)
  {
  return spread3D_64(v)
      | (spread3D_64(v>>21)<<1)
      | (spread3D_64(v>>42)<<2);
  }
uint64_t coord2morton3D_64 (uint64_t x, uint64_t y, uint64_t z)
  {
  return spread3D_64(x)
      | (spread3D_64(y)<<1)
      | (spread3D_64(z)<<2);
  }
uint64_t morton2block3D_64 (uint64_t v)
  {
  return compress3D_64(v)
      | (compress3D_64(v>>1)<<21)
      | (compress3D_64(v>>2)<<42);
  }
void morton2coord3D_64 (uint64_t v, uint64_t *x, uint64_t *y, uint64_t *z)
  {
  *x = compress3D_64(v);
  *y = compress3D_64(v>>1);
  *z = compress3D_64(v>>2);
  }

#endif

namespace {

const uint8_t m2p3D[24][8]={
{144,119, 97,110, 43, 44, 98,109},
{ 36, 35,101,106,127,152,102,105},
{ 96, 27,135, 28,161,162,182,181},
{170,169,189,190, 19,104, 20,143},
{134,137,151,112,133,138, 12, 11},
{130,141,  3,  4,129,142,120,159},
{174,173,185,186,103, 60,128, 59},
{ 52,111, 51,136,165,166,178,177},
{118,167,117, 92,153,168,154, 91},
{160,145, 83,146,175,126, 84,125},
{114, 75,113,176,157, 76,158,191},
{ 68,149,183,150, 67,122,184,121},
{ 16,139,  1,  2, 55,140, 14, 13},
{132, 63,  5,  6,131, 24, 10,  9},
{ 70,  7, 81, 32, 69,124, 82,123},
{116, 77,115, 90, 15, 78, 40, 89},
{ 38, 37, 23,108, 41, 42, 48,107},
{ 34, 33, 99, 56, 45, 46,100, 31},
{  0, 73, 39, 94,155, 74,156, 93},
{ 66,147, 85,148, 65,  8, 86, 47},
{ 72, 71,187,188, 17, 62, 18, 61},
{ 54, 25, 53, 26, 79, 64,180,179},
{172,171, 95, 80, 21, 58, 22, 57},
{ 50, 29, 49, 30,163,164, 88, 87}};

const uint8_t p2m3D[24][8]={
{144, 98,102, 44, 45,111,107,113},
{157,111,107, 33, 32, 98,102,124},
{ 96,164,165, 25, 27,183,182,130},
{109,169,168, 20, 22,186,187,143},
{115,137,141, 15, 14,132,128,146},
{126,132,128,  2,  3,137,141,159},
{134,186,187, 63, 61,169,168,100},
{139,183,182, 50, 48,164,165,105},
{173,156,158, 95, 91,114,112,161},
{160,145,147, 82, 86,127,125,172},
{179,114,112, 73, 77,156,158,191},
{190,127,125, 68, 64,145,147,178},
{ 16,  2,  3,137,141, 15, 14, 52},
{ 29, 15, 14,132,128,  2,  3, 57},
{ 35, 82, 86,127,125, 68, 64,  1},
{ 46, 95, 91,114,112, 73, 77, 12},
{ 54, 44, 45,111,107, 33, 32, 18},
{ 59, 33, 32, 98,102, 44, 45, 31},
{  0, 73, 77,156,158, 95, 91, 34},
{ 13, 68, 64,145,147, 82, 86, 47},
{ 72, 20, 22,186,187, 63, 61, 65},
{ 69, 25, 27,183,182, 50, 48, 76},
{ 83, 63, 61,169,168, 20, 22, 90},
{ 94, 50, 48,164,165, 25, 27, 87}};

const uint8_t m2p2D_1[4][4] = {
{ 4, 1, 11, 2},{0,15, 5, 6},{10,9,3,12},{14,7,13,8}};
uint8_t m2p2D_3[4][64];
const uint8_t p2m2D_1[4][4] = {
{ 4, 1, 3, 10},{0,6,7,13},{15,9,8,2},{11,14,12,5}};
uint8_t p2m2D_3[4][64];
int peano2d_done=0;

void init_peano2d (void)
  {
  peano2d_done=1;

  for (int d=0; d<4; ++d)
    for (uint32_t p=0; p<64; ++p)
      {
      unsigned rot = d;
      uint32_t v = p<<26;
      uint32_t res = 0;
      for (int i=0; i<3; ++i)
        {
        unsigned tab=m2p2D_1[rot][v>>30];
        v<<=2;
        res = (res<<2) | (tab&0x3);
        rot = tab>>2;
        }
      m2p2D_3[d][p]=res|(rot<<6);
      }
  for (int d=0; d<4; ++d)
    for (uint32_t p=0; p<64; ++p)
      {
      unsigned rot = d;
      uint32_t v = p<<26;
      uint32_t res = 0;
      for (int i=0; i<3; ++i)
        {
        unsigned tab=p2m2D_1[rot][v>>30];
        v<<=2;
        res = (res<<2) | (tab&0x3);
        rot = tab>>2;
        }
      p2m2D_3[d][p]=res|(rot<<6);
      }
  }

}

uint32_t morton2peano3D_32(uint32_t v, int bits)
  {
  unsigned rot = 0;
  uint32_t res = 0;
  v<<=3*(10-bits)+2;
  for (int i=0; i<bits; ++i)
    {
    unsigned tab=m2p3D[rot][v>>29];
    v<<=3;
    res = (res<<3) | (tab&0x7);
    rot = tab>>3;
    }
  return res;
  }
uint32_t peano2morton3D_32(uint32_t v, int bits)
  {
  unsigned rot = 0;
  uint32_t res = 0;
  v<<=3*(10-bits)+2;
  for (int i=0; i<bits; ++i)
    {
    unsigned tab=p2m3D[rot][v>>29];
    v<<=3;
    res = (res<<3) | (tab&0x7);
    rot = tab>>3;
    }
  return res;
  }

uint64_t morton2peano3D_64(uint64_t v, int bits)
  {
  unsigned rot = 0;
  uint64_t res = 0;
  v<<=3*(21-bits)+1;
  for (int i=0; i<bits; ++i)
    {
    unsigned tab=m2p3D[rot][v>>61];
    v<<=3;
    res = (res<<3) | (tab&0x7);
    rot = tab>>3;
    }
  return res;
  }
uint64_t peano2morton3D_64(uint64_t v, int bits)
  {
  unsigned rot = 0;
  uint64_t res = 0;
  v<<=3*(21-bits)+1;
  for (int i=0; i<bits; ++i)
    {
    unsigned tab=p2m3D[rot][v>>61];
    v<<=3;
    res = (res<<3) | (tab&0x7);
    rot = tab>>3;
    }
  return res;
  }

uint32_t morton2peano2D_32(uint32_t v, int bits)
  {
  if (!peano2d_done) init_peano2d();
  unsigned rot = 0;
  uint32_t res = 0;
  v<<=32-(bits<<1);
  int i=0;
  for (; i<bits-2; i+=3)
    {
    unsigned tab=m2p2D_3[rot][v>>26];
    v<<=6;
    res = (res<<6) | (tab&0x3fu);
    rot = tab>>6;
    }
  for (; i<bits; ++i)
    {
    unsigned tab=m2p2D_1[rot][v>>30];
    v<<=2;
    res = (res<<2) | (tab&0x3);
    rot = tab>>2;
    }
  return res;
  }
uint32_t peano2morton2D_32(uint32_t v, int bits)
  {
  if (!peano2d_done) init_peano2d();
  unsigned rot = 0;
  uint32_t res = 0;
  v<<=32-(bits<<1);
  int i=0;
  for (; i<bits-2; i+=3)
    {
    unsigned tab=p2m2D_3[rot][v>>26];
    v<<=6;
    res = (res<<6) | (tab&0x3fu);
    rot = tab>>6;
    }
  for (; i<bits; ++i)
    {
    unsigned tab=p2m2D_1[rot][v>>30];
    v<<=2;
    res = (res<<2) | (tab&0x3);
    rot = tab>>2;
    }
  return res;
  }
uint64_t morton2peano2D_64(uint64_t v, int bits)
  {
  if (!peano2d_done) init_peano2d();
  unsigned rot = 0;
  uint64_t res = 0;
  v<<=64-(bits<<1);
  int i=0;
  for (; i<bits-2; i+=3)
    {
    unsigned tab=m2p2D_3[rot][v>>58];
    v<<=6;
    res = (res<<6) | (tab&0x3fu);
    rot = tab>>6;
    }
  for (; i<bits; ++i)
    {
    unsigned tab=m2p2D_1[rot][v>>62];
    v<<=2;
    res = (res<<2) | (tab&0x3);
    rot = tab>>2;
    }
  return res;
  }
uint64_t peano2morton2D_64(uint64_t v, int bits)
  {
  if (!peano2d_done) init_peano2d();
  unsigned rot = 0;
  uint64_t res = 0;
  v<<=64-(bits<<1);
  int i=0;
  for (; i<bits-2; i+=3)
    {
    unsigned tab=p2m2D_3[rot][v>>58];
    v<<=6;
    res = (res<<6) | (tab&0x3fu);
    rot = tab>>6;
    }
  for (; i<bits; ++i)
    {
    unsigned tab=p2m2D_1[rot][v>>62];
    v<<=2;
    res = (res<<2) | (tab&0x3);
    rot = tab>>2;
    }
  return res;
  }

}