From 1a59e72ca1f2e073e5731dd446f51f7af9adb341 Mon Sep 17 00:00:00 2001 From: Jérémy Dufour Date: Wed, 31 Aug 2011 14:47:59 +0200 Subject: cesar/lib: add function to compute fixed round for 64 bits, closes #2788 --- cesar/lib/fixed.h | 14 ++++++++++++++ cesar/lib/test/fixed/src/test_fixed.c | 13 +++++++++++++ 2 files changed, 27 insertions(+) (limited to 'cesar') 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 @@ -56,6 +56,20 @@ fixed_round (s32 x, uint shift) return (x + (1 << (shift - 1))) >> 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 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 -- cgit v1.2.3