:: commit 16c5e981206d3c4fcff796a3096c445896dac467

mintsuki <mintsuki@protonmail.com> — 2022-08-26 19:34

parents: 9c389cedca

misc: Update ALIGN macros

diff --git a/common/lib/blib.h b/common/lib/blib.h
index a6ffa374..5a82f4c8 100644
--- a/common/lib/blib.h
+++ b/common/lib/blib.h
@@ -65,20 +65,24 @@ void memcpy32to64(uint64_t, uint64_t, uint64_t);
 #error Unknown architecture
 #endif
 
-#define DIV_ROUNDUP(a, b) (((a) + ((b) - 1)) / (b))
+#define DIV_ROUNDUP(a, b) ({ \
+    __auto_type DIV_ROUNDUP_a = (a); \
+    __auto_type DIV_ROUNDUP_b = (b); \
+    (DIV_ROUNDUP_a + (DIV_ROUNDUP_b - 1)) / DIV_ROUNDUP_b; \
+})
 
 #define ALIGN_UP(x, a) ({ \
-    typeof(x) value = x; \
-    typeof(a) align = a; \
-    value = DIV_ROUNDUP(value, align) * align; \
-    value; \
+    __auto_type ALIGN_UP_value = (x); \
+    __auto_type ALIGN_UP_align = (a); \
+    ALIGN_UP_value = DIV_ROUNDUP(ALIGN_UP_value, ALIGN_UP_align) * ALIGN_UP_align; \
+    ALIGN_UP_value; \
 })
 
 #define ALIGN_DOWN(x, a) ({ \
-    typeof(x) value = x; \
-    typeof(a) align = a; \
-    value = (value / align) * align; \
-    value; \
+    __auto_type ALIGN_DOWN_value = (x); \
+    __auto_type ALIGN_DOWN_align = (a); \
+    ALIGN_DOWN_value = (ALIGN_DOWN_value / ALIGN_DOWN_align) * ALIGN_DOWN_align; \
+    ALIGN_DOWN_value; \
 })
 
 #define SIZEOF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
tab: 248 wrap: offon