diff options
| author | Henrik Rydberg <rydberg@euromail.se> | 2010-11-07 18:04:46 +0100 |
|---|---|---|
| committer | Henrik Rydberg <rydberg@euromail.se> | 2010-11-07 18:04:46 +0100 |
| commit | 67fdae132708889ed89e302bd63a5cb91b2c75a7 (patch) | |
| tree | 29e6b2989ab5180eece929ad7a12def515e24259 /src/match_four.c | |
| parent | 868f53c8a5684524bc6b1e7cd39aabed56fad9df (diff) | |
Add and test a simple kernel matcher
This matcher is for up to four fingers, and has these properties:
* Approximately 1.4 times faster at 4 fingers
* Approximately 4.0 times faster at 2 fingers
* Roughly 100 lines of code
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/match_four.c')
| -rw-r--r-- | src/match_four.c | 129 |
1 files changed, 129 insertions, 0 deletions
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, 1, 2, 0, 1, 2, 3, 0, 0, 0, 0, 1, 1, | ||
| 10 | 1, 0, 0, 0, 1, 2, 1, 1, 0, 2, 2, 1, 2, 0, 0, 0, | ||
| 11 | 1, 2, 3, 1, 1, 0, 2, 3, 2, 1, 2, 0, 3, 3, 1, 2, | ||
| 12 | 3, 0, 0, 0, 1, 1, 1, 2, 1, 0, 0, 3, 0, 1, 1, 3, | ||
| 13 | 1, 0, 2, 2, 3, 1, 2, 0, 0, 4, 0, 1, 2, 2, 4, 2, | ||
| 14 | 1, 0, 0, 5, 0, 2, 1, 1, 5, 2, 0, 1, 1, 4, 1, 0, | ||
| 15 | 2, 3, 2, 4, 1, 2, 0, 3, 3, 4, 1, 2, 3, 0, 0, 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 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 | } | ||
