<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>code/c/common/competitor.c</filename>
    </added>
    <added>
      <filename>code/c/common/competitor.h</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -1,10 +1,105 @@
+/* ekg_sys.h
+ * Various niceties for programming.
+ */
+
 #define GET_CYCLES(x) do { \
   unsigned lo, hi; \
   __asm__ __volatile(&quot;.byte 0x0f, 0x31&quot; : &quot;=a&quot;(lo), &quot;=d&quot;(hi)); \
   x = ((unsigned long long)lo)|(((unsigned long long)hi)&lt;&lt;32); \
 } while(0)
 
+/* BIN2HEX
+ * A problem in C is converting a variable-length binary string
+ * into a character string containing hexidecimal representation
+ * of each byte.  The size of the resultant string given input size n
+ * is exactly 2n.  A version that allocates space and one that relies
+ * on the user are both implemented.
+ */
+
+/* Initialization function for a 256-byte hex string table */
+inline uint8_t bin2hex_init(char lt[256][2])
+{
+  uint16_t i;
+  if(!lt) return 0;
+  for(i=0;i&lt;256;i++) {
+    // Upper nibble
+    //printf(&quot;Doing upper nibble for %u\n&quot;, i); fflush(stdout);
+    switch(i&gt;&gt;4) {
+      case 0x0: lt[i][0] = '0'; break;
+      case 0x1: lt[i][0] = '1'; break;
+      case 0x2: lt[i][0] = '2'; break;
+      case 0x3: lt[i][0] = '3'; break;
+      case 0x4: lt[i][0] = '4'; break;
+      case 0x5: lt[i][0] = '5'; break;
+      case 0x6: lt[i][0] = '6'; break;
+      case 0x7: lt[i][0] = '7'; break;
+      case 0x8: lt[i][0] = '8'; break;
+      case 0x9: lt[i][0] = '9'; break;
+      case 0xA: lt[i][0] = 'A'; break;
+      case 0xB: lt[i][0] = 'B'; break;
+      case 0xC: lt[i][0] = 'C'; break;
+      case 0xD: lt[i][0] = 'D'; break;
+      case 0xE: lt[i][0] = 'E'; break;
+      case 0xF: lt[i][0] = 'F'; break;
+    }
+
+    // Lower nibble
+    //printf(&quot;Doing lower nibble for %u\n&quot;, i); fflush(stdout);
+    switch(i &amp; 0x0F) {
+      case 0x0: lt[i][1] = '0'; break;
+      case 0x1: lt[i][1] = '1'; break;
+      case 0x2: lt[i][1] = '2'; break;
+      case 0x3: lt[i][1] = '3'; break;
+      case 0x4: lt[i][1] = '4'; break;
+      case 0x5: lt[i][1] = '5'; break;
+      case 0x6: lt[i][1] = '6'; break;
+      case 0x7: lt[i][1] = '7'; break;
+      case 0x8: lt[i][1] = '8'; break;
+      case 0x9: lt[i][1] = '9'; break;
+      case 0xA: lt[i][1] = 'A'; break;
+      case 0xB: lt[i][1] = 'B'; break;
+      case 0xC: lt[i][1] = 'C'; break;
+      case 0xD: lt[i][1] = 'D'; break;
+      case 0xE: lt[i][1] = 'E'; break;
+      case 0xF: lt[i][1] = 'F'; break;
+    }
+  }
+  return 1;
+}
+
+/* Non-allocating */
+/* Params are Lookup Table, destination, source, and size of INPUT data */
+inline uint8_t bin2hex(char lt[256][2], char * destination, uint8_t * source, unsigned int size)
+{
+  unsigned int i = 0;
+  unsigned int pos = 0;
+  while(i &lt; size) {
+    destination[pos] = lt[source[i]][0];
+    destination[pos+1] = lt[source[i]][1];
+    i++, pos += 2;
+  }
+  return pos-1;
+}
+
+/* Allocating 
+ * ONLY USE IF YOU'RE NOT LOOPING OVER THIS FUNCTION!
+ */
+/* Params are Lookup Table, destination, source, and size of INPUT data */
+inline uint8_t abin2hex(char * destination, uint8_t * source, unsigned int size)
+{
+  unsigned int i = 0;
+  unsigned int pos = 0;
+  char lt[256][2];
+  bin2hex_init(lt);
+  while(i &lt; size) {
+    destination[pos] = lt[source[i]][0];
+    destination[pos+1] = lt[source[i]][1];
+    i++, pos += 2;
+  }
+  return pos-1;
+}
 
+/* ------------------------------------------------------------------- */
 /* FAST MEMCMP
  * Basically takes advantage of the registers on the processor to do
  * the full word comparisons of the bytes, then switches to stupid</diff>
      <filename>code/c/common/ekg_sys.h</filename>
    </modified>
    <modified>
      <diff>@@ -2,8 +2,85 @@
 #include &lt;stdlib.h&gt;
 #include &lt;stdint.h&gt;
 #include &quot;ekg_sys.h&quot;
+#include &quot;competitor.h&quot;
 
-int main(int argc, char ** argv) {
+#define CRAZY_SIZE (1&lt;&lt;10)
+
+int bin2hex_main(int argc, char ** argv) {
+  FILE * rand_fh;
+  char lt[256][2];
+  unsigned int x,y;
+  char * newhex_dest;
+  union {
+    uint32_t u32;
+    uint8_t u8[4];
+  } random_num;
+  uint16_t i;
+  printf(&quot;***************** BIN2HEX *****************\n&quot;);
+
+  if(bin2hex_init(lt) == 0) {
+    // Fail.. Leave
+    fprintf(stderr, &quot;Unable to init lookup table.\n&quot;);
+    return EXIT_FAILURE;
+  }
+
+  rand_fh = fopen(&quot;/dev/urandom&quot;, &quot;r&quot;);
+  if(!rand_fh) {
+    fprintf(stderr, &quot;Unable to open random buffer.\n&quot;);
+    return EXIT_FAILURE;
+  }
+  
+  printf(&quot;Table created: \n&quot;);
+  for(i=0; i&lt;256; i++) {
+    printf(&quot;%c%c&quot;, lt[i][0], lt[i][1]);
+    if(i%15 == 0) printf(&quot;\n&quot;);
+    else printf(&quot; &quot;);
+  }
+
+  // Initialize crazy binary array
+  uint8_t * crazy_hex = malloc(CRAZY_SIZE);
+  for(i=0; i&lt;CRAZY_SIZE-3; i++) {
+    if((fread(&amp;random_num.u32, 4, 1, rand_fh) == 0)) {
+      fprintf(stderr, &quot;Could not read 4 bytes from file.\n&quot;);
+      return EXIT_FAILURE;
+    }
+    crazy_hex[i] = random_num.u8[0];
+    crazy_hex[i+1] = random_num.u8[1];
+    crazy_hex[i+2] = random_num.u8[2];
+    crazy_hex[i+3] = random_num.u8[3];
+    i += 4;
+  }
+  char hex_dest[2 + 2*CRAZY_SIZE]; 
+  hex_dest[0] = '0';
+  hex_dest[1] = 'x';
+  GET_CYCLES(x);
+  bin2hex(lt, &amp;hex_dest[2], crazy_hex, CRAZY_SIZE);
+  GET_CYCLES(y);
+  printf(&quot;time=%u, &quot;, y-x);
+  printf(&quot;Output: %.*s\n&quot;, 2 + 2*CRAZY_SIZE, hex_dest);
+  printf(&quot;***************** BIN2HEX *****************\n&quot;);
+  printf(&quot;***************** BIN2HEX Competition *****************\n&quot;);
+
+  size_t hex2ascii_len = 256;
+  char** hex2ascii;
+  hex2ascii = malloc(hex2ascii_len*sizeof(char*));
+  for(i=0; i&lt;hex2ascii_len; i++) {
+    hex2ascii[i] = malloc(3*sizeof(char));
+    snprintf(hex2ascii[i], 3,&quot;%02X&quot;, i);
+  }
+  size_t len = 8;
+  GET_CYCLES(x);
+  newhex_dest = char_to_hex((const unsigned char*)crazy_hex, CRAZY_SIZE, (char**)hex2ascii);
+  GET_CYCLES(y);
+  printf(&quot;time=%u, &quot;, y-x);
+  printf(&quot;Output: 0x%s\n&quot;, newhex_dest);
+
+  fclose(rand_fh);
+
+  return EXIT_SUCCESS;
+}
+
+int fmemcmp_main(int argc, char ** argv) {
   char * buf, * buf2;
   uint32_t buflen, failure = 0, i;
   unsigned int x,y;
@@ -14,7 +91,7 @@ int main(int argc, char ** argv) {
     buflen = 1000000;
   } else {
     buflen = atoi(argv[1]);
-    if(buflen == 0) return 0;
+    if(buflen == 0) return EXIT_FAILURE;
   }
 
   if(!(buf = malloc(buflen))) {
@@ -87,14 +164,11 @@ int main(int argc, char ** argv) {
   printf(&quot;:: fmemcmp is %g times faster than forloop memcmp.\n&quot;, ((float)speed[1])/speed[2]);
 
   printf(&quot;***************** FMEMCMP *****************\n&quot;);
-  GET_CYCLES(x);
-  if(*(uint32_t *)buf != 0xFFFFFFFF) failure = 1;
-  GET_CYCLES(y);
-  printf(&quot;time=%u, &quot;, y-x);
-  GET_CYCLES(x);
-  if((memcmp(buf, buf2, 4)) != 0) failure = 1;
-  GET_CYCLES(y);
-  printf(&quot;time=%u, &quot;, y-x);
 
-  return 1;
+  return EXIT_SUCCESS;
+}
+
+int main(int argc, char ** argv) {
+  //fmemcmp_main(argc, argv);
+  bin2hex_main(argc, argv);
 }</diff>
      <filename>code/c/common/test.c</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>b2ddc1061c356ba75a7485d31e498dacb4178721</id>
    </parent>
    <parent>
      <id>578f84490337451d4e00f9cfe205c3cb52131cae</id>
    </parent>
  </parents>
  <author>
    <name>Erik Gregg</name>
    <email>ralree@gmail.com</email>
  </author>
  <url>http://github.com/hank/life/commit/76351f2698c290990c17152c9436597aa9033563</url>
  <id>76351f2698c290990c17152c9436597aa9033563</id>
  <committed-date>2009-10-30T00:49:07-07:00</committed-date>
  <authored-date>2009-10-30T00:49:07-07:00</authored-date>
  <message>Merge branch 'master' of git@github.com:hank/life</message>
  <tree>904d7661740043d6c846453b99d7a4256ababc85</tree>
  <committer>
    <name>Erik Gregg</name>
    <email>ralree@gmail.com</email>
  </committer>
</commit>
