]> www.ginac.de Git - cln.git/blob - src/modinteger/cl_MI_lshift.cc
0bfea4fafb6041772aadf4a75aaff727fdaef509
[cln.git] / src / modinteger / cl_MI_lshift.cc
1 // operator<< on cl_MI.
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_modinteger.h"
8
9
10 // Implementation.
11
12 #include "cl_integer.h"
13
14 const cl_MI operator<< (const cl_MI& x, sintL y) // assume 0 <= y < 2^31
15 {
16         if (y == 0)
17                 return x;
18         if (y == 1) // frequent case
19                 return x+x;
20         var const cl_modint_ring& R = x.ring();
21         // Method:
22         // Algorithm 1: divide (x.rep << y) by m.
23         //              asymptotical cost: O(y * log m).
24         // Algorithm 2: x * expt(2 mod m,y) using modular integer operations.
25         //              asymptotical cost: O(log y * (log m)^2).
26         // Use algorithm 1 for small y, algorithm 2 for large y.
27         if ((R->bits < 0) || (y <= 2*R->bits))
28                 return cl_MI(R, R->reduce_modulo(x.rep << y));
29         else
30                 return x * expt_pos(R->canonhom(2), (cl_I)(long)y);
31 }