1 | #ifndef MODULOP_H |
---|
2 | #define MODULOP_H |
---|
3 | /**************************************** |
---|
4 | * Computer Algebra System SINGULAR * |
---|
5 | ****************************************/ |
---|
6 | /* |
---|
7 | * ABSTRACT: numbers modulo p (<=32003) |
---|
8 | */ |
---|
9 | #include <misc/auxiliary.h> |
---|
10 | |
---|
11 | // defines are in struct.h |
---|
12 | // define if a*b is with mod instead of tables |
---|
13 | //#define HAVE_MULT_MOD |
---|
14 | // define if a/b is with mod instead of tables |
---|
15 | //#define HAVE_DIV_MOD |
---|
16 | // define if an if should be used |
---|
17 | //#define HAVE_GENERIC_ADD |
---|
18 | |
---|
19 | // enable large primes (32003 < p < 2^31-) |
---|
20 | #define NV_OPS |
---|
21 | #define NV_MAX_PRIME 32003 |
---|
22 | |
---|
23 | struct n_Procs_s; typedef struct n_Procs_s *coeffs; |
---|
24 | struct snumber; typedef struct snumber * number; |
---|
25 | |
---|
26 | BOOLEAN npInitChar(coeffs r, void* p); |
---|
27 | |
---|
28 | // inline number npMultM(number a, number b, int npPrimeM) |
---|
29 | // // return (a*b)%n |
---|
30 | // { |
---|
31 | // double ab; |
---|
32 | // long q, res; |
---|
33 | // |
---|
34 | // ab = ((double) ((int)a)) * ((double) ((int)b)); |
---|
35 | // q = (long) (ab/((double) npPrimeM)); // q could be off by (+/-) 1 |
---|
36 | // res = (long) (ab - ((double) q)*((double) npPrimeM)); |
---|
37 | // res += (res >> 31) & npPrimeM; |
---|
38 | // res -= npPrimeM; |
---|
39 | // res += (res >> 31) & npPrimeM; |
---|
40 | // return (number)res; |
---|
41 | // } |
---|
42 | #ifdef HAVE_MULT_MOD |
---|
43 | static inline number npMultM(number a, number b, const coeffs r) |
---|
44 | { |
---|
45 | return (number) |
---|
46 | ((((unsigned long) a)*((unsigned long) b)) % ((unsigned long) r->ch)); |
---|
47 | } |
---|
48 | #else |
---|
49 | static inline number npMultM(number a, number b, const coeffs r) |
---|
50 | { |
---|
51 | long x = (long)r->npLogTable[(long)a]+ r->npLogTable[(long)b]; |
---|
52 | return (number)(long)r->npExpTable[x<r->npPminus1M ? x : x- r->npPminus1M]; |
---|
53 | } |
---|
54 | #endif |
---|
55 | |
---|
56 | #if 0 |
---|
57 | inline number npAddAsm(number a, number b, int m) |
---|
58 | { |
---|
59 | number r; |
---|
60 | asm ("addl %2, %1; cmpl %3, %1; jb 0f; subl %3, %1; 0:" |
---|
61 | : "=&r" (r) |
---|
62 | : "%0" (a), "g" (b), "g" (m) |
---|
63 | : "cc"); |
---|
64 | return r; |
---|
65 | } |
---|
66 | inline number npSubAsm(number a, number b, int m) |
---|
67 | { |
---|
68 | number r; |
---|
69 | asm ("subl %2, %1; jnc 0f; addl %3, %1; 0:" |
---|
70 | : "=&r" (r) |
---|
71 | : "%0" (a), "g" (b), "g" (m) |
---|
72 | : "cc"); |
---|
73 | return r; |
---|
74 | } |
---|
75 | #endif |
---|
76 | #ifdef HAVE_GENERIC_ADD |
---|
77 | static inline number npAddM(number a, number b, const coeffs r) |
---|
78 | { |
---|
79 | unsigned long R = (unsigned long)a + (unsigned long)b; |
---|
80 | return (number)(R >= r->ch ? R - r->ch : R); |
---|
81 | } |
---|
82 | static inline number npSubM(number a, number b, const coeffs r) |
---|
83 | { |
---|
84 | return (number)((long)a<(long)b ? |
---|
85 | r->ch-(long)b+(long)a : (long)a-(long)b); |
---|
86 | } |
---|
87 | #else |
---|
88 | static inline number npAddM(number a, number b, const coeffs r) |
---|
89 | { |
---|
90 | unsigned long res = (long)((unsigned long)a + (unsigned long)b); |
---|
91 | res -= r->ch; |
---|
92 | #if SIZEOF_LONG == 8 |
---|
93 | res += ((long)res >> 63) & r->ch; |
---|
94 | #else |
---|
95 | res += ((long)res >> 31) & r->ch; |
---|
96 | #endif |
---|
97 | return (number)res; |
---|
98 | } |
---|
99 | static inline number npSubM(number a, number b, const coeffs r) |
---|
100 | { |
---|
101 | long res = ((long)a - (long)b); |
---|
102 | #if SIZEOF_LONG == 8 |
---|
103 | res += (res >> 63) & r->ch; |
---|
104 | #else |
---|
105 | res += (res >> 31) & r->ch; |
---|
106 | #endif |
---|
107 | return (number)res; |
---|
108 | } |
---|
109 | #endif |
---|
110 | |
---|
111 | static inline number npNegM(number a, const coeffs r) |
---|
112 | { |
---|
113 | return (number)((long)(r->ch)-(long)(a)); |
---|
114 | } |
---|
115 | |
---|
116 | static inline BOOLEAN npIsZeroM (number a, const coeffs) |
---|
117 | { |
---|
118 | return 0 == (long)a; |
---|
119 | } |
---|
120 | |
---|
121 | // inline number npMultM(number a, number b, int npPrimeM) |
---|
122 | // { |
---|
123 | // return (number)(((long)a*(long)b) % npPrimeM); |
---|
124 | // } |
---|
125 | |
---|
126 | // The folloing is reused inside gnumpc.cc, gnumpfl.cc and longrat.cc |
---|
127 | int npInt (number &n, const coeffs r); |
---|
128 | |
---|
129 | // The following is currently used in OPAE.cc, OPAEQ.cc and OPAEp.cc for setting their SetMap... |
---|
130 | nMapFunc npSetMap(const coeffs src, const coeffs dst); // FIXME! BUG? |
---|
131 | |
---|
132 | #define npEqualM(A,B,r) ((A)==(B)) |
---|
133 | |
---|
134 | |
---|
135 | #endif |
---|