binary.h 2.8 KB

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