From a81278aca8cc76a627ed38c9e74e4af199b6eccf Mon Sep 17 00:00:00 2001
From: sam <sam@92316355-f0b4-4df1-b90c-862c8a59935f>
Date: Thu, 28 May 2009 01:54:50 +0000
Subject: [PATCH] Beginning of the optim...

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@3535 92316355-f0b4-4df1-b90c-862c8a59935f
---
 examples/img2twit.cpp | 41 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 34 insertions(+), 7 deletions(-)

diff --git a/examples/img2twit.cpp b/examples/img2twit.cpp
index ad1c121..399e388 100644
--- a/examples/img2twit.cpp
+++ b/examples/img2twit.cpp
@@ -10,6 +10,11 @@
  *  http://sam.zoy.org/wtfpl/COPYING for more details.
  */
 
+/* TODO:
+ * - remove the complicated stuff from get_point/set_point, it's only
+ *   the final packing that really matters.
+ */
+
 #include "config.h"
 
 #include <stdio.h>
@@ -110,6 +115,8 @@ static int MAX_ITERATIONS;
 static unsigned int TOTAL_CELLS;
 
 #define RANGE_XY (RANGE_Y*RANGE_X)
+#define RANGE_XYXY (RANGE_XY*RANGE_XY)
+//#define RANGE_XYXY (RANGE_Y*RANGE_X*(RANGE_Y*RANGE_X+1)/2)
 #define RANGE_RXY (RANGE_Y*RANGE_X*RANGE_R)
 #define RANGE_GRXY (RANGE_Y*RANGE_X*RANGE_R*RANGE_G)
 #define RANGE_BGRXY (RANGE_Y*RANGE_X*RANGE_R*RANGE_G*RANGE_B)
@@ -550,6 +557,19 @@ static void add_point(float x, float y, float r, float g, float b, float s)
     npoints++;
 }
 
+static uint32_t pack_coords(int x1, int y1, int x2, int y2)
+{
+    return ((y2 * RANGE_X + x2) * RANGE_Y + y1) * RANGE_X + x1;
+}
+
+static void unpack_coords(uint32_t pack, int *x1, int *y1, int *x2, int *y2)
+{
+    *x1 = pack % RANGE_X; pack /= RANGE_X;
+    *y1 = pack % RANGE_Y; pack /= RANGE_Y;
+    *x2 = pack % RANGE_X; pack /= RANGE_X;
+    *y2 = pack % RANGE_Y;
+}
+
 #if 0
 static void add_random_point()
 {
@@ -1273,10 +1293,16 @@ int main(int argc, char *argv[])
         for(int i = 0; i < npoints; i += POINTS_PER_CELL)
         {
 #if POINTS_PER_CELL == 2
+            int x1, y1, x2, y2;
+            x1 = (points[i] / RANGE_Y) % RANGE_X;
+            y1 = points[i] % RANGE_Y;
+            x2 = (points[i + 1] / RANGE_Y) % RANGE_X;
+            y2 = points[i + 1] % RANGE_Y;
+            uint32_t pack = pack_coords(x1, y1, x2, y2);
+
             b.push(points[i] / RANGE_XY, RANGE_SBGR);
-            b.push(points[i] % RANGE_XY, RANGE_XY);
             b.push(points[i + 1] / RANGE_XY, RANGE_SBGR);
-            b.push(points[i + 1] % RANGE_XY, RANGE_XY);
+            b.push(pack, RANGE_XYXY);
 #else
             b.push(points[i], RANGE_SBGRXY);
 #endif
@@ -1295,12 +1321,13 @@ int main(int argc, char *argv[])
         for(int i = dw * dh; i--; )
         {
 #if POINTS_PER_CELL == 2
-            uint32_t c1 = b.pop(RANGE_XY);
-            uint32_t p1 = b.pop(RANGE_SBGR);
-            uint32_t c2 = b.pop(RANGE_XY);
+            uint32_t pack = b.pop(RANGE_XYXY);
             uint32_t p2 = b.pop(RANGE_SBGR);
-            points[i * 2 + 1] = p1 * RANGE_XY + c1;
-            points[i * 2] = p2 * RANGE_XY + c2;
+            uint32_t p1 = b.pop(RANGE_SBGR);
+            int x1, y1, x2, y2;
+            unpack_coords(pack, &x1, &y1, &x2, &y2);
+            points[i * 2] = p1 * RANGE_XY + x1 * RANGE_Y + y1;
+            points[i * 2 + 1] = p2 * RANGE_XY + x2 * RANGE_Y + y2;
 #else
             points[i] = b.pop(RANGE_SBGRXY);
 #endif