diff options
| -rw-r--r-- | src/Makefile.am | 3 | ||||
| -rw-r--r-- | src/match.h | 8 | ||||
| -rw-r--r-- | src/match_four.c | 129 | ||||
| -rw-r--r-- | test/mtdev-matching.c | 61 |
4 files changed, 186 insertions, 15 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 33677c0..1c22c33 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
| @@ -13,7 +13,8 @@ libmtdev_la_SOURCES = \ | |||
| 13 | caps.c \ | 13 | caps.c \ |
| 14 | core.c \ | 14 | core.c \ |
| 15 | iobuf.c \ | 15 | iobuf.c \ |
| 16 | match.c | 16 | match.c \ |
| 17 | match_four.c | ||
| 17 | 18 | ||
| 18 | AM_CFLAGS = $(CWARNFLAGS) | 19 | AM_CFLAGS = $(CWARNFLAGS) |
| 19 | 20 | ||
diff --git a/src/match.h b/src/match.h index 98e1fa5..efba6f5 100644 --- a/src/match.h +++ b/src/match.h | |||
| @@ -40,4 +40,12 @@ | |||
| 40 | void mtdev_match(int index[DIM_FINGER], int A[DIM2_FINGER], | 40 | void mtdev_match(int index[DIM_FINGER], int A[DIM2_FINGER], |
| 41 | int nrow, int ncol); | 41 | int nrow, int ncol); |
| 42 | 42 | ||
| 43 | struct trk_coord { | ||
| 44 | int x; | ||
| 45 | int y; | ||
| 46 | }; | ||
| 47 | |||
| 48 | const unsigned char *mtdev_match_four(const struct trk_coord *old, int nslot, | ||
| 49 | const struct trk_coord *pos, int npos); | ||
| 50 | |||
| 43 | #endif | 51 | #endif |
diff --git a/src/match_four.c b/src/match_four.c new file mode 100644 index 0000000..c7d2cd3 --- /dev/null +++ b/src/match_four.c | |||
| @@ -0,0 +1,129 @@ | |||
| 1 | #include "match.h" | ||
| 2 | #include <limits.h> | ||
| 3 | |||
| 4 | typedef unsigned char u8; | ||
| 5 | typedef unsigned int u32; | ||
| 6 | |||
| 7 | /* generated by mtdev-kernel - do not edit */ | ||
| 8 | static const u8 match_data[] = { | ||
| 9 | 0, 0, 1, 0,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, 3, 4, 6, 11, 2, 1, 3, 0, 7, 11, 0, 2, 3, | ||
| 46 | 3, 7, 11, 1, 2, 3, 3, 6, 9, 12, 3, 2, 1, 0, 2, 7, | ||
| 47 | 9, 12, 3, 2, 0, 1, 3, 5, 10, 12, 3, 1, 2, 0, 1, 7, | ||
| 48 | 10, 12, 3, 0, 2, 1, 2, 5, 11, 12, 3, 1, 0, 2, 1, 6, | ||
| 49 | 11, 12, 3, 0, 1, 2, 3, 6, 8, 13, 2, 3, 1, 0, 2, 7, | ||
| 50 | 8, 13, 2, 3, 0, 1, 3, 4, 10, 13, 1, 3, 2, 0, 0, 7, | ||
| 51 | 10, 13, 0, 3, 2, 1, 2, 4, 11, 13, 1, 3, 0, 2, 0, 6, | ||
| 52 | 11, 13, 0, 3, 1, 2, 3, 5, 8, 14, 2, 1, 3, 0, 1, 7, | ||
| 53 | 8, 14, 2, 0, 3, 1, 3, 4, 9, 14, 1, 2, 3, 0, 0, 7, | ||
| 54 | 9, 14, 0, 2, 3, 1, 1, 4, 11, 14, 1, 0, 3, 2, 0, 5, | ||
| 55 | 11, 14, 0, 1, 3, 2, 2, 5, 8, 15, 2, 1, 0, 3, 1, 6, | ||
| 56 | 8, 15, 2, 0, 1, 3, 2, 4, 9, 15, 1, 2, 0, 3, 0, 6, | ||
| 57 | 9, 15, 0, 2, 1, 3, 1, 4, 10, 15, 1, 0, 2, 3, 0, 5, | ||
| 58 | 10, 15, 0, 1, 2, 3, | ||
| 59 | }; | ||
| 60 | |||
| 61 | /* generated by mtdev-kernel - do not edit */ | ||
| 62 | static const int match_index[][5] = { | ||
| 63 | { 0, 0, 1, 3, 6 }, | ||
| 64 | { 10, 10, 12, 18, 30 }, | ||
| 65 | { 50, 50, 54, 62, 92 }, | ||
| 66 | { 164, 164, 170, 194, 230 }, | ||
| 67 | { 398, 398, 406, 454, 598 }, | ||
| 68 | { 790 } | ||
| 69 | }; | ||
| 70 | |||
| 71 | static void set_dist(u32 *dist, | ||
| 72 | const struct trk_coord *b1, const struct trk_coord *e1, | ||
| 73 | const struct trk_coord *b2, const struct trk_coord *e2) | ||
| 74 | { | ||
| 75 | const struct trk_coord *p, *q; | ||
| 76 | |||
| 77 | for (p = b1; p != e1; p++) | ||
| 78 | for (q = b2; q != e2; q++) | ||
| 79 | *dist++ = abs(q->x - p->x) + abs(q->y - p->y); | ||
| 80 | } | ||
| 81 | |||
| 82 | const u8 *mtdev_match_four(const struct trk_coord *old, int nslot, | ||
| 83 | const struct trk_coord *pos, int npos) | ||
| 84 | { | ||
| 85 | u32 d[16], obj, t; | ||
| 86 | const u8 *p, *b, *e; | ||
| 87 | const int *at; | ||
| 88 | |||
| 89 | set_dist(d, old, old + nslot, pos, pos + npos); | ||
| 90 | |||
| 91 | at = &match_index[nslot][npos]; | ||
| 92 | b = &match_data[at[0]]; | ||
| 93 | e = &match_data[at[1]]; | ||
| 94 | |||
| 95 | obj = UINT_MAX, p = b; | ||
| 96 | |||
| 97 | switch (minval(nslot, npos)) { | ||
| 98 | case 1: | ||
| 99 | for (; b != e; b += npos) { | ||
| 100 | t = d[*b++]; | ||
| 101 | if (t < obj) | ||
| 102 | obj = t, p = b; | ||
| 103 | } | ||
| 104 | break; | ||
| 105 | case 2: | ||
| 106 | for (; b != e; b += npos) { | ||
| 107 | t = d[*b++], t += d[*b++]; | ||
| 108 | if (t < obj) | ||
| 109 | obj = t, p = b; | ||
| 110 | } | ||
| 111 | break; | ||
| 112 | case 3: | ||
| 113 | for (; b != e; b += npos) { | ||
| 114 | t = d[*b++], t += d[*b++], t += d[*b++]; | ||
| 115 | if (t < obj) | ||
| 116 | obj = t, p = b; | ||
| 117 | } | ||
| 118 | break; | ||
| 119 | case 4: | ||
| 120 | for (; b != e; b += npos) { | ||
| 121 | t = d[*b++], t += d[*b++], t += d[*b++], t += d[*b++]; | ||
| 122 | if (t < obj) | ||
| 123 | obj = t, p = b; | ||
| 124 | } | ||
| 125 | break; | ||
| 126 | } | ||
| 127 | |||
| 128 | return p; | ||
| 129 | } | ||
diff --git a/test/mtdev-matching.c b/test/mtdev-matching.c index dc68994..799429f 100644 --- a/test/mtdev-matching.c +++ b/test/mtdev-matching.c | |||
| @@ -30,7 +30,9 @@ | |||
| 30 | #include <stdio.h> | 30 | #include <stdio.h> |
| 31 | #include <time.h> | 31 | #include <time.h> |
| 32 | 32 | ||
| 33 | #define ITS 1000000 | 33 | #define ITS 4000000 |
| 34 | const int n1 = 4; | ||
| 35 | const int n2 = 4; | ||
| 34 | 36 | ||
| 35 | static void test1() | 37 | static void test1() |
| 36 | { | 38 | { |
| @@ -99,32 +101,61 @@ static void speed1() | |||
| 99 | int A[DIM2_FINGER]; | 101 | int A[DIM2_FINGER]; |
| 100 | int x1[DIM_FINGER] = { 1, 5, 2, 3, 4, 5, 6, 7, 8 }; | 102 | int x1[DIM_FINGER] = { 1, 5, 2, 3, 4, 5, 6, 7, 8 }; |
| 101 | int y1[DIM_FINGER] = { 1, 5, 2, 3, 4, 6, 6, 7, 8 }; | 103 | int y1[DIM_FINGER] = { 1, 5, 2, 3, 4, 6, 6, 7, 8 }; |
| 102 | int x2[DIM_FINGER] = { 1.1, 3, 2, 4, 5, 6, 7, 8 }; | 104 | int x2[DIM_FINGER] = { 1, 3, 2, 4, 5, 6, 7, 8 }; |
| 103 | int y2[DIM_FINGER] = { 1, 3, 2, 4, 5, 6, 7, 8 }; | 105 | int y2[DIM_FINGER] = { 1, 3, 2, 4, 5, 6, 7, 8 }; |
| 104 | int index[DIM_FINGER]; | 106 | int index[DIM_FINGER]; |
| 105 | int n1 = 4; | 107 | int i, j, k; |
| 106 | int n2 = 7; | ||
| 107 | 108 | ||
| 108 | int i, j; | 109 | clock_t t1 = clock(); |
| 109 | 110 | for (k = 0; k < ITS; k++) { | |
| 110 | for (i = 0; i < n1; i++) { | 111 | for (i = 0; i < n1; i++) { |
| 111 | for (j = 0; j < n2; j++) { | 112 | for (j = 0; j < n2; j++) { |
| 112 | A[i + n1 * j] = | 113 | A[i + n1 * j] = |
| 113 | (x1[i] - x2[j]) * (x1[i] - x2[j]) + | 114 | (x1[i] - x2[j]) * (x1[i] - x2[j]) + |
| 114 | (y1[i] - y2[j]) * (y1[i] - y2[j]); | 115 | (y1[i] - y2[j]) * (y1[i] - y2[j]); |
| 116 | } | ||
| 115 | } | 117 | } |
| 118 | mtdev_match(index, A, n1, n2); | ||
| 116 | } | 119 | } |
| 120 | clock_t t2 = clock(); | ||
| 121 | |||
| 122 | printf("%lf matches per second\n", | ||
| 123 | ITS * ((float)CLOCKS_PER_SEC / (t2 - t1))); | ||
| 124 | |||
| 125 | for (i = 0; i < n1; i++) | ||
| 126 | printf("match[%d] = %d\n", i, index[i]); | ||
| 127 | |||
| 128 | } | ||
| 129 | |||
| 130 | static void speed2() | ||
| 131 | { | ||
| 132 | struct trk_coord p1[] = { | ||
| 133 | { 1, 1 }, | ||
| 134 | { 5, 5 }, | ||
| 135 | { 2, 2 }, | ||
| 136 | { 3, 3 }, | ||
| 137 | { 4, 4 }, | ||
| 138 | }; | ||
| 139 | struct trk_coord p2[] = { | ||
| 140 | { 1, 1 }, | ||
| 141 | { 3, 3 }, | ||
| 142 | { 2, 2 }, | ||
| 143 | { 4, 4 }, | ||
| 144 | { 5, 5 }, | ||
| 145 | }; | ||
| 146 | const unsigned char *p; | ||
| 147 | int i; | ||
| 117 | 148 | ||
| 118 | clock_t t1 = clock(); | 149 | clock_t t1 = clock(); |
| 119 | for (i = 0; i < ITS; i++) | 150 | for (i = 0; i < ITS; i++) |
| 120 | mtdev_match(index, A, n1, n2); | 151 | p = mtdev_match_four(p1, n1, p2, n2); |
| 121 | clock_t t2 = clock(); | 152 | clock_t t2 = clock(); |
| 122 | 153 | ||
| 123 | printf("%lf matches per second\n", | 154 | printf("%lf matches per second\n", |
| 124 | ITS * ((float)CLOCKS_PER_SEC / (t2 - t1))); | 155 | ITS * ((float)CLOCKS_PER_SEC / (t2 - t1))); |
| 125 | 156 | ||
| 126 | for (i = 0; i < n1; i++) | 157 | for (i = 0; i < n2; i++) |
| 127 | printf("match[%d] = %d\n", i, index[i]); | 158 | printf("match[%d] = %d\n", i, p[i]); |
| 128 | 159 | ||
| 129 | } | 160 | } |
| 130 | 161 | ||
| @@ -136,6 +167,8 @@ int main(int argc, char *argv[]) | |||
| 136 | test2(); | 167 | test2(); |
| 137 | printf("speed1\n"); | 168 | printf("speed1\n"); |
| 138 | speed1(); | 169 | speed1(); |
| 170 | printf("speed2\n"); | ||
| 171 | speed2(); | ||
| 139 | printf("done\n"); | 172 | printf("done\n"); |
| 140 | return 0; | 173 | return 0; |
| 141 | } | 174 | } |
