1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Copyright (C) 2010 William Hart
Copyright (C) 2013 Mike Hansen
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#ifdef T
#include "templates.h"
/* Assumes op1 and op2 are not length 0. */
void
_TEMPLATE(T, poly_mulhigh_classical) (
TEMPLATE(T, struct) * rop,
const TEMPLATE(T, struct) * op1, slong len1,
const TEMPLATE(T, struct) * op2, slong len2,
slong start, const TEMPLATE(T, ctx_t) ctx)
{
slong m, n;
_TEMPLATE(T, vec_zero) (rop, start, ctx);
if (len1 == 1) /* Special case if the length of both inputs is 1 */
{
if (start == 0)
TEMPLATE(T, mul) (rop, op1, op2, ctx);
}
else /* Ordinary case */
{
slong i;
/* Set res[i] = poly1[i]*poly2[0] */
if (start < len1)
_TEMPLATE3(T, vec_scalar_mul, T) (rop + start, op1 + start,
len1 - start, op2, ctx);
if (len2 == 1)
return;
/* Set res[i+len1-1] = in1[len1-1]*in2[i] */
m = FLINT_MAX(len1 - 1, start);
_TEMPLATE3(T, vec_scalar_mul, T) (rop + m, op2 + m - len1 + 1,
len2 - 1 + len1 - m, op1 + len1 - 1,
ctx);
/* out[i+j] += in1[i]*in2[j] */
m = FLINT_MAX(start, len2 - 1);
for (i = m - len2 + 1; i < len1 - 1; i++)
{
n = FLINT_MAX(i + 1, start);
_TEMPLATE3(T, vec_scalar_addmul, T) (rop + n, op2 + n - i,
len2 + i - n, op1 + i, ctx);
}
}
}
void
TEMPLATE(T, poly_mulhigh_classical) (TEMPLATE(T, poly_t) rop,
const TEMPLATE(T, poly_t) op1,
const TEMPLATE(T, poly_t) op2,
slong start, const TEMPLATE(T, ctx_t) ctx)
{
slong len_out = op1->length + op2->length - 1;
if (op1->length == 0 || op2->length == 0 || start >= len_out)
{
TEMPLATE(T, poly_zero) (rop, ctx);
return;
}
if (rop == op1 || rop == op2)
{
TEMPLATE(T, poly_t) temp;
TEMPLATE(T, poly_init2) (temp, len_out, ctx);
if (op1->length >= op2->length)
_TEMPLATE(T, poly_mulhigh_classical) (temp->coeffs, op1->coeffs,
op1->length, op2->coeffs,
op2->length, start, ctx);
else
_TEMPLATE(T, poly_mulhigh_classical) (temp->coeffs, op2->coeffs,
op2->length, op1->coeffs,
op1->length, start, ctx);
TEMPLATE(T, poly_swap) (rop, temp, ctx);
TEMPLATE(T, poly_clear) (temp, ctx);
}
else
{
TEMPLATE(T, poly_fit_length) (rop, len_out, ctx);
if (op1->length >= op2->length)
_TEMPLATE(T, poly_mulhigh_classical) (rop->coeffs, op1->coeffs,
op1->length, op2->coeffs,
op2->length, start, ctx);
else
_TEMPLATE(T, poly_mulhigh_classical) (rop->coeffs, op2->coeffs,
op2->length, op1->coeffs,
op1->length, start, ctx);
}
_TEMPLATE(T, poly_set_length) (rop, len_out, ctx);
_TEMPLATE(T, poly_normalise) (rop, ctx);
}
#endif