From 8e0cf4d649860f17f8c77f34b0c3c13ec99c87bf Mon Sep 17 00:00:00 2001 From: Ved Shanbhogue Date: Sun, 8 Sep 2024 10:31:11 -0500 Subject: [PATCH] add a portable version of log2szm1 --- iommu_ref_model/test/test_utils.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/iommu_ref_model/test/test_utils.c b/iommu_ref_model/test/test_utils.c index aed073a..8b3de80 100644 --- a/iommu_ref_model/test/test_utils.c +++ b/iommu_ref_model/test/test_utils.c @@ -34,11 +34,15 @@ get_free_gppn(uint64_t num_gppn, iohgatp_t iohgatp) { } uint32_t log2szm1(uint32_t n) { +#if defined(__GNUC__) || defined(__clang__) + return 31 - __builtin_clz(n); +#else uint32_t log2sz = 0; while (n >>= 1) { log2sz++; } return log2sz > 0 ? log2sz-1 : 0; +#endif } int8_t enable_cq( @@ -48,7 +52,7 @@ enable_cq( cqb.raw = 0; cqb.ppn = get_free_ppn(nppn); - cqb.log2szm1 = 31 - __builtin_clz((nppn * PAGESIZE)/CQ_ENTRY_SZ); + cqb.log2szm1 = log2szm1((nppn * PAGESIZE)/CQ_ENTRY_SZ); write_register(CQB_OFFSET, 8, cqb.raw); do { cqcsr.raw = read_register(CQCSR_OFFSET, 4); @@ -87,7 +91,7 @@ enable_fq( fqb.raw = 0; fqb.ppn = get_free_ppn(nppn); - fqb.log2szm1 = 31 - __builtin_clz((nppn * PAGESIZE)/FQ_ENTRY_SZ); + fqb.log2szm1 = log2szm1((nppn * PAGESIZE)/FQ_ENTRY_SZ); write_register(FQB_OFFSET, 8, fqb.raw); do { fqcsr.raw = read_register(FQCSR_OFFSET, 4); @@ -124,7 +128,7 @@ enable_disable_pq( if ( enable_disable == 1 ) { pqb.raw = 0; pqb.ppn = get_free_ppn(nppn); - pqb.log2szm1 = 31 - __builtin_clz((nppn * PAGESIZE)/PQ_ENTRY_SZ); + pqb.log2szm1 = log2szm1((nppn * PAGESIZE)/PQ_ENTRY_SZ); write_register(PQB_OFFSET, 8, pqb.raw); } do {