A SystemC productivity library for virtual platform development utilizing SCV and TLM2.0 https://www.minres.com/#opensource

ities.h 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*******************************************************************************
  2. * Copyright 2017, 2018 MINRES Technologies GmbH
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *******************************************************************************/
  16. #ifndef _UTIL_ITIES_H_
  17. #define _UTIL_ITIES_H_
  18. #include <array>
  19. #include <bitset>
  20. #include <type_traits>
  21. #include <vector>
  22. // some helper functions
  23. template <unsigned int bit, unsigned int width, typename T> inline constexpr T bit_sub(T v) {
  24. return (v >> bit) & ((T(1) << width) - 1);
  25. }
  26. #if __cplusplus < 201402L
  27. template <typename T, unsigned B> inline T signextend(const T x) {
  28. #else
  29. template <typename T, unsigned B> inline constexpr T signextend(const typename std::make_unsigned<T>::type x) {
  30. #endif
  31. struct X {
  32. T x : B;
  33. X(T x_)
  34. : x(x_) {}
  35. } s(x);
  36. return s.x;
  37. }
  38. // according to http://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend
  39. template <unsigned int bit, unsigned int width, typename T>
  40. inline constexpr typename std::make_signed<T>::type signed_bit_sub(T v) {
  41. #if __cplusplus < 201402L
  42. return ((v << (sizeof(T) * 8 - bit - width)) >> (sizeof(T) * 8 - width));
  43. #else
  44. typename std::make_signed<T>::type r = v << (sizeof(T) * 8 - bit - width);
  45. typename std::make_signed<T>::type ret = (r >> (sizeof(T) * 8 - width));
  46. return ret;
  47. #endif
  48. }
  49. namespace util {
  50. // according to
  51. // http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup
  52. static std::array<const int, 32> MultiplyDeBruijnBitPosition = {{0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20,
  53. 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19,
  54. 16, 7, 26, 12, 18, 6, 11, 5, 10, 9}};
  55. template <size_t N> constexpr size_t find_first(std::bitset<N> &bits) {
  56. static_assert(N <= 32, "find_first only supports bitsets smaller than 33");
  57. return MultiplyDeBruijnBitPosition[((uint32_t)((bits.to_ulong() & -bits.to_ulong()) * 0x077CB531U)) >> 27];
  58. }
  59. // according to
  60. // https://stackoverflow.com/questions/8871204/count-number-of-1s-in-binary-representation
  61. #if __cplusplus < 201402L
  62. constexpr size_t uCount(uint32_t u) { return u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111); }
  63. constexpr size_t bit_count(uint32_t u) { return ((uCount(u) + (uCount(u) >> 3)) & 030707070707) % 63; }
  64. #else
  65. constexpr size_t bit_count(uint32_t u) {
  66. size_t uCount = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111);
  67. return ((uCount + (uCount >> 3)) & 030707070707) % 63;
  68. }
  69. #endif
  70. /**
  71. * split a given string using specified separator
  72. *
  73. * @param s the string to split
  74. * @param separator the separator char
  75. * @return vector of splitted strings
  76. */
  77. inline std::vector<std::string> split(const std::string &s, char seperator) {
  78. std::vector<std::string> output;
  79. std::string::size_type prev_pos = 0, pos = 0;
  80. while ((pos = s.find(seperator, pos)) != std::string::npos) {
  81. std::string substring(s.substr(prev_pos, pos - prev_pos));
  82. output.push_back(substring);
  83. prev_pos = ++pos;
  84. }
  85. output.push_back(s.substr(prev_pos, pos - prev_pos)); // Last word
  86. return output;
  87. }
  88. }
  89. #endif /* _UTIL_ITIES_H_ */