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.

247 lines
6.7 KiB

  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. #if (defined __GNUC__) && (!defined __INTEL_COMPILER) && !defined(__arm__) && !defined(__powerpc__)
  43. #define BTL_DISABLE_SSE_EXCEPTIONS() { \
  44. int aux; \
  45. asm( \
  46. "stmxcsr %[aux] \n\t" \
  47. "orl $32832, %[aux] \n\t" \
  48. "ldmxcsr %[aux] \n\t" \
  49. : : [aux] "m" (aux)); \
  50. }
  51. #else
  52. #define BTL_DISABLE_SSE_EXCEPTIONS()
  53. #endif
  54. /** Enhanced std::string
  55. */
  56. class BtlString : public std::string
  57. {
  58. public:
  59. BtlString() : std::string() {}
  60. BtlString(const BtlString& str) : std::string(static_cast<const std::string&>(str)) {}
  61. BtlString(const std::string& str) : std::string(str) {}
  62. BtlString(const char* str) : std::string(str) {}
  63. operator const char* () const { return c_str(); }
  64. void trim( bool left = true, bool right = true )
  65. {
  66. int lspaces, rspaces, len = length(), i;
  67. lspaces = rspaces = 0;
  68. if ( left )
  69. for (i=0; i<len && (at(i)==' '||at(i)=='\t'||at(i)=='\r'||at(i)=='\n'); ++lspaces,++i);
  70. if ( right && lspaces < len )
  71. for(i=len-1; i>=0 && (at(i)==' '||at(i)=='\t'||at(i)=='\r'||at(i)=='\n'); rspaces++,i--);
  72. *this = substr(lspaces, len-lspaces-rspaces);
  73. }
  74. std::vector<BtlString> split( const BtlString& delims = "\t\n ") const
  75. {
  76. std::vector<BtlString> ret;
  77. unsigned int numSplits = 0;
  78. size_t start, pos;
  79. start = 0;
  80. do
  81. {
  82. pos = find_first_of(delims, start);
  83. if (pos == start)
  84. {
  85. ret.push_back("");
  86. start = pos + 1;
  87. }
  88. else if (pos == npos)
  89. ret.push_back( substr(start) );
  90. else
  91. {
  92. ret.push_back( substr(start, pos - start) );
  93. start = pos + 1;
  94. }
  95. //start = find_first_not_of(delims, start);
  96. ++numSplits;
  97. } while (pos != npos);
  98. return ret;
  99. }
  100. bool endsWith(const BtlString& str) const
  101. {
  102. if(str.size()>this->size())
  103. return false;
  104. return this->substr(this->size()-str.size(),str.size()) == str;
  105. }
  106. bool contains(const BtlString& str) const
  107. {
  108. return this->find(str)<this->size();
  109. }
  110. bool beginsWith(const BtlString& str) const
  111. {
  112. if(str.size()>this->size())
  113. return false;
  114. return this->substr(0,str.size()) == str;
  115. }
  116. BtlString toLowerCase( void )
  117. {
  118. std::transform(begin(), end(), begin(), static_cast<int(*)(int)>(::tolower) );
  119. return *this;
  120. }
  121. BtlString toUpperCase( void )
  122. {
  123. std::transform(begin(), end(), begin(), static_cast<int(*)(int)>(::toupper) );
  124. return *this;
  125. }
  126. /** Case insensitive comparison.
  127. */
  128. bool isEquiv(const BtlString& str) const
  129. {
  130. BtlString str0 = *this;
  131. str0.toLowerCase();
  132. BtlString str1 = str;
  133. str1.toLowerCase();
  134. return str0 == str1;
  135. }
  136. /** Decompose the current string as a path and a file.
  137. For instance: "dir1/dir2/file.ext" leads to path="dir1/dir2/" and filename="file.ext"
  138. */
  139. void decomposePathAndFile(BtlString& path, BtlString& filename) const
  140. {
  141. std::vector<BtlString> elements = this->split("/\\");
  142. path = "";
  143. filename = elements.back();
  144. elements.pop_back();
  145. if (this->at(0)=='/')
  146. path = "/";
  147. for (unsigned int i=0 ; i<elements.size() ; ++i)
  148. path += elements[i] + "/";
  149. }
  150. };
  151. class BtlConfig
  152. {
  153. public:
  154. BtlConfig()
  155. : overwriteResults(false), checkResults(true), realclock(false), tries(DEFAULT_NB_TRIES)
  156. {
  157. char * _config;
  158. _config = getenv ("BTL_CONFIG");
  159. if (_config!=NULL)
  160. {
  161. std::vector<BtlString> config = BtlString(_config).split(" \t\n");
  162. for (int i = 0; i<config.size(); i++)
  163. {
  164. if (config[i].beginsWith("-a"))
  165. {
  166. if (i+1==config.size())
  167. {
  168. std::cerr << "error processing option: " << config[i] << "\n";
  169. exit(2);
  170. }
  171. Instance.m_selectedActionNames = config[i+1].split(":");
  172. i += 1;
  173. }
  174. else if (config[i].beginsWith("-t"))
  175. {
  176. if (i+1==config.size())
  177. {
  178. std::cerr << "error processing option: " << config[i] << "\n";
  179. exit(2);
  180. }
  181. Instance.tries = atoi(config[i+1].c_str());
  182. i += 1;
  183. }
  184. else if (config[i].beginsWith("--overwrite"))
  185. {
  186. Instance.overwriteResults = true;
  187. }
  188. else if (config[i].beginsWith("--nocheck"))
  189. {
  190. Instance.checkResults = false;
  191. }
  192. else if (config[i].beginsWith("--real"))
  193. {
  194. Instance.realclock = true;
  195. }
  196. }
  197. }
  198. BTL_DISABLE_SSE_EXCEPTIONS();
  199. }
  200. BTL_DONT_INLINE static bool skipAction(const std::string& _name)
  201. {
  202. if (Instance.m_selectedActionNames.empty())
  203. return false;
  204. BtlString name(_name);
  205. for (int i=0; i<Instance.m_selectedActionNames.size(); ++i)
  206. if (name.contains(Instance.m_selectedActionNames[i]))
  207. return false;
  208. return true;
  209. }
  210. static BtlConfig Instance;
  211. bool overwriteResults;
  212. bool checkResults;
  213. bool realclock;
  214. int tries;
  215. protected:
  216. std::vector<BtlString> m_selectedActionNames;
  217. };
  218. #define BTL_MAIN \
  219. BtlConfig BtlConfig::Instance
  220. #endif // BTL_HH