Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShTypeInfoCasts.cpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
00020 #include <fstream>
00021 #include "ShTypeInfo.hpp"
00022 #include "ShCastManager.hpp"
00023 #include "ShVariantCast.hpp"
00024 
00025 namespace {
00026 using namespace SH;
00027 
00028 template<typename Dest, ShDataType DestDT, typename Src, ShDataType SrcDT>
00029 void addCast(bool automatic)
00030 {
00031   ShCastManager::instance()->addCast(ShDataVariantCast<Dest, DestDT, Src, SrcDT>::instance(), automatic);
00032 }
00033 // adds automatic promotion from src to dest
00034 // and a type conversion edge in the opposite direction 
00035 template<typename Dest, typename Src>
00036 void addPromotion()
00037 {
00038   addCast<Dest, SH_HOST, Src, SH_HOST>(true);
00039   addCast<Src, SH_HOST, Dest, SH_HOST>(false);
00040 }
00041 
00042 // adds automatic promotion from src to dest
00043 // and a type conversion edge in the opposite direction
00044 template<typename T>
00045 void addMemoryCast()
00046 {
00047   addCast<T, SH_HOST, T, SH_MEM>(false);
00048   addCast<T, SH_MEM, T, SH_HOST>(false);
00049 }
00050 
00051 }
00052 
00053 namespace SH {
00054 
00055 // This adds the available automatic and explicit casts 
00056 void ShTypeInfo::addCasts()
00057 {
00058   
00059   // automatic promotion DAG edges 
00060   // the inverse edges are added as automatic conversions, but not promotions
00061   // d <- f
00062   // f <- h, i, ui, fi, fs, fb, fui, fus, fub
00063   // i <- s, b, us, ub
00064   //
00065   // This should give a connected graph with maximum diameter (longest shortest
00066   // path between all pairs) of 3 (i.e. convering fi to b goes fi -> f -> i ->
00067   // b)
00068   //
00069   // We can add in a few more automatic conversions to turn this down to 2
00070   // (by making f the direct "supertype" of everything fractional or int)
00071   
00072   addPromotion<double, float>();
00073 
00074   addPromotion<float, ShHalf>();
00075   addPromotion<float, int>();
00076   addPromotion<float, unsigned int>();
00077   addPromotion<float, ShFracInt> ();
00078   addPromotion<float, ShFracShort> ();
00079   addPromotion<float, ShFracByte> ();
00080   addPromotion<float, ShFracUInt> ();
00081   addPromotion<float, ShFracUShort> ();
00082   addPromotion<float, ShFracUByte> ();
00083 
00084   addPromotion<int, short>();
00085   addPromotion<int, char>();
00086   addPromotion<int, unsigned short>();
00087   addPromotion<int, unsigned char>();
00088 
00089   // these are the extra conversions to make the diameter = 2
00090   addCast<float, SH_HOST, short, SH_HOST>(false);
00091   addCast<float, SH_HOST, char, SH_HOST>(false);
00092   addCast<float, SH_HOST, unsigned short, SH_HOST>(false);
00093   addCast<float, SH_HOST, unsigned char, SH_HOST>(false);
00094 
00095   addMemoryCast<double>();
00096   addMemoryCast<float>();
00097   addMemoryCast<ShHalf>();
00098 
00099   addMemoryCast<int>();
00100   addMemoryCast<short>();
00101   addMemoryCast<char>();
00102   addMemoryCast<unsigned int>();
00103   addMemoryCast<unsigned short>();
00104   addMemoryCast<unsigned char>();
00105 
00106   addMemoryCast<ShFraction<int> >();
00107   addMemoryCast<ShFraction<short> >();
00108   addMemoryCast<ShFraction<char> >();
00109   addMemoryCast<ShFraction<unsigned int> >();
00110   addMemoryCast<ShFraction<unsigned short> >();
00111   addMemoryCast<ShFraction<unsigned char> >();
00112 
00113   ShCastManager::instance()->init();
00114 
00115 #if 0
00116   std::ofstream fout("castgraph.dot");
00117   ShCastManager::instance()->graphvizDump(fout);
00118   system("dot -Tps < castgraph.dot > castgraph.ps");
00119 #endif
00120 }
00121 
00122 }

Generated on Thu Jul 28 17:33:06 2005 for Sh by  doxygen 1.4.3-20050530