~ubuntu-branches/ubuntu/intrepid/ecl/intrepid

« back to all changes in this revision

Viewing changes to src/gmp/mpn/generic/mullow_n.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Van Eynde
  • Date: 2007-04-09 11:51:51 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070409115151-ql8cr0kalzx1jmla
Tags: 0.9i-20070324-2
Upload to unstable. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mpn_mullow_n -- multiply two n-limb nunbers and return the low n limbs
 
2
   of their products.
 
3
 
 
4
   THIS IS (FOR NOW) AN INTERNAL FUNCTION.  IT IS ONLY SAFE TO REACH THIS
 
5
   FUNCTION THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST GUARANTEED
 
6
   THAT IT'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
 
7
 
 
8
Copyright 2004, 2005 Free Software Foundation, Inc.
 
9
 
 
10
This file is part of the GNU MP Library.
 
11
 
 
12
The GNU MP Library is free software; you can redistribute it and/or modify
 
13
it under the terms of the GNU Lesser General Public License as published by
 
14
the Free Software Foundation; either version 2.1 of the License, or (at your
 
15
option) any later version.
 
16
 
 
17
The GNU MP Library is distributed in the hope that it will be useful, but
 
18
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
19
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
20
License for more details.
 
21
 
 
22
You should have received a copy of the GNU Lesser General Public License
 
23
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
 
24
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
25
MA 02110-1301, USA. */
 
26
 
 
27
#include "gmp.h"
 
28
#include "gmp-impl.h"
 
29
 
 
30
 
 
31
#ifndef MULLOW_BASECASE_THRESHOLD
 
32
#define MULLOW_BASECASE_THRESHOLD 0     /* never use mpn_mul_basecase */
 
33
#endif
 
34
 
 
35
#ifndef MULLOW_DC_THRESHOLD
 
36
#define MULLOW_DC_THRESHOLD 3*MUL_KARATSUBA_THRESHOLD
 
37
#endif
 
38
 
 
39
#ifndef MULLOW_MUL_N_THRESHOLD
 
40
#define MULLOW_MUL_N_THRESHOLD 10*MULLOW_DC_THRESHOLD
 
41
#endif
 
42
 
 
43
/* Avoid zero allocations when MULLOW_BASECASE_THRESHOLD is 0.  */
 
44
#define MUL_BASECASE_ALLOC \
 
45
 (MULLOW_BASECASE_THRESHOLD_LIMIT == 0 ? 1 : 2*MULLOW_BASECASE_THRESHOLD_LIMIT)
 
46
 
 
47
/*
 
48
  FIXME: This function should accept a temporary area.
 
49
  FIXME: Perhaps call mpn_kara_mul_n instead of mpn_mul_n?
 
50
  THINK: If mpn_mul_basecase is always faster than mpn_mullow_basecase
 
51
         (typically thanks to mpn_addmul_2) should we unconditionally use
 
52
         mpn_mul_n?
 
53
  FIXME: The recursive calls to mpn_mullow_n use sizes n/2 (one uses floor(n/2)
 
54
         and the other ceil(n/2)).  Depending on the values of the various
 
55
         _THRESHOLDs, this may never trigger MULLOW_BASECASE_THRESHOLD.
 
56
         Should we worry about this overhead?
 
57
*/
 
58
 
 
59
void
 
60
mpn_mullow_n (mp_ptr rp, mp_srcptr xp, mp_srcptr yp, mp_size_t n)
 
61
{
 
62
  if (BELOW_THRESHOLD (n, MULLOW_BASECASE_THRESHOLD))
 
63
    {
 
64
      /* Allocate workspace of fixed size on stack: fast! */
 
65
      mp_limb_t ws[MUL_BASECASE_ALLOC];
 
66
      mpn_mul_basecase (ws, xp, n, yp, n);
 
67
      MPN_COPY (rp, ws, n);
 
68
    }
 
69
  else if (BELOW_THRESHOLD (n, MULLOW_DC_THRESHOLD))
 
70
    {
 
71
      mpn_mullow_basecase (rp, xp, yp, n);
 
72
    }
 
73
  else if (BELOW_THRESHOLD (n, MULLOW_MUL_N_THRESHOLD))
 
74
    {
 
75
      /* Divide-and-conquer */
 
76
      mp_size_t n2 = n >> 1;            /* floor(n/2) */
 
77
      mp_size_t n1 = n - n2;            /* ceil(n/2) */
 
78
      mp_ptr tp;
 
79
      TMP_SDECL;
 
80
      TMP_SMARK;
 
81
      tp = TMP_SALLOC_LIMBS (n1);
 
82
 
 
83
      /* Split as x = x1 2^(n1 GMP_NUMB_BITS) + x0,
 
84
                  y = y1 2^(n2 GMP_NUMB_BITS) + y0 */
 
85
 
 
86
      /* x0 * y0 */
 
87
      mpn_mul_n (rp, xp, yp, n2);
 
88
      if (n1 != n2)
 
89
        rp[2 * n2] = mpn_addmul_1 (rp + n2, yp, n2, xp[n2]);
 
90
 
 
91
      /* x1 * y0 * 2^(n1 GMP_NUMB_BITS) */
 
92
      mpn_mullow_n (tp, xp + n1, yp, n2);
 
93
      mpn_add_n (rp + n1, rp + n1, tp, n2);
 
94
 
 
95
      /* x0 * y1 * 2^(n2 GMP_NUMB_BITS) */
 
96
      mpn_mullow_n (tp, yp + n2, xp, n1);
 
97
      mpn_add_n (rp + n2, rp + n2, tp, n1);
 
98
      TMP_SFREE;
 
99
    }
 
100
  else
 
101
    {
 
102
      /* For really large operands, use plain mpn_mul_n but throw away upper n
 
103
         limbs of result.  */
 
104
      mp_ptr tp;
 
105
      TMP_DECL;
 
106
      TMP_MARK;
 
107
      tp = TMP_ALLOC_LIMBS (2 * n);
 
108
 
 
109
      mpn_mul_n (tp, xp, yp, n);
 
110
      MPN_COPY (rp, tp, n);
 
111
      TMP_FREE;
 
112
    }
 
113
}