kernel_utils.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright 2019-2020 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #pragma once
  16. #include <algorithm>
  17. #include <cassert>
  18. #include <cstddef>
  19. #include <datatypes.h>
  20. #ifdef __GNUC__
  21. #define CXX_RESTRICT __restrict__
  22. #elif _MSC_VER
  23. #define CXX_RESTRICT __restrict
  24. #else
  25. #define CXX_RESTRICT fvck
  26. #endif
  27. namespace nncase
  28. {
  29. namespace kernels
  30. {
  31. inline size_t offset(const runtime_shape_t &shape, const runtime_shape_t &index)
  32. {
  33. return (((size_t)index[0] * shape[1] + index[1]) * shape[2] + index[2]) * shape[3] + index[3];
  34. }
  35. namespace details
  36. {
  37. inline int32_t get_windowed_output_size(int32_t size, int32_t filter, int32_t stride, int32_t dilation, const padding &padding)
  38. {
  39. auto effective_filter_size = (filter - 1) * dilation + 1;
  40. return (size + padding.before + padding.after - effective_filter_size + stride) / stride;
  41. }
  42. inline size_t compute_size(const runtime_shape_t &shape)
  43. {
  44. return size_t(shape[0]) * shape[1] * shape[2] * shape[3];
  45. }
  46. template <class T>
  47. inline T apply_activation(T value, value_range<T> activation)
  48. {
  49. return std::clamp(value, activation.min, activation.max);
  50. }
  51. inline runtime_shape_t get_reduced_offset(const runtime_shape_t &in_offset, const runtime_shape_t &reduced_shape)
  52. {
  53. runtime_shape_t off;
  54. for (size_t i = 0; i < in_offset.size(); i++)
  55. {
  56. if (in_offset[i] >= reduced_shape[i])
  57. off[i] = 0;
  58. else
  59. off[i] = in_offset[i];
  60. }
  61. return off;
  62. }
  63. template <class T, class TRange>
  64. struct default_ptr_getter
  65. {
  66. T *operator()(const TRange &range) const noexcept { return range; }
  67. };
  68. template <int32_t Bits>
  69. int32_t to_signed(uint32_t value)
  70. {
  71. auto mask = uint32_t(1) << (Bits - 1);
  72. if (Bits != 32 && (value & mask) != 0)
  73. {
  74. auto sign = 0xFFFFFFFF << Bits;
  75. return (int)(value | sign);
  76. }
  77. return (int32_t)value;
  78. }
  79. template <int32_t Bits>
  80. int64_t to_signed(uint64_t value)
  81. {
  82. auto mask = uint64_t(1) << (Bits - 1);
  83. if ((value & mask) != 0)
  84. {
  85. auto sign = 0xFFFFFFFFFFFFFFFF << Bits;
  86. return (int64_t)(value | sign);
  87. }
  88. return (int64_t)value;
  89. }
  90. }
  91. }
  92. }