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 software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 #include <fstream>
00025 #include "ShTypeInfo.hpp"
00026 #include "ShCastManager.hpp"
00027 #include "ShVariantCast.hpp"
00028 
00029 namespace {
00030 using namespace SH;
00031 
00032 template<typename Dest, ShDataType DestDT, typename Src, ShDataType SrcDT>
00033 void addCast(bool automatic)
00034 {
00035   ShCastManager::instance()->addCast(ShDataVariantCast<Dest, DestDT, Src, SrcDT>::instance(), automatic);
00036 }
00037 // adds automatic promotion from src to dest
00038 // and a type conversion edge in the opposite direction 
00039 template<typename Dest, typename Src>
00040 void addPromotion()
00041 {
00042   addCast<Dest, SH_HOST, Src, SH_HOST>(true);
00043   addCast<Src, SH_HOST, Dest, SH_HOST>(false);
00044 }
00045 
00046 // adds automatic promotion from src to dest
00047 // and a type conversion edge in the opposite direction
00048 template<typename T>
00049 void addMemoryCast()
00050 {
00051   addCast<T, SH_HOST, T, SH_MEM>(false);
00052   addCast<T, SH_MEM, T, SH_HOST>(false);
00053 }
00054 
00055 }
00056 
00057 namespace SH {
00058 
00059 // This adds the available automatic and explicit casts 
00060 void ShTypeInfo::addCasts()
00061 {
00062   
00063   // automatic promotion DAG edges 
00064   // the inverse edges are added as automatic conversions, but not promotions
00065   // d <- f
00066   // f <- h, i, ui, fi, fs, fb, fui, fus, fub
00067   // i <- s, b, us, ub
00068   //
00069   // This should give a connected graph with maximum diameter (longest shortest
00070   // path between all pairs) of 3 (i.e. convering fi to b goes fi -> f -> i ->
00071   // b)
00072   //
00073   // We can add in a few more automatic conversions to turn this down to 2
00074   // (by making f the direct "supertype" of everything fractional or int)
00075   
00076   addPromotion<double, float>();
00077 
00078   addPromotion<float, ShHalf>();
00079   addPromotion<float, int>();
00080   addPromotion<float, unsigned int>();
00081   addPromotion<float, ShFracInt> ();
00082   addPromotion<float, ShFracShort> ();
00083   addPromotion<float, ShFracByte> ();
00084   addPromotion<float, ShFracUInt> ();
00085   addPromotion<float, ShFracUShort> ();
00086   addPromotion<float, ShFracUByte> ();
00087 
00088   addPromotion<int, short>();
00089   addPromotion<int, char>();
00090   addPromotion<int, unsigned short>();
00091   addPromotion<int, unsigned char>();
00092 
00093   // these are the extra conversions to make the diameter = 2
00094   addCast<float, SH_HOST, short, SH_HOST>(false);
00095   addCast<float, SH_HOST, char, SH_HOST>(false);
00096   addCast<float, SH_HOST, unsigned short, SH_HOST>(false);
00097   addCast<float, SH_HOST, unsigned char, SH_HOST>(false);
00098 
00099   addMemoryCast<double>();
00100   addMemoryCast<float>();
00101   addMemoryCast<ShHalf>();
00102 
00103   addMemoryCast<int>();
00104   addMemoryCast<short>();
00105   addMemoryCast<char>();
00106   addMemoryCast<unsigned int>();
00107   addMemoryCast<unsigned short>();
00108   addMemoryCast<unsigned char>();
00109 
00110   addMemoryCast<ShFraction<int> >();
00111   addMemoryCast<ShFraction<short> >();
00112   addMemoryCast<ShFraction<char> >();
00113   addMemoryCast<ShFraction<unsigned int> >();
00114   addMemoryCast<ShFraction<unsigned short> >();
00115   addMemoryCast<ShFraction<unsigned char> >();
00116 
00117   ShCastManager::instance()->init();
00118 
00119 #if 0
00120   std::ofstream fout("castgraph.dot");
00121   ShCastManager::instance()->graphvizDump(fout);
00122   system("dot -Tps < castgraph.dot > castgraph.ps");
00123 #endif
00124 }
00125 
00126 }

Generated on Thu Apr 21 17:32:49 2005 for Sh by  doxygen 1.4.2