1 | // $Source$ |
---|
2 | // $Revision: 1027 $ |
---|
3 | // $Date: 2003-07-17 12:15:17 +0000 (Thu, 17 Jul 2003) $ |
---|
4 | // $Author: pooh $ |
---|
5 | |
---|
6 | #include <rf_core.hh> |
---|
7 | #include <rf_short_int.hh> |
---|
8 | #include <rf_integer.ih> |
---|
9 | #include <inttypes.h> |
---|
10 | #include <gmp.h> |
---|
11 | |
---|
12 | namespace refal |
---|
13 | { |
---|
14 | |
---|
15 | using namespace rfrt; |
---|
16 | using namespace rftype; |
---|
17 | |
---|
18 | namespace Arithm |
---|
19 | { |
---|
20 | |
---|
21 | RF_FUNC (_p_, (RF_ARG s_Int1, s_Int2), (RF_RES s_Res)) |
---|
22 | |
---|
23 | Term* p_Int1 = s_Int1.get_first(); |
---|
24 | Term* p_Int2 = s_Int2.get_first(); |
---|
25 | |
---|
26 | if (p_Int1->get_type() == type_short_int) { |
---|
27 | intptr_t i1 = static_cast<ShortInt*>(p_Int1)->to_int(); |
---|
28 | if (p_Int2->get_type() == type_short_int) { |
---|
29 | intptr_t i2 = static_cast<ShortInt*>(p_Int2)->to_int(); |
---|
30 | if ((((i1 > 0) && (i2 > 0)) || ((i1 < 0) && (i2 < 0))) && |
---|
31 | imaxabs(i1) >= INTPTR_MAX - imaxabs(i2) |
---|
32 | ) { |
---|
33 | s_Res = Integer::create_expr(i1 + Integer(i2)); |
---|
34 | } else { |
---|
35 | s_Res = ShortInt::create_expr(i1 + i2); |
---|
36 | } |
---|
37 | } else if (p_Int2->get_type() == type_int) { |
---|
38 | Integer* i2 = static_cast<Integer*>(p_Int2); |
---|
39 | s_Res = Integer::create_expr(i1 + (*i2)); |
---|
40 | } else { |
---|
41 | RF_LIB_ERROR("Invalid argument"); |
---|
42 | } |
---|
43 | } else if (p_Int1->get_type() == type_int) { |
---|
44 | Integer* i1 = static_cast<Integer*>(p_Int1); |
---|
45 | if (p_Int2->get_type() == type_short_int) { |
---|
46 | intptr_t i2 = static_cast<ShortInt*>(p_Int2)->to_int(); |
---|
47 | s_Res = Integer::create_expr((*i1) + i2); |
---|
48 | } else if (p_Int2->get_type() == type_int) { |
---|
49 | Integer* i2 = static_cast<Integer*>(p_Int2); |
---|
50 | s_Res = Integer::create_expr((*i1) + (*i2)); |
---|
51 | } else { |
---|
52 | RF_LIB_ERROR("Invalid argument"); |
---|
53 | } |
---|
54 | } else { |
---|
55 | RF_LIB_ERROR("Invalid argument"); |
---|
56 | } |
---|
57 | RF_END |
---|
58 | } |
---|
59 | |
---|
60 | } |
---|