From: batt Date: Wed, 7 Oct 2009 21:47:10 +0000 (+0000) Subject: Add REVERSE macro and its test unit. X-Git-Tag: 2.3.0~41 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=3869cb55efcc879e7df05b785ce9759cb59b5836;p=bertos.git Add REVERSE macro and its test unit. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3042 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/algo/reverse_test.c b/bertos/algo/reverse_test.c new file mode 100644 index 00000000..5377e1d2 --- /dev/null +++ b/bertos/algo/reverse_test.c @@ -0,0 +1,82 @@ +/** + * \file + * + * + * \brief REVERSE macro test. + * + * \version $Id$ + * \author Francesco Sacchi + */ + +#include +#include +#include + + +int reverse_testSetup(void) +{ + kdbg_init(); + return 0; +} + +int reverse_testTearDown(void) +{ + return 0; +} + +/** + * Naice reverse implementation. + */ +static uint8_t reverse(uint8_t b) +{ + uint8_t r = 0; + + for (int i = 0; i < 8; i++) + { + r <<= 1; + r |= (b & BV(i)) ? 1 : 0; + } + + return r; +} + +int reverse_testRun(void) +{ + for (int i = 0; i < 256; i++) + { + kprintf("i [%02X], REVERSE(i) [%02X], reverse(i) [%02X]\n", i, REVERSE(i), reverse(i)); + ASSERT(reverse(i) == REVERSE(i)); + } + return 0; +} + +TEST_MAIN(reverse); diff --git a/bertos/cfg/macros.h b/bertos/cfg/macros.h index 9e813968..0dad9d39 100644 --- a/bertos/cfg/macros.h +++ b/bertos/cfg/macros.h @@ -154,6 +154,12 @@ (b) = tmp; \ } while (0) +/** + * Reverse the bits contained in b (LSB becomes the MSB and so on). + * \note \a b is evaluated twice + */ +#define REVERSE(b) \ + ((uint8_t)((((b) * 0x0802UL & 0x22110UL) | ((b) * 0x8020UL & 0x88440UL)) * 0x10101UL >> 16)) #ifndef BV /** Convert a bit value to a binary flag. */