source: to-imperative/trunk/library/Arithm/gcd.cc @ 1105

Last change on this file since 1105 was 1105, checked in by luba, 17 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.0 KB
Line 
1// $Source$
2// $Revision: 1105 $
3// $Date: 2003-08-06 13:27:37 +0000 (Wed, 06 Aug 2003) $
4// $Author: luba $
5
6#include <rf_core.hh>
7#include <rf_short_int.hh>
8
9namespace refal
10{
11
12using namespace rfrt;
13using namespace rftype;
14
15namespace Arithm
16{
17
18RF_DECL(Rem);
19
20RF_FUNC(GCD, (RF_ARG s_Int1, s_Int2), (RF_RES s_Gcd))
21
22  Term* p_Int1 = s_Int1.get_first();
23  Term* p_Int2 = s_Int2.get_first();
24
25  Expr e1, e2;
26
27  if (p_Int1->get_type() == type_int) {
28    Integer* n = static_cast<Integer*>(p_Int1);
29    if (n->sign() < 0) e1 = Integer::create_expr(-(*n));
30    else e1 = s_Int1;
31  } else {
32    RF_LIB_ERROR("Invalid argument");
33  }
34  if (p_Int2->get_type() == type_int) {
35    Integer* n = static_cast<Integer*>(p_Int2);
36    if (n->sign() < 0) e2 = Integer::create_expr(-(*n));
37    else e2 = s_Int2;
38  } else {
39    RF_LIB_ERROR("Invalid argument");
40  }
41
42  Expr r;
43  while (true) {
44    p_Int2 = e2.get_first();
45    if (p_Int2->get_type() == type_int) {
46      if (static_cast<Integer*>(p_Int2)->sign() == 0) break;
47    }
48    RF_CALL(Rem, (e1, e2), (r));
49    e1 = e2;
50    e2 = r;
51  }
52
53  s_Gcd = e1;
54
55RF_END
56
57}
58
59}
Note: See TracBrowser for help on using the repository browser.