binary.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**************************************************************************/
  2. /*!
  3. @file binary.h
  4. @author hathach (tinyusb.org)
  5. @section LICENSE
  6. Software License Agreement (BSD License)
  7. Copyright (c) 2013, hathach (tinyusb.org)
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holders nor the
  17. names of its contributors may be used to endorse or promote products
  18. derived from this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  20. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  23. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
  26. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. This file is part of the tinyusb stack.
  30. */
  31. /**************************************************************************/
  32. /** \file
  33. * \brief TBD
  34. *
  35. * \note TBD
  36. */
  37. /** \ingroup TBD
  38. * \defgroup TBD
  39. * \brief TBD
  40. *
  41. * @{
  42. */
  43. #ifndef _TUSB_BINARY_H_
  44. #define _TUSB_BINARY_H_
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /// n-th Bit
  49. #define BIT_(n) (1 << (n))
  50. /// set n-th bit of x to 1
  51. #define BIT_SET_(x, n) ( (x) | BIT_(n) )
  52. /// clear n-th bit of x
  53. #define BIT_CLR_(x, n) ( (x) & (~BIT_(n)) )
  54. /// test n-th bit of x
  55. #define BIT_TEST_(x, n) ( (x) & BIT_(n) )
  56. #if defined(__GNUC__) && !defined(__CC_ARM)
  57. #define BIN8(x) ((uint8_t) (0b##x))
  58. #define BIN16(b1, b2) ((uint16_t) (0b##b1##b2))
  59. #define BIN32(b1, b2, b3, b4) ((uint32_t) (0b##b1##b2##b3##b4))
  60. #else
  61. // internal macro of B8, B16, B32
  62. #define _B8__(x) (((x&0x0000000FUL)?1:0) \
  63. +((x&0x000000F0UL)?2:0) \
  64. +((x&0x00000F00UL)?4:0) \
  65. +((x&0x0000F000UL)?8:0) \
  66. +((x&0x000F0000UL)?16:0) \
  67. +((x&0x00F00000UL)?32:0) \
  68. +((x&0x0F000000UL)?64:0) \
  69. +((x&0xF0000000UL)?128:0))
  70. #define BIN8(d) ((uint8_t) _B8__(0x##d##UL))
  71. #define BIN16(dmsb,dlsb) (((uint16_t)BIN8(dmsb)<<8) + BIN8(dlsb))
  72. #define BIN32(dmsb,db2,db3,dlsb) \
  73. (((uint32_t)BIN8(dmsb)<<24) \
  74. + ((uint32_t)BIN8(db2)<<16) \
  75. + ((uint32_t)BIN8(db3)<<8) \
  76. + BIN8(dlsb))
  77. #endif
  78. #ifdef __cplusplus
  79. }
  80. #endif
  81. #endif /* _TUSB_BINARY_H_ */
  82. /** @} */