This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmapm_gcd.c
More file actions
284 lines (229 loc) · 5.4 KB
/
mapm_gcd.c
File metadata and controls
284 lines (229 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* M_APM - mapm_gcd.c
*
* Copyright (C) 2001 - 2007 Michael C. Ring
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose with or without fee is hereby granted,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
*
* Permission to modify the software is granted. Permission to distribute
* the modified code is granted. Modifications are to be distributed by
* using the file 'license.txt' as a template to modify the file header.
* 'license.txt' is available in the official MAPM distribution.
*
* This software is provided "as is" without express or implied warranty.
*/
/*
* $Id: mapm_gcd.c,v 1.8 2007/12/03 01:41:05 mike Exp $
*
* This file contains the GCD and LCM functions
*
* $Log: mapm_gcd.c,v $
* Revision 1.8 2007/12/03 01:41:05 mike
* Update license
*
* Revision 1.7 2003/07/21 20:16:43 mike
* Modify error messages to be in a consistent format.
*
* Revision 1.6 2003/03/31 22:12:33 mike
* call generic error handling function
*
* Revision 1.5 2002/11/03 22:44:21 mike
* Updated function parameters to use the modern style
*
* Revision 1.4 2002/05/17 22:17:47 mike
* fix comment
*
* Revision 1.3 2002/05/17 22:16:36 mike
* move even/odd util functions to mapmutl2
*
* Revision 1.2 2001/07/15 20:55:20 mike
* add comments
*
* Revision 1.1 2001/07/15 20:48:27 mike
* Initial revision
*/
#include "m_apm_lc.h"
/****************************************************************************/
/*
* From Knuth, The Art of Computer Programming:
*
* This is the binary GCD algorithm as described
* in the book (Algorithm B)
*/
void m_apm_gcd(M_APM r, M_APM u, M_APM v)
{
M_APM tmpM, tmpN, tmpT, tmpU, tmpV;
int kk, kr, mm;
long pow_2;
/* 'is_integer' will return 0 || 1 */
if ((m_apm_is_integer(u) + m_apm_is_integer(v)) != 2)
{
M_apm_log_error_msg(M_APM_RETURN, "\'m_apm_gcd\', Non-integer input");
M_set_to_zero(r);
return;
}
if (u->m_apm_sign == 0)
{
m_apm_absolute_value(r, v);
return;
}
if (v->m_apm_sign == 0)
{
m_apm_absolute_value(r, u);
return;
}
tmpM = M_get_stack_var();
tmpN = M_get_stack_var();
tmpT = M_get_stack_var();
tmpU = M_get_stack_var();
tmpV = M_get_stack_var();
m_apm_absolute_value(tmpU, u);
m_apm_absolute_value(tmpV, v);
/* Step B1 */
kk = 0;
while (TRUE)
{
mm = 1;
if (m_apm_is_odd(tmpU))
break;
mm = 0;
if (m_apm_is_odd(tmpV))
break;
m_apm_multiply(tmpN, MM_0_5, tmpU);
m_apm_copy(tmpU, tmpN);
m_apm_multiply(tmpN, MM_0_5, tmpV);
m_apm_copy(tmpV, tmpN);
kk++;
}
/* Step B2 */
if (mm)
{
m_apm_negate(tmpT, tmpV);
goto B4;
}
m_apm_copy(tmpT, tmpU);
/* Step: */
B3:
m_apm_multiply(tmpN, MM_0_5, tmpT);
m_apm_copy(tmpT, tmpN);
/* Step: */
B4:
if (m_apm_is_even(tmpT))
goto B3;
/* Step B5 */
if (tmpT->m_apm_sign == 1)
m_apm_copy(tmpU, tmpT);
else
m_apm_negate(tmpV, tmpT);
/* Step B6 */
m_apm_subtract(tmpT, tmpU, tmpV);
if (tmpT->m_apm_sign != 0)
goto B3;
/*
* result = U * 2 ^ kk
*/
if (kk == 0)
m_apm_copy(r, tmpU);
else
{
if (kk == 1)
m_apm_multiply(r, tmpU, MM_Two);
if (kk == 2)
m_apm_multiply(r, tmpU, MM_Four);
if (kk >= 3)
{
mm = kk / 28;
kr = kk % 28;
pow_2 = 1L << kr;
if (mm == 0)
{
m_apm_set_long(tmpN, pow_2);
m_apm_multiply(r, tmpU, tmpN);
}
else
{
m_apm_copy(tmpN, MM_One);
m_apm_set_long(tmpM, 0x10000000L); /* 2 ^ 28 */
while (TRUE)
{
m_apm_multiply(tmpT, tmpN, tmpM);
m_apm_copy(tmpN, tmpT);
if (--mm == 0)
break;
}
if (kr == 0)
{
m_apm_multiply(r, tmpU, tmpN);
}
else
{
m_apm_set_long(tmpM, pow_2);
m_apm_multiply(tmpT, tmpN, tmpM);
m_apm_multiply(r, tmpU, tmpT);
}
}
}
}
M_restore_stack(5);
}
/****************************************************************************/
/*
* u * v
* LCM(u,v) = ------------
* GCD(u,v)
*/
void m_apm_lcm(M_APM r, M_APM u, M_APM v)
{
M_APM tmpN, tmpG;
tmpN = M_get_stack_var();
tmpG = M_get_stack_var();
m_apm_multiply(tmpN, u, v);
m_apm_gcd(tmpG, u, v);
m_apm_integer_divide(r, tmpN, tmpG);
M_restore_stack(2);
}
/****************************************************************************/
#ifdef BIG_COMMENT_BLOCK
/*
* traditional GCD included for reference
* (also useful for testing ...)
*/
/*
* From Knuth, The Art of Computer Programming:
*
* To compute GCD(u,v)
*
* A1:
* if (v == 0) return (u)
* A2:
* t = u mod v
* u = v
* v = t
* goto A1
*/
void m_apm_gcd_traditional(M_APM r, M_APM u, M_APM v)
{
M_APM tmpD, tmpN, tmpU, tmpV;
tmpD = M_get_stack_var();
tmpN = M_get_stack_var();
tmpU = M_get_stack_var();
tmpV = M_get_stack_var();
m_apm_absolute_value(tmpU, u);
m_apm_absolute_value(tmpV, v);
while (TRUE)
{
if (tmpV->m_apm_sign == 0)
break;
m_apm_integer_div_rem(tmpD, tmpN, tmpU, tmpV);
m_apm_copy(tmpU, tmpV);
m_apm_copy(tmpV, tmpN);
}
m_apm_copy(r, tmpU);
M_restore_stack(4);
}
/****************************************************************************/
#endif