You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.1KB

  1. // Copyright 2012 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. #ifndef STMLIB_STMLIB_H_
  25. #define STMLIB_STMLIB_H_
  26. #include <inttypes.h>
  27. #include <stddef.h>
  28. #ifndef NULL
  29. #define NULL 0
  30. #endif
  31. #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
  32. TypeName(const TypeName&); \
  33. void operator=(const TypeName&)
  34. #define CLIP(x) if (x < -32767) x = -32767; if (x > 32767) x = 32767;
  35. #define CONSTRAIN(var, min, max) \
  36. if (var < (min)) { \
  37. var = (min); \
  38. } else if (var > (max)) { \
  39. var = (max); \
  40. }
  41. #define JOIN(lhs, rhs) JOIN_1(lhs, rhs)
  42. #define JOIN_1(lhs, rhs) JOIN_2(lhs, rhs)
  43. #define JOIN_2(lhs, rhs) lhs##rhs
  44. #define STATIC_ASSERT(expression, message)\
  45. struct JOIN(__static_assertion_at_line_, __LINE__)\
  46. {\
  47. impl::StaticAssertion<static_cast<bool>((expression))> JOIN(JOIN(JOIN(STATIC_ASSERTION_FAILED_AT_LINE_, __LINE__), _), message);\
  48. };\
  49. typedef impl::StaticAssertionTest<sizeof(JOIN(__static_assertion_at_line_, __LINE__))> JOIN(__static_assertion_test_at_line_, __LINE__)
  50. namespace impl {
  51. template <bool>
  52. struct StaticAssertion;
  53. template <>
  54. struct StaticAssertion<true>
  55. {
  56. }; // StaticAssertion<true>
  57. template<int i>
  58. struct StaticAssertionTest
  59. {
  60. }; // StaticAssertionTest<int>
  61. } // namespace impl
  62. #ifndef TEST
  63. #define IN_RAM __attribute__ ((section (".ramtext")))
  64. #else
  65. #define IN_RAM
  66. #endif // TEST
  67. #define UNROLL2(x) x; x;
  68. #define UNROLL4(x) x; x; x; x;
  69. #define UNROLL8(x) x; x; x; x; x; x; x; x;
  70. template<bool b>
  71. inline void StaticAssertImplementation() {
  72. char static_assert_size_mismatch[b] = { 0 };
  73. }
  74. namespace stmlib {
  75. typedef union {
  76. uint16_t value;
  77. uint8_t bytes[2];
  78. } Word;
  79. typedef union {
  80. uint32_t value;
  81. uint16_t words[2];
  82. uint8_t bytes[4];
  83. } LongWord;
  84. template<uint32_t a, uint32_t b, uint32_t c, uint32_t d>
  85. struct FourCC {
  86. static const uint32_t value = (((((d << 8) | c) << 8) | b) << 8) | a;
  87. };
  88. } // namespace stmlib
  89. #endif // STMLIB_STMLIB_H_