Reorganisation of the source tree
[ghc-hetmet.git] / rts / gmp / mpn / generic / sqr_basecase.c
1 /* mpn_sqr_basecase -- Internal routine to square two natural numbers
2    of length m and n.
3
4    THIS IS AN INTERNAL FUNCTION WITH A MUTABLE INTERFACE.  IT IS ONLY
5    SAFE TO REACH THIS FUNCTION THROUGH DOCUMENTED INTERFACES.
6
7
8 Copyright (C) 1991, 1992, 1993, 1994, 1996, 1997, 2000 Free Software
9 Foundation, Inc.
10
11 This file is part of the GNU MP Library.
12
13 The GNU MP Library is free software; you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as published by
15 the Free Software Foundation; either version 2.1 of the License, or (at your
16 option) any later version.
17
18 The GNU MP Library is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
21 License for more details.
22
23 You should have received a copy of the GNU Lesser General Public License
24 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
25 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26 MA 02111-1307, USA. */
27
28 #include "gmp.h"
29 #include "gmp-impl.h"
30 #include "longlong.h"
31
32 void
33 #if __STDC__
34 mpn_sqr_basecase (mp_ptr prodp, mp_srcptr up, mp_size_t n)
35 #else
36 mpn_sqr_basecase (prodp, up, n)
37      mp_ptr prodp;
38      mp_srcptr up;
39      mp_size_t n;
40 #endif
41 {
42   mp_size_t i;
43
44   {
45     /* N.B.!  We need the superfluous indirection through argh to work around
46        a reloader bug in GCC 2.7.*.  */
47     mp_limb_t x;
48     mp_limb_t argh;
49     x = up[0];
50     umul_ppmm (argh, prodp[0], x, x);
51     prodp[1] = argh;
52   }
53   if (n > 1)
54     {
55       mp_limb_t tarr[2 * KARATSUBA_SQR_THRESHOLD];
56       mp_ptr tp = tarr;
57       mp_limb_t cy;
58
59       /* must fit 2*n limbs in tarr */
60       ASSERT (n <= KARATSUBA_SQR_THRESHOLD);
61
62       cy = mpn_mul_1 (tp, up + 1, n - 1, up[0]);
63       tp[n - 1] = cy;
64       for (i = 2; i < n; i++)
65         {
66           mp_limb_t cy;
67           cy = mpn_addmul_1 (tp + 2 * i - 2, up + i, n - i, up[i - 1]);
68           tp[n + i - 2] = cy;
69         }
70       for (i = 1; i < n; i++)
71         {
72           mp_limb_t x;
73           x = up[i];
74           umul_ppmm (prodp[2 * i + 1], prodp[2 * i], x, x);
75         }
76       {
77         mp_limb_t cy;
78         cy = mpn_lshift (tp, tp, 2 * n - 2, 1);
79         cy += mpn_add_n (prodp + 1, prodp + 1, tp, 2 * n - 2);
80         prodp[2 * n - 1] += cy;
81       }
82     }
83 }