chiark / gitweb /
macro: add DIV_ROUND_UP()
authorDavid Herrmann <dh.herrmann@gmail.com>
Mon, 29 Dec 2014 16:51:36 +0000 (17:51 +0100)
committerDavid Herrmann <dh.herrmann@gmail.com>
Tue, 30 Dec 2014 00:39:01 +0000 (01:39 +0100)
This macro calculates A / B but rounds up instead of down. We explicitly
do *NOT* use:
        (A + B - 1) / A
as it suffers from an integer overflow, even though the passed values are
properly tested against overflow. Our test-cases show this behavior.

Instead, we use:
        A / B + !!(A % B)

Note that on "Real CPUs" this does *NOT* result in two divisions. Instead,
instructions like idivl@x86 provide both, the quotient and the remainder.
Therefore, both algorithms should perform equally well (I didn't verify
this, though).

src/shared/macro.h
src/test/test-util.c

index 548294e47b65f5cc4636dbc0776bfece60245ea2..6a5742824479165f846e2df2f32c69d935998d9d 100644 (file)
@@ -197,6 +197,17 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
                                         UNIQ_T(X,xq);                   \
         })
 
                                         UNIQ_T(X,xq);                   \
         })
 
+/* [(x + y - 1) / y] suffers from an integer overflow, even though the
+ * computation should be possible in the given type. Therefore, we use
+ * [x / y + !!(x % y)]. Note that on "Real CPUs" a division returns both the
+ * quotient and the remainder, so both should be equally fast. */
+#define DIV_ROUND_UP(_x, _y)                                            \
+        __extension__ ({                                                \
+                const typeof(_x) __x = (_x);                            \
+                const typeof(_y) __y = (_y);                            \
+                (__x / __y + !!(__x % __y));                            \
+        })
+
 #define assert_se(expr)                                                 \
         do {                                                            \
                 if (_unlikely_(!(expr)))                                \
 #define assert_se(expr)                                                 \
         do {                                                            \
                 if (_unlikely_(!(expr)))                                \
index 93f11eb6e90a2e2e6aca3926a417d81473b87ae4..3f1b5487f01050f27ecd28866a6d91c1323c2727 100644 (file)
@@ -145,6 +145,39 @@ static void test_alloca(void) {
         assert_se(!memcmp(t, zero, 997));
 }
 
         assert_se(!memcmp(t, zero, 997));
 }
 
+static void test_div_round_up(void) {
+        int div;
+
+        /* basic tests */
+        assert_se(DIV_ROUND_UP(0, 8) == 0);
+        assert_se(DIV_ROUND_UP(1, 8) == 1);
+        assert_se(DIV_ROUND_UP(8, 8) == 1);
+        assert_se(DIV_ROUND_UP(12, 8) == 2);
+        assert_se(DIV_ROUND_UP(16, 8) == 2);
+
+        /* test multiple evaluation */
+        div = 0;
+        assert_se(DIV_ROUND_UP(div++, 8) == 0 && div == 1);
+        assert_se(DIV_ROUND_UP(++div, 8) == 1 && div == 2);
+        assert_se(DIV_ROUND_UP(8, div++) == 4 && div == 3);
+        assert_se(DIV_ROUND_UP(8, ++div) == 2 && div == 4);
+
+        /* overflow test with exact division */
+        assert_se(sizeof(0U) == 4);
+        assert_se(0xfffffffaU % 10U == 0U);
+        assert_se(0xfffffffaU / 10U == 429496729U);
+        assert_se(DIV_ROUND_UP(0xfffffffaU, 10U) == 429496729U);
+        assert_se((0xfffffffaU + 10U - 1U) / 10U == 0U);
+        assert_se(0xfffffffaU / 10U + !!(0xfffffffaU % 10U) == 429496729U);
+
+        /* overflow test with rounded division */
+        assert_se(0xfffffffdU % 10U == 3U);
+        assert_se(0xfffffffdU / 10U == 429496729U);
+        assert_se(DIV_ROUND_UP(0xfffffffdU, 10U) == 429496730U);
+        assert_se((0xfffffffdU + 10U - 1U) / 10U == 0U);
+        assert_se(0xfffffffdU / 10U + !!(0xfffffffdU % 10U) == 429496730U);
+}
+
 static void test_first_word(void) {
         assert_se(first_word("Hello", ""));
         assert_se(first_word("Hello", "Hello"));
 static void test_first_word(void) {
         assert_se(first_word("Hello", ""));
         assert_se(first_word("Hello", "Hello"));
@@ -1357,6 +1390,7 @@ int main(int argc, char *argv[]) {
         test_max();
         test_container_of();
         test_alloca();
         test_max();
         test_container_of();
         test_alloca();
+        test_div_round_up();
         test_first_word();
         test_close_many();
         test_parse_boolean();
         test_first_word();
         test_close_many();
         test_parse_boolean();