5 * Copyright (C) 1999 - 2007 Michael C. Ring
7 * Permission to use, copy, and distribute this software and its
8 * documentation for any purpose with or without fee is hereby granted,
9 * provided that the above copyright notice appear in all copies and
10 * that both that copyright notice and this permission notice appear
11 * in supporting documentation.
13 * Permission to modify the software is granted. Permission to distribute
14 * the modified code is granted. Modifications are to be distributed by
15 * using the file 'license.txt' as a template to modify the file header.
16 * 'license.txt' is available in the official MAPM distribution.
18 * This software is provided "as is" without express or implied warranty.
22 * $Id: mapm_mul.c,v 1.16 2007/12/03 01:45:27 mike Exp $
24 * This file contains basic multiplication function.
26 * $Log: mapm_mul.c,v $
27 * Revision 1.16 2007/12/03 01:45:27 mike
30 * Revision 1.15 2004/02/21 04:30:35 mike
31 * minor optimization by using pointers instead of array indexes.
33 * Revision 1.14 2003/07/21 20:18:59 mike
34 * Modify error messages to be in a consistent format.
36 * Revision 1.13 2003/03/31 22:14:05 mike
37 * call generic error handling function
39 * Revision 1.12 2002/11/03 22:25:36 mike
40 * Updated function parameters to use the modern style
42 * Revision 1.11 2001/07/24 18:24:26 mike
43 * access div/rem lookup table directly
46 * Revision 1.10 2001/02/11 22:31:39 mike
47 * modify parameters to REALLOC
49 * Revision 1.9 2000/07/09 00:20:03 mike
50 * change break even point again ....
52 * Revision 1.8 2000/07/08 18:51:43 mike
53 * change break even point between this O(n^2)
54 * multiply and the FFT multiply
56 * Revision 1.7 2000/04/14 16:27:45 mike
57 * change the break even point between the 2 multiply
58 * functions since we made the fast one even faster.
60 * Revision 1.6 2000/02/03 22:46:40 mike
61 * use MAPM_* generic memory function
63 * Revision 1.5 1999/09/19 21:10:14 mike
64 * change the break even point between the 2 multiply choices
66 * Revision 1.4 1999/08/09 23:57:17 mike
69 * Revision 1.3 1999/08/09 02:38:17 mike
70 * tweak break even point and add comments
72 * Revision 1.2 1999/08/08 18:35:20 mike
73 * add call to fast algorithm if input numbers are large
75 * Revision 1.1 1999/05/10 20:56:31 mike
81 extern void M_fast_multiply(M_APM
, M_APM
, M_APM
);
83 /****************************************************************************/
84 void m_apm_multiply(M_APM r
, M_APM a
, M_APM b
)
86 int ai
, itmp
, sign
, nexp
, ii
, jj
, indexa
, indexb
, index0
, numdigits
;
87 UCHAR
*cp
, *cpr
, *cp_div
, *cp_rem
;
90 sign
= a
->m_apm_sign
* b
->m_apm_sign
;
91 nexp
= a
->m_apm_exponent
+ b
->m_apm_exponent
;
93 if (sign
== 0) /* one number is zero, result is zero */
99 numdigits
= a
->m_apm_datalength
+ b
->m_apm_datalength
;
100 indexa
= (a
->m_apm_datalength
+ 1) >> 1;
101 indexb
= (b
->m_apm_datalength
+ 1) >> 1;
104 * If we are multiplying 2 'big' numbers, use the fast algorithm.
106 * This is a **very** approx break even point between this algorithm
107 * and the FFT multiply. Note that different CPU's, operating systems,
108 * and compiler's may yield a different break even point. This point
109 * (~96 decimal digits) is how the test came out on the author's system.
112 if (indexa
>= 48 && indexb
>= 48)
114 M_fast_multiply(r
, a
, b
);
118 ii
= (numdigits
+ 1) >> 1; /* required size of result, in bytes */
120 if (ii
> r
->m_apm_malloclength
)
122 if ((vp
= MAPM_REALLOC(r
->m_apm_data
, (ii
+ 32))) == NULL
)
124 /* fatal, this does not return */
126 M_apm_log_error_msg(M_APM_FATAL
, "\'m_apm_multiply\', Out of memory");
129 r
->m_apm_malloclength
= ii
+ 28;
130 r
->m_apm_data
= (UCHAR
*)vp
;
133 M_get_div_rem_addr(&cp_div
, &cp_rem
);
135 index0
= indexa
+ indexb
;
137 memset(cp
, 0, index0
);
145 ai
= (int)a
->m_apm_data
[--ii
];
149 itmp
= ai
* b
->m_apm_data
[--jj
];
151 *(cpr
-1) += cp_div
[itmp
];
152 *cpr
+= cp_rem
[itmp
];
176 r
->m_apm_sign
= sign
;
177 r
->m_apm_exponent
= nexp
;
178 r
->m_apm_datalength
= numdigits
;
182 /****************************************************************************/