Guest User

Untitled

a guest
Jun 25th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main (int,char**)
  4. {
  5. int n=1;
  6. for (;;++n) {
  7. int msb;
  8. asm("bsrl %1,%0" : "=r"(msb) : "r"(n));
  9. std::cout << n << " : " << msb << std::endl;
  10. }
  11. return 0;
  12. }
  13.  
  14. double ff=(double)(v|1);
  15. return ((*(1+(unsigned long *)&ff))>>20)-1023; // assumes x86 endianness
  16.  
  17. int msb(unsigned int v) {
  18. static const int pos[32] = {0, 1, 28, 2, 29, 14, 24, 3,
  19. 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19,
  20. 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
  21. v |= v >> 1;
  22. v |= v >> 2;
  23. v |= v >> 4;
  24. v |= v >> 8;
  25. v |= v >> 16;
  26. v = (v >> 1) + 1;
  27. return pos[(v * 0x077CB531UL) >> 27];
  28. }
  29.  
  30. unsigned int v;
  31. unsigned r = 0;
  32.  
  33. while (v >>= 1) {
  34. r++;
  35. }
  36.  
  37. unsigned int v;
  38. unsigned r = 0;
  39.  
  40. while (v >>= 1) {
  41. r++;
  42. }
  43.  
  44. unsigned int
  45. msb32(register unsigned int x)
  46. {
  47. x |= (x >> 1);
  48. x |= (x >> 2);
  49. x |= (x >> 4);
  50. x |= (x >> 8);
  51. x |= (x >> 16);
  52. return(x & ~(x >> 1));
  53. }
  54.  
  55. position = sizeof(int)*8
  56. while(!(n & cmp)){
  57. n <<=1;
  58. position--;
  59. }
  60.  
  61. //////// go.c ///////////////////////////////
  62. // compile with: gcc go.c -o go -lm
  63. #include <math.h>
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #include <time.h>
  67.  
  68. /***************** math ********************/
  69.  
  70. #define POS_OF_HIGHESTBITmath(a) /* 0th position is the Least-Signif-Bit */
  71. ((unsigned) log2(a)) /* thus: do not use if a <= 0 */
  72.  
  73. #define NUM_OF_HIGHESTBITmath(a) ((a)
  74. ? (1U << POS_OF_HIGHESTBITmath(a))
  75. : 0)
  76.  
  77.  
  78.  
  79. /***************** clz ********************/
  80.  
  81. unsigned NUM_BITS_U = ((sizeof(unsigned) << 3) - 1);
  82. #define POS_OF_HIGHESTBITclz(a) (NUM_BITS_U - __builtin_clz(a)) /* only works for a != 0 */
  83.  
  84. #define NUM_OF_HIGHESTBITclz(a) ((a)
  85. ? (1U << POS_OF_HIGHESTBITclz(a))
  86. : 0)
  87.  
  88.  
  89. /***************** i2f ********************/
  90.  
  91. double FF;
  92. #define POS_OF_HIGHESTBITi2f(a) (FF = (double)(ui|1), ((*(1+(unsigned*)&FF))>>20)-1023)
  93.  
  94.  
  95. #define NUM_OF_HIGHESTBITi2f(a) ((a)
  96. ? (1U << POS_OF_HIGHESTBITi2f(a))
  97. : 0)
  98.  
  99.  
  100.  
  101.  
  102. /***************** asm ********************/
  103.  
  104. unsigned OUT;
  105. #define POS_OF_HIGHESTBITasm(a) (({asm("bsrl %1,%0" : "=r"(OUT) : "r"(a));}), OUT)
  106.  
  107. #define NUM_OF_HIGHESTBITasm(a) ((a)
  108. ? (1U << POS_OF_HIGHESTBITasm(a))
  109. : 0)
  110.  
  111.  
  112.  
  113.  
  114. /***************** bitshift1 ********************/
  115.  
  116. #define NUM_OF_HIGHESTBITbitshift1(a) (({
  117. OUT = a;
  118. OUT |= (OUT >> 1);
  119. OUT |= (OUT >> 2);
  120. OUT |= (OUT >> 4);
  121. OUT |= (OUT >> 8);
  122. OUT |= (OUT >> 16);
  123. }), (OUT & ~(OUT >> 1)))
  124.  
  125.  
  126.  
  127. /***************** bitshift2 ********************/
  128. int POS[32] = {0, 1, 28, 2, 29, 14, 24, 3,
  129. 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19,
  130. 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
  131.  
  132. #define POS_OF_HIGHESTBITbitshift2(a) (({
  133. OUT = a;
  134. OUT |= OUT >> 1;
  135. OUT |= OUT >> 2;
  136. OUT |= OUT >> 4;
  137. OUT |= OUT >> 8;
  138. OUT |= OUT >> 16;
  139. OUT = (OUT >> 1) + 1;
  140. }), POS[(OUT * 0x077CB531UL) >> 27])
  141.  
  142. #define NUM_OF_HIGHESTBITbitshift2(a) ((a)
  143. ? (1U << POS_OF_HIGHESTBITbitshift2(a))
  144. : 0)
  145.  
  146.  
  147.  
  148. #define LOOPS 100000000U
  149.  
  150. int main()
  151. {
  152. time_t start, end;
  153. unsigned ui;
  154. unsigned n;
  155.  
  156. /********* Checking the first few unsigned values (you'll need to check all if you want to use an algorithm here) **************/
  157. printf("mathn");
  158. for (ui = 0U; ui < 18; ++ui)
  159. printf("%it%in", ui, NUM_OF_HIGHESTBITmath(ui));
  160.  
  161. printf("nn");
  162.  
  163. printf("clzn");
  164. for (ui = 0U; ui < 18U; ++ui)
  165. printf("%it%in", ui, NUM_OF_HIGHESTBITclz(ui));
  166.  
  167. printf("nn");
  168.  
  169. printf("i2fn");
  170. for (ui = 0U; ui < 18U; ++ui)
  171. printf("%it%in", ui, NUM_OF_HIGHESTBITi2f(ui));
  172.  
  173. printf("nn");
  174.  
  175. printf("asmn");
  176. for (ui = 0U; ui < 18U; ++ui) {
  177. printf("%it%in", ui, NUM_OF_HIGHESTBITasm(ui));
  178. }
  179.  
  180. printf("nn");
  181.  
  182. printf("bitshift1n");
  183. for (ui = 0U; ui < 18U; ++ui) {
  184. printf("%it%in", ui, NUM_OF_HIGHESTBITbitshift1(ui));
  185. }
  186.  
  187. printf("nn");
  188.  
  189. printf("bitshift2n");
  190. for (ui = 0U; ui < 18U; ++ui) {
  191. printf("%it%in", ui, NUM_OF_HIGHESTBITbitshift2(ui));
  192. }
  193.  
  194. printf("nnPlease wait...nn");
  195.  
  196.  
  197. /************************* Simple clock() benchmark ******************/
  198. start = clock();
  199. for (ui = 0; ui < LOOPS; ++ui)
  200. n = NUM_OF_HIGHESTBITmath(ui);
  201. end = clock();
  202. printf("math:t%en", (double)(end-start)/CLOCKS_PER_SEC);
  203.  
  204. start = clock();
  205. for (ui = 0; ui < LOOPS; ++ui)
  206. n = NUM_OF_HIGHESTBITclz(ui);
  207. end = clock();
  208. printf("clz:t%en", (double)(end-start)/CLOCKS_PER_SEC);
  209.  
  210. start = clock();
  211. for (ui = 0; ui < LOOPS; ++ui)
  212. n = NUM_OF_HIGHESTBITi2f(ui);
  213. end = clock();
  214. printf("i2f:t%en", (double)(end-start)/CLOCKS_PER_SEC);
  215.  
  216. start = clock();
  217. for (ui = 0; ui < LOOPS; ++ui)
  218. n = NUM_OF_HIGHESTBITasm(ui);
  219. end = clock();
  220. printf("asm:t%en", (double)(end-start)/CLOCKS_PER_SEC);
  221.  
  222. start = clock();
  223. for (ui = 0; ui < LOOPS; ++ui)
  224. n = NUM_OF_HIGHESTBITbitshift1(ui);
  225. end = clock();
  226. printf("bitshift1:t%en", (double)(end-start)/CLOCKS_PER_SEC);
  227.  
  228. start = clock();
  229. for (ui = 0; ui < LOOPS; ++ui)
  230. n = NUM_OF_HIGHESTBITbitshift2(ui);
  231. end = clock();
  232. printf("bitshift2t%en", (double)(end-start)/CLOCKS_PER_SEC);
  233.  
  234. printf("nThe lower, the better. Take note that a negative exponent is good! ;)n");
  235.  
  236. return EXIT_SUCCESS;
  237. }
  238.  
  239. /***************** clz2 ********************/
  240.  
  241. #define NUM_OF_HIGHESTBITclz2(a) ((a)
  242. ? (((1U) << (sizeof(unsigned)*8-1)) >> __builtin_clz(a))
  243. : 0)
  244.  
  245. int highest_bit(unsigned int a) {
  246. static const unsigned int maskv[] = { 0xffff, 0xff, 0xf, 0x3, 0x1 };
  247. const unsigned int *mask = maskv;
  248. int l, h;
  249.  
  250. if (a == 0) return -1;
  251.  
  252. l = 0;
  253. h = 32;
  254.  
  255. do {
  256. int m = l + (h - l) / 2;
  257.  
  258. if ((a >> m) != 0) l = m;
  259. else if ((a & (*mask << l)) != 0) h = m;
  260.  
  261. mask++;
  262. } while (l < h - 1);
  263.  
  264. return l;
  265. }
  266.  
  267. Funct() {
  268. int number; int count;
  269.  
  270. while(number > 0) {
  271. number = number << 1;
  272. count++;
  273. }
  274.  
  275. printf("It is the no "%d" bit from the left", (count+1));
  276. }
Add Comment
Please, Sign In to add comment