summaryrefslogtreecommitdiff
path: root/cesar/lib
diff options
context:
space:
mode:
authordufour2010-04-13 15:03:42 +0000
committerdufour2010-04-13 15:03:42 +0000
commit00d554ff3f2bd8f7b047319c4c4a5f41bb5adc26 (patch)
tree605be17f3babe3ea1ff230bacef40da6a67675c0 /cesar/lib
parente9a4470f08d76441a647d76e2130ef363b274fb0 (diff)
cesar/lib: add a macro to compute the round of a division
git-svn-id: svn+ssh://pessac/svn/cesar/trunk@6921 017c9cb6-072f-447c-8318-d5b54f68fe89
Diffstat (limited to 'cesar/lib')
-rw-r--r--cesar/lib/test/utils/src/test_utils.c28
-rw-r--r--cesar/lib/utils.h9
2 files changed, 37 insertions, 0 deletions
diff --git a/cesar/lib/test/utils/src/test_utils.c b/cesar/lib/test/utils/src/test_utils.c
index 941d97c3fe..f7c9a6402f 100644
--- a/cesar/lib/test/utils/src/test_utils.c
+++ b/cesar/lib/test/utils/src/test_utils.c
@@ -192,6 +192,33 @@ ceil_test_case (test_t t)
}
void
+round_div_test_case (test_t t)
+{
+ test_case_begin (t, "round div");
+
+ test_begin (t, "verify")
+ {
+ const uint vector[][3] =
+ {
+ { 0, 42, 0 }, /* 0/42 => 0. */
+ { 1, 3, 0 }, /* 1/3 => 0. */
+ { 1, 2, 1 }, /* 1/2 => 1. */
+ { 9, 10, 1 }, /* 9/10 => 1. */
+ { 10, 10, 1 }, /* 10/10 => 1. */
+ { 11, 10, 1 }, /* 11/10 => 1. */
+ { 5, 10, 1 }, /* 5/10 => 1. */
+ { 5, 9, 1 }, /* 5/9 => 1. */
+ { 7, 4, 2 }, /* 7/4 => 2. */
+ { 42, 7, 6 }, /* 42/7 => 6. */
+ };
+ uint i;
+ for (i = 0; i < (COUNT (vector)); i++)
+ test_fail_if (ROUND_DIV (vector[i][0], vector[i][1])
+ != vector[i][2]);
+ } test_end;
+}
+
+void
utils_test_suite (test_t t)
{
test_suite_begin (t, "utils");
@@ -199,6 +226,7 @@ utils_test_suite (test_t t)
bf_test_case (t);
rot_test_case (t);
ceil_test_case (t);
+ round_div_test_case (t);
}
int
diff --git a/cesar/lib/utils.h b/cesar/lib/utils.h
index 97db8117b1..7a130b7855 100644
--- a/cesar/lib/utils.h
+++ b/cesar/lib/utils.h
@@ -87,6 +87,15 @@ lesseq_mod2p16 (u16 a, u16 b)
*/
#define CEIL_DIV(a, b) ( ((a) + (b) - 1) / (b) )
+/**
+ * Return the nearest integer (away from 0) of a divided by b (a/b).
+ * \param a the numerator (must be >= 0)
+ * \param b the divisor (must be > 0)
+ *
+ * \warning b is evaluated twice.
+ */
+#define ROUND_DIV(a, b) ( ((a) + (b) / 2) / (b) )
+
/** Exchange two value. */
#define XCH(a, b) do { \
typeof (b) _tmp = (a); \