| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| |
| 7 |
|
| /* rotate/flip a quadrant appropriately */ |
| 8 |
8 |
| static void hilbert_rot(IV n, IV *x, IV *y, IV rx, IV ry) { |
| 9 |
8 |
▶ | if (ry) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ry) { |
2 |
6 |
8 |
|
| 10 |
2 |
| return; |
| 11 |
|
| } |
| 12 |
|
| |
| 13 |
6 |
▶ | if (rx) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rx) { |
6 |
0 |
6 |
|
| 14 |
6 |
| *x = n - 1 - *x; |
| 15 |
6 |
| *y = n - 1 - *y; |
| 16 |
|
| } |
| 17 |
|
| |
| 18 |
|
| /* swap x and y, reusing ry shamelessly */ |
| 19 |
6 |
| ry = *x; |
| 20 |
6 |
| *x = *y; |
| 21 |
6 |
| *y = ry; |
| 22 |
|
| } |
| 23 |
|
| |
| 24 |
2 |
| static IV hilbert_valid_n(IV side) { |
| 25 |
2 |
| int count = 0; |
| 26 |
12 |
▶ | for (count = 0; side > 0; ++count) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (count = 0; side > 0; ++count) { |
10 |
2 |
12 |
|
| 27 |
10 |
| side >>= 1; |
| 28 |
|
| } |
| 29 |
2 |
| return 1 << (count - 1); |
| 30 |
|
| } |
| 31 |
|
| |
| 32 |
|
| /* convert (x,y) to d */ |
| 33 |
1 |
| static IV hilbert_xy2d(IV side, IV x, IV y) { |
| 34 |
1 |
| IV n = hilbert_valid_n(side); |
| 35 |
1 |
| IV d = 0; |
| 36 |
|
| IV s; |
| 37 |
|
| |
| 38 |
5 |
▶ | for (s = n / 2; s > 0; s /= 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = n / 2; s > 0; s /= 2) { |
4 |
1 |
5 |
|
| 39 |
4 |
| IV rx = (x & s) > 0; |
| 40 |
4 |
| IV ry = (y & s) > 0; |
| 41 |
4 |
| d += s * s * ((3 * rx) ^ ry); |
| 42 |
4 |
| hilbert_rot(s, &x, &y, rx, ry); |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
1 |
| return d * side / n; |
| 46 |
|
| } |
| 47 |
|
| |
| 48 |
|
| /* convert d to (x,y) */ |
| 49 |
1 |
| static void hilbert_d2xy(IV side, IV d, IV *x, IV *y) { |
| 50 |
1 |
| IV n = hilbert_valid_n(side); |
| 51 |
1 |
| IV t = d; |
| 52 |
|
| IV s; |
| 53 |
|
| |
| 54 |
1 |
| *x = 0; |
| 55 |
1 |
| *y = 0; |
| 56 |
|
| |
| 57 |
5 |
▶ | for (s = 1; s < n; s *= 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = 1; s < n; s *= 2) { |
4 |
1 |
5 |
|
| 58 |
4 |
| IV rx = 1 & (t / 2); |
| 59 |
4 |
| IV ry = 1 & (t ^ rx); |
| 60 |
4 |
| hilbert_rot(s, x, y, rx, ry); |
| 61 |
4 |
| *x += s * rx; |
| 62 |
4 |
| *y += s * ry; |
| 63 |
4 |
| t /= 4; |
| 64 |
|
| } |
| 65 |
1 |
| *x *= side / n; |
| 66 |
1 |
| *y *= side / n; |
| 67 |
1 |
| } |
| 68 |
|
| |
| 69 |
|
| |
| 70 |
|
| MODULE = Path::Hilbert::XS PACKAGE = Path::Hilbert::XS |
| 71 |
|
| PROTOTYPES: DISABLE |
| 72 |
|
| |
| 73 |
|
| # convert (x,y) to d |
| 74 |
|
| IV |
| 75 |
|
| xy2d(IV side, IV x, IV y) |
| 76 |
|
| |
| 77 |
|
| CODE: |
| 78 |
1 |
| RETVAL = hilbert_xy2d(side, x, y); |
| 79 |
|
| |
| 80 |
|
| OUTPUT: RETVAL |
| 81 |
|
| |
| 82 |
|
| |
| 83 |
|
| # convert d to (x,y) |
| 84 |
|
| void |
| 85 |
|
| d2xy(IV side, IV d) |
| 86 |
|
| |
| 87 |
|
| PREINIT: |
| 88 |
|
| IV x; |
| 89 |
|
| IV y; |
| 90 |
|
| |
| 91 |
|
| PPCODE: |
| 92 |
1 |
| hilbert_d2xy(side, d, &x, &y); |
| 93 |
|
| |
| 94 |
1 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
1 |
1 |
|
| 95 |
1 |
| mPUSHi(x); |
| 96 |
1 |
| mPUSHi(y); |