iscontrol(8): Fix synopsis, sync usage() & improve markup
[dragonfly.git] / contrib / mpfr / mulders.c
blob13f037e10b91ede67972fee043b9167489e379a6
1 /* Mulder's MulHigh function (short product)
3 Copyright 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
26 #ifndef MUL_FFT_THRESHOLD
27 #define MUL_FFT_THRESHOLD 8448
28 #endif
30 /* Don't use MPFR_MULHIGH_SIZE since it is handled by tuneup */
31 #ifdef MPFR_MULHIGH_TAB_SIZE
32 static short mulhigh_ktab[MPFR_MULHIGH_TAB_SIZE];
33 #else
34 static short mulhigh_ktab[] = {MPFR_MULHIGH_TAB};
35 #define MPFR_MULHIGH_TAB_SIZE \
36 ((mp_size_t) (sizeof(mulhigh_ktab) / sizeof(mulhigh_ktab[0])))
37 #endif
39 /* Put in rp[n..2n-1] an approximation of the n high limbs
40 of {mp, n} * {np, n}.
41 The error is at worst of ln(n) for rp[n] and rp[n-1] is totally wrong */
42 static void
43 mpfr_mulhigh_n_basecase (mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n)
45 mp_size_t i;
47 rp += n-1;
48 umul_ppmm (rp[1], rp[0], up[n-1], vp[0]);
49 for (i = 1 ; i < n ; i++)
50 rp[i+1] = mpn_addmul_1 (rp, up + (n - i - 1), i+1, vp[i]);
53 void
54 mpfr_mulhigh_n (mp_ptr rp, mp_srcptr np, mp_srcptr mp, mp_size_t n)
56 mp_size_t k;
58 MPFR_ASSERTD (MPFR_MULHIGH_TAB_SIZE > 4);
59 k = MPFR_LIKELY (n < MPFR_MULHIGH_TAB_SIZE) ? mulhigh_ktab[n] : 2*n/3;
60 MPFR_ASSERTD (k == -1 || k == 0 || (k > n/2 && k < n));
61 if (k < 0)
62 mpn_mul_basecase (rp, np, n, mp, n);
63 else if (k == 0)
64 mpfr_mulhigh_n_basecase (rp, np, mp, n);
65 else if (n > MUL_FFT_THRESHOLD)
66 mpn_mul_n (rp, np, mp, n);
67 else
69 mp_size_t l = n - k;
70 mp_limb_t cy;
72 mpn_mul_n (rp + 2 * l, np + l, mp + l, k); /* fills rp[2l..2n-1] */
73 mpfr_mulhigh_n (rp, np + k, mp, l); /* fills rp[l-1..2l-1] */
74 cy = mpn_add_n (rp + n - 1, rp + n - 1, rp + l - 1, l + 1);
75 mpfr_mulhigh_n (rp, np, mp + k, l); /* fills rp[l-1..2l-1] */
76 cy += mpn_add_n (rp + n - 1, rp + n - 1, rp + l - 1, l + 1);
77 mpn_add_1 (rp + n + l, rp + n + l, k, cy); /* propagate carry */
81 #ifdef MPFR_SQRHIGH_TAB_SIZE
82 static short sqrhigh_ktab[MPFR_SQRHIGH_TAB_SIZE];
83 #else
84 static short sqrhigh_ktab[] = {MPFR_SQRHIGH_TAB};
85 #define MPFR_SQRHIGH_TAB_SIZE (sizeof(sqrhigh_ktab) / sizeof(sqrhigh_ktab[0]))
86 #endif
88 void
89 mpfr_sqrhigh_n (mp_ptr rp, mp_srcptr np, mp_size_t n)
91 mp_size_t k;
93 MPFR_ASSERTD (MPFR_SQRHIGH_TAB_SIZE > 4);
94 k = MPFR_LIKELY (n < MPFR_SQRHIGH_TAB_SIZE) ? sqrhigh_ktab[n] : 2*n/3;
95 MPFR_ASSERTD (k == -1 || k == 0 || (k > n/2 && k < n));
96 if (k < 0)
97 /* we can't use mpn_sqr_basecase here, since it requires
98 n <= SQR_KARATSUBA_THRESHOLD, where SQR_KARATSUBA_THRESHOLD
99 is not exported by GMP */
100 mpn_sqr_n (rp, np, n);
101 else if (k == 0)
102 mpfr_mulhigh_n_basecase (rp, np, np, n);
103 else
105 mp_size_t l = n - k;
106 mp_limb_t cy;
108 mpn_sqr_n (rp + 2 * l, np + l, k); /* fills rp[2l..2n-1] */
109 mpfr_mulhigh_n (rp, np, np + k, l); /* fills rp[l-1..2l-1] */
110 /* FIXME: maybe shift by 2 is a better idea but it has to handle carry*/
111 cy = mpn_add_n (rp + n - 1, rp + n - 1, rp + l - 1, l + 1);
112 cy += mpn_add_n (rp + n - 1, rp + n - 1, rp + l - 1, l + 1);
113 mpn_add_1 (rp + n + l, rp + n + l, k, cy); /* propagate carry */