btl.hh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //=====================================================
  2. // File : btl.hh
  3. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
  4. //=====================================================
  5. //
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. //
  19. #ifndef BTL_HH
  20. #define BTL_HH
  21. #include "bench_parameter.hh"
  22. #include <iostream>
  23. #include <algorithm>
  24. #include <vector>
  25. #include <string>
  26. #include "utilities.h"
  27. #if (defined __GNUC__)
  28. #define BTL_ALWAYS_INLINE __attribute__((always_inline)) inline
  29. #else
  30. #define BTL_ALWAYS_INLINE inline
  31. #endif
  32. #if (defined __GNUC__)
  33. #define BTL_DONT_INLINE __attribute__((noinline))
  34. #else
  35. #define BTL_DONT_INLINE
  36. #endif
  37. #if (defined __GNUC__)
  38. #define BTL_ASM_COMMENT(X) asm("#" X)
  39. #else
  40. #define BTL_ASM_COMMENT(X)
  41. #endif
  42. #ifdef __SSE__
  43. #include "xmmintrin.h"
  44. // This enables flush to zero (FTZ) and denormals are zero (DAZ) modes:
  45. #define BTL_DISABLE_SSE_EXCEPTIONS() { _mm_setcsr(_mm_getcsr() | 0x8040); }
  46. #else
  47. #define BTL_DISABLE_SSE_EXCEPTIONS()
  48. #endif
  49. /** Enhanced std::string
  50. */
  51. class BtlString : public std::string
  52. {
  53. public:
  54. BtlString() : std::string() {}
  55. BtlString(const BtlString& str) : std::string(static_cast<const std::string&>(str)) {}
  56. BtlString(const std::string& str) : std::string(str) {}
  57. BtlString(const char* str) : std::string(str) {}
  58. operator const char* () const { return c_str(); }
  59. void trim( bool left = true, bool right = true )
  60. {
  61. int lspaces, rspaces, len = length(), i;
  62. lspaces = rspaces = 0;
  63. if ( left )
  64. for (i=0; i<len && (at(i)==' '||at(i)=='\t'||at(i)=='\r'||at(i)=='\n'); ++lspaces,++i);
  65. if ( right && lspaces < len )
  66. for(i=len-1; i>=0 && (at(i)==' '||at(i)=='\t'||at(i)=='\r'||at(i)=='\n'); rspaces++,i--);
  67. *this = substr(lspaces, len-lspaces-rspaces);
  68. }
  69. std::vector<BtlString> split( const BtlString& delims = "\t\n ") const
  70. {
  71. std::vector<BtlString> ret;
  72. unsigned int numSplits = 0;
  73. size_t start, pos;
  74. start = 0;
  75. do
  76. {
  77. pos = find_first_of(delims, start);
  78. if (pos == start)
  79. {
  80. ret.push_back("");
  81. start = pos + 1;
  82. }
  83. else if (pos == npos)
  84. ret.push_back( substr(start) );
  85. else
  86. {
  87. ret.push_back( substr(start, pos - start) );
  88. start = pos + 1;
  89. }
  90. //start = find_first_not_of(delims, start);
  91. ++numSplits;
  92. } while (pos != npos);
  93. return ret;
  94. }
  95. bool endsWith(const BtlString& str) const
  96. {
  97. if(str.size()>this->size())
  98. return false;
  99. return this->substr(this->size()-str.size(),str.size()) == str;
  100. }
  101. bool contains(const BtlString& str) const
  102. {
  103. return this->find(str)<this->size();
  104. }
  105. bool beginsWith(const BtlString& str) const
  106. {
  107. if(str.size()>this->size())
  108. return false;
  109. return this->substr(0,str.size()) == str;
  110. }
  111. BtlString toLowerCase( void )
  112. {
  113. std::transform(begin(), end(), begin(), static_cast<int(*)(int)>(::tolower) );
  114. return *this;
  115. }
  116. BtlString toUpperCase( void )
  117. {
  118. std::transform(begin(), end(), begin(), static_cast<int(*)(int)>(::toupper) );
  119. return *this;
  120. }
  121. /** Case insensitive comparison.
  122. */
  123. bool isEquiv(const BtlString& str) const
  124. {
  125. BtlString str0 = *this;
  126. str0.toLowerCase();
  127. BtlString str1 = str;
  128. str1.toLowerCase();
  129. return str0 == str1;
  130. }
  131. /** Decompose the current string as a path and a file.
  132. For instance: "dir1/dir2/file.ext" leads to path="dir1/dir2/" and filename="file.ext"
  133. */
  134. void decomposePathAndFile(BtlString& path, BtlString& filename) const
  135. {
  136. std::vector<BtlString> elements = this->split("/\\");
  137. path = "";
  138. filename = elements.back();
  139. elements.pop_back();
  140. if (this->at(0)=='/')
  141. path = "/";
  142. for (unsigned int i=0 ; i<elements.size() ; ++i)
  143. path += elements[i] + "/";
  144. }
  145. };
  146. class BtlConfig
  147. {
  148. public:
  149. BtlConfig()
  150. : overwriteResults(false), checkResults(true), realclock(false), tries(DEFAULT_NB_TRIES)
  151. {
  152. char * _config;
  153. _config = getenv ("BTL_CONFIG");
  154. if (_config!=NULL)
  155. {
  156. std::vector<BtlString> config = BtlString(_config).split(" \t\n");
  157. for (unsigned int i = 0; i<config.size(); i++)
  158. {
  159. if (config[i].beginsWith("-a"))
  160. {
  161. if (i+1==config.size())
  162. {
  163. std::cerr << "error processing option: " << config[i] << "\n";
  164. exit(2);
  165. }
  166. Instance.m_selectedActionNames = config[i+1].split(":");
  167. i += 1;
  168. }
  169. else if (config[i].beginsWith("-t"))
  170. {
  171. if (i+1==config.size())
  172. {
  173. std::cerr << "error processing option: " << config[i] << "\n";
  174. exit(2);
  175. }
  176. Instance.tries = atoi(config[i+1].c_str());
  177. i += 1;
  178. }
  179. else if (config[i].beginsWith("--overwrite"))
  180. {
  181. Instance.overwriteResults = true;
  182. }
  183. else if (config[i].beginsWith("--nocheck"))
  184. {
  185. Instance.checkResults = false;
  186. }
  187. else if (config[i].beginsWith("--real"))
  188. {
  189. Instance.realclock = true;
  190. }
  191. }
  192. }
  193. BTL_DISABLE_SSE_EXCEPTIONS();
  194. }
  195. BTL_DONT_INLINE static bool skipAction(const std::string& _name)
  196. {
  197. if (Instance.m_selectedActionNames.empty())
  198. return false;
  199. BtlString name(_name);
  200. for (unsigned int i=0; i<Instance.m_selectedActionNames.size(); ++i)
  201. if (name.contains(Instance.m_selectedActionNames[i]))
  202. return false;
  203. return true;
  204. }
  205. static BtlConfig Instance;
  206. bool overwriteResults;
  207. bool checkResults;
  208. bool realclock;
  209. int tries;
  210. protected:
  211. std::vector<BtlString> m_selectedActionNames;
  212. };
  213. #define BTL_MAIN \
  214. BtlConfig BtlConfig::Instance
  215. #endif // BTL_HH