source: git/libpolys/coeffs/modulop.h @ 6ce030f

jengelh-datetimespielwiese
Last change on this file since 6ce030f was 6ce030f, checked in by Oleksandr Motsak <motsak@…>, 10 years ago
removal of the $Id$ svn tag from everywhere NOTE: the git SHA1 may be used instead (only on special places) NOTE: the libraries Singular/LIB/*.lib still contain the marker due to our current use of svn
  • 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/*
7* ABSTRACT: numbers modulo p (<=32003)
8*/
9#include <coeffs/coeffs.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// extern int npGen; // obsolete
24
25BOOLEAN npInitChar(coeffs r, void* p);
26
27BOOLEAN npGreaterZero (number k, const coeffs r);
28number  npMult        (number a, number b, const coeffs r);
29number  npInit        (long i, const coeffs r);
30int     npInt         (number &n, const coeffs r);
31number  npAdd         (number a, number b,const coeffs r);
32number  npSub         (number a, number b,const coeffs r);
33void    npPower       (number a, int i, number * result,const coeffs r);
34BOOLEAN npIsZero      (number a,const coeffs r);
35BOOLEAN npIsOne       (number a,const coeffs r);
36BOOLEAN npIsMOne       (number a,const coeffs r);
37number  npDiv         (number a, number b,const coeffs r);
38number  npNeg         (number c,const coeffs r);
39number  npInvers      (number c,const coeffs r);
40BOOLEAN npGreater     (number a, number b,const coeffs r);
41BOOLEAN npEqual       (number a, number b,const coeffs r);
42void    npWrite       (number &a, const coeffs r);
43void    npCoeffWrite  (const coeffs r, BOOLEAN details);
44const char *  npRead  (const char *s, number *a,const coeffs r);
45#ifdef LDEBUG
46BOOLEAN npDBTest      (number a, const char *f, const int l, const coeffs r);
47#define npTest(A,r)     npDBTest(A,__FILE__,__LINE__, r)
48#else
49#define npTest(A,r)     (0)
50#endif
51
52//int     npGetChar();
53
54nMapFunc npSetMap(const coeffs src, const coeffs dst);
55number  npMapP(number from, const coeffs src, const coeffs r);
56/*-------specials for spolys, do NOT use otherwise--------------------------*/
57/* for npMultM, npSubM, npNegM, npEqualM : */
58#ifdef HAVE_DIV_MOD
59extern unsigned short *npInvTable;
60#else
61#ifndef HAVE_MULT_MOD
62extern long npPminus1M;
63extern unsigned short *npExpTable;
64extern unsigned short *npLogTable;
65#endif
66#endif
67
68// inline number npMultM(number a, number b, int npPrimeM)
69// // return (a*b)%n
70// {
71//    double ab;
72//    long q, res;
73//
74//    ab = ((double) ((int)a)) * ((double) ((int)b));
75//    q  = (long) (ab/((double) npPrimeM));  // q could be off by (+/-) 1
76//    res = (long) (ab - ((double) q)*((double) npPrimeM));
77//    res += (res >> 31) & npPrimeM;
78//    res -= npPrimeM;
79//    res += (res >> 31) & npPrimeM;
80//    return (number)res;
81// }
82#ifdef HAVE_MULT_MOD
83static inline number npMultM(number a, number b, const coeffs r)
84{
85  return (number) 
86    ((((unsigned long) a)*((unsigned long) b)) % ((unsigned long) r->ch));
87}
88#else
89static inline number npMultM(number a, number b, const coeffs r)
90{
91  long x = (long)r->npLogTable[(long)a]+ r->npLogTable[(long)b];
92  return (number)(long)r->npExpTable[x<r->npPminus1M ? x : x- r->npPminus1M];
93}
94#endif
95
96#if 0
97inline number npAddAsm(number a, number b, int m)
98{
99  number r;
100    asm ("addl %2, %1; cmpl %3, %1; jb 0f; subl %3, %1; 0:"
101         : "=&r" (r)
102         : "%0" (a), "g" (b), "g" (m)
103         : "cc");
104  return r;
105}
106inline number npSubAsm(number a, number b, int m)
107{
108  number r;
109  asm ("subl %2, %1; jnc 0f; addl %3, %1; 0:"
110        : "=&r" (r)
111        : "%0" (a), "g" (b), "g" (m)
112        : "cc");
113  return r;
114}
115#endif
116#ifdef HAVE_GENERIC_ADD
117static inline number npAddM(number a, number b, const coeffs r)
118{
119  long R = (long)a + (long)b;
120  return (number)(R >= r->ch ? R - r->ch : R);
121}
122static inline number npSubM(number a, number b, const coeffs r)
123{
124  return (number)((long)a<(long)b ?
125                       r->ch-(long)b+(long)a : (long)a-(long)b);
126}
127#else
128static inline number npAddM(number a, number b, const coeffs r)
129{
130   long res = ((long)a + (long)b);
131   res -= r->ch;
132#if SIZEOF_LONG == 8
133   res += (res >> 63) & r->ch;
134#else
135   res += (res >> 31) & r->ch;
136#endif
137   return (number)res;
138}
139static inline number npSubM(number a, number b, const coeffs r)
140{
141   long res = ((long)a - (long)b);
142#if SIZEOF_LONG == 8
143   res += (res >> 63) & r->ch;
144#else
145   res += (res >> 31) & r->ch;
146#endif
147   return (number)res;
148}
149#endif
150
151static inline BOOLEAN npIsZeroM (number  a, const coeffs)
152{
153  return 0 == (long)a;
154}
155
156// inline number npMultM(number a, number b, int npPrimeM)
157// {
158//   return (number)(((long)a*(long)b) % npPrimeM);
159// }
160
161
162#define npNegM(A,r)      (number)(r->ch-(long)(A))
163#define npEqualM(A,B,r)  ((A)==(B))
164
165
166#endif
Note: See TracBrowser for help on using the repository browser.