summaryrefslogtreecommitdiff
path: root/cesar
diff options
context:
space:
mode:
authorJérémy Dufour2011-08-31 14:47:59 +0200
committerJérémy Dufour2011-11-17 14:01:52 +0100
commit1a59e72ca1f2e073e5731dd446f51f7af9adb341 (patch)
tree6463f171d38f926d113abb7f154782f1642eed93 /cesar
parent58308b0bf6b3f3d9153ccb9b232f0bb61a2a9abf (diff)
cesar/lib: add function to compute fixed round for 64 bits, closes #2788
Diffstat (limited to 'cesar')
-rw-r--r--cesar/lib/fixed.h14
-rw-r--r--cesar/lib/test/fixed/src/test_fixed.c13
2 files changed, 27 insertions, 0 deletions
diff --git a/cesar/lib/fixed.h b/cesar/lib/fixed.h
index 6b9e598768..31e199ba2c 100644
--- a/cesar/lib/fixed.h
+++ b/cesar/lib/fixed.h
@@ -57,6 +57,20 @@ fixed_round (s32 x, uint shift)
}
/**
+ * Reduce fixed number precision, round to nearest, 64 bits version.
+ * \param x number to round
+ * \param shift precision reduction (1 to 63)
+ * \return number rounded
+ *
+ * Rounds to nearest, halfway up.
+ */
+extern inline s64
+fixed_round_64 (s64 x, uint shift)
+{
+ return (x + (1ll << (shift - 1))) >> shift;
+}
+
+/**
* Fixed point product.
* \param a first operand
* \param b second operand
diff --git a/cesar/lib/test/fixed/src/test_fixed.c b/cesar/lib/test/fixed/src/test_fixed.c
index 15fe7ca3e0..01ed500cd8 100644
--- a/cesar/lib/test/fixed/src/test_fixed.c
+++ b/cesar/lib/test/fixed/src/test_fixed.c
@@ -159,6 +159,19 @@ fixed_basic_test_case (test_t t)
test_fail_unless (fixed_round (-0x18000, 16) == -0x1);
test_fail_unless (fixed_round (-0x1ffff, 16) == -0x2);
} test_end;
+ test_begin (t, "round 64")
+ {
+ test_fail_unless (fixed_round_64 (0x1000000000000ull, 48) == 0x1);
+ test_fail_unless (fixed_round_64 (0x1000000000001ull, 48) == 0x1);
+ test_fail_unless (fixed_round_64 (0x17fffffffffffull, 48) == 0x1);
+ test_fail_unless (fixed_round_64 (0x1800000000000ull, 48) == 0x2);
+ test_fail_unless (fixed_round_64 (0x1ffffffffffffull, 48) == 0x2);
+ test_fail_unless (fixed_round_64 (-0x1000000000000ll, 48) == -0x1);
+ test_fail_unless (fixed_round_64 (-0x1000000000001ll, 48) == -0x1);
+ test_fail_unless (fixed_round_64 (-0x17fffffffffffll, 48) == -0x1);
+ test_fail_unless (fixed_round_64 (-0x1800000000000ll, 48) == -0x1);
+ test_fail_unless (fixed_round_64 (-0x1ffffffffffffll, 48) == -0x2);
+ } test_end;
}
bool