summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordufour2009-11-24 10:28:47 +0000
committerdufour2009-11-24 10:28:47 +0000
commit99710e2601264da812c5123d53726626d1f4bf8f (patch)
tree1a251efe64cf58aea482c6442a08c3dd8cbcc124
parent7067a37a0677d9ac157cb7e983d5f9c12538b9bb (diff)
cesar/lib: add a CEIL_DIV macro
git-svn-id: svn+ssh://pessac/svn/cesar/trunk@6471 017c9cb6-072f-447c-8318-d5b54f68fe89
-rw-r--r--cesar/lib/test/utils/src/test_utils.c29
-rw-r--r--cesar/lib/utils.h14
2 files changed, 43 insertions, 0 deletions
diff --git a/cesar/lib/test/utils/src/test_utils.c b/cesar/lib/test/utils/src/test_utils.c
index 6a14efc017..941d97c3fe 100644
--- a/cesar/lib/test/utils/src/test_utils.c
+++ b/cesar/lib/test/utils/src/test_utils.c
@@ -164,12 +164,41 @@ rot_test_case (test_t t)
}
void
+ceil_test_case (test_t t)
+{
+ test_case_begin (t, "ceil");
+
+ test_begin (t, "verify")
+ {
+ uint i, j;
+ for (i = 0; i < 42; i++)
+ {
+ for (j = 1; j < 24; j++)
+ {
+ if (i % j)
+ test_fail_if (CEIL_DIV (i, j) != i / j + 1);
+ else
+ test_fail_if (CEIL_DIV (i, j) != i / j);
+ }
+ }
+ } test_end;
+
+ test_begin (t, "table declaration")
+ {
+ test_fail_if (CEIL_DIV (1, 1) != 1);
+ static uint i[CEIL_DIV (1, 1)];
+ i[0] = 1;
+ } test_end;
+}
+
+void
utils_test_suite (test_t t)
{
test_suite_begin (t, "utils");
bits_test_case (t);
bf_test_case (t);
rot_test_case (t);
+ ceil_test_case (t);
}
int
diff --git a/cesar/lib/utils.h b/cesar/lib/utils.h
index e649d37e35..98000d2b46 100644
--- a/cesar/lib/utils.h
+++ b/cesar/lib/utils.h
@@ -73,6 +73,20 @@ lesseq_mod2p16 (u16 a, u16 b)
#define MIN(a, b) ({ typeof (a) _a = (a); typeof (b) _b = (b); \
_a < _b ? _a : _b; })
+/**
+ * Return the upper rounded integer value of a divided by b (a/b).
+ * \param a the numerator
+ * \param b the divisor
+ *
+ * \warning b is evaluated twice. You need to be careful if you do:
+ * \code
+ * uint b = 1;
+ * uint a = CEIL_DIV (1, b++);
+ * // a = (1 + b++ - 1) / b++;
+ * \endcode
+ */
+#define CEIL_DIV(a, b) ( ((a) + (b) - 1) / (b) )
+
/** Exchange two value. */
#define XCH(a, b) do { \
typeof (b) _tmp = (a); \