Developer documentation

numerics_hdwi.hpp
Go to the documentation of this file.
1 #ifndef realroot_NUMERICS_HDWI_H
2 #define realroot_NUMERICS_HDWI_H
3 
4 #include <realroot/texp.hpp>
5 
6 namespace mmx {
7 
8 namespace numerics
9 {
10  template< class T >
11  struct is_rounded { static const bool result = false; typedef texp::false_t result_t; };
12  template< >
13  struct is_rounded<float> { static const bool result = true; typedef texp::true_t result_t; };
14  template< >
15  struct is_rounded<double> { static const bool result = true; typedef texp::true_t result_t; };
16  template< >
17  struct is_rounded<long double> { static const bool result = true; typedef texp::true_t result_t; };
18 
19  template< class T >
21  { static const int nbit = sizeof(unsigned)*8*3; };
22  template<>
23  struct bit_resolution<double>
24  { static const int nbit = sizeof(double)*8; };
25  template<>
26  struct bit_resolution<long double> { static const int nbit = sizeof(long double); };
27 
28  template< class T >
29  unsigned bitprec( const T& e, const T& l = T(1.0) )
30  {
31  T tmp(l);
32  unsigned b = 2;
33  while ( tmp > e ) { tmp/=2; b++; };
34  return b;
35  };
36 
37  template<typename hdwi, unsigned n>
38  struct hdwimax { static const hdwi result; };
39  template<typename T,unsigned n>
41  template<typename hdwi>
42  struct hdwimax<hdwi,0> { static const hdwi result; };
43  template<typename T> const T hdwimax<T,0>::result(0);
44  template< class T >
45  struct hdwintp { static const bool result = false; };
46  template< class hardware_int >
47  struct hdwi
48  {
49 
50  typedef hardware_int hdw_int;
51  //enum { nbit = sizeof(hardware_int)*8 };
52  static const unsigned nbit = sizeof(hardware_int)*8;
53  static const hardware_int nmax;
54  static hdw_int reverse( hdw_int a )
55  {
56  hdw_int res = 0;
57  for ( unsigned i = 0; i < nbit; i ++ )
58  {
59  res <<= 1;
60  res |= a & 1;
61  a >>= 1;
62  };
63  return res;
64  };
65  static void reverse( unsigned h, hdw_int& a)
66  {
67  unsigned i;
68  hdw_int c;
69  c = 0;
70  for ( i = 0; i < h; i ++ )
71  {
72  c <<= 1;
73  c |= a&1;
74  a >>= 1;
75  };
76  a = c;
77  };
78  };
79  template<class T> const T hdwi<T>::nmax( hdwimax<T,sizeof(T)*8>::result );
80 
81  template<class unsigned_t> // a faire = sar
82  void sal( unsigned_t& a, unsigned n )
83  {
85  if ( a & 1 )
86  {
87  unsigned_t msk(hdwi<unsigned_t>::nmax);
88  msk >>= (hdwi<unsigned_t>::nbit-n);
89  a <<= n;
90  a |= msk;
91  }
92  else
93  {
94  // std::cout << "SAL( " << n << ") \n";
95  // std::cout << a << std::endl;
96  a <<= n;
97  // std::cout << a << std::endl;
98  };
99 
100  };
101 
102 
103  /* met (ha,a) et (hb,b) dans la meme representation
104  * par un shift arithmetique (SAL), c'est a dire en repetant
105  * le bit de poids faible.
106  */
107  template<typename unsigned_t>
108  void hsal( unsigned& ha, unsigned_t& a, unsigned& hb, unsigned_t& b )
109  {
110  if ( ha == hb ) return; //
111  if ( ha > hb )
112  {
113  sal(b,ha-hb);
114  hb = ha;
115  }
116  else hsal(hb,b,ha,a);
117  };
118 
119 };
120 
121 } //namespace mmx
122 
123 #endif
Definition: numerics_hdwi.hpp:45
static const bool result
Definition: numerics_hdwi.hpp:11
Definition: numerics_hdwi.hpp:11
const C & b
Definition: Interval_glue.hpp:25
texp::true_t result_t
Definition: numerics_hdwi.hpp:17
static hdw_int reverse(hdw_int a)
Definition: numerics_hdwi.hpp:54
void hsal(unsigned &ha, unsigned_t &a, unsigned &hb, unsigned_t &b)
Definition: numerics_hdwi.hpp:108
texp::true_t result_t
Definition: numerics_hdwi.hpp:13
void sal(unsigned_t &a, unsigned n)
Definition: numerics_hdwi.hpp:82
static const unsigned nbit
Definition: numerics_hdwi.hpp:52
Definition: numerics_hdwi.hpp:38
hardware_int hdw_int
Definition: numerics_hdwi.hpp:50
texp::false_t result_t
Definition: numerics_hdwi.hpp:11
static const int nbit
Definition: numerics_hdwi.hpp:21
Definition: numerics_hdwi.hpp:20
structure defining a positive answer
Definition: texp_bool.hpp:7
static void reverse(unsigned h, hdw_int &a)
Definition: numerics_hdwi.hpp:65
static const hdwi result
Definition: numerics_hdwi.hpp:42
static const hdwi result
Definition: numerics_hdwi.hpp:38
texp::true_t result_t
Definition: numerics_hdwi.hpp:15
Definition: numerics_hdwi.hpp:47
unsigned bitprec(const T &e, const T &l=T(1.0))
Definition: numerics_hdwi.hpp:29
const C & c
Definition: Interval_glue.hpp:45
static const bool result
Definition: numerics_hdwi.hpp:45
structure defining a negative answer
Definition: texp_bool.hpp:9
Definition: array.hpp:12
static const hardware_int nmax
Definition: numerics_hdwi.hpp:53
#define assert(expr, msg)
Definition: shared_object.hpp:57
Home