summaryrefslogtreecommitdiff
path: root/tnet_io/KaldiLib/Common.h
blob: 9cd96588ba760fe7c4d162a45e0883575dcca405 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifndef TNet_Common_h
#define TNet_Common_h

#include <cstdlib>
#include <string.h> // C string stuff like strcpy
#include <string>
#include <sstream>
#include <stdexcept>

/* Alignment of critical dynamic data structure
 *
 * Not all platforms support memalign so we provide a stk_memalign wrapper
 * void *stk_memalign( size_t align, size_t size, void **pp_orig )
 * *pp_orig is the pointer that has to be freed afterwards.
 */
#ifdef HAVE_POSIX_MEMALIGN
#  define stk_memalign(align,size,pp_orig) \
     ( !posix_memalign( pp_orig, align, size ) ? *(pp_orig) : NULL )
#  ifdef STK_MEMALIGN_MANUAL
#    undef STK_MEMALIGN_MANUAL
#  endif
#elif defined(HAVE_MEMALIGN)
   /* Some systems have memalign() but no declaration for it */
   //void * memalign( size_t align, size_t size );
#  define stk_memalign(align,size,pp_orig) \
     ( *(pp_orig) = memalign( align, size ) )
#  ifdef STK_MEMALIGN_MANUAL
#    undef STK_MEMALIGN_MANUAL
#  endif
#else /* We don't have any choice but to align manually */
#  define stk_memalign(align,size,pp_orig) \
     (( *(pp_orig) = malloc( size + align - 1 )) ? \
     (void *)( (((unsigned long)*(pp_orig)) + 15) & ~0xFUL ) : NULL )
#  define STK_MEMALIGN_MANUAL
#endif


#define swap8(a) { \
  char t=((char*)&a)[0]; ((char*)&a)[0]=((char*)&a)[7]; ((char*)&a)[7]=t;\
      t=((char*)&a)[1]; ((char*)&a)[1]=((char*)&a)[6]; ((char*)&a)[6]=t;\
      t=((char*)&a)[2]; ((char*)&a)[2]=((char*)&a)[5]; ((char*)&a)[5]=t;\
      t=((char*)&a)[3]; ((char*)&a)[3]=((char*)&a)[4]; ((char*)&a)[4]=t;}
#define swap4(a) { \
  char t=((char*)&a)[0]; ((char*)&a)[0]=((char*)&a)[3]; ((char*)&a)[3]=t;\
      t=((char*)&a)[1]; ((char*)&a)[1]=((char*)&a)[2]; ((char*)&a)[2]=t;}
#define swap2(a) { \
  char t=((char*)&a)[0]; ((char*)&a)[0]=((char*)&a)[1]; ((char*)&a)[1]=t;}


namespace TNet
{
  /** **************************************************************************
   ** **************************************************************************
   * @brief Aligns a number to a specified base
   * @param n Number of type @c _T to align
   * @return Aligned value of type @c _T
   */
  template<size_t _align, typename _T>
    inline _T 
    align(const _T n)
    {
      const _T x(_align - 1); 
      return (n + x) & ~(x);
    }


  /** 
   * @brief Returns true if architecture is big endian
   */
  bool 
  IsBigEndian();


  /** 
   * @brief Returns true if two numbers are close enough to each other
   * 
   * @param f1  First operand
   * @param f2  Second operand
   * @param nRounds Expected number of operations prior to this comparison
  */
  bool 
  CloseEnough(const float f1, const float f2, const float nRounds);
  

  /** 
   * @brief Returns true if two numbers are close enough to each other
   * 
   * @param f1  First operand
   * @param f2  Second operand
   * @param nRounds Expected number of operations prior to this comparison
  */
  bool 
  CloseEnough(const double f1, const double f2, const double nRounds);
  

  /** 
   * @brief Parses a HTK-style string into a C++ std::string readable
   * 
   * @param rIn  HTK input string
   * @param rOut output parsed string
   */
  void
  ParseHTKString(const std::string & rIn, std::string & rOut);

  
  /** 
   * @brief Synthesize new file name based on name, path, and extension
   * 
   * @param pOutFileName  full ouptut file name
   * @param pInFileName   file name
   * @param pOutDir       directory
   * @param pOutExt       extension
   */
  void    
  MakeHtkFileName(char *pOutFileName, const char* pInFileName, const char *pOutDir, 
      const char *pOutExt);
  

  /** 
   * @brief Removes the leading and trailing white chars
   *
   * @param rStr Refference to the string to be processed
   * @return Refference to the original string
   *
   * The white characters are determined by the @c WHITE_CHARS macro defined 
   * above.
   */
  std::string&
  Trim(std::string& rStr);


  char*
  StrToUpper(char* pStr);

  char* 
  ExpandHtkFilterCmd(const char *command, const char *filename, const char* pFilter);
  
  
  template <class T>
  std::string to_string(const T& val)
  {
    std::stringstream ss;
    ss << val;
    return ss.str();
  }
  
  inline void 
  ExpectKeyword(std::istream &i_stream, const char *kwd)
  {
     std::string token;
     i_stream >> token;
     if (token != kwd) {
       throw std::runtime_error(std::string(kwd) + " expected");
     }
  }
  
  extern const int MATRIX_IOS_FORMAT_IWORD;

  enum MatrixVectorIostreamControlBits {
    ACCUMULATE_INPUT = 1,
//  BINARY_OUTPUT    = 2
  };
  
  class MatrixVectorIostreamControl
  {
    public:
      MatrixVectorIostreamControl(enum MatrixVectorIostreamControlBits bitsToBeSet, bool valueToBeSet)
      : mBitsToBeSet(bitsToBeSet), mValueToBeSet(valueToBeSet) {}
      
      static long Flags(std::ios_base &rIos, enum MatrixVectorIostreamControlBits bits)
      { return rIos.iword(MATRIX_IOS_FORMAT_IWORD); }
      
      long mBitsToBeSet;
      bool mValueToBeSet;
            
      friend std::ostream & operator <<(std::ostream &rOs, const MatrixVectorIostreamControl modifier)
      {
        if(modifier.mValueToBeSet) {
          rOs.iword(MATRIX_IOS_FORMAT_IWORD) |= modifier.mBitsToBeSet;
        } else {
          rOs.iword(MATRIX_IOS_FORMAT_IWORD) &= ~modifier.mBitsToBeSet;
        }
        return rOs;
      }

      friend std::istream & operator >>(std::istream &rIs, const MatrixVectorIostreamControl modifier)
      {
        if(modifier.mValueToBeSet) {
          rIs.iword(MATRIX_IOS_FORMAT_IWORD) |= modifier.mBitsToBeSet;
        } else {
          rIs.iword(MATRIX_IOS_FORMAT_IWORD) &= ~modifier.mBitsToBeSet;
        }
        return rIs;
      }
  };
  
  
  

} // namespace TNet

#ifdef __ICC
#pragma warning (disable: 383) // ICPC remark we don't want.
#pragma warning (disable: 810) // ICPC remark we don't want.
#pragma warning (disable: 981) // ICPC remark we don't want.
#pragma warning (disable: 1418) // ICPC remark we don't want.
#pragma warning (disable: 444) // ICPC remark we don't want.
#pragma warning (disable: 869) // ICPC remark we don't want.
#pragma warning (disable: 1287) // ICPC remark we don't want.
#pragma warning (disable: 279) // ICPC remark we don't want.
#pragma warning (disable: 981) // ICPC remark we don't want.
#endif

//#ifdef CYGWIN
#if 1
#undef assert
#ifndef NDEBUG
#define assert(e)          ((e) ? (void)0 : assertf(__FILE__, __LINE__, #e))
#else
#define assert(e)         ((void)0)
#endif
void assertf(const char *c, int i, const char *msg); // Just make it possible to break into assert on gdb-- has some kind of bug on cygwin.
#else
#include <cassert>
#endif

#define assert_throw(e)          ((e) ? (void)0 : assertf_throw(__FILE__, __LINE__, #e))
void assertf_throw(const char *c, int i, const char *msg); 

#define DAN_STYLE_IO

#endif // ifndef TNet_Common_h