]> www.ginac.de Git - cln.git/blob - src/modinteger/cl_MI_fix29.h
Initial revision
[cln.git] / src / modinteger / cl_MI_fix29.h
1 // 1 < m < 2^(cl_value_len-1), standard representation
2 // Assuming (cl_value_len <= 32).
3
4 static const _cl_MI fix29_plus (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y)
5 {
6         var uint32 zr = FN_to_UL(x.rep) + FN_to_UL(y.rep);
7         if (zr >= FN_to_UL(R->modulus)) { zr = zr - FN_to_UL(R->modulus); }
8         return _cl_MI(R, L_to_FN(zr));
9 }
10
11 static const _cl_MI fix29_minus (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y)
12 {
13         var uint32 xr = FN_to_UL(x.rep);
14         var uint32 yr = FN_to_UL(y.rep);
15         var sint32 zr = xr - yr;
16         if (zr < 0) { zr = zr + FN_to_UL(R->modulus); }
17         return _cl_MI(R, L_to_FN(zr));
18 }
19
20 static const _cl_MI fix29_uminus (cl_heap_modint_ring* R, const _cl_MI& x)
21 {
22         var uint32 xr = FN_to_UL(x.rep);
23         var uint32 zr = (xr==0 ? 0 : FN_to_UL(R->modulus)-xr);
24         return _cl_MI(R, L_to_FN(zr));
25 }
26
27 static const _cl_MI fix29_mul (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y)
28 {
29         var uint32 xr = FN_to_UL(x.rep);
30         var uint32 yr = FN_to_UL(y.rep);
31         var uint32 zrhi;
32         var uint32 zrlo;
33         mulu32(xr,yr,zrhi=,zrlo=);
34         var uint32 zr;
35         divu_6432_3232(zrhi,zrlo,FN_to_UL(R->modulus),,zr=);
36         return _cl_MI(R, L_to_FN(zr));
37 }
38
39 static const _cl_MI fix29_square (cl_heap_modint_ring* R, const _cl_MI& x)
40 {
41         var uint32 xr = FN_to_UL(x.rep);
42         var uint32 zrhi;
43         var uint32 zrlo;
44         mulu32(xr,xr,zrhi=,zrlo=);
45         var uint32 zr;
46         divu_6432_3232(zrhi,zrlo,FN_to_UL(R->modulus),,zr=);
47         return _cl_MI(R, L_to_FN(zr));
48 }
49
50 static cl_modint_addops fix29_addops = {
51         std_zero,
52         std_zerop,
53         fix29_plus,
54         fix29_minus,
55         fix29_uminus
56 };
57 static cl_modint_mulops fix29_mulops = {
58         std_one,
59         std_canonhom,
60         fix29_mul,
61         fix29_square,
62         std_expt_pos,
63         std_recip,
64         std_div,
65         std_expt,
66         std_reduce_modulo,
67         std_retract
68 };
69
70 class cl_heap_modint_ring_fix29 : public cl_heap_modint_ring {
71         SUBCLASS_cl_heap_modint_ring()
72 public:
73         // Constructor.
74         cl_heap_modint_ring_fix29 (const cl_I& m)
75                 : cl_heap_modint_ring (m, &std_setops, &fix29_addops, &fix29_mulops) {}
76         // Virtual destructor.
77         ~cl_heap_modint_ring_fix29 () {}
78 };