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

index.cpp 1.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "test/common/index.h"
  2. #include "test/common/utils.h"
  3. namespace megdnn {
  4. namespace test {
  5. Index::Index(TensorLayout layout, size_t linear) : m_layout(layout), m_linear(linear) {
  6. linear_to_array();
  7. array_to_offset();
  8. }
  9. Index::Index(TensorLayout layout, TensorShape array)
  10. : m_layout(layout), m_array(array) {
  11. array_to_linear();
  12. array_to_offset();
  13. }
  14. void Index::linear_to_array() {
  15. auto linear = m_linear;
  16. auto& array = m_array;
  17. array.ndim = m_layout.ndim;
  18. for (size_t j = m_layout.ndim; j > 0; --j) {
  19. size_t i = j - 1;
  20. array[i] = linear % m_layout[i];
  21. linear /= m_layout[i];
  22. }
  23. megdnn_assert(linear == 0);
  24. }
  25. void Index::array_to_linear() {
  26. auto& linear = m_linear;
  27. megdnn_assert(m_array.ndim == m_layout.ndim);
  28. linear = 0;
  29. for (size_t i = 0; i < m_array.ndim; ++i) {
  30. megdnn_assert(m_array[i] < m_layout[i]);
  31. linear = linear * m_layout[i] + m_array[i];
  32. }
  33. }
  34. void Index::array_to_offset() {
  35. auto& offset = m_offset;
  36. megdnn_assert(m_array.ndim == m_layout.ndim);
  37. offset = 0;
  38. for (size_t i = 0; i < m_array.ndim; ++i) {
  39. megdnn_assert(m_array[i] < m_layout[i]);
  40. offset += m_array[i] * m_layout.stride[i];
  41. }
  42. }
  43. std::string Index::to_string() const {
  44. std::string res = "";
  45. res.append("{");
  46. res.append("array=");
  47. res.append(m_array.to_string());
  48. res.append(",linear=");
  49. res.append(std::to_string(m_linear));
  50. res.append(",offset=");
  51. res.append(std::to_string(m_offset));
  52. res.append("}");
  53. return res;
  54. }
  55. } // namespace test
  56. } // namespace megdnn
  57. // vim: syntax=cpp.doxygen