1 /*
2     Copyright (C) 2010 Sebastian Pancratz
3 
4     This file is part of FLINT.
5 
6     FLINT is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
10 */
11 
12 #include <gmp.h>
13 #include "flint.h"
14 #include "fmpz.h"
15 #include "fmpz_poly.h"
16 
17 void
_fmpz_poly_reverse(fmpz * res,const fmpz * poly,slong len,slong n)18 _fmpz_poly_reverse(fmpz * res, const fmpz * poly, slong len, slong n)
19 {
20     if (res == poly)
21     {
22         slong i;
23 
24         for (i = 0; i < n / 2; i++)
25         {
26             fmpz t = res[i];
27             res[i] = res[n - 1 - i];
28             res[n - 1 - i] = t;
29         }
30 
31         for (i = 0; i < n - len; i++)
32             fmpz_zero(res + i);
33     }
34     else
35     {
36         slong i;
37 
38         for (i = 0; i < n - len; i++)
39             fmpz_zero(res + i);
40 
41         for (i = 0; i < len; i++)
42             fmpz_set(res + (n - len) + i, poly + (len - 1) - i);
43     }
44 }
45 
46 void
fmpz_poly_reverse(fmpz_poly_t res,const fmpz_poly_t poly,slong n)47 fmpz_poly_reverse(fmpz_poly_t res, const fmpz_poly_t poly, slong n)
48 {
49     slong len = FLINT_MIN(n, poly->length);
50     if (len == 0)
51     {
52         fmpz_poly_zero(res);
53         return;
54     }
55 
56     fmpz_poly_fit_length(res, n);
57 
58     _fmpz_poly_reverse(res->coeffs, poly->coeffs, len, n);
59 
60     _fmpz_poly_set_length(res, n);
61     _fmpz_poly_normalise(res);
62 }
63