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