Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int main (int,char**)
- {
- int n=1;
- for (;;++n) {
- int msb;
- asm("bsrl %1,%0" : "=r"(msb) : "r"(n));
- std::cout << n << " : " << msb << std::endl;
- }
- return 0;
- }
- double ff=(double)(v|1);
- return ((*(1+(unsigned long *)&ff))>>20)-1023; // assumes x86 endianness
- int msb(unsigned int v) {
- static const int pos[32] = {0, 1, 28, 2, 29, 14, 24, 3,
- 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19,
- 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
- v |= v >> 1;
- v |= v >> 2;
- v |= v >> 4;
- v |= v >> 8;
- v |= v >> 16;
- v = (v >> 1) + 1;
- return pos[(v * 0x077CB531UL) >> 27];
- }
- unsigned int v;
- unsigned r = 0;
- while (v >>= 1) {
- r++;
- }
- unsigned int v;
- unsigned r = 0;
- while (v >>= 1) {
- r++;
- }
- unsigned int
- msb32(register unsigned int x)
- {
- x |= (x >> 1);
- x |= (x >> 2);
- x |= (x >> 4);
- x |= (x >> 8);
- x |= (x >> 16);
- return(x & ~(x >> 1));
- }
- position = sizeof(int)*8
- while(!(n & cmp)){
- n <<=1;
- position--;
- }
- //////// go.c ///////////////////////////////
- // compile with: gcc go.c -o go -lm
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- /***************** math ********************/
- #define POS_OF_HIGHESTBITmath(a) /* 0th position is the Least-Signif-Bit */
- ((unsigned) log2(a)) /* thus: do not use if a <= 0 */
- #define NUM_OF_HIGHESTBITmath(a) ((a)
- ? (1U << POS_OF_HIGHESTBITmath(a))
- : 0)
- /***************** clz ********************/
- unsigned NUM_BITS_U = ((sizeof(unsigned) << 3) - 1);
- #define POS_OF_HIGHESTBITclz(a) (NUM_BITS_U - __builtin_clz(a)) /* only works for a != 0 */
- #define NUM_OF_HIGHESTBITclz(a) ((a)
- ? (1U << POS_OF_HIGHESTBITclz(a))
- : 0)
- /***************** i2f ********************/
- double FF;
- #define POS_OF_HIGHESTBITi2f(a) (FF = (double)(ui|1), ((*(1+(unsigned*)&FF))>>20)-1023)
- #define NUM_OF_HIGHESTBITi2f(a) ((a)
- ? (1U << POS_OF_HIGHESTBITi2f(a))
- : 0)
- /***************** asm ********************/
- unsigned OUT;
- #define POS_OF_HIGHESTBITasm(a) (({asm("bsrl %1,%0" : "=r"(OUT) : "r"(a));}), OUT)
- #define NUM_OF_HIGHESTBITasm(a) ((a)
- ? (1U << POS_OF_HIGHESTBITasm(a))
- : 0)
- /***************** bitshift1 ********************/
- #define NUM_OF_HIGHESTBITbitshift1(a) (({
- OUT = a;
- OUT |= (OUT >> 1);
- OUT |= (OUT >> 2);
- OUT |= (OUT >> 4);
- OUT |= (OUT >> 8);
- OUT |= (OUT >> 16);
- }), (OUT & ~(OUT >> 1)))
- /***************** bitshift2 ********************/
- int POS[32] = {0, 1, 28, 2, 29, 14, 24, 3,
- 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19,
- 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
- #define POS_OF_HIGHESTBITbitshift2(a) (({
- OUT = a;
- OUT |= OUT >> 1;
- OUT |= OUT >> 2;
- OUT |= OUT >> 4;
- OUT |= OUT >> 8;
- OUT |= OUT >> 16;
- OUT = (OUT >> 1) + 1;
- }), POS[(OUT * 0x077CB531UL) >> 27])
- #define NUM_OF_HIGHESTBITbitshift2(a) ((a)
- ? (1U << POS_OF_HIGHESTBITbitshift2(a))
- : 0)
- #define LOOPS 100000000U
- int main()
- {
- time_t start, end;
- unsigned ui;
- unsigned n;
- /********* Checking the first few unsigned values (you'll need to check all if you want to use an algorithm here) **************/
- printf("mathn");
- for (ui = 0U; ui < 18; ++ui)
- printf("%it%in", ui, NUM_OF_HIGHESTBITmath(ui));
- printf("nn");
- printf("clzn");
- for (ui = 0U; ui < 18U; ++ui)
- printf("%it%in", ui, NUM_OF_HIGHESTBITclz(ui));
- printf("nn");
- printf("i2fn");
- for (ui = 0U; ui < 18U; ++ui)
- printf("%it%in", ui, NUM_OF_HIGHESTBITi2f(ui));
- printf("nn");
- printf("asmn");
- for (ui = 0U; ui < 18U; ++ui) {
- printf("%it%in", ui, NUM_OF_HIGHESTBITasm(ui));
- }
- printf("nn");
- printf("bitshift1n");
- for (ui = 0U; ui < 18U; ++ui) {
- printf("%it%in", ui, NUM_OF_HIGHESTBITbitshift1(ui));
- }
- printf("nn");
- printf("bitshift2n");
- for (ui = 0U; ui < 18U; ++ui) {
- printf("%it%in", ui, NUM_OF_HIGHESTBITbitshift2(ui));
- }
- printf("nnPlease wait...nn");
- /************************* Simple clock() benchmark ******************/
- start = clock();
- for (ui = 0; ui < LOOPS; ++ui)
- n = NUM_OF_HIGHESTBITmath(ui);
- end = clock();
- printf("math:t%en", (double)(end-start)/CLOCKS_PER_SEC);
- start = clock();
- for (ui = 0; ui < LOOPS; ++ui)
- n = NUM_OF_HIGHESTBITclz(ui);
- end = clock();
- printf("clz:t%en", (double)(end-start)/CLOCKS_PER_SEC);
- start = clock();
- for (ui = 0; ui < LOOPS; ++ui)
- n = NUM_OF_HIGHESTBITi2f(ui);
- end = clock();
- printf("i2f:t%en", (double)(end-start)/CLOCKS_PER_SEC);
- start = clock();
- for (ui = 0; ui < LOOPS; ++ui)
- n = NUM_OF_HIGHESTBITasm(ui);
- end = clock();
- printf("asm:t%en", (double)(end-start)/CLOCKS_PER_SEC);
- start = clock();
- for (ui = 0; ui < LOOPS; ++ui)
- n = NUM_OF_HIGHESTBITbitshift1(ui);
- end = clock();
- printf("bitshift1:t%en", (double)(end-start)/CLOCKS_PER_SEC);
- start = clock();
- for (ui = 0; ui < LOOPS; ++ui)
- n = NUM_OF_HIGHESTBITbitshift2(ui);
- end = clock();
- printf("bitshift2t%en", (double)(end-start)/CLOCKS_PER_SEC);
- printf("nThe lower, the better. Take note that a negative exponent is good! ;)n");
- return EXIT_SUCCESS;
- }
- /***************** clz2 ********************/
- #define NUM_OF_HIGHESTBITclz2(a) ((a)
- ? (((1U) << (sizeof(unsigned)*8-1)) >> __builtin_clz(a))
- : 0)
- int highest_bit(unsigned int a) {
- static const unsigned int maskv[] = { 0xffff, 0xff, 0xf, 0x3, 0x1 };
- const unsigned int *mask = maskv;
- int l, h;
- if (a == 0) return -1;
- l = 0;
- h = 32;
- do {
- int m = l + (h - l) / 2;
- if ((a >> m) != 0) l = m;
- else if ((a & (*mask << l)) != 0) h = m;
- mask++;
- } while (l < h - 1);
- return l;
- }
- Funct() {
- int number; int count;
- while(number > 0) {
- number = number << 1;
- count++;
- }
- printf("It is the no "%d" bit from the left", (count+1));
- }
Add Comment
Please, Sign In to add comment