builtin_float.hh

00001 // Copyright (C) 2002, 2003  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef NTG_REAL_BUILTIN_FLOAT_HH
00029 # define NTG_REAL_BUILTIN_FLOAT_HH
00030 
00031 # include <ntg/config/system.hh>
00032 # include <ntg/real/behavior.hh>
00033 # include <ntg/core/internal/global_ops_traits.hh>
00034 # include <ntg/core/internal/traits.hh>
00035 # include <ntg/real/builtin_properties.hh>
00036 # include <ntg/real/optraits_real.hh>
00037 # include <ntg/core/predecls.hh>
00038 
00039 # include <mlc/cmp.hh>
00040 
00041 namespace ntg {
00042 
00043   namespace internal {
00044 
00045     /*------------------.
00046     | typetraits<float> |
00047     `------------------*/
00048 
00049     template <>
00050     struct typetraits<float> : public typetraits<float_value<float> >
00051     {
00052       typedef float     self;
00053       typedef decimal   abstract_type;
00054       typedef self      ntg_type;
00055 
00056       ntg_build_value_type(float_value<E>);
00057 
00058       typedef optraits<self>    optraits_type;
00059       typedef unsafe::get<self> behavior_type;
00060       typedef unsafe            abstract_behavior_type;
00061 
00062       typedef self base_type;
00063       typedef self storage_type;
00064       typedef self signed_type;
00065       typedef self unsigned_type;
00066       typedef self cumul_type;
00067       typedef self largest_type;
00068       typedef self signed_largest_type;
00069       typedef self signed_cumul_type;
00070       typedef self unsigned_largest_type;
00071       typedef self unsigned_cumul_type;
00072       typedef self integer_type;
00073     };
00074 
00075     /*-------------------.
00076     | typetraits<double> |
00077     `-------------------*/
00078 
00079     template <>
00080     struct typetraits<double> : public typetraits<float_value<double> >
00081     {
00082       typedef double    self;
00083       typedef decimal   abstract_type;
00084       typedef self      ntg_type;
00085 
00086       ntg_build_value_type(float_value<E>);
00087 
00088       typedef optraits<self>    optraits_type;
00089       typedef unsafe::get<self> behavior_type;
00090       typedef unsafe            abstract_behavior_type;
00091 
00092       typedef self base_type;
00093       typedef self storage_type;
00094       typedef self signed_type;
00095       typedef self unsigned_type;
00096       typedef self cumul_type;
00097       typedef self largest_type;
00098       typedef self signed_largest_type;
00099       typedef self signed_cumul_type;
00100       typedef self unsigned_largest_type;
00101       typedef self unsigned_cumul_type;
00102       typedef self integer_type;
00103     };
00104 
00105     /*----------------.
00106     | optraits<float> |
00107     `----------------*/
00108 
00109     template <>
00110     struct optraits<float> : public optraits<float_value<float> >
00111     {
00112     private:
00113       typedef float self;
00114       typedef typetraits<self>::storage_type storage_type_;
00115 
00116     public:
00117       // Don't define min() and max() for float and double.
00118       // This is error-prone because max()-1 == max().
00119       // Furthermore max()-min() can't be computed.
00120       static storage_type_ sup() { return NTG_FLOAT_INFINITY; }
00121       static storage_type_ inf() { return - sup (); }
00122       static std::string name()  { return "float"; }
00123     };
00124 
00125     /*--------------------------.
00126     | operator traits for float |
00127     `--------------------------*/
00128 
00129     struct all_float_traits
00130     {
00131       enum { commutative = true };
00132       typedef float ret;
00133       typedef float impl;
00134     };
00135 
00136     //
00137     // plus
00138     //
00139 
00140     template <unsigned nbits, class B>
00141     struct operator_traits<operator_plus, float, int_u<nbits, B> >
00142       : all_float_traits {};
00143 
00144     template <unsigned nbits, class B>
00145     struct operator_traits<operator_plus, float, int_s<nbits, B> >
00146       : all_float_traits {};
00147 
00148     // We need this because ntg_cast(float) gives float.
00149     // So when using cycle<float,..> + float, it searches
00150     // operator_traits<operator_plus, float,float>
00151     template <>
00152     struct operator_traits<operator_plus, float, float>
00153       : all_float_traits {};
00154 
00155     //
00156     // minus
00157     //
00158 
00159     template <unsigned nbits, class B>
00160     struct operator_traits<operator_minus, float, int_u<nbits, B> >
00161       : all_float_traits {};
00162 
00163     template <unsigned nbits, class B>
00164     struct operator_traits<operator_minus, float, int_s<nbits, B> >
00165       : all_float_traits {};
00166 
00167     template <>
00168     struct operator_traits<operator_minus, float, float>
00169       : all_float_traits {};
00170 
00171     //
00172     // times
00173     //
00174 
00175     template <unsigned nbits, class B>
00176     struct operator_traits<operator_times, float, int_u<nbits, B> >
00177       : all_float_traits {};
00178 
00179     template <unsigned nbits, class B>
00180     struct operator_traits<operator_times, float, int_s<nbits, B> >
00181       : all_float_traits {};
00182 
00183     template <>
00184     struct operator_traits<operator_times, float, float>
00185       : all_float_traits {};
00186 
00187     //
00188     // div
00189     //
00190 
00191     template <unsigned nbits, class B>
00192     struct operator_traits<operator_div, float, int_u<nbits, B> >
00193       : all_float_traits {};
00194 
00195     template <unsigned nbits, class B>
00196     struct operator_traits<operator_div, float, int_s<nbits, B> >
00197       : all_float_traits {};
00198 
00199     template <>
00200     struct operator_traits<operator_div, float, float>
00201       : all_float_traits {};
00202 
00203     //
00204     // min
00205     //
00206 
00207     template <unsigned nbits, class B>
00208     struct operator_traits<operator_min, float, int_u<nbits, B> >
00209       : all_float_traits {};
00210 
00211     template <unsigned nbits, class B>
00212     struct operator_traits<operator_min, float, int_s<nbits, B> >
00213       : all_float_traits {};
00214 
00215     template <>
00216     struct operator_traits<operator_min, float, float>
00217       : all_float_traits {};
00218 
00219     //
00220     // max
00221     //
00222 
00223     template <unsigned nbits, class B>
00224     struct operator_traits<operator_max, float, int_u<nbits, B> >
00225       : all_float_traits {};
00226 
00227     template <unsigned nbits, class B>
00228     struct operator_traits<operator_max, float, int_s<nbits, B> >
00229       : all_float_traits {};
00230 
00231     template <>
00232     struct operator_traits<operator_max, float, float>
00233       : all_float_traits {};
00234 
00235     //
00236     // Comparison
00237     //
00238 
00239     template <unsigned nbits, class B>
00240     struct operator_traits<operator_cmp, float, int_u<nbits, B> >
00241       : all_float_traits {};
00242 
00243     template <unsigned nbits, class B>
00244     struct operator_traits<operator_cmp, float, int_s<nbits, B> >
00245       : all_float_traits {};
00246 
00247     template <>
00248     struct operator_traits<operator_cmp, float, float>
00249       : all_float_traits {};
00250 
00251     /*-----------------.
00252     | optraits<double> |
00253     `-----------------*/
00254 
00255     template <>
00256     struct optraits<double> : public optraits<float_value<double> >
00257     {
00258     private:
00259       // shortcuts
00260       typedef double self;
00261       typedef typetraits<self>::storage_type storage_type_;
00262 
00263     public:
00264       // Don't define min() and max() for float and double.
00265       // This is error-prone because max()-1 == max().
00266       // Furthermore max()-min() can't be computed.
00267       static storage_type_ sup() { return NTG_DOUBLE_INFINITY; }
00268       static storage_type_ inf() { return - sup (); }
00269       static std::string name()  { return "double"; }
00270     };
00271 
00272     /*---------------------------.
00273     | operator traits for double |
00274     `---------------------------*/
00275 
00276     struct all_double_traits
00277     {
00278       enum { commutative = true };
00279       typedef double ret;
00280       typedef double impl;
00281     };
00282 
00283     //
00284     // plus
00285     //
00286 
00287     template <unsigned nbits, class B>
00288     struct operator_traits<operator_plus, double, int_u<nbits, B> >
00289       : all_double_traits {};
00290 
00291     template <unsigned nbits, class B>
00292     struct operator_traits<operator_plus, double, int_s<nbits, B> >
00293       : all_double_traits {};
00294 
00295     template <>
00296     struct operator_traits<operator_plus, double, double>
00297       : all_double_traits {};
00298 
00299     template <>
00300     struct operator_traits<operator_plus, double, float>
00301       : all_double_traits {};
00302 
00303     //
00304     // minus
00305     //
00306 
00307     template <unsigned nbits, class B>
00308     struct operator_traits<operator_minus, double, int_u<nbits, B> >
00309       : all_double_traits {};
00310 
00311     template <unsigned nbits, class B>
00312     struct operator_traits<operator_minus, double, int_s<nbits, B> >
00313       : all_double_traits {};
00314 
00315     template <>
00316     struct operator_traits<operator_minus, double, double>
00317       : all_double_traits {};
00318 
00319     template <>
00320     struct operator_traits<operator_minus, double, float>
00321       : all_double_traits {};
00322 
00323     //
00324     // times
00325     //
00326 
00327     template <unsigned nbits, class B>
00328     struct operator_traits<operator_times, double, int_u<nbits, B> >
00329       : all_double_traits {};
00330 
00331     template <unsigned nbits, class B>
00332     struct operator_traits<operator_times, double, int_s<nbits, B> >
00333       : all_double_traits {};
00334 
00335     template <>
00336     struct operator_traits<operator_times, double, double>
00337       : all_double_traits {};
00338 
00339     template <>
00340     struct operator_traits<operator_times, double, float>
00341       : all_double_traits {};
00342 
00343     //
00344     // div
00345     //
00346 
00347     template <unsigned nbits, class B>
00348     struct operator_traits<operator_div, double, int_u<nbits, B> >
00349       : all_double_traits {};
00350 
00351     template <unsigned nbits, class B>
00352     struct operator_traits<operator_div, double, int_s<nbits, B> >
00353       : all_double_traits {};
00354 
00355     template <>
00356     struct operator_traits<operator_div, double, double>
00357       : all_double_traits {};
00358 
00359     template <>
00360     struct operator_traits<operator_div, double, float>
00361       : all_double_traits {};
00362 
00363     //
00364     // min
00365     //
00366 
00367     template <unsigned nbits, class B>
00368     struct operator_traits<operator_min, double, int_u<nbits, B> >
00369       : all_double_traits {};
00370 
00371     template <unsigned nbits, class B>
00372     struct operator_traits<operator_min, double, int_s<nbits, B> >
00373       : all_double_traits {};
00374 
00375     template <>
00376     struct operator_traits<operator_min, double, double>
00377       : all_double_traits {};
00378 
00379     template <>
00380     struct operator_traits<operator_min, double, float>
00381       : all_double_traits {};
00382 
00383     //
00384     // max
00385     //
00386 
00387     template <unsigned nbits, class B>
00388     struct operator_traits<operator_max, double, int_u<nbits, B> >
00389       : all_double_traits {};
00390 
00391     template <unsigned nbits, class B>
00392     struct operator_traits<operator_max, double, int_s<nbits, B> >
00393       : all_double_traits {};
00394 
00395     template <>
00396     struct operator_traits<operator_max, double, double>
00397       : all_double_traits {};
00398 
00399     template <>
00400     struct operator_traits<operator_max, double, float>
00401       : all_double_traits {};
00402 
00403     //
00404     // Comparison
00405     //
00406 
00407     template <unsigned nbits, class B>
00408     struct operator_traits<operator_cmp, double, int_u<nbits, B> >
00409       : all_double_traits {};
00410 
00411     template <unsigned nbits, class B>
00412     struct operator_traits<operator_cmp, double, int_s<nbits, B> >
00413       : all_double_traits {};
00414 
00415     template <>
00416     struct operator_traits<operator_cmp, double, double>
00417       : all_double_traits {};
00418 
00419     template <>
00420     struct operator_traits<operator_cmp, double, float>
00421       : all_double_traits {};
00422 
00423   } // end of internal.
00424 
00425 } // end of ntg.
00426 
00427 #endif // !NTG_REAL_BUILTIN_FLOAT_HH

Generated on Thu Apr 15 20:13:07 2004 for Olena by doxygen 1.3.6-20040222