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.

convert_utils_base.h 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  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 MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_BASE_H_
  17. #define MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_BASE_H_
  18. #include <limits>
  19. #include <memory>
  20. #include "utils/log_adapter.h"
  21. namespace mindspore {
  22. inline int SizeToInt(size_t u) {
  23. if (u > static_cast<size_t>((std::numeric_limits<int>::max)())) {
  24. MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of int.";
  25. }
  26. return static_cast<int>(u);
  27. }
  28. inline uint32_t SizeToUint(size_t u) {
  29. if (u > static_cast<size_t>((std::numeric_limits<uint32_t>::max)())) {
  30. MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of uint32_t.";
  31. }
  32. return static_cast<uint32_t>(u);
  33. }
  34. inline int64_t SizeToLong(size_t u) {
  35. if (u > static_cast<size_t>((std::numeric_limits<int64_t>::max)())) {
  36. MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of int64_t.";
  37. }
  38. return static_cast<int64_t>(u);
  39. }
  40. inline size_t IntToSize(int u) {
  41. if (u < 0) {
  42. MS_LOG(EXCEPTION) << "The int value(" << u << ") is less than 0.";
  43. }
  44. return static_cast<size_t>(u);
  45. }
  46. inline size_t LongToSize(int64_t u) {
  47. if (u < 0) {
  48. MS_LOG(EXCEPTION) << "The int64_t value(" << u << ") is less than 0.";
  49. }
  50. return static_cast<size_t>(u);
  51. }
  52. inline size_t FloatToSize(float u) {
  53. if (u < 0) {
  54. MS_LOG(EXCEPTION) << "The float value(" << u << ") is less than 0.";
  55. }
  56. if (u > static_cast<float>((std::numeric_limits<size_t>::max)())) {
  57. MS_LOG(EXCEPTION) << "The float value(" << u << ") exceeds the maximum value of size_t.";
  58. }
  59. return static_cast<size_t>(u);
  60. }
  61. inline float IntToFloat(int32_t v) { return static_cast<float>(v); }
  62. inline uint32_t IntToUint(int32_t u) {
  63. if (u < 0) {
  64. MS_LOG(EXCEPTION) << "The int32_t value(" << u << ") is less than 0.";
  65. }
  66. return static_cast<uint32_t>(u);
  67. }
  68. inline int32_t UintToInt(uint32_t u) {
  69. if (u > static_cast<uint32_t>((std::numeric_limits<int32_t>::max)())) {
  70. MS_LOG(EXCEPTION) << "The uint32_t value(" << u << ") exceeds the maximum value of int32_t.";
  71. }
  72. return static_cast<int32_t>(u);
  73. }
  74. inline unsigned int UlongToUint(size_t u) {
  75. if (u > static_cast<size_t>((std::numeric_limits<unsigned int>::max)())) {
  76. MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of unsigned int.";
  77. }
  78. return static_cast<unsigned int>(u);
  79. }
  80. inline void IntMulWithOverflowCheck(int a, int b, int *c) {
  81. int out = a * b;
  82. if (a != 0) {
  83. bool ok = ((out / a) != b);
  84. if (ok) {
  85. MS_LOG(EXCEPTION) << "Mul: a(" << a << ") * b(" << b << ") result is overflow";
  86. }
  87. }
  88. *c = out;
  89. }
  90. inline size_t SizetMulWithOverflowCheck(size_t a, size_t b) {
  91. size_t out = a * b;
  92. if (a != 0) {
  93. if ((out / a) != b) {
  94. MS_LOG(EXCEPTION) << "Mul: a(" << a << ") * b(" << b << ") result is overflow";
  95. }
  96. }
  97. return out;
  98. }
  99. inline uint8_t *AddressOffset(void *address, size_t offset) {
  100. MS_EXCEPTION_IF_NULL(address);
  101. return static_cast<uint8_t *>(address) + offset;
  102. }
  103. } // namespace mindspore
  104. #endif // MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_BASE_H_