This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Vector Extensions in GCC


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]