cheshirekow  v0.1.0
divide.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef MPBLOCKS_CUDA_POLYNOMIAL_DIVIDE_H_
28 #define MPBLOCKS_CUDA_POLYNOMIAL_DIVIDE_H_
29 
30 namespace mpblocks {
31 namespace cuda {
32 namespace polynomial {
33 
34 template< class NumSpec, class DenSpec >
36 {
37  enum
38  {
42  };
43 
45 };
46 
47 template < class NumSpec, class DenSpec >
49 {
50  enum
51  {
54  };
55 
57 };
58 
59 template < class NumSpec, class DenSpec >
61 {
62  enum
63  {
66  };
67 
69 };
70 
71 
72 
74 template< bool Enabled, int i, typename Scalar,
75  class NumExp, class NumSpec,
76  class DenExp, class DenSpec >
77 struct Divide
78 {
80  static void step( const NumExp& n, const DenExp& d,
82  Polynomial<Scalar, typename ScratchSpec<NumSpec,DenSpec>::result >& r )
83  {}
84 };
85 
86 template< int i, typename Scalar,
87  class NumExp, class NumSpec,
88  class DenExp, class DenSpec >
89 struct Divide< true, i, Scalar, NumExp, NumSpec, DenExp, DenSpec>
90 {
91  enum
92  {
95  max_coeff_Q = max_coeff_N - max_coeff_D,
96  max_coeff_R = max_coeff_D - 1,
97  coeff_N = max_coeff_N - i,
98  coeff_Q = max_coeff_Q - i,
99  };
100 
102  static void step( const NumExp& n, const DenExp& d,
104  Polynomial<Scalar, typename ScratchSpec<NumSpec,DenSpec>::result >& r )
105  {
106  set<coeff_Q>(q) = get<coeff_N>(r) / get<max_coeff_D>(d);
107  r = n - q*d;
108  }
109 };
110 
112 template< typename Scalar,
113  class NumExp, class NumSpec,
114  class DenExp, class DenSpec >
115 struct Divide< true, 0, Scalar, NumExp,NumSpec,DenExp,DenSpec >
116 {
117  enum
118  {
119  i = 0,
122  max_coeff_Q = max_coeff_N - max_coeff_D,
123  max_coeff_R = max_coeff_D - 1,
124  coeff_N = max_coeff_N - i,
125  coeff_Q = max_coeff_Q - i,
126  };
127 
129  static void step( const NumExp& n, const DenExp& d,
131  Polynomial<Scalar, typename ScratchSpec<NumSpec,DenSpec>::result >& r )
132  {
133  set<coeff_Q>(q) = get<coeff_N>(n) / get<max_coeff_D>(d);
134  r = n - q*d;
135  }
136 };
137 
138 
139 
140 
141 template< typename Scalar,
142  class NumExp, class NumSpec,
143  class DenExp, class DenSpec >
145 void divide( const NumExp& n, const DenExp& d,
148 {
149  enum
150  {
151  max_coeff_N = intlist::max<NumSpec>::value,
152  max_coeff_D = intlist::max<DenSpec>::value,
153  max_coeff_Q = max_coeff_N - max_coeff_D,
154  max_coeff_R = max_coeff_D - 1,
155  end_step = max_coeff_Q + 1
156  };
157 
159  q.fill(0);
170  r = s;
171 }
172 
173 template< typename Scalar, class NumSpec, class DenSpec >
175 void mod(
179 {
180  enum
181  {
182  max_coeff_N = intlist::max<NumSpec>::value,
183  max_coeff_D = intlist::max<DenSpec>::value,
184  max_coeff_Q = max_coeff_N - max_coeff_D,
185  max_coeff_R = max_coeff_D - 1,
186  end_step = max_coeff_Q + 1
187  };
188 
189  typedef Polynomial<Scalar,NumSpec> NumExp;
190  typedef Polynomial<Scalar,DenSpec> DenExp;
191 
192  typedef typename QuotientSpec<NumSpec,DenSpec>::result SpecQ;
193  typedef typename ScratchSpec<NumSpec,DenSpec>::result SpecS;
194 
197 
198  q.fill(0);
209  r = s;
210 }
211 
212 
213 
214 
215 
216 } // namespace polynomial
217 } // namespace cuda
218 } // nampespace mpblocks
219 
220 
221 
222 
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 #endif // DIVIDE_H_
A sparse, statically sized polynomial.
Definition: Polynomial.h:266
#define __forceinline__
Definition: fakecuda.h:38
intlist::range< 0, max_coeff_Q >::result result
Definition: divide.h:44
__host__ __device__ static __forceinline__ void step(const NumExp &n, const DenExp &d, Polynomial< Scalar, typename QuotientSpec< NumSpec, DenSpec >::result > &q, Polynomial< Scalar, typename ScratchSpec< NumSpec, DenSpec >::result > &r)
Definition: divide.h:129
intlist::range< 0, max_coeff_R >::result result
Definition: divide.h:56
__host__ __device__ __forceinline__ void mod(const Polynomial< Scalar, NumSpec > &n, const Polynomial< Scalar, DenSpec > &d, Polynomial< Scalar, typename RemainderSpec< NumSpec, DenSpec >::result > &r)
Definition: divide.h:175
specialization for the first step uses actual numerator, not remainder
Definition: divide.h:77
__host__ __device__ static __forceinline__ void step(const NumExp &n, const DenExp &d, Polynomial< Scalar, typename QuotientSpec< NumSpec, DenSpec >::result > &q, Polynomial< Scalar, typename ScratchSpec< NumSpec, DenSpec >::result > &r)
Definition: divide.h:80
intlist::range< 0, max_coeff_S >::result result
Definition: divide.h:68
__host__ __device__ __forceinline__ void divide(const NumExp &n, const DenExp &d, Polynomial< Scalar, typename QuotientSpec< NumSpec, DenSpec >::result > &q, Polynomial< Scalar, typename RemainderSpec< NumSpec, DenSpec >::result > &r)
Definition: divide.h:145
#define __device__
Definition: fakecuda.h:34
retrieve the max element of the list (linear without short circut so it works with non-sorted arrays ...
Definition: IntList.h:415
#define __host__
Definition: fakecuda.h:37
__host__ __device__ static __forceinline__ void step(const NumExp &n, const DenExp &d, Polynomial< Scalar, typename QuotientSpec< NumSpec, DenSpec >::result > &q, Polynomial< Scalar, typename ScratchSpec< NumSpec, DenSpec >::result > &r)
Definition: divide.h:102