fork download
  1. #include <stdint.h>
  2. #include <stdio.h>
  3.  
  4. #if 0
  5. uint8_t my_log2 (uint32_t v)
  6. {
  7. uint8_t *bytes = (void *)&v;
  8.  
  9. uint8_t i = 24;
  10. while (1) {
  11. uint8_t byte = bytes[i / 8];
  12. if (byte) {
  13. do {
  14. i++;
  15. byte >>= 1;
  16. } while (byte);
  17. return i;
  18. }
  19. if (i == 0) {
  20. return 0;
  21. }
  22. i -= 8;
  23. }
  24. }
  25. #endif
  26.  
  27. uint8_t my_log2 (uint32_t op)
  28. {
  29. uint8_t res;
  30. asm(
  31. " clr r16\n"
  32. " cpse %D[op],__zero_reg__\n"
  33. " rjmp foundD%=\n"
  34. " cpse %C[op],__zero_reg__\n"
  35. " rjmp foundC%=\n"
  36. " cpse %B[op],__zero_reg__\n"
  37. " rjmp foundB%=\n"
  38. " cpse %A[op],__zero_reg__\n"
  39. " rjmp foundA%=\n"
  40. " rjmp end%=\n"
  41. "foundD%=:\n"
  42. " ori r16,$18\n"
  43. " mov r17,%D[op]\n"
  44. " rjmp examine%=\n"
  45. "foundC%=:\n"
  46. " ori r16,$10\n"
  47. " mov r17,%C[op]\n"
  48. " rjmp examine%=\n"
  49. "foundB%=:\n"
  50. " ori r16,$08\n"
  51. " mov r17,%B[op]\n"
  52. " rjmp examine%=\n"
  53. "foundA%=:\n"
  54. " mov r17,%A[op]\n"
  55. "examine%=:\n"
  56. " cpse r17,__zero_reg__\n"
  57. " inc r16\n"
  58. " lsr r17\n"
  59. " cpse r17,__zero_reg__\n"
  60. " inc r16\n"
  61. " lsr r17\n"
  62. " cpse r17,__zero_reg__\n"
  63. " inc r16\n"
  64. " lsr r17\n"
  65. " cpse r17,__zero_reg__\n"
  66. " inc r16\n"
  67. " lsr r17\n"
  68. " cpse r17,__zero_reg__\n"
  69. " inc r16\n"
  70. " lsr r17\n"
  71. " cpse r17,__zero_reg__\n"
  72. " inc r16\n"
  73. " lsr r17\n"
  74. " cpse r17,__zero_reg__\n"
  75. " inc r16\n"
  76. " lsr r17\n"
  77. " cpse r17,__zero_reg__\n"
  78. " inc r16\n"
  79. "end%=:\n"
  80. " mov %[res],r16\n"
  81. : [res] "=r" (res)
  82. : [op] "r" (op)
  83. : "r16", "r17"
  84. );
  85. return res;
  86. }
  87.  
  88. int main ()
  89. {
  90. uint32_t x = UINT32_C(0x1FFF0FFF);
  91. printf("%d\n", my_log2(x));
  92. }
  93.  
  94.  
  95. /tmp/ccSWpg3O.s: Assembler messages:
  96. /tmp/ccSWpg3O.s:30: Error: junk at end of line, first unrecognized character is `1'
  97. /tmp/ccSWpg3O.s:34: Error: junk at end of line, first unrecognized character is `1'
  98. /tmp/ccSWpg3O.s:38: Error: junk at end of line, first unrecognized character is `0
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty