This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Vector Extensions in GCC
- To: Mark Mitchell <mark at codesourcery dot com>
- Subject: Re: Vector Extensions in GCC
- From: Gabriel Dos Reis <Gabriel dot Dos-Reis at cmla dot ens-cachan dot fr>
- Date: 12 Jul 2001 10:57:52 +0200
- Cc: "Joseph S. Myers" <jsm28 at cam dot ac dot uk>, Stan Shebs <shebs at apple dot com>, "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Organization: CMLA, ENS Cachan -- CNRS UMR 8536 (France)
- References: <112560000.994923891@warlock.codesourcery.com>
Mark Mitchell <mark@codesourcery.com> writes:
| For example:
|
| template <typename T> T fmax (T t1, T t2)
| { return t1 ? t2 : t1 : t2; }
|
| I am not sure what the right semantics are if `t1' and `t2' do
| not have the same type. You might still need specializations to
| handle all the arithmetic promotions, etc.
Yes, I did show something similar on comp.std.c when Zack once asked a
question about that. The actual code is fairly obvious:
1) Write a class template (with specialization) to compute promotion.
template<typename T, typename U>
struct promote { } ;
template<typename T>
struct promote<T, T> { typedef T type; };
#define SPECIALIZE_PROMOTE(T, U, V) \
template<> struct promote<T, U> { typedef V type; }; \
template<> struct promote<U, T> { typedef V type; }
SPECIALIZE_PROMOTE(float, double, double);
SPECIALIZE_PROMOTE(float, long double, long double);
SPECIALIZE_PROMOTE(double, long double, long double);
// and so one.
2) Write the algorithme to operate on ONE type
struct fmax_algorithm {
template<typename T>
static T compute(T a, T b) { return a < b ? b : a; }
};
3) Write the actual function using the promoter:
template<typeanme U, typename V>
typename promote<U, V>::type fmax(U a, V b)
{
typedef typename typename promote<U, V>::type T;
return fmax_algorithm::compute((T)a, (T)b);
}
That is just a sketch and is refinable.
-- Gaby