1 | #ifndef POLYS_IMPL_H |
---|
2 | #define POLYS_IMPL_H |
---|
3 | /**************************************** |
---|
4 | * Computer Algebra System SINGULAR * |
---|
5 | ****************************************/ |
---|
6 | /* $Id: polys-impl.h,v 1.21 1998-03-23 22:51:03 obachman Exp $ */ |
---|
7 | |
---|
8 | /*************************************************************** |
---|
9 | * |
---|
10 | * File: polys-impl.h |
---|
11 | * Purpose: low-level definition and declarations for polys |
---|
12 | * |
---|
13 | * If you touch anything here, you better know what you are doing. |
---|
14 | * What is here should not be used directly from other routines -- the |
---|
15 | * encapsulations in polys.h should be used, instead. |
---|
16 | * |
---|
17 | ***************************************************************/ |
---|
18 | #include "structs.h" |
---|
19 | #include "mmemory.h" |
---|
20 | |
---|
21 | /*************************************************************** |
---|
22 | * |
---|
23 | * definition of the poly structure and its fields |
---|
24 | * |
---|
25 | ***************************************************************/ |
---|
26 | |
---|
27 | // EXPONENT_TYPE is determined by configure und defined in mod2.h |
---|
28 | typedef EXPONENT_TYPE Exponent_t; |
---|
29 | |
---|
30 | #define VARS (10) /*max. number of variables as constant*/ |
---|
31 | |
---|
32 | typedef Exponent_t monomial[VARS + 1]; |
---|
33 | typedef Exponent_t* Exponent_pt; |
---|
34 | |
---|
35 | typedef long Order_t; |
---|
36 | struct spolyrec |
---|
37 | { |
---|
38 | poly next; |
---|
39 | number coef; |
---|
40 | Order_t Order; |
---|
41 | monomial exp; // make sure that exp is aligned |
---|
42 | }; |
---|
43 | |
---|
44 | /*************************************************************** |
---|
45 | * |
---|
46 | * variables/defines used for managment of monomials |
---|
47 | * |
---|
48 | ***************************************************************/ |
---|
49 | |
---|
50 | #define POLYSIZE (sizeof(poly) + sizeof(number) + sizeof(Order_t)) |
---|
51 | #define POLYSIZEW (POLYSIZE / sizeof(long)) |
---|
52 | // number of Variables |
---|
53 | extern int pVariables; |
---|
54 | // size of a monom in bytes - always a multiple of sizeof(void*) |
---|
55 | extern int pMonomSize; |
---|
56 | // size of a monom in units of sizeof(void*) -- i.e. in words |
---|
57 | extern int pMonomSizeW; |
---|
58 | // Ceiling((pVariables+1) / sizeof(void*)) == length of exp-vector in words |
---|
59 | extern int pVariables1W; |
---|
60 | // Ceiling((pVariables) / sizeof(void*)) |
---|
61 | extern int pVariablesW; |
---|
62 | extern int pVarOffset; |
---|
63 | extern int pVarLowIndex; |
---|
64 | extern int pVarHighIndex; |
---|
65 | extern int pVarCompIndex; |
---|
66 | |
---|
67 | /*************************************************************** |
---|
68 | * |
---|
69 | * Primitives for determening/setting the way exponents are arranged |
---|
70 | * |
---|
71 | ***************************************************************/ |
---|
72 | // And here is how we determine the way exponents are stored: |
---|
73 | // There are the following four possibilities: |
---|
74 | // |
---|
75 | // |
---|
76 | // BIGENDIAN -- lex order |
---|
77 | // e_1, e_2, ... , e_n,..,comp: pVarOffset = -1, |
---|
78 | // pVarLowIndex = 0, |
---|
79 | // pVarHighIndex = pVariables-1 |
---|
80 | // pVarCompIndex = pVariables + #(..) |
---|
81 | // BIGENDIAN -- rev lex order |
---|
82 | // e_n, ... , e_2, e_1,..,comp: pVarOffset = pVariables, |
---|
83 | // pVarLowIndex = 0, |
---|
84 | // pVarHighIndex = pVariables-1 |
---|
85 | // pVarCompIndex = pVariables + #(..) |
---|
86 | // LITTLEENDIAN -- rev lex order |
---|
87 | // comp,.., e_1, e_2, ... , e_n : pVarOffset = #(..), |
---|
88 | // pVarLowIndex = 1 + #(..), |
---|
89 | // pVarHighIndex = #(..) + pVariables |
---|
90 | // pVarCompIndex = 0 |
---|
91 | // LITTLEENDIAN -- lex order |
---|
92 | // comp,..,e_n, .... , e_2, e_1 : pVarOffset = pVariables + 1 + #(..) |
---|
93 | // pVarLowIndex = 1 + #(..) |
---|
94 | // pVarHighIndex = #(..) + pVariables |
---|
95 | // pVarCompIndex = 0 |
---|
96 | // |
---|
97 | // Furthermore, the size of the exponent vector is always a multiple |
---|
98 | // of the word size -- "empty exponents" (exactly #(..) ones) are |
---|
99 | // filled in between comp and first/last exponent -- i.e. comp and |
---|
100 | // first/last exponent might not be next to each other |
---|
101 | |
---|
102 | #ifdef WORDS_BIGENDIAN |
---|
103 | |
---|
104 | #define _pHasReverseExp (pVarOffset != -1) |
---|
105 | |
---|
106 | #define _pExpIndex(i) \ |
---|
107 | (pVarOffset == -1 ? (i) - 1 : pVarOffset - (i)) |
---|
108 | #define _pRingExpIndex(r, i) \ |
---|
109 | ((r)->VarOffset == -1 ? (i) - 1 : (r)->VarOffset - (i)) |
---|
110 | |
---|
111 | #define _pCompIndex pVarCompIndex |
---|
112 | #define _pRingCompIndex(r) (r->VarCompIndex) |
---|
113 | |
---|
114 | #else // ! WORDS_BIGENDIAN |
---|
115 | |
---|
116 | #define _pHasReverseExp (pVarOffset > (SIZEOF_LONG / SIZEOF_EXPONENT) - 1) |
---|
117 | |
---|
118 | #define _pExpIndex(i) \ |
---|
119 | (pVarOffset > (SIZEOF_LONG / SIZEOF_EXPONENT) - 1 ? \ |
---|
120 | pVarOffset - (i) : pVarOffset + (i)) |
---|
121 | #define _pRingExpIndex(r, i) \ |
---|
122 | ((r)->VarOffset > (SIZEOF_LONG / SIZEOF_EXPONENT) - 1 ? \ |
---|
123 | (r)->VarOffset - (i) : (r)->VarOffset + (i)) |
---|
124 | |
---|
125 | #define _pCompIndex 0 |
---|
126 | #define _pRingCompIndex(r) 0 |
---|
127 | |
---|
128 | #endif // WORDS_BIGENDIAN |
---|
129 | |
---|
130 | inline void pGetVarIndicies_Lex(int nvars, int &VarOffset, int &VarCompIndex, |
---|
131 | int &VarLowIndex, int &VarHighIndex) |
---|
132 | { |
---|
133 | long temp = (nvars+1)*sizeof(Exponent_t); |
---|
134 | if ((temp % sizeof(long)) == 0) |
---|
135 | temp = temp / sizeof(long); |
---|
136 | else |
---|
137 | temp = (temp / sizeof(long)) + 1; // temp == pVariables1W |
---|
138 | #ifdef WORDS_BIGENDIAN |
---|
139 | VarCompIndex = temp * sizeof(long)/sizeof(Exponent_t) - 1; |
---|
140 | VarOffset = -1; |
---|
141 | VarLowIndex = 0; |
---|
142 | VarHighIndex = nvars - 1; |
---|
143 | #else // ! WORDS_BIGENDIAN |
---|
144 | temp *= sizeof(long)/sizeof(Exponent_t); |
---|
145 | VarHighIndex = temp -1; |
---|
146 | VarLowIndex = temp - nvars; |
---|
147 | VarOffset = temp; |
---|
148 | VarCompIndex = 0; |
---|
149 | #endif // WORDS_BIGENDIAN |
---|
150 | } |
---|
151 | |
---|
152 | #define pSetVarIndicies_Lex(nvars) \ |
---|
153 | pGetVarIndicies_Lex(nvars,pVarOffset,pVarCompIndex, \ |
---|
154 | pVarLowIndex,pVarHighIndex) |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | inline void pGetVarIndicies_RevLex(int nvars,int &VarOffset,int &VarCompIndex, |
---|
159 | int &VarLowIndex, int &VarHighIndex) |
---|
160 | { |
---|
161 | long temp = (nvars+1)*sizeof(Exponent_t); |
---|
162 | if ((temp % sizeof(long)) == 0) |
---|
163 | temp = temp / sizeof(long); |
---|
164 | else |
---|
165 | temp = (temp / sizeof(long)) + 1; // temp == pVariables1W |
---|
166 | #ifdef WORDS_BIGENDIAN |
---|
167 | VarCompIndex = temp * sizeof(long)/sizeof(Exponent_t) - 1; |
---|
168 | VarOffset = nvars; |
---|
169 | VarLowIndex = 0; |
---|
170 | VarHighIndex = nvars-1; |
---|
171 | #else // ! WORDS_BIGENDIAN |
---|
172 | temp *= sizeof(long)/sizeof(Exponent_t); |
---|
173 | VarHighIndex = temp -1; |
---|
174 | VarLowIndex = temp - nvars; |
---|
175 | VarOffset = temp - nvars -1; |
---|
176 | VarCompIndex = 0; |
---|
177 | #endif // WORDS_BIGENDIAN |
---|
178 | } |
---|
179 | |
---|
180 | #define pSetVarIndicies_RevLex(nvars) \ |
---|
181 | pGetVarIndicies_RevLex(nvars,pVarOffset,pVarCompIndex, \ |
---|
182 | pVarLowIndex,pVarHighIndex) |
---|
183 | |
---|
184 | // The default settings: |
---|
185 | inline void pGetVarIndicies(int nvars, int &VarOffset, int &VarCompIndex, |
---|
186 | int &VarLowIndex, int &VarHighIndex) |
---|
187 | { |
---|
188 | pGetVarIndicies_Lex(nvars,VarOffset, VarCompIndex, VarLowIndex,VarHighIndex); |
---|
189 | } |
---|
190 | // gets var indicies w.r.t. the ring r |
---|
191 | extern void pGetVarIndicies(ring r, int &VarOffset, int &VarCompIndex, |
---|
192 | int &VarLowIndex, int &VarHighIndex); |
---|
193 | |
---|
194 | #define pSetVarIndicies(nvars) \ |
---|
195 | pGetVarIndicies(nvars, pVarOffset, pVarCompIndex, \ |
---|
196 | pVarLowIndex, pVarHighIndex) |
---|
197 | |
---|
198 | |
---|
199 | /*************************************************************** |
---|
200 | * |
---|
201 | * Primitives for accessing and setting fields of a poly |
---|
202 | * |
---|
203 | ***************************************************************/ |
---|
204 | #define _pNext(p) ((p)->next) |
---|
205 | #define _pIter(p) ((p) = (p)->next) |
---|
206 | |
---|
207 | #define _pGetCoeff(p) ((p)->coef) |
---|
208 | #define _pSetCoeff(p,n) {nDelete(&((p)->coef));(p)->coef=n;} |
---|
209 | #define _pSetCoeff0(p,n) (p)->coef=n |
---|
210 | |
---|
211 | #define _pGetOrder(p) ((p)->Order) |
---|
212 | |
---|
213 | #if defined(PDEBUG) && PDEBUG != 0 |
---|
214 | extern Exponent_t pPDSetExp(poly p, int v, Exponent_t e, char* f, int l); |
---|
215 | extern Exponent_t pPDGetExp(poly p, int v, char* f, int l); |
---|
216 | extern Exponent_t pPDIncrExp(poly p, int v, char* f, int l); |
---|
217 | extern Exponent_t pPDDecrExp(poly p, int v, char* f, int l); |
---|
218 | extern Exponent_t pPDAddExp(poly p, int v, Exponent_t e, char* f, int l); |
---|
219 | extern Exponent_t pPDMultExp(poly p, int v, Exponent_t e, char* f, int l); |
---|
220 | extern Exponent_t pPDSubExp(poly p, int v, Exponent_t e, char* f, int l); |
---|
221 | |
---|
222 | extern Exponent_t pPDRingSetExp(ring r,poly p,int v,Exponent_t e,char* f,int l); |
---|
223 | extern Exponent_t pPDRingGetExp(ring r,poly p, int v, char* f, int l); |
---|
224 | |
---|
225 | #define _pSetExp(p,v,e) pPDSetExp(p,v,e,__FILE__,__LINE__) |
---|
226 | #define _pGetExp(p,v) pPDGetExp(p,v,__FILE__,__LINE__) |
---|
227 | #define _pIncrExp(p,v) pPDIncrExp(p,v,__FILE__,__LINE__) |
---|
228 | #define _pDecrExp(p,v) pPDDecrExp(p,v,__FILE__,__LINE__) |
---|
229 | #define _pAddExp(p,i,v) pPDAddExp(p,i,v,__FILE__,__LINE__) |
---|
230 | #define _pSubExp(p,i,v) pPDSubExp(p,i,v,__FILE__,__LINE__) |
---|
231 | #define _pMultExp(p,i,v) pPDMultExp(p,i,v,__FILE__,__LINE__) |
---|
232 | |
---|
233 | #define _pRingSetExp(r,p,v,e) pPDRingSetExp(r,p,v,e,__FILE__,__LINE__) |
---|
234 | #define _pRingGetExp(r,p,v) pPDRingGetExp(r,p,v,__FILE__,__LINE__) |
---|
235 | |
---|
236 | #else // ! (defined(PDEBUG) && PDEBUG != 0) |
---|
237 | |
---|
238 | #define _pSetExp(p,v,e) (p)->exp[_pExpIndex(v)]=(e) |
---|
239 | #define _pGetExp(p,v) (p)->exp[_pExpIndex(v)] |
---|
240 | #define _pIncrExp(p,v) ((p)->exp[_pExpIndex(v)])++ |
---|
241 | #define _pDecrExp(p,v) ((p)->exp[_pExpIndex(v)])-- |
---|
242 | #define _pAddExp(p,i,v) ((p)->exp[_pExpIndex(i)]) += (v) |
---|
243 | #define _pSubExp(p,i,v) ((p)->exp[_pExpIndex(i)]) -= (v) |
---|
244 | #define _pMultExp(p,i,v) ((p)->exp[_pExpIndex(i)]) *= (v) |
---|
245 | |
---|
246 | #define _pRingSetExp(r,p,v,e) (p)->exp[_pRingExpIndex(r,v)]=(e) |
---|
247 | #define _pRingGetExp(r,p,v) (p)->exp[_pRingExpIndex(r,v)] |
---|
248 | |
---|
249 | #endif // defined(PDEBUG) && PDEBUG != 0 |
---|
250 | |
---|
251 | inline Exponent_t _pGetExpSum(poly p1, poly p2, int i) |
---|
252 | { |
---|
253 | int index = _pExpIndex(i); |
---|
254 | return p1->exp[index] + p2->exp[index]; |
---|
255 | } |
---|
256 | inline Exponent_t _pGetExpDiff(poly p1, poly p2, int i) |
---|
257 | { |
---|
258 | int index = _pExpIndex(i); |
---|
259 | return p1->exp[index] - p2->exp[index]; |
---|
260 | } |
---|
261 | |
---|
262 | #define _pGetComp(p) ((p)->exp[_pCompIndex]) |
---|
263 | #define _pSetComp(p,k) _pGetComp(p) = (k) |
---|
264 | #define _pIncrComp(p) _pGetComp(p)++ |
---|
265 | #define _pDecrComp(p) _pGetComp(p)-- |
---|
266 | #define _pAddComp(p,v) _pGetComp(p) += (v) |
---|
267 | #define _pSubComp(p,v) _pGetComp(p) -= (v) |
---|
268 | |
---|
269 | #define _pRingGetComp(r,p) ((p)->exp[_pRingCompIndex(r)]) |
---|
270 | #define _pRingSetComp(r,p,k) (_pRingGetComp(p) = (k)) |
---|
271 | |
---|
272 | inline void _pGetExpV(poly p, Exponent_t *ev) |
---|
273 | { |
---|
274 | if (_pHasReverseExp) |
---|
275 | { |
---|
276 | for (int i = pVarLowIndex, j = pVariables; j; i++, j--) |
---|
277 | ev[j] = p->exp[i]; |
---|
278 | } |
---|
279 | else |
---|
280 | memcpy(&ev[1], &(p->exp[pVarLowIndex]), pVariables*sizeof(Exponent_t)); |
---|
281 | ev[0] = _pGetComp(p); |
---|
282 | } |
---|
283 | |
---|
284 | extern pSetmProc pSetm; |
---|
285 | inline void _pSetExpV(poly p, Exponent_t *ev) |
---|
286 | { |
---|
287 | if (_pHasReverseExp) |
---|
288 | { |
---|
289 | for (int i = pVarLowIndex, j = pVariables; j; i++, j--) |
---|
290 | p->exp[i] = ev[j]; |
---|
291 | } |
---|
292 | else |
---|
293 | memcpy(&(p->exp[pVarLowIndex]), &ev[1], pVariables*sizeof(Exponent_t)); |
---|
294 | _pSetComp(p, ev[0]); |
---|
295 | pSetm(p); |
---|
296 | } |
---|
297 | |
---|
298 | /*************************************************************** |
---|
299 | * |
---|
300 | * Storage Managament Routines |
---|
301 | * |
---|
302 | ***************************************************************/ |
---|
303 | #ifdef PDEBUG |
---|
304 | |
---|
305 | poly pDBNew(char *f, int l); |
---|
306 | poly pDBInit(char * f,int l); |
---|
307 | |
---|
308 | void pDBDelete(poly * a, char * f, int l); |
---|
309 | void pDBDelete1(poly * a, char * f, int l); |
---|
310 | void pDBFree1(poly a, char * f, int l); |
---|
311 | |
---|
312 | poly pDBCopy(poly a, char *f, int l); |
---|
313 | poly pDBCopy1(poly a, char *f, int l); |
---|
314 | poly pDBHead(poly a, char *f, int l); |
---|
315 | poly pDBHead0(poly a, char *f, int l); |
---|
316 | poly pDBFetchCopy(ring r, poly a, char *f, int l); |
---|
317 | |
---|
318 | void pDBDelete(poly * a, char * f, int l); |
---|
319 | void pDBDelete1(poly * a, char * f, int l); |
---|
320 | void pDBFree1(poly a, char * f, int l); |
---|
321 | |
---|
322 | #define _pNew() pDBNew(__FILE__,__LINE__) |
---|
323 | #define _pInit() pDBInit(__FILE__,__LINE__) |
---|
324 | |
---|
325 | #define _pDelete(a) pDBDelete((a),__FILE__,__LINE__) |
---|
326 | #define _pDelete1(a) pDBDelete1((a),__FILE__,__LINE__) |
---|
327 | #define _pFree1(a) \ |
---|
328 | do \ |
---|
329 | { \ |
---|
330 | pDBFree1((a),__FILE__,__LINE__); \ |
---|
331 | (a)=NULL; \ |
---|
332 | } \ |
---|
333 | while(0) |
---|
334 | |
---|
335 | #define _pCopy(A) pDBCopy(A,__FILE__,__LINE__) |
---|
336 | #define _pCopy1(A) pDBCopy1(A, __FILE__,__LINE__) |
---|
337 | #define _pHead(A) pDBHead(A,__FILE__,__LINE__) |
---|
338 | #define _pHead0(A) pDBHead0(A, __FILE__,__LINE__) |
---|
339 | #define _pFetchCopy(r,A) pDBFetchCopy(r, A,__FILE__,__LINE__) |
---|
340 | |
---|
341 | #else // ! PDEBUG |
---|
342 | |
---|
343 | #ifdef MDEBUG |
---|
344 | #define _pNew() (poly) mmDBAllocSpecialized(__FILE__,__LINE__) |
---|
345 | #else |
---|
346 | #define _pNew() (poly) mmAllocSpecialized() |
---|
347 | #endif |
---|
348 | |
---|
349 | #include <string.h> |
---|
350 | inline poly _pInit(void) |
---|
351 | { |
---|
352 | #ifdef MDEBUG |
---|
353 | poly p=(poly)mmDBAllocSpecialized(__FILE__,__LINE__); |
---|
354 | #else |
---|
355 | poly p=(poly)mmAllocSpecialized(); |
---|
356 | #endif |
---|
357 | memset(p,0, pMonomSize); |
---|
358 | return p; |
---|
359 | } |
---|
360 | |
---|
361 | extern void _pDelete(poly * a); |
---|
362 | extern void _pDelete1(poly * a); |
---|
363 | #ifdef MDEBUG |
---|
364 | #define _pFree1(a) mmDBFreeSpecialized((ADDRESS)a,__FILE__,__LINE__) |
---|
365 | #else |
---|
366 | #define _pFree1(a) mmFreeSpecialized((ADDRESS)a) |
---|
367 | #endif |
---|
368 | |
---|
369 | extern poly _pCopy(poly a); |
---|
370 | extern poly _pCopy1(poly a); |
---|
371 | extern poly _pHead(poly a); |
---|
372 | extern poly _pHead0(poly a); |
---|
373 | extern poly _pFetchCopy(ring r,poly a); |
---|
374 | #endif // PDEBUG |
---|
375 | |
---|
376 | #define _pCopy2(p1, p2) memcpyW(p1, p2, pMonomSizeW) |
---|
377 | |
---|
378 | /*************************************************************** |
---|
379 | * |
---|
380 | * Routines which work on vectors instead of single exponents |
---|
381 | * |
---|
382 | ***************************************************************/ |
---|
383 | // Here is a handy Macro which disables inlining when run with |
---|
384 | // profiling and enables it otherwise |
---|
385 | |
---|
386 | #ifdef DO_DEEP_PROFILE |
---|
387 | |
---|
388 | #ifndef POLYS_IMPL_CC |
---|
389 | |
---|
390 | #define DECLARE(type, arglist) type arglist; \ |
---|
391 | static type dummy_##arglist |
---|
392 | #else |
---|
393 | #define DECLARE(type, arglist) type arglist |
---|
394 | #endif // POLYS_IMPL_CC |
---|
395 | |
---|
396 | #else //! DO_DEEP_PROFILE |
---|
397 | |
---|
398 | #define DECLARE(type, arglist ) inline type arglist |
---|
399 | |
---|
400 | #endif // DO_DEEP_PROFILE |
---|
401 | |
---|
402 | |
---|
403 | #if defined(PDEBUG) && PDEBUG == 1 |
---|
404 | #define _pMonAddFast(p1, p2) pDBMonAddFast(p1, p2, __FILE__, __LINE__) |
---|
405 | extern void pDBMonAddFast(poly p1, poly p2, char* f, int l); |
---|
406 | inline void __pMonAddFast(poly p1, poly p2) |
---|
407 | #else |
---|
408 | DECLARE(void, _pMonAddFast(poly p1, poly p2)) |
---|
409 | #endif // defined(PDEBUG) && PDEBUG == 1 |
---|
410 | { |
---|
411 | // OK -- this might be the only place where we are kind of quick and |
---|
412 | // dirty: the following only works correctly if all exponents are |
---|
413 | // positive and the sum of two exponents does not exceed |
---|
414 | // EXPONENT_MAX |
---|
415 | Exponent_t c2 = _pGetComp(p2); |
---|
416 | int i = pVariables1W; |
---|
417 | unsigned long* s1 = (unsigned long*) &(p1->exp[0]); |
---|
418 | const unsigned long* s2 = (unsigned long*) &(p2->exp[0]); |
---|
419 | // set comp of p2 temporarily to 0, so that nothing is added to comp of p1 |
---|
420 | _pSetComp(p2, 0); |
---|
421 | |
---|
422 | for (;;) |
---|
423 | { |
---|
424 | *s1 += *s2; |
---|
425 | i--; |
---|
426 | if (i==0) break; |
---|
427 | s1++; |
---|
428 | s2++; |
---|
429 | } |
---|
430 | // reset comp of p2 |
---|
431 | _pSetComp(p2, c2); |
---|
432 | _pGetOrder(p1) += _pGetOrder(p2); |
---|
433 | } |
---|
434 | |
---|
435 | // Makes p1 a copy of p2 and adds on exponets of p3 |
---|
436 | #if defined(PDEBUG) && PDEBUG == 1 |
---|
437 | #define _pCopyAddFast(p1, p2, p3) pDBCopyAddFast(p1, p2, p3, __FILE__, __LINE__) |
---|
438 | extern void pDBCopyAddFast(poly p1, poly p2, poly p3, char* f, int l); |
---|
439 | inline void __pCopyAddFast(poly p1, poly p2, poly p3) |
---|
440 | #else |
---|
441 | DECLARE(void, _pCopyAddFast(poly p1, poly p2, poly p3)) |
---|
442 | #endif // defined(PDEBUG) && PDEBUG == 1 |
---|
443 | { |
---|
444 | unsigned long* s1 = (unsigned long*) &(p1->exp[0]); |
---|
445 | const unsigned long* s2 = (unsigned long*) &(p2->exp[0]); |
---|
446 | const unsigned long* s3 = (unsigned long*) &(p3->exp[0]); |
---|
447 | const unsigned long* const ub = s3 + pVariables1W; |
---|
448 | |
---|
449 | p1->next = p2->next; |
---|
450 | p1->coef = p2->coef; |
---|
451 | |
---|
452 | for (;;) |
---|
453 | { |
---|
454 | *s1 = *s2 + *s3; |
---|
455 | s3++; |
---|
456 | if (s3 == ub) break; |
---|
457 | s1++; |
---|
458 | s2++; |
---|
459 | } |
---|
460 | // we first are supposed to do a copy from p2 to p1 -- therefore, |
---|
461 | // component of p1 is set to comp of p2 |
---|
462 | _pSetComp(p1, _pGetComp(p2)); |
---|
463 | _pGetOrder(p1) = _pGetOrder(p2) + _pGetOrder(p3); |
---|
464 | } |
---|
465 | |
---|
466 | // Similar to pCopyAddFast, except that we do not care about the "next" field |
---|
467 | #if defined(PDEBUG) && PDEBUG == 1 |
---|
468 | #define _pCopyAddFast0(p1, p2, p3) pDBCopyAddFast(p1, p2, p3, __FILE__, __LINE__) |
---|
469 | extern void pDBCopyAddFast(poly p1, poly p2, poly p3, char* f, int l); |
---|
470 | inline void __pCopyAddFast0(poly p1, poly p2, poly p3) |
---|
471 | #else |
---|
472 | DECLARE(void, _pCopyAddFast0(poly p1, poly p2, poly p3)) |
---|
473 | #endif // defined(PDEBUG) && PDEBUG == 1 |
---|
474 | { |
---|
475 | unsigned long* s1 = (unsigned long*) &(p1->exp[0]); |
---|
476 | const unsigned long* s2 = (unsigned long*) &(p2->exp[0]); |
---|
477 | const unsigned long* s3 = (unsigned long*) &(p3->exp[0]); |
---|
478 | const unsigned long* const ub = s3 + pVariables1W; |
---|
479 | |
---|
480 | p1->coef = p2->coef; |
---|
481 | |
---|
482 | for (;;) |
---|
483 | { |
---|
484 | *s1 = *s2 + *s3; |
---|
485 | s3++; |
---|
486 | if (s3 == ub) break; |
---|
487 | s1++; |
---|
488 | s2++; |
---|
489 | } |
---|
490 | _pSetComp(p1, _pGetComp(p2)); |
---|
491 | _pGetOrder(p1) = _pGetOrder(p2) + _pGetOrder(p3); |
---|
492 | } |
---|
493 | |
---|
494 | // Similar to pCopyAddFast0, except that we do not recompute the Order, |
---|
495 | // but assume that it is the sum of the Order of p2 and p3 |
---|
496 | #if defined(PDEBUG) && PDEBUG == 1 |
---|
497 | #define _pCopyAddFastHomog(p1, p2, p3, Order) \ |
---|
498 | pDBCopyAddFastHomog(p1, p2, p3, Order, __FILE__, __LINE__) |
---|
499 | extern void pDBCopyAddFastHomog(poly p1, poly p2, poly p3, Order_t Order, |
---|
500 | char* f, int l); |
---|
501 | inline void __pCopyAddFastHomog(poly p1, poly p2, poly p3, Order_t Order) |
---|
502 | #else |
---|
503 | DECLARE(void, _pCopyAddFastHomog(poly p1, poly p2, poly p3, Order_t Order)) |
---|
504 | #endif // defined(PDEBUG) && PDEBUG == 1 |
---|
505 | { |
---|
506 | unsigned long* s1 = (unsigned long*) &(p1->exp[0]); |
---|
507 | const unsigned long* s2 = (unsigned long*) &(p2->exp[0]); |
---|
508 | const unsigned long* s3 = (unsigned long*) &(p3->exp[0]); |
---|
509 | const unsigned long* const ub = s3 + pVariables1W; |
---|
510 | |
---|
511 | p1->coef = p2->coef; |
---|
512 | p1->Order = Order; |
---|
513 | |
---|
514 | for (;;) |
---|
515 | { |
---|
516 | *s1 = *s2 + *s3; |
---|
517 | s3++; |
---|
518 | if (s3 == ub) break; |
---|
519 | s1++; |
---|
520 | s2++; |
---|
521 | } |
---|
522 | _pSetComp(p1, _pGetComp(p2)); |
---|
523 | } |
---|
524 | |
---|
525 | #if SIZEOF_LONG == 4 |
---|
526 | |
---|
527 | #if SIZEOF_EXPONENT == 1 |
---|
528 | #define P_DIV_MASK 0x80808080 |
---|
529 | #else // SIZEOF_EXPONENT == 2 |
---|
530 | #define P_DIV_MASK 0x80008000 |
---|
531 | #endif |
---|
532 | |
---|
533 | #else // SIZEOF_LONG == 8 |
---|
534 | |
---|
535 | #if SIZEOF_EXPONENT == 1 |
---|
536 | #define P_DIV_MASK 0x8080808080808080 |
---|
537 | #elif SIZEOF_EXPONENT == 2 |
---|
538 | #define P_DIV_MASK 0x8000800080008000 |
---|
539 | #else // SIZEOF_EXPONENT == 4 |
---|
540 | #define P_DIV_MASK 0x8000000080000000 |
---|
541 | #endif |
---|
542 | |
---|
543 | #endif |
---|
544 | |
---|
545 | DECLARE(BOOLEAN, __pDivisibleBy(poly a, poly b)) |
---|
546 | { |
---|
547 | #ifdef WORDS_BIGENDIAN |
---|
548 | const unsigned long* const lb = (unsigned long*) &(a->exp[0]);; |
---|
549 | const unsigned long* s1 = ((unsigned long*) a) + pMonomSizeW -1; |
---|
550 | const unsigned long* s2 = ((unsigned long*) b) + pMonomSizeW -1; |
---|
551 | #else |
---|
552 | const unsigned long* const lb = ((unsigned long*) a) + pMonomSizeW; |
---|
553 | const unsigned long* s1 = (unsigned long*) &(a->exp[0]); |
---|
554 | const unsigned long* s2 = (unsigned long*) &(b->exp[0]); |
---|
555 | #endif |
---|
556 | |
---|
557 | for (;;) |
---|
558 | { |
---|
559 | // Yes, the following is correct, provided that the exponents do |
---|
560 | // not have their first bit set |
---|
561 | if ((*s2 - *s1) & P_DIV_MASK) return FALSE; |
---|
562 | #ifdef WORDS_BIGENDIAN |
---|
563 | if (s1 == lb) return TRUE; |
---|
564 | s1--; |
---|
565 | s2--; |
---|
566 | #else |
---|
567 | s1++; |
---|
568 | if (s1 == lb) return TRUE; |
---|
569 | s2++; |
---|
570 | #endif |
---|
571 | } |
---|
572 | } |
---|
573 | |
---|
574 | #if defined(PDEBUG) && PDEBUG == 1 |
---|
575 | #define _pDivisibleBy(a,b) pDBDivisibleBy(a, b, __FILE__, __LINE__) |
---|
576 | extern BOOLEAN pDBDivisibleBy(poly p1, poly p2, char* f, int l); |
---|
577 | inline BOOLEAN _pDivisibleBy_orig(poly a, poly b) |
---|
578 | #else |
---|
579 | inline BOOLEAN _pDivisibleBy(poly a, poly b) |
---|
580 | #endif // defined(PDEBUG) && PDEBUG == 1 |
---|
581 | { |
---|
582 | if ((a!=NULL)&&((_pGetComp(a)==0) || (_pGetComp(a) == _pGetComp(b)))) |
---|
583 | { |
---|
584 | return __pDivisibleBy(a,b); |
---|
585 | } |
---|
586 | return FALSE; |
---|
587 | } |
---|
588 | |
---|
589 | #if defined(PDEBUG) && PDEBUG == 1 |
---|
590 | #define _pDivisibleBy1(a,b) pDBDivisibleBy1(a, b, __FILE__, __LINE__) |
---|
591 | extern BOOLEAN pDBDivisibleBy1(poly p1, poly p2, char* f, int l); |
---|
592 | inline BOOLEAN _pDivisibleBy1_orig(poly a, poly b) |
---|
593 | #else |
---|
594 | inline BOOLEAN _pDivisibleBy1(poly a, poly b) |
---|
595 | #endif // defined(PDEBUG) && PDEBUG == 1 |
---|
596 | { |
---|
597 | if (_pGetComp(a) == 0 || _pGetComp(a) == _pGetComp(b)) |
---|
598 | return __pDivisibleBy(a,b); |
---|
599 | return FALSE; |
---|
600 | } |
---|
601 | |
---|
602 | #if defined(PDEBUG) && PDEBUG == 1 |
---|
603 | #define _pDivisibleBy2(a,b) pDBDivisibleBy2(a, b, __FILE__, __LINE__) |
---|
604 | extern BOOLEAN pDBDivisibleBy2(poly p1, poly p2, char* f, int l); |
---|
605 | #else |
---|
606 | #define _pDivisibleBy2(a,b) __pDivisibleBy(a,b) |
---|
607 | #endif // defined(PDEBUG) && PDEBUG == 1 |
---|
608 | |
---|
609 | |
---|
610 | DECLARE(BOOLEAN, _pEqual(poly p1, poly p2)) |
---|
611 | { |
---|
612 | const long *s1 = (long*) &(p1->exp[0]); |
---|
613 | const long *s2 = (long*) &(p2->exp[0]); |
---|
614 | const long* const lb = s1 + pVariables1W; |
---|
615 | |
---|
616 | for(;;) |
---|
617 | { |
---|
618 | if (*s1 != *s2) return FALSE; |
---|
619 | s1++; |
---|
620 | if (s1 == lb) return TRUE; |
---|
621 | s2++; |
---|
622 | } |
---|
623 | } |
---|
624 | |
---|
625 | /*************************************************************** |
---|
626 | * |
---|
627 | * Routines which implement low-level manipulations/operations |
---|
628 | * on exponents and "are allowed" to access single exponetns |
---|
629 | * |
---|
630 | ***************************************************************/ |
---|
631 | |
---|
632 | DECLARE(int, __pExpQuerSum2(poly p, int from, int to)) |
---|
633 | { |
---|
634 | int j = p->exp[from]; |
---|
635 | int i = from + 1; |
---|
636 | |
---|
637 | for(;;) |
---|
638 | { |
---|
639 | if (i > to) return j; |
---|
640 | j += p->exp[i]; |
---|
641 | i++; |
---|
642 | } |
---|
643 | } |
---|
644 | |
---|
645 | #define _pExpQuerSum(p) __pExpQuerSum2(p, pVarLowIndex, pVarHighIndex) |
---|
646 | |
---|
647 | #define _pExpQuerSum1(p,to) \ |
---|
648 | (_pHasReverseExp ? \ |
---|
649 | __pExpQuerSum2(p, _pExpIndex(to), _pExpIndex(1)) : \ |
---|
650 | __pExpQuerSum2(p, _pExpIndex(1), _pExpIndex(to))) |
---|
651 | |
---|
652 | #define _pExpQuerSum2(p,from,to) \ |
---|
653 | (_pHasReverseExp ? \ |
---|
654 | __pExpQuerSum2(p, _pExpIndex(to), _pExpIndex(from)) : \ |
---|
655 | __pExpQuerSum2(p, _pExpIndex(from), _pExpIndex(to))) |
---|
656 | |
---|
657 | #endif // POLYS_IMPL_H |
---|