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