fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. static uint32_t calc_napot_granularity(uint32_t addr)
  5. {
  6. /* In NAPOT mode the size of a region is determined
  7.   * by the number of least significant bits set, that is
  8.   * the size is 2^(3+LSZB) (LSZB = least significant zero bit).
  9.   * That is if the address is 0x20009ff, then the size is encoded
  10.   * by bits 0x1ff, which are bits 0~8, so LSZB=9, so the size of the region is
  11.   * 2^(3+9) = 4096
  12.   * That is the granularity in terms of G required for this size is
  13.   * G = LSZB + 1
  14.   */
  15. uint32_t index = 0;
  16. while ((addr >> index & 0x1) == 1)
  17. index ++;
  18.  
  19. return index;
  20. }
  21.  
  22. int main(void) {
  23. printf("%u\n", calc_napot_granularity(0x20009FF));
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0s 5544KB
stdin
Standard input is empty
stdout
9