r57shell

Karatsuba test new version

Apr 10th, 2014
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2.  
  3. // standard add carry flag calculation
  4. bool add_c(int a, int b)
  5. {
  6.     if ((unsigned int)a > 0xF || (unsigned int)b > 0xF)
  7.         printf("Assertation failed!\n");
  8.     int r = a + b;
  9.     return ((a&0x8)&b)|(((~r)&0x8)&b)|(((~r)&0x8)&a);
  10. }
  11.  
  12. // if you don't belive to previous function, use this
  13. bool add_c1(int a, int b)
  14. {
  15.     if ((unsigned int)a > 0xF || (unsigned int)b > 0xF)
  16.         printf("Assertation failed!\n");
  17.     int r = a + b;
  18.     return r>0xF;
  19. }
  20.  
  21. // standard sub carry flag calculation
  22. bool sub_c(int a, int b)
  23. {
  24.     int r = a - b;
  25.     return (((~a)&0x8)&b)|((r&0x8)&b)|((r&0x8)&(~a));
  26. }
  27.  
  28. // if you don't belive to previous function, use this
  29. bool sub_c1(int a, int b)
  30. {
  31.     if ((unsigned int)a > 0xF || (unsigned int)b > 0xF)
  32.         printf("Assertation failed!\n");
  33.     return a<b;
  34. }
  35.  
  36.  
  37. // simple signed 4.4 fixed point multiplication
  38. int mul(int a, int b)
  39. {
  40.     if ((unsigned int)a > 0xFF || (unsigned int)b > 0xFF)
  41.         printf("Assertation failed!\n");
  42.     return (((int)(char)a*(int)(char)b)>>4)&0xFF;
  43. }
  44.  
  45. // karatsuba test signed 4.4 fixed point multiplication
  46. int mul2(int a, int b)
  47. {
  48.     if ((unsigned int)a > 0xFF || (unsigned int)b > 0xFF)
  49.         printf("Assertation failed!\n");
  50.     int s=1;
  51.     if ((char)a < 0)
  52.     {
  53.         s *= -1;
  54.         a = -(char)a;
  55.     }
  56.     if ((char)b < 0)
  57.     {
  58.         s *= -1;
  59.         b = -(char)b;
  60.     }
  61.     if ((unsigned int)a > 0xFF || (unsigned int)b > 0xFF)
  62.         printf("Assertation failed!\n");
  63.     int z1 = ((a&0xF)*(b&0xF))&0xFF; //mul low
  64.     int z3 = ((a>>4)*(b>>4))&0xFF; // mul high
  65.     int z = 1;
  66.     int aa = ((a>>4)-(a&0xF))&0xF;
  67.     if (sub_c((a>>4),(a&0xF)))
  68.     {
  69.         z = -z;
  70.         aa = (-aa)&0xF;
  71.     }
  72.     int bb = ((b>>4)-(b&0xF))&0xF;
  73.     if (sub_c((b>>4),(b&0xF)))
  74.     {
  75.         z = -z;
  76.         bb = (-bb)&0xF;
  77.     }
  78.     int x = aa*bb&0xFF; // mul :S
  79.     if (z == -1)
  80.         x = (-x)&0xFF;
  81.     int y = (z1-x)&0xFF;
  82.     int z2 = (y+z3)&0xFF; // z2 now calculated
  83.     int r1 = ((z1>>4)+z2&0xF); // sum low part
  84.     int c = add_c((z1>>4),z2&0xF)?1:0; // check carry
  85.     int r2 = ((z2>>4)+(z3&0xF)+c)&0xF; // sum high part
  86.     int r = (r1)|(r2<<4); // combine result
  87.     if (s == -1) // sign invert
  88.         r = ((~r)+((z1)&0xF?0:1))&0xFF;
  89.     return r; // hurray!
  90. }
  91.  
  92. int main()
  93. {
  94.     for (int a=0; a<0x100; ++a)
  95.         for (int b=0; b<0x100; ++b)
  96.         {
  97.             if (mul(a,b) != mul2(a,b))
  98.                 printf("mul(%02X,%02X)=%02X!=%02X=mul2(%02X,%02X)\n",a,b,mul(a,b),mul2(a,b),a,b);
  99.         }
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment