btl.hh
Go to the documentation of this file.
00001 //=====================================================
00002 // File   :  btl.hh
00003 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00004 //=====================================================
00005 //
00006 // This program is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU General Public License
00008 // as published by the Free Software Foundation; either version 2
00009 // of the License, or (at your option) any later version.
00010 //
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 #ifndef BTL_HH
00020 #define BTL_HH
00021 
00022 #include "bench_parameter.hh"
00023 #include <iostream>
00024 #include <algorithm>
00025 #include <vector>
00026 #include <string>
00027 #include "utilities.h"
00028 
00029 #if (defined __GNUC__)
00030 #define BTL_ALWAYS_INLINE __attribute__((always_inline)) inline
00031 #else
00032 #define BTL_ALWAYS_INLINE inline
00033 #endif
00034 
00035 #if (defined __GNUC__)
00036 #define BTL_DONT_INLINE __attribute__((noinline))
00037 #else
00038 #define BTL_DONT_INLINE
00039 #endif
00040 
00041 #if (defined __GNUC__)
00042 #define BTL_ASM_COMMENT(X)  asm("#"X)
00043 #else
00044 #define BTL_ASM_COMMENT(X)
00045 #endif
00046 
00047 #if (defined __GNUC__) && (!defined __INTEL_COMPILER) && !defined(__arm__) && !defined(__powerpc__)
00048 #define BTL_DISABLE_SSE_EXCEPTIONS()  { \
00049   int aux; \
00050   asm( \
00051   "stmxcsr   %[aux]           \n\t" \
00052   "orl       $32832, %[aux]   \n\t" \
00053   "ldmxcsr   %[aux]           \n\t" \
00054   : : [aux] "m" (aux)); \
00055 }
00056 #else
00057 #define BTL_DISABLE_SSE_EXCEPTIONS()
00058 #endif
00059 
00062 class BtlString : public std::string
00063 {
00064 public:
00065     BtlString() : std::string() {}
00066     BtlString(const BtlString& str) : std::string(static_cast<const std::string&>(str)) {}
00067     BtlString(const std::string& str) : std::string(str) {}
00068     BtlString(const char* str) : std::string(str) {}
00069 
00070     operator const char* () const { return c_str(); }
00071 
00072     void trim( bool left = true, bool right = true )
00073     {
00074         int lspaces, rspaces, len = length(), i;
00075         lspaces = rspaces = 0;
00076 
00077         if ( left )
00078             for (i=0; i<len && (at(i)==' '||at(i)=='\t'||at(i)=='\r'||at(i)=='\n'); ++lspaces,++i);
00079 
00080         if ( right && lspaces < len )
00081             for(i=len-1; i>=0 && (at(i)==' '||at(i)=='\t'||at(i)=='\r'||at(i)=='\n'); rspaces++,i--);
00082 
00083         *this = substr(lspaces, len-lspaces-rspaces);
00084     }
00085 
00086     std::vector<BtlString> split( const BtlString& delims = "\t\n ") const
00087     {
00088         std::vector<BtlString> ret;
00089         unsigned int numSplits = 0;
00090         size_t start, pos;
00091         start = 0;
00092         do
00093         {
00094             pos = find_first_of(delims, start);
00095             if (pos == start)
00096             {
00097                 ret.push_back("");
00098                 start = pos + 1;
00099             }
00100             else if (pos == npos)
00101                 ret.push_back( substr(start) );
00102             else
00103             {
00104                 ret.push_back( substr(start, pos - start) );
00105                 start = pos + 1;
00106             }
00107             //start = find_first_not_of(delims, start);
00108             ++numSplits;
00109         } while (pos != npos);
00110         return ret;
00111     }
00112 
00113     bool endsWith(const BtlString& str) const
00114     {
00115         if(str.size()>this->size())
00116             return false;
00117         return this->substr(this->size()-str.size(),str.size()) == str;
00118     }
00119     bool contains(const BtlString& str) const
00120     {
00121         return this->find(str)<this->size();
00122     }
00123     bool beginsWith(const BtlString& str) const
00124     {
00125         if(str.size()>this->size())
00126             return false;
00127         return this->substr(0,str.size()) == str;
00128     }
00129 
00130     BtlString toLowerCase( void )
00131     {
00132         std::transform(begin(), end(), begin(), static_cast<int(*)(int)>(::tolower) );
00133         return *this;
00134     }
00135     BtlString toUpperCase( void )
00136     {
00137         std::transform(begin(), end(), begin(), static_cast<int(*)(int)>(::toupper) );
00138         return *this;
00139     }
00140 
00143     bool isEquiv(const BtlString& str) const
00144     {
00145         BtlString str0 = *this;
00146         str0.toLowerCase();
00147         BtlString str1 = str;
00148         str1.toLowerCase();
00149         return str0 == str1;
00150     }
00151 
00155     void decomposePathAndFile(BtlString& path, BtlString& filename) const
00156     {
00157         std::vector<BtlString> elements = this->split("/\\");
00158         path = "";
00159         filename = elements.back();
00160         elements.pop_back();
00161         if (this->at(0)=='/')
00162             path = "/";
00163         for (unsigned int i=0 ; i<elements.size() ; ++i)
00164             path += elements[i] + "/";
00165     }
00166 };
00167 
00168 class BtlConfig
00169 {
00170 public:
00171   BtlConfig()
00172     : overwriteResults(false), checkResults(true), realclock(false), tries(DEFAULT_NB_TRIES)
00173   {
00174     char * _config;
00175     _config = getenv ("BTL_CONFIG");
00176     if (_config!=NULL)
00177     {
00178       std::vector<BtlString> config = BtlString(_config).split(" \t\n");
00179       for (int i = 0; i<config.size(); i++)
00180       {
00181         if (config[i].beginsWith("-a"))
00182         {
00183           if (i+1==config.size())
00184           {
00185             std::cerr << "error processing option: " << config[i] << "\n";
00186             exit(2);
00187           }
00188           Instance.m_selectedActionNames = config[i+1].split(":");
00189 
00190           i += 1;
00191         }
00192         else if (config[i].beginsWith("-t"))
00193         {
00194           if (i+1==config.size())
00195           {
00196             std::cerr << "error processing option: " << config[i] << "\n";
00197             exit(2);
00198           }
00199           Instance.tries = atoi(config[i+1].c_str());
00200 
00201           i += 1;
00202         }
00203         else if (config[i].beginsWith("--overwrite"))
00204         {
00205           Instance.overwriteResults = true;
00206         }
00207         else if (config[i].beginsWith("--nocheck"))
00208         {
00209           Instance.checkResults = false;
00210         }
00211         else if (config[i].beginsWith("--real"))
00212         {
00213           Instance.realclock = true;
00214         }
00215       }
00216     }
00217 
00218     BTL_DISABLE_SSE_EXCEPTIONS();
00219   }
00220 
00221   BTL_DONT_INLINE static bool skipAction(const std::string& _name)
00222   {
00223     if (Instance.m_selectedActionNames.empty())
00224       return false;
00225 
00226     BtlString name(_name);
00227     for (int i=0; i<Instance.m_selectedActionNames.size(); ++i)
00228       if (name.contains(Instance.m_selectedActionNames[i]))
00229         return false;
00230 
00231     return true;
00232   }
00233 
00234   static BtlConfig Instance;
00235   bool overwriteResults;
00236   bool checkResults;
00237   bool realclock;
00238   int tries;
00239 
00240 protected:
00241   std::vector<BtlString> m_selectedActionNames;
00242 };
00243 
00244 #define BTL_MAIN \
00245   BtlConfig BtlConfig::Instance
00246 
00247 #endif // BTL_HH


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:30:48