1 | // $Source$ |
---|
2 | // $Revision: 1035 $ |
---|
3 | // $Date: 2003-07-18 06:43:34 +0000 (Fri, 18 Jul 2003) $ |
---|
4 | // $Author: pooh $ |
---|
5 | |
---|
6 | #include <rf_core.hh> |
---|
7 | #include <rf_short_int.hh> |
---|
8 | |
---|
9 | namespace refal |
---|
10 | { |
---|
11 | |
---|
12 | using namespace rfrt; |
---|
13 | using namespace rftype; |
---|
14 | |
---|
15 | namespace Arithm |
---|
16 | { |
---|
17 | |
---|
18 | RF_DECL(Rem); |
---|
19 | |
---|
20 | RF_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_short_int) { |
---|
28 | intptr_t n = static_cast<ShortInt*>(p_Int1)->to_int(); |
---|
29 | if (n < 0) e1 = ShortInt::create_expr(-n); |
---|
30 | else e1 = s_Int1; |
---|
31 | } else if (p_Int1->get_type() == type_int) { |
---|
32 | Integer* n = static_cast<Integer*>(p_Int1); |
---|
33 | if (n->sign() < 0) e1 = Integer::create_expr(-(*n)); |
---|
34 | else e1 = s_Int1; |
---|
35 | } else { |
---|
36 | RF_LIB_ERROR("Invalid argument"); |
---|
37 | } |
---|
38 | if (p_Int2->get_type() == type_short_int) { |
---|
39 | intptr_t n = static_cast<ShortInt*>(p_Int2)->to_int(); |
---|
40 | if (n < 0) e2 = ShortInt::create_expr(-n); |
---|
41 | else e2 = s_Int2; |
---|
42 | } else if (p_Int2->get_type() == type_int) { |
---|
43 | Integer* n = static_cast<Integer*>(p_Int2); |
---|
44 | if (n->sign() < 0) e2 = Integer::create_expr(-(*n)); |
---|
45 | else e2 = s_Int2; |
---|
46 | } else { |
---|
47 | RF_LIB_ERROR("Invalid argument"); |
---|
48 | } |
---|
49 | |
---|
50 | Expr r; |
---|
51 | while (true) { |
---|
52 | p_Int2 = e2.get_first(); |
---|
53 | if (p_Int2->get_type() == type_short_int) { |
---|
54 | if (static_cast<ShortInt*>(p_Int2)->to_int() == 0) break; |
---|
55 | } else if (p_Int2->get_type() == type_int) { |
---|
56 | if (static_cast<Integer*>(p_Int2)->sign() == 0) break; |
---|
57 | } |
---|
58 | RF_CALL(Rem, (e1, e2), (r)); |
---|
59 | e1 = e2; |
---|
60 | e2 = r; |
---|
61 | } |
---|
62 | |
---|
63 | s_Gcd = e1; |
---|
64 | |
---|
65 | RF_END |
---|
66 | |
---|
67 | } |
---|
68 | |
---|
69 | } |
---|