source: git/libpolys/coeffs/modulop.h @ 7a8011

spielwiese
Last change on this file since 7a8011 was c7e3d7, checked in by Hans Schoenemann <hannes@…>, 13 years ago
cfCoeffWrite introduced
  • Property mode set to 100644
File size: 5.2 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
24extern int npGen;
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);
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);
57number  npMap0(number from, const coeffs src, const coeffs r);
58/*-------specials for spolys, do NOT use otherwise--------------------------*/
59/* for npMultM, npSubM, npNegM, npEqualM : */
60#ifdef HAVE_DIV_MOD
61extern unsigned short *npInvTable;
62#else
63#ifndef HAVE_MULT_MOD
64extern long npPminus1M;
65extern unsigned short *npExpTable;
66extern unsigned short *npLogTable;
67#endif
68#endif
69
70#if 0
71inline number npMultM(number a, number b)
72// return (a*b)%n
73{
74   double ab;
75   long q, res;
76
77   ab = ((double) ((int)a)) * ((double) ((int)b));
78   q  = (long) (ab/((double) npPrimeM));  // q could be off by (+/-) 1
79   res = (long) (ab - ((double) q)*((double) npPrimeM));
80   res += (res >> 31) & npPrimeM;
81   res -= npPrimeM;
82   res += (res >> 31) & npPrimeM;
83   return (number)res;
84}
85#endif
86#ifdef HAVE_MULT_MOD
87static inline number npMultM(number a, number b, const coeffs r)
88{
89  return (number) 
90    ((((unsigned long) a)*((unsigned long) b)) % ((unsigned long) r->npPrimeM));
91}
92#else
93static inline number npMultM(number a, number b, const coeffs r)
94{
95  long x = (long)r->npLogTable[(long)a]+ r->npLogTable[(long)b];
96  return (number)(long)r->npExpTable[x<r->npPminus1M ? x : x- r->npPminus1M];
97}
98#endif
99
100#if 0
101inline number npAddAsm(number a, number b, int m)
102{
103  number r;
104    asm ("addl %2, %1; cmpl %3, %1; jb 0f; subl %3, %1; 0:"
105         : "=&r" (r)
106         : "%0" (a), "g" (b), "g" (m)
107         : "cc");
108  return r;
109}
110inline number npSubAsm(number a, number b, int m)
111{
112  number r;
113  asm ("subl %2, %1; jnc 0f; addl %3, %1; 0:"
114        : "=&r" (r)
115        : "%0" (a), "g" (b), "g" (m)
116        : "cc");
117  return r;
118}
119#endif
120#ifdef HAVE_GENERIC_ADD
121static inline number npAddM(number a, number b, const coeffs r)
122{
123  long R = (long)a + (long)b;
124  return (number)(R >= r->npPrimeM ? R - r->npPrimeM : R);
125}
126static inline number npSubM(number a, number b, const coeffs r)
127{
128  return (number)((long)a<(long)b ?
129                       r->npPrimeM-(long)b+(long)a : (long)a-(long)b);
130}
131#else
132static inline number npAddM(number a, number b, const coeffs r)
133{
134   long res = ((long)a + (long)b);
135   res -= r->npPrimeM;
136#if SIZEOF_LONG == 8
137   res += (res >> 63) & r->npPrimeM;
138#else
139   res += (res >> 31) & r->npPrimeM;
140#endif
141   return (number)res;
142}
143static inline number npSubM(number a, number b, const coeffs r)
144{
145   long res = ((long)a - (long)b);
146#if SIZEOF_LONG == 8
147   res += (res >> 63) & r->npPrimeM;
148#else
149   res += (res >> 31) & r->npPrimeM;
150#endif
151   return (number)res;
152}
153#endif
154
155static inline BOOLEAN npIsZeroM (number  a, const coeffs r)
156{
157  return 0 == (long)a;
158}
159
160/*
161*inline number npMultM(number a, number b)
162*{
163*  return (number)(((long)a*(long)b) % npPrimeM);
164*}
165*/
166
167#define npNegM(A,r)      (number)(r->npPrimeM-(long)(A))
168#define npEqualM(A,B,r)  ((A)==(B))
169
170
171#ifdef NV_OPS
172#pragma GCC diagnostic ignored "-Wlong-long"
173static inline number nvMultM(number a, number b, const coeffs r)
174{
175#if SIZEOF_LONG == 4
176#define ULONG64 (unsigned long long)(unsigned long)
177#else
178#define ULONG64 (unsigned long)
179#endif
180  return (number) 
181    (unsigned long)((ULONG64 a)*(ULONG64 b) % (ULONG64 r->npPrimeM));
182}
183number  nvMult        (number a, number b, const coeffs r);
184number  nvDiv         (number a, number b, const coeffs r);
185number  nvInvers      (number c, const coeffs r);
186void    nvPower       (number a, int i, number * result, const coeffs r);
187#endif
188
189#endif
Note: See TracBrowser for help on using the repository browser.