|
4 | 4 | #include <string.h> |
5 | 5 | #include <stdio.h> |
6 | 6 |
|
| 7 | +#include <iostream> |
7 | 8 | #include <immintrin.h> |
8 | 9 | #include "Geohash.h" |
9 | 10 |
|
10 | | -//where is 'A'???? oh my god, we lost 'I', 'L', 'O' too. :( |
11 | | -#define BASE32_COUNT 32 |
12 | | -static const char base32Table[BASE32_COUNT] = {'0', '1', '2', '3', '4', '5', '6', '7', |
13 | | - '8', '9', 'b', 'c', 'd', 'e', 'f', 'g', |
14 | | - 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', |
15 | | - 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; |
16 | | -static int searchInBase32Table(char c) { |
17 | | - int f, l, m; |
18 | | - f = 0; |
19 | | - l = BASE32_COUNT; |
20 | | - while (f < l) { |
21 | | - m = (f + l) >> 1; |
22 | | - if (c <= base32Table[m]) |
23 | | - l = m; |
24 | | - else |
25 | | - f = m+1; |
26 | | - } |
27 | | - return l; |
28 | | -} |
29 | | -////////////////////////////////////////////////////////////////////////// |
30 | | - |
31 | | -typedef struct interval { |
32 | | - double min, max; |
33 | | -} interval_t; |
34 | | -/////////////////////////////////////////////////////// |
35 | | - |
36 | | -void GeohashEncode(double srcLat, |
37 | | - double srcLon, |
38 | | - char* geohash, |
39 | | - uint8_t precision) { |
40 | | - interval_t lat = {-90.0, 90.0}; |
41 | | - interval_t lon = {-180.0, 180.0}; |
42 | | - interval_t *ci; |
43 | | - bool isEven = true; |
44 | | - double mid, cd; |
45 | | - int32_t idx = 0; // index into base32 map |
46 | | - int8_t bit = 0; // each char holds 5 bits |
| 11 | +static uint64_t interleave(uint64_t x, uint64_t y) { |
| 12 | + x = (x | (x << 16)) & 0x0000ffff0000ffff; |
| 13 | + x = (x | (x << 8)) & 0x00ff00ff00ff00ff; |
| 14 | + x = (x | (x << 4)) & 0x0f0f0f0f0f0f0f0f; |
| 15 | + x = (x | (x << 2)) & 0x3333333333333333; |
| 16 | + x = (x | (x << 1)) & 0x5555555555555555; |
47 | 17 |
|
48 | | - while (precision) { |
49 | | - if (isEven) { |
50 | | - ci = &lon; |
51 | | - cd = srcLon; |
52 | | - } else { |
53 | | - ci = ⪫ |
54 | | - cd = srcLat; |
55 | | - } |
| 18 | + y = (y | (y << 16)) & 0x0000ffff0000ffff; |
| 19 | + y = (y | (y << 8)) & 0x00ff00ff00ff00ff; |
| 20 | + y = (y | (y << 4)) & 0x0f0f0f0f0f0f0f0f; |
| 21 | + y = (y | (y << 2)) & 0x3333333333333333; |
| 22 | + y = (y | (y << 1)) & 0x5555555555555555; |
56 | 23 |
|
57 | | - mid = (ci->min + ci->max) / 2.0; |
58 | | - idx <<= 1; //idx *= 2 |
59 | | - if (cd >= mid) { |
60 | | - ci->min = mid; |
61 | | - idx += 1; |
62 | | - } else { |
63 | | - ci->max = mid; |
64 | | - } |
| 24 | + return x | (y << 1); |
65 | 25 |
|
66 | | - isEven = !isEven; |
67 | | - |
68 | | - if (++bit == 5) { |
69 | | - *geohash++ = base32Table[idx]; |
70 | | - idx = bit = 0; |
71 | | - --precision; |
72 | | - } |
73 | | - } |
74 | | - *geohash++ = 0; |
| 26 | + //use pdep instructions |
| 27 | +// return _pdep_u64(x, 0x5555555555555555) | _pdep_u64(y, 0xaaaaaaaaaaaaaaaa); |
75 | 28 | } |
76 | | -////////////////////////////////////////////////////////////////////////// |
77 | | - |
78 | | -void GeohashDecode(const char* str, |
79 | | - double *pLon, |
80 | | - double *pLat) { |
81 | | - interval_t latInterval = {-90.0, 90.0}; |
82 | | - interval_t lonInterval = {-180.0, 180.0}; |
83 | | - interval_t *ci; |
84 | | - bool isEven = true; |
85 | | - int idx ; |
86 | | - double mid; |
| 29 | +/////////////////////////////////////////////////////// |
87 | 30 |
|
88 | | - for (; *str; ++str) { |
89 | | - idx = searchInBase32Table(*str); |
90 | | - for (int i = 0; i < 5; ++i) { |
91 | | - ci = isEven ? &lonInterval : &latInterval; |
92 | | - mid = (ci->max - ci->min) / 2.0; |
93 | | - if ((idx << i) & 0x10) { |
94 | | - ci->min += mid; |
95 | | - } else { |
96 | | - ci->max -= mid; |
97 | | - } |
98 | | - isEven = !isEven; |
99 | | - } |
100 | | - } //for c in str |
101 | | - *pLat = latInterval.max - ((latInterval.max - latInterval.min) / 2.0); |
102 | | - *pLon = lonInterval.max - ((lonInterval.max - lonInterval.min) / 2.0); |
| 31 | +uint64_t GeohashEncodeU64(double lat, double lon) { |
| 32 | + lat = lat/180.0 + 1.5; |
| 33 | + lon = lon/360.0 + 1.5; |
| 34 | + uint64_t ilat = *((uint64_t*)&lat); |
| 35 | + uint64_t ilon = *((uint64_t*)&lon); |
| 36 | + ilat >>= 20; |
| 37 | + ilon >>= 20; |
| 38 | + ilat &= 0x00000000ffffffff; |
| 39 | + ilon &= 0x00000000ffffffff; |
| 40 | + |
| 41 | + return interleave(ilat, ilon); |
103 | 42 | } |
104 | | -////////////////////////////////////////////////////////////////////////// |
105 | 43 |
|
106 | | -int GeohashComparePoints(double lon1, double lat1, |
107 | | - double lon2, double lat2, |
108 | | - int precision) { |
| 44 | +int GeohashComparePointsU64(double lon1, double lat1, |
| 45 | + double lon2, double lat2, |
| 46 | + int precision) { |
109 | 47 | assert(precision >= 1 && precision <= GEOHASH_MAX_PRECISION); |
110 | | - static char geohash1[GEOHASH_MAX_PRECISION+1] = {0}; |
111 | | - static char geohash2[GEOHASH_MAX_PRECISION+1] = {0}; |
112 | | - |
113 | | - GeohashEncode(lat1, lon1, geohash1, precision); |
114 | | - GeohashEncode(lat2, lon2, geohash2, precision); |
115 | | - return memcmp(geohash1, geohash2, precision); |
| 48 | + uint64_t gh1 = GeohashEncodeU64(lat1, lon1); |
| 49 | + uint64_t gh2 = GeohashEncodeU64(lat2, lon2); |
| 50 | + gh1 >>= precision*5 + 4; |
| 51 | + gh2 >>= precision*5 + 4; |
| 52 | + return gh1 - gh2; |
116 | 53 | } |
0 commit comments