1 /* mpn_toom2_sqr -- Square {ap,an}.
3 Contributed to the GNU project by Torbjorn Granlund.
5 THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY
6 SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
7 GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
9 Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
11 This file is part of the GNU MP Library.
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 3 of the License, or (at your
16 option) any later version.
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.
23 You should have received a copy of the GNU Lesser General Public License
24 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
30 /* Evaluate in: -1, 0, +inf
37 vm1 = (a0- a1)^2 # A(-1)^2
38 vinf= a1 ^2 # A(inf)^2
41 #if TUNE_PROGRAM_BUILD
42 #define MAYBE_sqr_toom2 1
44 #define MAYBE_sqr_toom2 \
45 (SQR_TOOM3_THRESHOLD >= 2 * SQR_TOOM2_THRESHOLD)
48 #define TOOM2_SQR_REC(p, a, n, ws) \
50 if (! MAYBE_sqr_toom2 \
51 || BELOW_THRESHOLD (n, SQR_TOOM2_THRESHOLD)) \
52 mpn_sqr_basecase (p, a, n); \
54 mpn_toom2_sqr (p, a, n, ws); \
58 mpn_toom2_sqr (mp_ptr pp,
59 mp_srcptr ap, mp_size_t an,
72 ASSERT (0 < s && s <= n);
79 if (mpn_cmp (a0, a1, n) < 0)
81 mpn_sub_n (asm1, a1, a0, n);
85 mpn_sub_n (asm1, a0, a1, n);
90 if (mpn_zero_p (a0 + s, n - s) && mpn_cmp (a0, a1, s) < 0)
92 mpn_sub_n (asm1, a1, a0, s);
93 MPN_ZERO (asm1 + s, n - s);
97 mpn_sub (asm1, a0, n, a1, s);
101 #define v0 pp /* 2n */
102 #define vinf (pp + 2 * n) /* s+s */
103 #define vm1 scratch /* 2n */
104 #define scratch_out scratch + 2 * n
107 TOOM2_SQR_REC (vm1, asm1, n, scratch_out);
109 /* vinf, s+s limbs */
110 TOOM2_SQR_REC (vinf, a1, s, scratch_out);
113 TOOM2_SQR_REC (v0, ap, n, scratch_out);
115 /* H(v0) + L(vinf) */
116 cy = mpn_add_n (pp + 2 * n, v0 + n, vinf, n);
119 cy2 = cy + mpn_add_n (pp + n, pp + 2 * n, v0, n);
121 /* L(vinf) + H(vinf) */
122 cy += mpn_add (pp + 2 * n, pp + 2 * n, n, vinf + n, s + s - n);
124 cy -= mpn_sub_n (pp + n, pp + n, vm1, 2 * n);
126 ASSERT (cy + 1 <= 3);
129 mpn_incr_u (pp + 2 * n, cy2);
130 if (LIKELY (cy <= 2))
131 mpn_incr_u (pp + 3 * n, cy);
133 mpn_decr_u (pp + 3 * n, 1);