fork download
  1. #include <stdint.h>
  2. #include <stdio.h>
  3.  
  4. uint8_t my_log2 (uint32_t v)
  5. {
  6. uint8_t *bytes = (void *)&v;
  7.  
  8. uint8_t i = 24;
  9. while (1) {
  10. uint8_t byte = bytes[i / 8];
  11. if (byte) {
  12. do {
  13. i++;
  14. byte >>= 1;
  15. } while (byte);
  16. return i;
  17. }
  18. if (i == 0) {
  19. return 0;
  20. }
  21. i -= 8;
  22. }
  23. }
  24.  
  25. int main ()
  26. {
  27. uint32_t x = UINT32_C(0x1FFF0FFF);
  28. printf("%d\n", my_log2(x));
  29. }
  30.  
  31. #if 0
  32. my_log2:
  33. push r28
  34. push r29
  35. rcall .
  36. rcall .
  37. in r28,__SP_L__
  38. in r29,__SP_H__
  39. /* prologue: function */
  40. /* frame size = 4 */
  41. /* stack size = 6 */
  42. .L__stack_usage = 6
  43. std Y+1,r22
  44. std Y+2,r23
  45. std Y+3,r24
  46. std Y+4,r25
  47. ldd r25,Y+4
  48. cpse r25,__zero_reg__
  49. rjmp .L6
  50. ldd r25,Y+3
  51. cpse r25,__zero_reg__
  52. rjmp .L7
  53. ldd r25,Y+2
  54. cpse r25,__zero_reg__
  55. rjmp .L8
  56. ldd r25,Y+1
  57. ldi r24,0
  58. cpse r25,__zero_reg__
  59. rjmp .L5
  60. .L4:
  61. /* epilogue start */
  62. pop __tmp_reg__
  63. pop __tmp_reg__
  64. pop __tmp_reg__
  65. pop __tmp_reg__
  66. pop r29
  67. pop r28
  68. ret
  69. .L8:
  70. ldi r24,lo8(8)
  71. .L5:
  72. subi r24,lo8(-(1))
  73. lsr r25
  74. breq .L4
  75. subi r24,lo8(-(1))
  76. lsr r25
  77. brne .L5
  78. rjmp .L4
  79. .L6:
  80. ldi r24,lo8(24)
  81. rjmp .L5
  82. .L7:
  83. ldi r24,lo8(16)
  84. rjmp .L5
  85. .size my_log2, .-my_log2
  86. .section .rodata.str1.1,"aMS",@progbits,1
  87. #endif
Success #stdin #stdout 0s 1832KB
stdin
Standard input is empty
stdout
29