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

spielwiese
Last change on this file since 7f7b2a was 7f7b2a, checked in by Oleksandr Motsak <motsak@…>, 14 years ago
more debuggin messages for errors while initing/assumes fixed long-long error on Mac OS X.
  • 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;
25
26void 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);
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);
56number  npMap0(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#if 0
70inline number npMultM(number a, number b)
71// return (a*b)%n
72{
73   double ab;
74   long q, res;
75
76   ab = ((double) ((int)a)) * ((double) ((int)b));
77   q  = (long) (ab/((double) npPrimeM));  // q could be off by (+/-) 1
78   res = (long) (ab - ((double) q)*((double) npPrimeM));
79   res += (res >> 31) & npPrimeM;
80   res -= npPrimeM;
81   res += (res >> 31) & npPrimeM;
82   return (number)res;
83}
84#endif
85#ifdef HAVE_MULT_MOD
86static inline number npMultM(number a, number b, const coeffs r)
87{
88  return (number) 
89    ((((unsigned long) a)*((unsigned long) b)) % ((unsigned long) r->npPrimeM));
90}
91#else
92static inline number npMultM(number a, number b, const coeffs r)
93{
94  long x = (long)r->npLogTable[(long)a]+ r->npLogTable[(long)b];
95  return (number)(long)r->npExpTable[x<r->npPminus1M ? x : x- r->npPminus1M];
96}
97#endif
98
99#if 0
100inline number npAddAsm(number a, number b, int m)
101{
102  number r;
103    asm ("addl %2, %1; cmpl %3, %1; jb 0f; subl %3, %1; 0:"
104         : "=&r" (r)
105         : "%0" (a), "g" (b), "g" (m)
106         : "cc");
107  return r;
108}
109inline number npSubAsm(number a, number b, int m)
110{
111  number r;
112  asm ("subl %2, %1; jnc 0f; addl %3, %1; 0:"
113        : "=&r" (r)
114        : "%0" (a), "g" (b), "g" (m)
115        : "cc");
116  return r;
117}
118#endif
119#ifdef HAVE_GENERIC_ADD
120static inline number npAddM(number a, number b, const coeffs r)
121{
122  long R = (long)a + (long)b;
123  return (number)(R >= r->npPrimeM ? R - r->npPrimeM : R);
124}
125static inline number npSubM(number a, number b, const coeffs r)
126{
127  return (number)((long)a<(long)b ?
128                       r->npPrimeM-(long)b+(long)a : (long)a-(long)b);
129}
130#else
131static inline number npAddM(number a, number b, const coeffs r)
132{
133   long res = ((long)a + (long)b);
134   res -= r->npPrimeM;
135#if SIZEOF_LONG == 8
136   res += (res >> 63) & r->npPrimeM;
137#else
138   res += (res >> 31) & r->npPrimeM;
139#endif
140   return (number)res;
141}
142static inline number npSubM(number a, number b, const coeffs r)
143{
144   long res = ((long)a - (long)b);
145#if SIZEOF_LONG == 8
146   res += (res >> 63) & r->npPrimeM;
147#else
148   res += (res >> 31) & r->npPrimeM;
149#endif
150   return (number)res;
151}
152#endif
153
154static inline BOOLEAN npIsZeroM (number  a, const coeffs r)
155{
156  return 0 == (long)a;
157}
158
159/*
160*inline number npMultM(number a, number b)
161*{
162*  return (number)(((long)a*(long)b) % npPrimeM);
163*}
164*/
165
166#define npNegM(A,r)      (number)(r->npPrimeM-(long)(A))
167#define npEqualM(A,B,r)  ((A)==(B))
168
169
170#ifdef NV_OPS
171#pragma GCC diagnostic ignored "-Wlong-long"
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.