beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / mul_n.c
blob5df8b16fa01b34dd6522911de414115d3d340cbb
1 /* mpn_mul_n -- multiply natural numbers.
3 Copyright 1991, 1993, 1994, 1996-2003, 2005, 2008, 2009 Free Software
4 Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of either:
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
17 * the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any
19 later version.
21 or both in parallel, as here.
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 for more details.
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library. If not,
30 see https://www.gnu.org/licenses/. */
32 #include "gmp.h"
33 #include "gmp-impl.h"
34 #include "longlong.h"
36 void
37 mpn_mul_n (mp_ptr p, mp_srcptr a, mp_srcptr b, mp_size_t n)
39 ASSERT (n >= 1);
40 ASSERT (! MPN_OVERLAP_P (p, 2 * n, a, n));
41 ASSERT (! MPN_OVERLAP_P (p, 2 * n, b, n));
43 if (BELOW_THRESHOLD (n, MUL_TOOM22_THRESHOLD))
45 mpn_mul_basecase (p, a, n, b, n);
47 else if (BELOW_THRESHOLD (n, MUL_TOOM33_THRESHOLD))
49 /* Allocate workspace of fixed size on stack: fast! */
50 mp_limb_t ws[mpn_toom22_mul_itch (MUL_TOOM33_THRESHOLD_LIMIT-1,
51 MUL_TOOM33_THRESHOLD_LIMIT-1)];
52 ASSERT (MUL_TOOM33_THRESHOLD <= MUL_TOOM33_THRESHOLD_LIMIT);
53 mpn_toom22_mul (p, a, n, b, n, ws);
55 else if (BELOW_THRESHOLD (n, MUL_TOOM44_THRESHOLD))
57 mp_ptr ws;
58 TMP_SDECL;
59 TMP_SMARK;
60 ws = TMP_SALLOC_LIMBS (mpn_toom33_mul_itch (n, n));
61 mpn_toom33_mul (p, a, n, b, n, ws);
62 TMP_SFREE;
64 else if (BELOW_THRESHOLD (n, MUL_TOOM6H_THRESHOLD))
66 mp_ptr ws;
67 TMP_SDECL;
68 TMP_SMARK;
69 ws = TMP_SALLOC_LIMBS (mpn_toom44_mul_itch (n, n));
70 mpn_toom44_mul (p, a, n, b, n, ws);
71 TMP_SFREE;
73 else if (BELOW_THRESHOLD (n, MUL_TOOM8H_THRESHOLD))
75 mp_ptr ws;
76 TMP_SDECL;
77 TMP_SMARK;
78 ws = TMP_SALLOC_LIMBS (mpn_toom6_mul_n_itch (n));
79 mpn_toom6h_mul (p, a, n, b, n, ws);
80 TMP_SFREE;
82 else if (BELOW_THRESHOLD (n, MUL_FFT_THRESHOLD))
84 mp_ptr ws;
85 TMP_DECL;
86 TMP_MARK;
87 ws = TMP_ALLOC_LIMBS (mpn_toom8_mul_n_itch (n));
88 mpn_toom8h_mul (p, a, n, b, n, ws);
89 TMP_FREE;
91 else
93 /* The current FFT code allocates its own space. That should probably
94 change. */
95 mpn_fft_mul (p, a, n, b, n);