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.

159 lines
6.0 KiB

  1. // base/kaldi-utils.h
  2. // Copyright 2009-2011 Ondrej Glembek; Microsoft Corporation;
  3. // Saarland University; Karel Vesely; Yanmin Qian
  4. // See ../../COPYING for clarification regarding multiple authors
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  14. // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  15. // MERCHANTABLITY OR NON-INFRINGEMENT.
  16. // See the Apache 2 License for the specific language governing permissions and
  17. // limitations under the License.
  18. #ifndef KALDI_BASE_KALDI_UTILS_H_
  19. #define KALDI_BASE_KALDI_UTILS_H_ 1
  20. #if defined(_MSC_VER)
  21. #define WIN32_LEAN_AND_MEAN
  22. #define NOMINMAX
  23. #include <windows.h>
  24. #endif
  25. #ifdef _MSC_VER
  26. #include <stdio.h>
  27. #define unlink _unlink
  28. #else
  29. #include <unistd.h>
  30. #endif
  31. #include <limits>
  32. #include <string>
  33. #if defined(_MSC_VER)
  34. #pragma warning(disable : 4244 4056 4305 4800 4267 4996 4756 4661)
  35. #if _MSC_VER < 1400
  36. #define __restrict__
  37. #else
  38. #define __restrict__ __restrict
  39. #endif
  40. #endif
  41. #if defined(_MSC_VER)
  42. #define KALDI_MEMALIGN(align, size, pp_orig) \
  43. (*(pp_orig) = _aligned_malloc(size, align))
  44. #define KALDI_MEMALIGN_FREE(x) _aligned_free(x)
  45. #elif defined(__CYGWIN__)
  46. #define KALDI_MEMALIGN(align, size, pp_orig) \
  47. (*(pp_orig) = aligned_alloc(align, size))
  48. #define KALDI_MEMALIGN_FREE(x) free(x)
  49. #else
  50. #define KALDI_MEMALIGN(align, size, pp_orig) \
  51. (!posix_memalign(pp_orig, align, size) ? *(pp_orig) : NULL)
  52. #define KALDI_MEMALIGN_FREE(x) free(x)
  53. #endif
  54. #ifdef __ICC
  55. #pragma warning(disable : 383) // ICPC remark we don't want.
  56. #pragma warning(disable : 810) // ICPC remark we don't want.
  57. #pragma warning(disable : 981) // ICPC remark we don't want.
  58. #pragma warning(disable : 1418) // ICPC remark we don't want.
  59. #pragma warning(disable : 444) // ICPC remark we don't want.
  60. #pragma warning(disable : 869) // ICPC remark we don't want.
  61. #pragma warning(disable : 1287) // ICPC remark we don't want.
  62. #pragma warning(disable : 279) // ICPC remark we don't want.
  63. #pragma warning(disable : 981) // ICPC remark we don't want.
  64. #endif
  65. namespace kaldi {
  66. // CharToString prints the character in a human-readable form, for debugging.
  67. std::string CharToString(const char& c);
  68. inline int MachineIsLittleEndian() {
  69. int check = 1;
  70. return (*reinterpret_cast<char*>(&check) != 0);
  71. }
  72. // This function kaldi::Sleep() provides a portable way
  73. // to sleep for a possibly fractional
  74. // number of seconds. On Windows it's only accurate to microseconds.
  75. void Sleep(float seconds);
  76. } // namespace kaldi
  77. #define KALDI_SWAP8(a) \
  78. do { \
  79. int t = (reinterpret_cast<char*>(&a))[0]; \
  80. (reinterpret_cast<char*>(&a))[0] = (reinterpret_cast<char*>(&a))[7]; \
  81. (reinterpret_cast<char*>(&a))[7] = t; \
  82. t = (reinterpret_cast<char*>(&a))[1]; \
  83. (reinterpret_cast<char*>(&a))[1] = (reinterpret_cast<char*>(&a))[6]; \
  84. (reinterpret_cast<char*>(&a))[6] = t; \
  85. t = (reinterpret_cast<char*>(&a))[2]; \
  86. (reinterpret_cast<char*>(&a))[2] = (reinterpret_cast<char*>(&a))[5]; \
  87. (reinterpret_cast<char*>(&a))[5] = t; \
  88. t = (reinterpret_cast<char*>(&a))[3]; \
  89. (reinterpret_cast<char*>(&a))[3] = (reinterpret_cast<char*>(&a))[4]; \
  90. (reinterpret_cast<char*>(&a))[4] = t; \
  91. } while (0)
  92. #define KALDI_SWAP4(a) \
  93. do { \
  94. int t = (reinterpret_cast<char*>(&a))[0]; \
  95. (reinterpret_cast<char*>(&a))[0] = (reinterpret_cast<char*>(&a))[3]; \
  96. (reinterpret_cast<char*>(&a))[3] = t; \
  97. t = (reinterpret_cast<char*>(&a))[1]; \
  98. (reinterpret_cast<char*>(&a))[1] = (reinterpret_cast<char*>(&a))[2]; \
  99. (reinterpret_cast<char*>(&a))[2] = t; \
  100. } while (0)
  101. #define KALDI_SWAP2(a) \
  102. do { \
  103. int t = (reinterpret_cast<char*>(&a))[0]; \
  104. (reinterpret_cast<char*>(&a))[0] = (reinterpret_cast<char*>(&a))[1]; \
  105. (reinterpret_cast<char*>(&a))[1] = t; \
  106. } while (0)
  107. // Makes copy constructor and operator= private.
  108. #define KALDI_DISALLOW_COPY_AND_ASSIGN(type) \
  109. type(const type&); \
  110. void operator=(const type&)
  111. template <bool B>
  112. class KaldiCompileTimeAssert {};
  113. template <>
  114. class KaldiCompileTimeAssert<true> {
  115. public:
  116. static inline void Check() {}
  117. };
  118. #define KALDI_COMPILE_TIME_ASSERT(b) KaldiCompileTimeAssert<(b)>::Check()
  119. #define KALDI_ASSERT_IS_INTEGER_TYPE(I) \
  120. KaldiCompileTimeAssert<std::numeric_limits<I>::is_specialized && \
  121. std::numeric_limits<I>::is_integer>::Check()
  122. #define KALDI_ASSERT_IS_FLOATING_TYPE(F) \
  123. KaldiCompileTimeAssert<std::numeric_limits<F>::is_specialized && \
  124. !std::numeric_limits<F>::is_integer>::Check()
  125. #if defined(_MSC_VER)
  126. #define KALDI_STRCASECMP _stricmp
  127. #elif defined(__CYGWIN__)
  128. #include <strings.h>
  129. #define KALDI_STRCASECMP strcasecmp
  130. #else
  131. #define KALDI_STRCASECMP strcasecmp
  132. #endif
  133. #ifdef _MSC_VER
  134. #define KALDI_STRTOLL(cur_cstr, end_cstr) _strtoi64(cur_cstr, end_cstr, 10);
  135. #else
  136. #define KALDI_STRTOLL(cur_cstr, end_cstr) strtoll(cur_cstr, end_cstr, 10);
  137. #endif
  138. #endif // KALDI_BASE_KALDI_UTILS_H_