From 51b6c6ff41e17560ae054298e68762298d0f5d7c Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 12 Dec 2013 12:21:38 +0700 Subject: implement inline bit manipulation function --- tests/lpc18xx_43xx/test/test_binary.c | 126 ++++++++++++++++++++++++++++ tests/lpc18xx_43xx/test/test_binary_const.c | 90 -------------------- tinyusb/common/assertion.h | 6 -- tinyusb/common/binary.h | 58 +++++++++---- tinyusb/common/common.h | 8 +- tinyusb/common/primitive_types.h | 6 -- 6 files changed, 174 insertions(+), 120 deletions(-) create mode 100644 tests/lpc18xx_43xx/test/test_binary.c delete mode 100644 tests/lpc18xx_43xx/test/test_binary_const.c diff --git a/tests/lpc18xx_43xx/test/test_binary.c b/tests/lpc18xx_43xx/test/test_binary.c new file mode 100644 index 000000000..fb61df0fa --- /dev/null +++ b/tests/lpc18xx_43xx/test/test_binary.c @@ -0,0 +1,126 @@ +/**************************************************************************/ +/*! + @file test_binary.c + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +#include +#include "unity.h" +#include "common/binary.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_binary_8(void) +{ + TEST_ASSERT_EQUAL_HEX8(0x00, BIN8(00000000)); + TEST_ASSERT_EQUAL_HEX8(0x01, BIN8(00000001)); + TEST_ASSERT_EQUAL_HEX8(0x02, BIN8(00000010)); + TEST_ASSERT_EQUAL_HEX8(0x04, BIN8(00000100)); + TEST_ASSERT_EQUAL_HEX8(0x08, BIN8(00001000)); + TEST_ASSERT_EQUAL_HEX8(0x10, BIN8(00010000)); + TEST_ASSERT_EQUAL_HEX8(0x20, BIN8(00100000)); + TEST_ASSERT_EQUAL_HEX8(0x40, BIN8(01000000)); + TEST_ASSERT_EQUAL_HEX8(0x80, BIN8(10000000)); + + TEST_ASSERT_EQUAL_HEX8(0x0f, BIN8(00001111)); + TEST_ASSERT_EQUAL_HEX8(0xf0, BIN8(11110000)); + TEST_ASSERT_EQUAL_HEX8(0xff, BIN8(11111111)); +} + +void test_binary_16(void) +{ + TEST_ASSERT_EQUAL_HEX16(0x0000, BIN16(00000000, 00000000)); + TEST_ASSERT_EQUAL_HEX16(0x000f, BIN16(00000000, 00001111)); + TEST_ASSERT_EQUAL_HEX16(0x00f0, BIN16(00000000, 11110000)); + TEST_ASSERT_EQUAL_HEX16(0x0f00, BIN16(00001111, 00000000)); + TEST_ASSERT_EQUAL_HEX16(0xf000, BIN16(11110000, 00000000)); + TEST_ASSERT_EQUAL_HEX16(0xffff, BIN16(11111111, 11111111)); +} + +void test_binary_32(void) +{ + TEST_ASSERT_EQUAL_HEX32(0x00000000, BIN32(00000000, 00000000, 00000000, 00000000)); + TEST_ASSERT_EQUAL_HEX32(0x0000000f, BIN32(00000000, 00000000, 00000000, 00001111)); + TEST_ASSERT_EQUAL_HEX32(0x000000f0, BIN32(00000000, 00000000, 00000000, 11110000)); + TEST_ASSERT_EQUAL_HEX32(0x00000f00, BIN32(00000000, 00000000, 00001111, 00000000)); + TEST_ASSERT_EQUAL_HEX32(0x0000f000, BIN32(00000000, 00000000, 11110000, 00000000)); + TEST_ASSERT_EQUAL_HEX32(0x000f0000, BIN32(00000000, 00001111, 00000000, 00000000)); + TEST_ASSERT_EQUAL_HEX32(0x00f00000, BIN32(00000000, 11110000, 00000000, 00000000)); + TEST_ASSERT_EQUAL_HEX32(0x0f000000, BIN32(00001111, 00000000, 00000000, 00000000)); + TEST_ASSERT_EQUAL_HEX32(0xf0000000, BIN32(11110000, 00000000, 00000000, 00000000)); + TEST_ASSERT_EQUAL_HEX32(0xffffffff, BIN32(11111111, 11111111, 11111111, 11111111)); +} + +void test_bit_set(void) +{ + TEST_ASSERT_EQUAL_HEX32( BIN8(00001101), bit_set( BIN8(00001001), 2)); + TEST_ASSERT_EQUAL_HEX32( BIN8(10001101), bit_set( BIN8(00001101), 7)); +} + +void test_bit_clear(void) +{ + TEST_ASSERT_EQUAL_HEX32( BIN8(00001001), bit_clear( BIN8(00001101), 2)); + TEST_ASSERT_EQUAL_HEX32( BIN8(00001101), bit_clear( BIN8(10001101), 7)); +} + +void test_bit_mask(void) +{ + TEST_ASSERT_EQUAL_HEX32(0x0000ffff, bit_mask(16)); + TEST_ASSERT_EQUAL_HEX32(0x00ffffff, bit_mask(24)); + TEST_ASSERT_EQUAL_HEX32(0xffffffff, bit_mask(32)); +} + +void test_bit_range(void) +{ + TEST_ASSERT_EQUAL_HEX32(BIN8(00001111), bit_mask_range(0, 3)); + TEST_ASSERT_EQUAL_HEX32(BIN8(01100000), bit_mask_range(5, 6)); + + TEST_ASSERT_EQUAL_HEX32(BIN16(00001111, 00000000), bit_mask_range(8, 11)); + TEST_ASSERT_EQUAL_HEX32(0xf0000000, bit_mask_range(28, 31)); +} + +void test_bit_set_range(void) +{ + TEST_ASSERT_EQUAL_HEX32(BIN8(01011001), bit_set_range(BIN8(00001001), 4, 6, BIN8(101))); + + TEST_ASSERT_EQUAL_HEX32(BIN32(11001011, 10100000, 00000000, 00001001), + bit_set_range(BIN8(00001001), 21, 31, BIN16(110, 01011101))); +} diff --git a/tests/lpc18xx_43xx/test/test_binary_const.c b/tests/lpc18xx_43xx/test/test_binary_const.c deleted file mode 100644 index 5d69c994f..000000000 --- a/tests/lpc18xx_43xx/test/test_binary_const.c +++ /dev/null @@ -1,90 +0,0 @@ -/**************************************************************************/ -/*! - @file test_binary_const.c - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2013, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -#include -#include "unity.h" -#include "common/binary.h" - -void setUp(void) -{ -} - -void tearDown(void) -{ -} - -void test_binary_8() -{ - TEST_ASSERT_EQUAL_HEX8(0x00, BIN8(00000000)); - TEST_ASSERT_EQUAL_HEX8(0x01, BIN8(00000001)); - TEST_ASSERT_EQUAL_HEX8(0x02, BIN8(00000010)); - TEST_ASSERT_EQUAL_HEX8(0x04, BIN8(00000100)); - TEST_ASSERT_EQUAL_HEX8(0x08, BIN8(00001000)); - TEST_ASSERT_EQUAL_HEX8(0x10, BIN8(00010000)); - TEST_ASSERT_EQUAL_HEX8(0x20, BIN8(00100000)); - TEST_ASSERT_EQUAL_HEX8(0x40, BIN8(01000000)); - TEST_ASSERT_EQUAL_HEX8(0x80, BIN8(10000000)); - - TEST_ASSERT_EQUAL_HEX8(0x0f, BIN8(00001111)); - TEST_ASSERT_EQUAL_HEX8(0xf0, BIN8(11110000)); - TEST_ASSERT_EQUAL_HEX8(0xff, BIN8(11111111)); -} - -void test_binary_16() -{ - TEST_ASSERT_EQUAL_HEX16(0x0000, BIN16(00000000, 00000000)); - TEST_ASSERT_EQUAL_HEX16(0x000f, BIN16(00000000, 00001111)); - TEST_ASSERT_EQUAL_HEX16(0x00f0, BIN16(00000000, 11110000)); - TEST_ASSERT_EQUAL_HEX16(0x0f00, BIN16(00001111, 00000000)); - TEST_ASSERT_EQUAL_HEX16(0xf000, BIN16(11110000, 00000000)); - TEST_ASSERT_EQUAL_HEX16(0xffff, BIN16(11111111, 11111111)); -} - -void test_binary_32() -{ - TEST_ASSERT_EQUAL_HEX32(0x00000000, BIN32(00000000, 00000000, 00000000, 00000000)); - TEST_ASSERT_EQUAL_HEX32(0x0000000f, BIN32(00000000, 00000000, 00000000, 00001111)); - TEST_ASSERT_EQUAL_HEX32(0x000000f0, BIN32(00000000, 00000000, 00000000, 11110000)); - TEST_ASSERT_EQUAL_HEX32(0x00000f00, BIN32(00000000, 00000000, 00001111, 00000000)); - TEST_ASSERT_EQUAL_HEX32(0x0000f000, BIN32(00000000, 00000000, 11110000, 00000000)); - TEST_ASSERT_EQUAL_HEX32(0x000f0000, BIN32(00000000, 00001111, 00000000, 00000000)); - TEST_ASSERT_EQUAL_HEX32(0x00f00000, BIN32(00000000, 11110000, 00000000, 00000000)); - TEST_ASSERT_EQUAL_HEX32(0x0f000000, BIN32(00001111, 00000000, 00000000, 00000000)); - TEST_ASSERT_EQUAL_HEX32(0xf0000000, BIN32(11110000, 00000000, 00000000, 00000000)); - TEST_ASSERT_EQUAL_HEX32(0xffffffff, BIN32(11111111, 11111111, 11111111, 11111111)); -} diff --git a/tinyusb/common/assertion.h b/tinyusb/common/assertion.h index 7d8b3fcba..91d10632b 100644 --- a/tinyusb/common/assertion.h +++ b/tinyusb/common/assertion.h @@ -36,12 +36,6 @@ */ /**************************************************************************/ -/** \file - * \brief TBD - * - * \note TBD - */ - /** \ingroup TBD * \defgroup TBD * \brief TBD diff --git a/tinyusb/common/binary.h b/tinyusb/common/binary.h index 0badd6b3b..d704f93b8 100644 --- a/tinyusb/common/binary.h +++ b/tinyusb/common/binary.h @@ -36,12 +36,6 @@ */ /**************************************************************************/ -/** \file - * \brief TBD - * - * \note TBD - */ - /** \ingroup TBD * \defgroup TBD * \brief TBD @@ -56,18 +50,54 @@ extern "C" { #endif -/// n-th Bit -#define BIT_(n) (1U << (n)) +#include "primitive_types.h" +#include "compiler/compiler.h" -/// set n-th bit of x to 1 -#define BIT_SET_(x, n) ( (x) | BIT_(n) ) +//------------- Bit manipulation -------------// +#define BIT_(n) (1U << (n)) ///< n-th Bit +#define BIT_SET_(x, n) ( (x) | BIT_(n) ) ///< set n-th bit of x to 1 +#define BIT_CLR_(x, n) ( (x) & (~BIT_(n)) ) ///< clear n-th bit of x +#define BIT_TEST_(x, n) ( ((x) & BIT_(n)) ? true : false ) ///< check if n-th bit of x is 1 + +static inline uint32_t bit_set(uint32_t value, uint8_t n) ATTR_CONST ATTR_ALWAYS_INLINE; +static inline uint32_t bit_set(uint32_t value, uint8_t n) +{ + return value | BIT_(n); +} + +static inline uint32_t bit_clear(uint32_t value, uint8_t n) ATTR_CONST ATTR_ALWAYS_INLINE; +static inline uint32_t bit_clear(uint32_t value, uint8_t n) +{ + return value & (~BIT_(n)); +} -/// clear n-th bit of x -#define BIT_CLR_(x, n) ( (x) & (~BIT_(n)) ) +static inline bool bit_test(uint32_t value, uint8_t n) ATTR_CONST ATTR_ALWAYS_INLINE; +static inline bool bit_test(uint32_t value, uint8_t n) +{ + return (value & BIT_(n)) ? true : false; +} + +///< create a mask with n-bit lsb set to 1 +static inline uint32_t bit_mask(uint8_t n) ATTR_CONST ATTR_ALWAYS_INLINE; +static inline uint32_t bit_mask(uint8_t n) +{ + return (n < 32) ? ( BIT_(n) - 1 ) : UINT32_MAX; +} + +static inline uint32_t bit_mask_range(uint8_t start, uint32_t end) ATTR_CONST ATTR_ALWAYS_INLINE; +static inline uint32_t bit_mask_range(uint8_t start, uint32_t end) +{ + return bit_mask(end+1) & ~ bit_mask(start); +} + +static inline uint32_t bit_set_range(uint32_t value, uint8_t start, uint8_t end, uint32_t pattern) ATTR_CONST ATTR_ALWAYS_INLINE; +static inline uint32_t bit_set_range(uint32_t value, uint8_t start, uint8_t end, uint32_t pattern) +{ + return ( value & ~bit_mask_range(start, end) ) | (pattern << start); +} -/// test n-th bit of x -#define BIT_TEST_(x, n) ( ((x) & BIT_(n)) ? true : false ) +//------------- Binary Constant -------------// #if defined(__GNUC__) && !defined(__CC_ARM) #define BIN8(x) ((uint8_t) (0b##x)) diff --git a/tinyusb/common/common.h b/tinyusb/common/common.h index 2cd29417f..399b419b9 100644 --- a/tinyusb/common/common.h +++ b/tinyusb/common/common.h @@ -89,10 +89,10 @@ //--------------------------------------------------------------------+ // MACROS //--------------------------------------------------------------------+ -#define STRING_(x) #x // stringify without expand -#define XSTRING_(x) STRING_(x) // expand then stringify -#define STRING_CONCAT_(a, b) a##b // concat without expand -#define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) // expand then concat +#define STRING_(x) #x ///< stringify without expand +#define XSTRING_(x) STRING_(x) ///< expand then stringify +#define STRING_CONCAT_(a, b) a##b ///< concat without expand +#define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) ///< expand then concat #define MAX_OF(a, b) ( (a) > (b) ? (a) : (b) ) #define MIN_OF(a, b) ( (a) < (b) ? (a) : (b) ) diff --git a/tinyusb/common/primitive_types.h b/tinyusb/common/primitive_types.h index fe761d163..0f2fe733e 100644 --- a/tinyusb/common/primitive_types.h +++ b/tinyusb/common/primitive_types.h @@ -36,12 +36,6 @@ */ /**************************************************************************/ -/** \file - * \brief TBD - * - * \note TBD - */ - /** \ingroup TBD * \defgroup TBD * \brief TBD -- cgit v1.3.1