All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 #ifdef _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 #else
54 # define KALDI_MEMALIGN(align, size, pp_orig) \
55  (!posix_memalign(pp_orig, align, size) ? *(pp_orig) : NULL)
56 # define KALDI_MEMALIGN_FREE(x) free(x)
57 #endif
58 
59 #ifdef __ICC
60 #pragma warning(disable: 383) // ICPC remark we don't want.
61 #pragma warning(disable: 810) // ICPC remark we don't want.
62 #pragma warning(disable: 981) // ICPC remark we don't want.
63 #pragma warning(disable: 1418) // ICPC remark we don't want.
64 #pragma warning(disable: 444) // ICPC remark we don't want.
65 #pragma warning(disable: 869) // ICPC remark we don't want.
66 #pragma warning(disable: 1287) // ICPC remark we don't want.
67 #pragma warning(disable: 279) // ICPC remark we don't want.
68 #pragma warning(disable: 981) // ICPC remark we don't want.
69 #endif
70 
71 
72 namespace kaldi {
73 
74 
75 // CharToString prints the character in a human-readable form, for debugging.
76 std::string CharToString(const char &c);
77 
78 
79 inline int MachineIsLittleEndian() {
80  int check = 1;
81  return (*reinterpret_cast<char*>(&check) != 0);
82 }
83 
84 // This function kaldi::Sleep() provides a portable way
85 // to sleep for a possibly fractional
86 // number of seconds. On Windows it's only accurate to microseconds.
87 void Sleep(float seconds);
88 }
89 
90 #define KALDI_SWAP8(a) { \
91  int t = (reinterpret_cast<char*>(&a))[0];\
92  (reinterpret_cast<char*>(&a))[0]=(reinterpret_cast<char*>(&a))[7];\
93  (reinterpret_cast<char*>(&a))[7]=t;\
94  t = (reinterpret_cast<char*>(&a))[1];\
95  (reinterpret_cast<char*>(&a))[1]=(reinterpret_cast<char*>(&a))[6];\
96  (reinterpret_cast<char*>(&a))[6]=t;\
97  t = (reinterpret_cast<char*>(&a))[2];\
98  (reinterpret_cast<char*>(&a))[2]=(reinterpret_cast<char*>(&a))[5];\
99  (reinterpret_cast<char*>(&a))[5]=t;\
100  t = (reinterpret_cast<char*>(&a))[3];\
101  (reinterpret_cast<char*>(&a))[3]=(reinterpret_cast<char*>(&a))[4];\
102  (reinterpret_cast<char*>(&a))[4]=t;}
103 #define KALDI_SWAP4(a) { \
104  int t = (reinterpret_cast<char*>(&a))[0];\
105  (reinterpret_cast<char*>(&a))[0]=(reinterpret_cast<char*>(&a))[3];\
106  (reinterpret_cast<char*>(&a))[3]=t;\
107  t = (reinterpret_cast<char*>(&a))[1];\
108  (reinterpret_cast<char*>(&a))[1]=(reinterpret_cast<char*>(&a))[2];\
109  (reinterpret_cast<char*>(&a))[2]=t;}
110 #define KALDI_SWAP2(a) { \
111  int t = (reinterpret_cast<char*>(&a))[0];\
112  (reinterpret_cast<char*>(&a))[0]=(reinterpret_cast<char*>(&a))[1];\
113  (reinterpret_cast<char*>(&a))[1]=t;}
114 
115 
116 // Makes copy constructor and operator= private.
117 #define KALDI_DISALLOW_COPY_AND_ASSIGN(type) \
118  type(const type&); \
119  void operator = (const type&)
120 
121 template<bool B> class KaldiCompileTimeAssert { };
122 template<> class KaldiCompileTimeAssert<true> {
123  public:
124  static inline void Check() { }
125 };
126 
127 #define KALDI_COMPILE_TIME_ASSERT(b) KaldiCompileTimeAssert<(b)>::Check()
128 
129 #define KALDI_ASSERT_IS_INTEGER_TYPE(I) \
130  KaldiCompileTimeAssert<std::numeric_limits<I>::is_specialized \
131  && std::numeric_limits<I>::is_integer>::Check()
132 
133 #define KALDI_ASSERT_IS_FLOATING_TYPE(F) \
134  KaldiCompileTimeAssert<std::numeric_limits<F>::is_specialized \
135  && !std::numeric_limits<F>::is_integer>::Check()
136 
137 #ifdef _MSC_VER
138 #define KALDI_STRCASECMP _stricmp
139 #else
140 #define KALDI_STRCASECMP strcasecmp
141 #endif
142 #ifdef _MSC_VER
143 # define KALDI_STRTOLL(cur_cstr, end_cstr) _strtoi64(cur_cstr, end_cstr, 10);
144 #else
145 # define KALDI_STRTOLL(cur_cstr, end_cstr) strtoll(cur_cstr, end_cstr, 10);
146 #endif
147 
148 #endif // KALDI_BASE_KALDI_UTILS_H_
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void Sleep(float seconds)
Definition: kaldi-utils.cc:45
int MachineIsLittleEndian()
Definition: kaldi-utils.h:79
std::string CharToString(const char &c)
Definition: kaldi-utils.cc:36