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