#include <stdio.h>
#include <stdint.h>
static uint32_t calc_napot_granularity(uint32_t addr)
{
/* In NAPOT mode the size of a region is determined
* by the number of least significant bits set, that is
* the size is 2^(3+LSZB) (LSZB = least significant zero bit).
* That is if the address is 0x20009ff, then the size is encoded
* by bits 0x1ff, which are bits 0~8, so LSZB=9, so the size of the region is
* 2^(3+9) = 4096
* That is the granularity in terms of G required for this size is
* G = LSZB + 1
*/
uint32_t index = 0;
while ((addr >> index & 0x1) == 1)
index ++;
return index;
}
int main(void) {
printf("%u\n", calc_napot_granularity
(0x20009FF)); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRpbnQuaD4KCnN0YXRpYyB1aW50MzJfdCBjYWxjX25hcG90X2dyYW51bGFyaXR5KHVpbnQzMl90IGFkZHIpCnsKICAgIC8qIEluIE5BUE9UIG1vZGUgdGhlIHNpemUgb2YgYSByZWdpb24gaXMgZGV0ZXJtaW5lZAogICAgICogYnkgdGhlIG51bWJlciBvZiBsZWFzdCBzaWduaWZpY2FudCBiaXRzIHNldCwgdGhhdCBpcwogICAgICogdGhlIHNpemUgaXMgMl4oMytMU1pCKSAoTFNaQiA9IGxlYXN0IHNpZ25pZmljYW50IHplcm8gYml0KS4KICAgICAqIFRoYXQgaXMgaWYgdGhlIGFkZHJlc3MgaXMgMHgyMDAwOWZmLCB0aGVuIHRoZSBzaXplIGlzIGVuY29kZWQKICAgICAqIGJ5IGJpdHMgMHgxZmYsIHdoaWNoIGFyZSBiaXRzIDB+OCwgc28gTFNaQj05LCBzbyB0aGUgc2l6ZSBvZiB0aGUgcmVnaW9uIGlzCiAgICAgKiAyXigzKzkpID0gNDA5NgogICAgICogVGhhdCBpcyB0aGUgZ3JhbnVsYXJpdHkgaW4gdGVybXMgb2YgRyByZXF1aXJlZCBmb3IgdGhpcyBzaXplIGlzCiAgICAgKiBHID0gTFNaQiArIDEKICAgICAqLwogICAgdWludDMyX3QgaW5kZXggPSAwOwogICAgd2hpbGUgKChhZGRyID4+IGluZGV4ICYgMHgxKSA9PSAxKQogICAgICAgIGluZGV4ICsrOwoKICAgIHJldHVybiBpbmRleDsKfQoKaW50IG1haW4odm9pZCkgewoJcHJpbnRmKCIldVxuIiwgY2FsY19uYXBvdF9ncmFudWxhcml0eSgweDIwMDA5RkYpKTsKCXJldHVybiAwOwp9Cg==