1 // Modular integer operations.
9 #include "cln/modinteger.h"
18 #include "cln/integer_io.h"
21 #include "cln/abort.h"
22 #include "cl_alloca.h"
24 // MacOS X does "#define _R 0x00040000L"
30 cl_heap_modint_ring::cl_heap_modint_ring (cl_I m, cl_modint_setops* setopv, cl_modint_addops* addopv, cl_modint_mulops* mulopv)
31 : setops (setopv), addops (addopv), mulops (mulopv), modulus (m)
33 refcount = 0; // will be incremented by the `cl_modint_ring' constructor
34 type = &cl_class_modint_ring;
35 if (minusp(m)) cl_abort();
37 var uintL b = integer_length(m-1);
38 // m <= 2^b, hence one needs b bits for a representative mod m.
40 log2_bits = 0; bits = 1;
41 } else if (b <= cl_word_size) {
43 integerlength32(b-1,bb=); // b <= 2^bb with bb minimal
44 log2_bits = bb; bits = 1<<bb;
46 log2_bits = -1; bits = -1;
49 log2_bits = -1; bits = -1;
53 static void cl_modint_ring_destructor (cl_heap* pointer)
55 (*(cl_heap_modint_ring*)pointer).~cl_heap_modint_ring();
58 cl_class cl_class_modint_ring = {
59 cl_modint_ring_destructor,
63 // This tells the compiler to put the `cl_heap_modint_ring' vtable
65 void cl_heap_modint_ring::dummy () {}
68 static cl_boolean modint_equal (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y)
71 return equal(x.rep,y.rep);
75 #include "cl_MI_int.h"
76 #include "cl_MI_std.h"
77 #include "cl_MI_fix16.h"
78 #if (cl_value_len <= 32)
79 #include "cl_MI_fix29.h"
80 #include "cl_MI_int32.h"
82 #include "cl_MI_fix32.h"
84 #include "cl_MI_pow2.h"
85 #include "cl_MI_pow2m1.h"
86 #include "cl_MI_pow2p1.h"
87 #include "cl_MI_montgom.h"
91 static inline cl_heap_modint_ring* make_modint_ring (const cl_I& m) // m >= 0
94 return new cl_heap_modint_ring_int();
97 var uintL log2_m = power2p(m);
99 return new cl_heap_modint_ring_pow2(m,log2_m-1);
103 var uintL log2_m = integer_length(m); // = integerlength(m-1)
104 if (log2_m < 16) // m < 0x10000
105 return new cl_heap_modint_ring_fix16(m);
106 #if (cl_value_len <= 32)
107 if (log2_m < cl_value_len)
108 return new cl_heap_modint_ring_fix29(m);
109 if (log2_m < 32) // m < 0x100000000
110 return new cl_heap_modint_ring_int32(m);
112 if (log2_m < 32) // m < 0x100000000
113 return new cl_heap_modint_ring_fix32(m);
117 var uintL m1 = power2p(m+1);
119 return new cl_heap_modint_ring_pow2m1(m,m1-1);
122 var uintL m1 = power2p(m-1);
124 return new cl_heap_modint_ring_pow2p1(m,m1-1);
127 var cl_heap_modint_ring* R = try_make_modint_ring_montgom(m);
131 return new cl_heap_modint_ring_std(m);
135 // The table of modular integer rings.
136 // A weak hash table cl_I -> cl_modint_ring.
137 // (It could also be a weak hashuniq table cl_I -> cl_modint_ring.)
140 #include "cl_I_hashweak_rcpointer.h"
143 // An entry can be collected when the value (the ring) isn't referenced any more
144 // except from the hash table, and when the key (the modulus) isn't referenced
145 // any more except from the hash table and the ring. Note that the ring contains
146 // exactly one reference to the modulus.
148 static cl_boolean maygc_htentry (const cl_htentry_from_integer_to_rcpointer& entry)
150 if (!entry.key.pointer_p() || (entry.key.heappointer->refcount == 2))
151 if (!entry.val.pointer_p() || (entry.val.heappointer->refcount == 1))
156 static const cl_wht_from_integer_to_rcpointer modint_ring_table = cl_wht_from_integer_to_rcpointer(maygc_htentry);
158 static inline cl_modint_ring* get_modint_ring (const cl_I& m)
160 return (cl_modint_ring*) modint_ring_table.get(m);
163 static inline void store_modint_ring (const cl_modint_ring& R)
165 modint_ring_table.put(R->modulus,R);
169 const cl_modint_ring find_modint_ring (const cl_I& m)
173 var cl_modint_ring* ring_in_table = get_modint_ring(m);
174 if (!ring_in_table) {
175 var cl_modint_ring R = make_modint_ring(m);
176 store_modint_ring(R);
177 ring_in_table = get_modint_ring(m);
181 return *ring_in_table;
185 const cl_modint_ring cl_modint0_ring = find_modint_ring(0);
189 CL_PROVIDE_END(cl_MI)