1 /*
2     Copyright (C) 2008, 2009 William Hart
3     Copyright (C) 2010, 2012 Sebastian Pancratz
4     Copyright (C) 2013 Mike Hansen
5 
6     This file is part of FLINT.
7 
8     FLINT is free software: you can redistribute it and/or modify it under
9     the terms of the GNU Lesser General Public License (LGPL) as published
10     by the Free Software Foundation; either version 2.1 of the License, or
11     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
12 */
13 
14 #ifdef T
15 
16 #include "templates.h"
17 #include "fmpz_poly.h"
18 
19 void
_TEMPLATE(T,poly_sqr_KS)20 _TEMPLATE(T, poly_sqr_KS) (TEMPLATE(T, struct) * rop,
21                            const TEMPLATE(T, struct) * op, slong len,
22                            const TEMPLATE(T, ctx_t) ctx)
23 {
24     const slong in_len = len;
25     const slong d = TEMPLATE(T, ctx_degree) (ctx);
26     slong bits, i;
27     fmpz *f, *g;
28 
29     TEMPLATE(CAP_T, VEC_NORM) (op, len, ctx);
30 
31     if (!len)
32     {
33         if (2 * in_len - 1 > 0)
34             _TEMPLATE(T, poly_zero) (rop, 2 * in_len - 1, ctx);
35         return;
36     }
37 
38     bits = 2 * fmpz_bits(TEMPLATE(T, ctx_prime) (ctx))
39         + FLINT_BIT_COUNT(d) + FLINT_BIT_COUNT(len);
40 
41     f = _fmpz_vec_init((2 * len - 1) + len);
42     g = f + (2 * len - 1);
43 
44     for (i = 0; i < len; i++)
45     {
46         TEMPLATE(T, bit_pack) (g + i, op + i, bits, ctx);
47     }
48 
49     _fmpz_poly_sqr(f, g, len);
50 
51     for (i = 0; i < 2 * len - 1; i++)
52     {
53         TEMPLATE(T, bit_unpack) (rop + i, f + i, bits, ctx);
54     }
55 
56     _TEMPLATE(T, poly_zero) (rop + (2 * len - 1), 2 * (in_len - len), ctx);
57 
58     _fmpz_vec_clear(f, (2 * len - 1) + len);
59 }
60 
61 void
TEMPLATE(T,poly_sqr_KS)62 TEMPLATE(T, poly_sqr_KS) (TEMPLATE(T, poly_t) rop,
63                           const TEMPLATE(T, poly_t) op,
64                           const TEMPLATE(T, ctx_t) ctx)
65 {
66     const slong len = 2 * op->length - 1;
67 
68     if (op->length == 0)
69     {
70         TEMPLATE(T, poly_zero) (rop, ctx);
71     }
72     else
73     {
74         TEMPLATE(T, poly_fit_length) (rop, len, ctx);
75         _TEMPLATE(T, poly_sqr_KS) (rop->coeffs, op->coeffs, op->length, ctx);
76         _TEMPLATE(T, poly_set_length) (rop, len, ctx);
77     }
78 }
79 
80 
81 #endif
82