source: git/coeffs/modulop.h @ 7d90aa

spielwiese
Last change on this file since 7d90aa was 7d90aa, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
initial changes to 'coeffs' + first build system
  • Property mode set to 100644
File size: 5.1 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.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;
25extern long npMapPrime;
26
27BOOLEAN npGreaterZero (number k, const coeffs r);
28number  npMult        (number a, number b, const coeffs r);
29number  npInit        (int 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);
43const char *  npRead  (const char *s, number *a,const coeffs r);
44#ifdef LDEBUG
45BOOLEAN npDBTest      (number a,const coeffs r, const char *f, const int l);
46#define npTest(A,r)     npDBTest(A,r,__FILE__,__LINE__)
47#else
48#define npTest(A,r)     (0)
49#endif
50void    npSetChar(int c, coeffs r);
51void    npInitChar(int c, coeffs r);
52
53//int     npGetChar();
54
55nMapFunc npSetMap(const coeffs src, const coeffs dst);
56number  npMapP(number from, const coeffs r);
57number  npMap0(number from, 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
172static inline number nvMultM(number a, number b, const coeffs r)
173{
174#if SIZEOF_LONG == 4
175#define ULONG64 (unsigned long long)(unsigned long)
176#else
177#define ULONG64 (unsigned long)
178#endif
179  return (number) 
180    (unsigned long)((ULONG64 a)*(ULONG64 b) % (ULONG64 r->npPrimeM));
181}
182number  nvMult        (number a, number b, const coeffs r);
183number  nvDiv         (number a, number b, const coeffs r);
184number  nvInvers      (number c, const coeffs r);
185void    nvPower       (number a, int i, number * result, const coeffs r);
186#endif
187
188#endif
Note: See TracBrowser for help on using the repository browser.