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 #ifndef SHSTORAGETYPEIMPL_HPP 00025 #define SHSTORAGETYPEIMPL_HPP 00026 00027 #include <climits> 00028 #include <cmath> 00029 #include <algorithm> 00030 #include "ShStorageType.hpp" 00031 00032 namespace SH { 00033 00034 inline bool shIsFloat(ShValueType value_type) 00035 { 00036 return !shIsInvalidValueType(value_type) && 00037 (value_type & SH_VALUETYPE_TYPE_MASK) == SH_VALUETYPE_TYPE_FLOAT; 00038 } 00039 00040 inline bool shIsInteger(ShValueType value_type) 00041 { 00042 return !shIsInvalidValueType(value_type) && 00043 (value_type & SH_VALUETYPE_TYPE_MASK) == SH_VALUETYPE_TYPE_INT; 00044 } 00045 00046 inline bool shIsFraction(ShValueType value_type) 00047 { 00048 return !shIsInvalidValueType(value_type) && 00049 (value_type & SH_VALUETYPE_TYPE_MASK) == SH_VALUETYPE_TYPE_FRAC; 00050 } 00051 00052 inline bool shIsSigned(ShValueType value_type) 00053 { 00054 return !shIsInvalidValueType(value_type) && 00055 (value_type & SH_VALUETYPE_SIGNED_MASK) == SH_VALUETYPE_SIGNED; 00056 } 00057 00058 inline bool shIsRegularValueType(ShValueType value_type) 00059 { 00060 return !shIsInvalidValueType(value_type) && 00061 (value_type & SH_VALUETYPE_SPECIAL_MASK) == SH_VALUETYPE_SPECIAL_NONE; 00062 } 00063 00064 inline bool shIsInvalidValueType(ShValueType value_type) 00065 { 00066 return value_type == SH_VALUETYPE_END; 00067 } 00068 00069 inline 00070 ShValueType shRegularValueType(ShValueType value_type) 00071 { 00072 return shIsInvalidValueType(value_type) ? value_type : 00073 (value_type & ~SH_VALUETYPE_SPECIAL_MASK); 00074 } 00075 00076 /*** Implementation of the automatic promotion tree lookup 00077 * This decides the argument conversions used in operators 00078 * and the result type given by operators (and library funcs). 00079 * (The tree is built explicitly in ShTypeInfoCasts.cpp as well, 00080 * and this should match those tree edges) 00081 * 00082 * @see ShTypeInfoCasts.cpp 00083 * 00084 * Rules (checked in order until one matches) 00085 * 1) If either is double, use double 00086 * 2) If either is float or fractional, use float 00087 * 3) If both are half, use half 00088 * 4) Use int 00089 */ 00090 template<typename T1, typename T2> 00091 struct ShCommonType { 00092 static const ShValueType V1 = ShStorageTypeInfo<T1>::value_type; 00093 static const ShValueType V2 = ShStorageTypeInfo<T2>::value_type; 00094 00095 static const bool eitherDouble = V1 == SH_DOUBLE || V2 == SH_DOUBLE; 00096 static const bool eitherFloat = V1 == SH_FLOAT || V2 == SH_FLOAT; 00097 static const bool eitherFraction = ShIsFraction<T1>::matches || ShIsFraction<T2>::matches; 00098 static const bool bothHalf = V1 == SH_HALF && V2 == SH_HALF; 00099 00100 static const ShValueType value_type = 00101 (eitherDouble ? 00102 SH_DOUBLE : 00103 ((eitherFloat || eitherFraction) ? 00104 SH_FLOAT : 00105 (bothHalf ? 00106 SH_HALF : 00107 SH_INT))); 00108 00109 typedef typename ShValueTypeInfo<value_type>::storage_type type; 00110 }; 00111 #undef SH_MATCH 00112 00113 00114 } 00115 00116 #endif
1.4.3-20050530