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