Bitvec.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. #include "Bitvec.h"
  2. #include "Random.h"
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #ifndef DEBUG
  6. #undef assert
  7. void assert ( bool )
  8. {
  9. }
  10. #endif
  11. //----------------------------------------------------------------------------
  12. void printbits ( const void * blob, int len )
  13. {
  14. const uint8_t * data = (const uint8_t *)blob;
  15. printf("[");
  16. for(int i = 0; i < len; i++)
  17. {
  18. unsigned char byte = data[i];
  19. int hi = (byte >> 4);
  20. int lo = (byte & 0xF);
  21. if(hi) printf("%01x",hi);
  22. else printf(".");
  23. if(lo) printf("%01x",lo);
  24. else printf(".");
  25. if(i != len-1) printf(" ");
  26. }
  27. printf("]");
  28. }
  29. void printbits2 ( const uint8_t * k, int nbytes )
  30. {
  31. printf("[");
  32. for(int i = nbytes-1; i >= 0; i--)
  33. {
  34. uint8_t b = k[i];
  35. for(int j = 7; j >= 0; j--)
  36. {
  37. uint8_t c = (b & (1 << j)) ? '#' : ' ';
  38. putc(c,stdout);
  39. }
  40. }
  41. printf("]");
  42. }
  43. void printhex32 ( const void * blob, int len )
  44. {
  45. assert((len & 3) == 0);
  46. uint32_t * d = (uint32_t*)blob;
  47. printf("{ ");
  48. for(int i = 0; i < len/4; i++)
  49. {
  50. printf("0x%08x, ",d[i]);
  51. }
  52. printf("}");
  53. }
  54. void printbytes ( const void * blob, int len )
  55. {
  56. uint8_t * d = (uint8_t*)blob;
  57. printf("{ ");
  58. for(int i = 0; i < len; i++)
  59. {
  60. printf("0x%02x, ",d[i]);
  61. }
  62. printf(" };");
  63. }
  64. void printbytes2 ( const void * blob, int len )
  65. {
  66. uint8_t * d = (uint8_t*)blob;
  67. for(int i = 0; i < len; i++)
  68. {
  69. printf("%02x ",d[i]);
  70. }
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Bit-level manipulation
  74. // These two are from the "Bit Twiddling Hacks" webpage
  75. uint32_t popcount ( uint32_t v )
  76. {
  77. v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
  78. v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
  79. uint32_t c = ((v + ((v >> 4) & 0xF0F0F0F)) * 0x1010101) >> 24; // count
  80. return c;
  81. }
  82. uint32_t parity ( uint32_t v )
  83. {
  84. v ^= v >> 1;
  85. v ^= v >> 2;
  86. v = (v & 0x11111111U) * 0x11111111U;
  87. return (v >> 28) & 1;
  88. }
  89. //-----------------------------------------------------------------------------
  90. uint32_t getbit ( const void * block, int len, uint32_t bit )
  91. {
  92. uint8_t * b = (uint8_t*)block;
  93. int byte = bit >> 3;
  94. bit = bit & 0x7;
  95. if(byte < len) return (b[byte] >> bit) & 1;
  96. return 0;
  97. }
  98. uint32_t getbit_wrap ( const void * block, int len, uint32_t bit )
  99. {
  100. uint8_t * b = (uint8_t*)block;
  101. int byte = bit >> 3;
  102. bit = bit & 0x7;
  103. byte %= len;
  104. return (b[byte] >> bit) & 1;
  105. }
  106. void setbit ( void * block, int len, uint32_t bit )
  107. {
  108. uint8_t * b = (uint8_t*)block;
  109. int byte = bit >> 3;
  110. bit = bit & 0x7;
  111. if(byte < len) b[byte] |= (1 << bit);
  112. }
  113. void setbit ( void * block, int len, uint32_t bit, uint32_t val )
  114. {
  115. val ? setbit(block,len,bit) : clearbit(block,len,bit);
  116. }
  117. void clearbit ( void * block, int len, uint32_t bit )
  118. {
  119. uint8_t * b = (uint8_t*)block;
  120. int byte = bit >> 3;
  121. bit = bit & 0x7;
  122. if(byte < len) b[byte] &= ~(1 << bit);
  123. }
  124. void flipbit ( void * block, int len, uint32_t bit )
  125. {
  126. uint8_t * b = (uint8_t*)block;
  127. int byte = bit >> 3;
  128. bit = bit & 0x7;
  129. if(byte < len) b[byte] ^= (1 << bit);
  130. }
  131. // from the "Bit Twiddling Hacks" webpage
  132. int countbits ( uint32_t v )
  133. {
  134. v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
  135. v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
  136. int c = ((v + ((v >> 4) & 0xF0F0F0F)) * 0x1010101) >> 24; // count
  137. return c;
  138. }
  139. //-----------------------------------------------------------------------------
  140. void lshift1 ( void * blob, int len, int c )
  141. {
  142. int nbits = len*8;
  143. for(int i = nbits-1; i >= 0; i--)
  144. {
  145. setbit(blob,len,i,getbit(blob,len,i-c));
  146. }
  147. }
  148. void lshift8 ( void * blob, int nbytes, int c )
  149. {
  150. uint8_t * k = (uint8_t*)blob;
  151. if(c == 0) return;
  152. int b = c >> 3;
  153. c &= 7;
  154. for(int i = nbytes-1; i >= b; i--)
  155. {
  156. k[i] = k[i-b];
  157. }
  158. for(int i = b-1; i >= 0; i--)
  159. {
  160. k[i] = 0;
  161. }
  162. if(c == 0) return;
  163. for(int i = nbytes-1; i >= 0; i--)
  164. {
  165. uint8_t a = k[i];
  166. uint8_t b = (i == 0) ? 0 : k[i-1];
  167. k[i] = (a << c) | (b >> (8-c));
  168. }
  169. }
  170. void lshift32 ( void * blob, int len, int c )
  171. {
  172. assert((len & 3) == 0);
  173. int nbytes = len;
  174. int ndwords = nbytes / 4;
  175. uint32_t * k = reinterpret_cast<uint32_t*>(blob);
  176. if(c == 0) return;
  177. //----------
  178. int b = c / 32;
  179. c &= (32-1);
  180. for(int i = ndwords-1; i >= b; i--)
  181. {
  182. k[i] = k[i-b];
  183. }
  184. for(int i = b-1; i >= 0; i--)
  185. {
  186. k[i] = 0;
  187. }
  188. if(c == 0) return;
  189. for(int i = ndwords-1; i >= 0; i--)
  190. {
  191. uint32_t a = k[i];
  192. uint32_t b = (i == 0) ? 0 : k[i-1];
  193. k[i] = (a << c) | (b >> (32-c));
  194. }
  195. }
  196. //-----------------------------------------------------------------------------
  197. void rshift1 ( void * blob, int len, int c )
  198. {
  199. int nbits = len*8;
  200. for(int i = 0; i < nbits; i++)
  201. {
  202. setbit(blob,len,i,getbit(blob,len,i+c));
  203. }
  204. }
  205. void rshift8 ( void * blob, int nbytes, int c )
  206. {
  207. uint8_t * k = (uint8_t*)blob;
  208. if(c == 0) return;
  209. int b = c >> 3;
  210. c &= 7;
  211. for(int i = 0; i < nbytes-b; i++)
  212. {
  213. k[i] = k[i+b];
  214. }
  215. for(int i = nbytes-b; i < nbytes; i++)
  216. {
  217. k[i] = 0;
  218. }
  219. if(c == 0) return;
  220. for(int i = 0; i < nbytes; i++)
  221. {
  222. uint8_t a = (i == nbytes-1) ? 0 : k[i+1];
  223. uint8_t b = k[i];
  224. k[i] = (a << (8-c) ) | (b >> c);
  225. }
  226. }
  227. void rshift32 ( void * blob, int len, int c )
  228. {
  229. assert((len & 3) == 0);
  230. int nbytes = len;
  231. int ndwords = nbytes / 4;
  232. uint32_t * k = (uint32_t*)blob;
  233. //----------
  234. if(c == 0) return;
  235. int b = c / 32;
  236. c &= (32-1);
  237. for(int i = 0; i < ndwords-b; i++)
  238. {
  239. k[i] = k[i+b];
  240. }
  241. for(int i = ndwords-b; i < ndwords; i++)
  242. {
  243. k[i] = 0;
  244. }
  245. if(c == 0) return;
  246. for(int i = 0; i < ndwords; i++)
  247. {
  248. uint32_t a = (i == ndwords-1) ? 0 : k[i+1];
  249. uint32_t b = k[i];
  250. k[i] = (a << (32-c) ) | (b >> c);
  251. }
  252. }
  253. //-----------------------------------------------------------------------------
  254. void lrot1 ( void * blob, int len, int c )
  255. {
  256. int nbits = len * 8;
  257. for(int i = 0; i < c; i++)
  258. {
  259. uint32_t bit = getbit(blob,len,nbits-1);
  260. lshift1(blob,len,1);
  261. setbit(blob,len,0,bit);
  262. }
  263. }
  264. void lrot8 ( void * blob, int len, int c )
  265. {
  266. int nbytes = len;
  267. uint8_t * k = (uint8_t*)blob;
  268. if(c == 0) return;
  269. //----------
  270. int b = c / 8;
  271. c &= (8-1);
  272. for(int j = 0; j < b; j++)
  273. {
  274. uint8_t t = k[nbytes-1];
  275. for(int i = nbytes-1; i > 0; i--)
  276. {
  277. k[i] = k[i-1];
  278. }
  279. k[0] = t;
  280. }
  281. uint8_t t = k[nbytes-1];
  282. if(c == 0) return;
  283. for(int i = nbytes-1; i >= 0; i--)
  284. {
  285. uint8_t a = k[i];
  286. uint8_t b = (i == 0) ? t : k[i-1];
  287. k[i] = (a << c) | (b >> (8-c));
  288. }
  289. }
  290. void lrot32 ( void * blob, int len, int c )
  291. {
  292. assert((len & 3) == 0);
  293. int nbytes = len;
  294. int ndwords = nbytes/4;
  295. uint32_t * k = (uint32_t*)blob;
  296. if(c == 0) return;
  297. //----------
  298. int b = c / 32;
  299. c &= (32-1);
  300. for(int j = 0; j < b; j++)
  301. {
  302. uint32_t t = k[ndwords-1];
  303. for(int i = ndwords-1; i > 0; i--)
  304. {
  305. k[i] = k[i-1];
  306. }
  307. k[0] = t;
  308. }
  309. uint32_t t = k[ndwords-1];
  310. if(c == 0) return;
  311. for(int i = ndwords-1; i >= 0; i--)
  312. {
  313. uint32_t a = k[i];
  314. uint32_t b = (i == 0) ? t : k[i-1];
  315. k[i] = (a << c) | (b >> (32-c));
  316. }
  317. }
  318. //-----------------------------------------------------------------------------
  319. void rrot1 ( void * blob, int len, int c )
  320. {
  321. int nbits = len * 8;
  322. for(int i = 0; i < c; i++)
  323. {
  324. uint32_t bit = getbit(blob,len,0);
  325. rshift1(blob,len,1);
  326. setbit(blob,len,nbits-1,bit);
  327. }
  328. }
  329. void rrot8 ( void * blob, int len, int c )
  330. {
  331. int nbytes = len;
  332. uint8_t * k = (uint8_t*)blob;
  333. if(c == 0) return;
  334. //----------
  335. int b = c / 8;
  336. c &= (8-1);
  337. for(int j = 0; j < b; j++)
  338. {
  339. uint8_t t = k[0];
  340. for(int i = 0; i < nbytes-1; i++)
  341. {
  342. k[i] = k[i+1];
  343. }
  344. k[nbytes-1] = t;
  345. }
  346. if(c == 0) return;
  347. //----------
  348. uint8_t t = k[0];
  349. for(int i = 0; i < nbytes; i++)
  350. {
  351. uint8_t a = (i == nbytes-1) ? t : k[i+1];
  352. uint8_t b = k[i];
  353. k[i] = (a << (8-c)) | (b >> c);
  354. }
  355. }
  356. void rrot32 ( void * blob, int len, int c )
  357. {
  358. assert((len & 3) == 0);
  359. int nbytes = len;
  360. int ndwords = nbytes/4;
  361. uint32_t * k = (uint32_t*)blob;
  362. if(c == 0) return;
  363. //----------
  364. int b = c / 32;
  365. c &= (32-1);
  366. for(int j = 0; j < b; j++)
  367. {
  368. uint32_t t = k[0];
  369. for(int i = 0; i < ndwords-1; i++)
  370. {
  371. k[i] = k[i+1];
  372. }
  373. k[ndwords-1] = t;
  374. }
  375. if(c == 0) return;
  376. //----------
  377. uint32_t t = k[0];
  378. for(int i = 0; i < ndwords; i++)
  379. {
  380. uint32_t a = (i == ndwords-1) ? t : k[i+1];
  381. uint32_t b = k[i];
  382. k[i] = (a << (32-c)) | (b >> c);
  383. }
  384. }
  385. //-----------------------------------------------------------------------------
  386. uint32_t window1 ( void * blob, int len, int start, int count )
  387. {
  388. int nbits = len*8;
  389. start %= nbits;
  390. uint32_t t = 0;
  391. for(int i = 0; i < count; i++)
  392. {
  393. setbit(&t,sizeof(t),i, getbit_wrap(blob,len,start+i));
  394. }
  395. return t;
  396. }
  397. uint32_t window8 ( void * blob, int len, int start, int count )
  398. {
  399. int nbits = len*8;
  400. start %= nbits;
  401. uint32_t t = 0;
  402. uint8_t * k = (uint8_t*)blob;
  403. if(count == 0) return 0;
  404. int c = start & (8-1);
  405. int d = start / 8;
  406. for(int i = 0; i < 4; i++)
  407. {
  408. int ia = (i + d + 1) % len;
  409. int ib = (i + d + 0) % len;
  410. uint32_t a = k[ia];
  411. uint32_t b = k[ib];
  412. uint32_t m = (a << (8-c)) | (b >> c);
  413. t |= (m << (8*i));
  414. }
  415. t &= ((1 << count)-1);
  416. return t;
  417. }
  418. uint32_t window32 ( void * blob, int len, int start, int count )
  419. {
  420. int nbits = len*8;
  421. start %= nbits;
  422. assert((len & 3) == 0);
  423. int ndwords = len / 4;
  424. uint32_t * k = (uint32_t*)blob;
  425. if(count == 0) return 0;
  426. int c = start & (32-1);
  427. int d = start / 32;
  428. if(c == 0) return (k[d] & ((1 << count) - 1));
  429. int ia = (d + 1) % ndwords;
  430. int ib = (d + 0) % ndwords;
  431. uint32_t a = k[ia];
  432. uint32_t b = k[ib];
  433. uint32_t t = (a << (32-c)) | (b >> c);
  434. t &= ((1 << count)-1);
  435. return t;
  436. }
  437. //-----------------------------------------------------------------------------
  438. bool test_shift ( void )
  439. {
  440. Rand r(1123);
  441. int nbits = 64;
  442. int nbytes = nbits / 8;
  443. int reps = 10000;
  444. for(int j = 0; j < reps; j++)
  445. {
  446. if(j % (reps/10) == 0) printf(".");
  447. uint64_t a = r.rand_u64();
  448. uint64_t b;
  449. for(int i = 0; i < nbits; i++)
  450. {
  451. b = a; lshift1 (&b,nbytes,i); assert(b == (a << i));
  452. b = a; lshift8 (&b,nbytes,i); assert(b == (a << i));
  453. b = a; lshift32 (&b,nbytes,i); assert(b == (a << i));
  454. b = a; rshift1 (&b,nbytes,i); assert(b == (a >> i));
  455. b = a; rshift8 (&b,nbytes,i); assert(b == (a >> i));
  456. b = a; rshift32 (&b,nbytes,i); assert(b == (a >> i));
  457. b = a; lrot1 (&b,nbytes,i); assert(b == ROTL64(a,i));
  458. b = a; lrot8 (&b,nbytes,i); assert(b == ROTL64(a,i));
  459. b = a; lrot32 (&b,nbytes,i); assert(b == ROTL64(a,i));
  460. b = a; rrot1 (&b,nbytes,i); assert(b == ROTR64(a,i));
  461. b = a; rrot8 (&b,nbytes,i); assert(b == ROTR64(a,i));
  462. b = a; rrot32 (&b,nbytes,i); assert(b == ROTR64(a,i));
  463. }
  464. }
  465. printf("PASS\n");
  466. return true;
  467. }
  468. //-----------------------------------------------------------------------------
  469. template < int nbits >
  470. bool test_window2 ( void )
  471. {
  472. Rand r(83874);
  473. struct keytype
  474. {
  475. uint8_t bytes[nbits/8];
  476. };
  477. int nbytes = nbits / 8;
  478. int reps = 10000;
  479. for(int j = 0; j < reps; j++)
  480. {
  481. if(j % (reps/10) == 0) printf(".");
  482. keytype k;
  483. r.rand_p(&k,nbytes);
  484. for(int start = 0; start < nbits; start++)
  485. {
  486. for(int count = 0; count < 32; count++)
  487. {
  488. uint32_t a = window1(&k,nbytes,start,count);
  489. uint32_t b = window8(&k,nbytes,start,count);
  490. uint32_t c = window(&k,nbytes,start,count);
  491. assert(a == b);
  492. assert(a == c);
  493. }
  494. }
  495. }
  496. printf("PASS %d\n",nbits);
  497. return true;
  498. }
  499. bool test_window ( void )
  500. {
  501. Rand r(48402);
  502. int reps = 10000;
  503. for(int j = 0; j < reps; j++)
  504. {
  505. if(j % (reps/10) == 0) printf(".");
  506. int nbits = 64;
  507. int nbytes = nbits / 8;
  508. uint64_t x = r.rand_u64();
  509. for(int start = 0; start < nbits; start++)
  510. {
  511. for(int count = 0; count < 32; count++)
  512. {
  513. uint32_t a = (uint32_t)ROTR64(x,start);
  514. a &= ((1 << count)-1);
  515. uint32_t b = window1 (&x,nbytes,start,count);
  516. uint32_t c = window8 (&x,nbytes,start,count);
  517. uint32_t d = window32(&x,nbytes,start,count);
  518. uint32_t e = window (x,start,count);
  519. assert(a == b);
  520. assert(a == c);
  521. assert(a == d);
  522. assert(a == e);
  523. }
  524. }
  525. }
  526. printf("PASS 64\n");
  527. test_window2<8>();
  528. test_window2<16>();
  529. test_window2<24>();
  530. test_window2<32>();
  531. test_window2<40>();
  532. test_window2<48>();
  533. test_window2<56>();
  534. test_window2<64>();
  535. return true;
  536. }
  537. //-----------------------------------------------------------------------------