source: git/libpolys/coeffs/modulop.h @ e77676

spielwiese
Last change on this file since e77676 was e77676, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
Zp cleanup (no more coeffs::npPrimeM) CHG: removed coeffs::npPrimeM in favour of coeffs::ch
  • Property mode set to 100644
File size: 4.6 KB
Line 
1#ifndef MODULOP_H
2#define MODULOP_H
3/****************************************
4*  Computer Algebra System SINGULAR     *
5****************************************/
6/* $Id$ */
7/*
8* ABSTRACT: numbers modulo p (<=32003)
9*/
10#include <coeffs/coeffs.h>
11
12// defines are in struct.h
13// define if a*b is with mod instead of tables
14//#define HAVE_MULT_MOD
15// define if a/b is with mod instead of tables
16//#define HAVE_DIV_MOD
17// define if an if should be used
18//#define HAVE_GENERIC_ADD
19
20// enable large primes (32003 < p < 2^31-)
21#define NV_OPS
22#define NV_MAX_PRIME 32003
23
24// extern int npGen; // obsolete
25
26BOOLEAN npInitChar(coeffs r, void* p);
27
28BOOLEAN npGreaterZero (number k, const coeffs r);
29number  npMult        (number a, number b, const coeffs r);
30number  npInit        (int i, const coeffs r);
31int     npInt         (number &n, const coeffs r);
32number  npAdd         (number a, number b,const coeffs r);
33number  npSub         (number a, number b,const coeffs r);
34void    npPower       (number a, int i, number * result,const coeffs r);
35BOOLEAN npIsZero      (number a,const coeffs r);
36BOOLEAN npIsOne       (number a,const coeffs r);
37BOOLEAN npIsMOne       (number a,const coeffs r);
38number  npDiv         (number a, number b,const coeffs r);
39number  npNeg         (number c,const coeffs r);
40number  npInvers      (number c,const coeffs r);
41BOOLEAN npGreater     (number a, number b,const coeffs r);
42BOOLEAN npEqual       (number a, number b,const coeffs r);
43void    npWrite       (number &a, const coeffs r);
44void    npCoeffWrite  (const coeffs r, BOOLEAN details);
45const char *  npRead  (const char *s, number *a,const coeffs r);
46#ifdef LDEBUG
47BOOLEAN npDBTest      (number a, const char *f, const int l, const coeffs r);
48#define npTest(A,r)     npDBTest(A,__FILE__,__LINE__, r)
49#else
50#define npTest(A,r)     (0)
51#endif
52
53//int     npGetChar();
54
55nMapFunc npSetMap(const coeffs src, const coeffs dst);
56number  npMapP(number from, const coeffs src, const coeffs r);
57/*-------specials for spolys, do NOT use otherwise--------------------------*/
58/* for npMultM, npSubM, npNegM, npEqualM : */
59#ifdef HAVE_DIV_MOD
60extern unsigned short *npInvTable;
61#else
62#ifndef HAVE_MULT_MOD
63extern long npPminus1M;
64extern unsigned short *npExpTable;
65extern unsigned short *npLogTable;
66#endif
67#endif
68
69// inline number npMultM(number a, number b, int npPrimeM)
70// // return (a*b)%n
71// {
72//    double ab;
73//    long q, res;
74//
75//    ab = ((double) ((int)a)) * ((double) ((int)b));
76//    q  = (long) (ab/((double) npPrimeM));  // q could be off by (+/-) 1
77//    res = (long) (ab - ((double) q)*((double) npPrimeM));
78//    res += (res >> 31) & npPrimeM;
79//    res -= npPrimeM;
80//    res += (res >> 31) & npPrimeM;
81//    return (number)res;
82// }
83#ifdef HAVE_MULT_MOD
84static inline number npMultM(number a, number b, const coeffs r)
85{
86  return (number) 
87    ((((unsigned long) a)*((unsigned long) b)) % ((unsigned long) r->ch));
88}
89#else
90static inline number npMultM(number a, number b, const coeffs r)
91{
92  long x = (long)r->npLogTable[(long)a]+ r->npLogTable[(long)b];
93  return (number)(long)r->npExpTable[x<r->npPminus1M ? x : x- r->npPminus1M];
94}
95#endif
96
97#if 0
98inline number npAddAsm(number a, number b, int m)
99{
100  number r;
101    asm ("addl %2, %1; cmpl %3, %1; jb 0f; subl %3, %1; 0:"
102         : "=&r" (r)
103         : "%0" (a), "g" (b), "g" (m)
104         : "cc");
105  return r;
106}
107inline number npSubAsm(number a, number b, int m)
108{
109  number r;
110  asm ("subl %2, %1; jnc 0f; addl %3, %1; 0:"
111        : "=&r" (r)
112        : "%0" (a), "g" (b), "g" (m)
113        : "cc");
114  return r;
115}
116#endif
117#ifdef HAVE_GENERIC_ADD
118static inline number npAddM(number a, number b, const coeffs r)
119{
120  long R = (long)a + (long)b;
121  return (number)(R >= r->ch ? R - r->ch : R);
122}
123static inline number npSubM(number a, number b, const coeffs r)
124{
125  return (number)((long)a<(long)b ?
126                       r->ch-(long)b+(long)a : (long)a-(long)b);
127}
128#else
129static inline number npAddM(number a, number b, const coeffs r)
130{
131   long res = ((long)a + (long)b);
132   res -= r->ch;
133#if SIZEOF_LONG == 8
134   res += (res >> 63) & r->ch;
135#else
136   res += (res >> 31) & r->ch;
137#endif
138   return (number)res;
139}
140static inline number npSubM(number a, number b, const coeffs r)
141{
142   long res = ((long)a - (long)b);
143#if SIZEOF_LONG == 8
144   res += (res >> 63) & r->ch;
145#else
146   res += (res >> 31) & r->ch;
147#endif
148   return (number)res;
149}
150#endif
151
152static inline BOOLEAN npIsZeroM (number  a, const coeffs)
153{
154  return 0 == (long)a;
155}
156
157// inline number npMultM(number a, number b, int npPrimeM)
158// {
159//   return (number)(((long)a*(long)b) % npPrimeM);
160// }
161
162
163#define npNegM(A,r)      (number)(r->ch-(long)(A))
164#define npEqualM(A,B,r)  ((A)==(B))
165
166
167#endif
Note: See TracBrowser for help on using the repository browser.