Changeset 1100


Ignore:
Timestamp:
Aug 6, 2003, 5:20:29 PM (17 years ago)
Author:
luba
Message:
  • Short Arithmetic functions
Location:
to-imperative/trunk/library/ShortArithm
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/library/ShortArithm/add.cc

    r438 r1100  
    55
    66#include <rf_core.hh>
    7 #include <rf_int.hh>
    87#include <rf_short_int.hh>
    98
     
    1716{
    1817
    19 RF_FUNC (_p_, (RF_ARG s_Int1, s_Int2), (RF_RES s_Int))
     18RF_FUNC (_p_, (RF_ARG s_Int1, s_Int2), (RF_RES s_Res))
    2019
    21   Term* p_Int1 = s_Int1.get_first(); 
    22   Term* p_Int2 = s_Int2.get_first(); 
     20  Term* p_Int1 = s_Int1.get_first();
     21  Term* p_Int2 = s_Int2.get_first();
    2322
    24   if (p_Int1->get_type() != RF_TYPE(ShortInt) ||
    25       p_Int2->get_type() != RF_TYPE(ShortInt))
    26   {
     23  if (p_Int1->get_type() == type_int32) {
     24    intptr_t i1 = static_cast<Int32*>(p_Int1)->to_int();
     25    if (p_Int2->get_type() == type_int32) {
     26      intptr_t i2 = static_cast<Int32*>(p_Int2)->to_int();
     27      s_Res = Int32::create_expr(i1 + i2);
     28    }
     29    else
     30      RF_LIB_ERROR("Invalid argument");
     31  } 
     32  else
    2733    RF_LIB_ERROR("Invalid argument");
    28   }
     34   
    2935
    30   intptr_t int1 = ShortInt(*p_Int1);
    31   intptr_t int2 = ShortInt(*p_Int2);
    32 
    33   if (int1 > 0)
    34   {
    35     if (int2 > 0 && int1 > INTPTR_MAX - int2)
    36       RF_LIB_ERROR("Size limit exceeded");
    37   }
    38   else
    39   if (int2 < 0 && int1 < INTPTR_MIN - int2)
    40     RF_LIB_ERROR("Size limit exceeded");
    41 
    42   s_Int = ShortInt(int1 + int2).to_symbol();
    43          
    4436RF_END
    4537}
     38
    4639}
  • to-imperative/trunk/library/ShortArithm/div.cc

    r435 r1100  
    1616{
    1717
    18 RF_FUNC (Div, (RF_ARG s_Int1, s_Int2), (RF_RES s_Quo))
     18RF_FUNC (Div, (RF_ARG s_Int1, s_Int2), (RF_RES s_Res))
    1919
    20   Term* p_Int1 = s_Int1.get_first(); 
    21   Term* p_Int2 = s_Int2.get_first(); 
     20  Term* p_Int1 = s_Int1.get_first();
     21  Term* p_Int2 = s_Int2.get_first();
    2222
    23   if ( p_Int1->get_type() != RF_TYPE(ShortInt) ||
    24        p_Int2->get_type() != RF_TYPE(ShortInt)
    25      )
    26   {
     23  if (p_Int1->get_type() == type_int32) {
     24    intptr_t i1 = static_cast<Int32*>(p_Int1)->to_int();
     25    if (p_Int2->get_type() == type_int32) {
     26      intptr_t i2 = static_cast<Int32*>(p_Int2)->to_int();
     27      s_Res = Int32::create_expr(i1 / i2);
     28    }
     29    else
     30      RF_LIB_ERROR("Invalid argument");
     31  }
     32  else
    2733    RF_LIB_ERROR("Invalid argument");
    28   }
    29 
    30   intptr_t int1 = ShortInt(*p_Int1);
    31   intptr_t int2 = ShortInt(*p_Int2);
    32 
    33   if (!int2) RF_LIB_ERROR("Divide by zero");
    34 
    35   s_Quo = ShortInt(int1 / int2).to_symbol();
    3634
    3735RF_END
     36}
    3837
    3938}
    40 }
  • to-imperative/trunk/library/ShortArithm/div_rem.cc

    r438 r1100  
    55
    66#include <rf_core.hh>
    7 #include <rf_int.hh>
    87#include <rf_short_int.hh>
    98
     
    1918RF_FUNC (Div_m_Rem, (RF_ARG s_Int1, s_Int2), (RF_RES s_Quo, s_Rem))
    2019
    21   Term* p_Int1 = s_Int1.get_first(); 
    22   Term* p_Int2 = s_Int2.get_first(); 
     20  Term* p_Int1 = s_Int1.get_first();
     21  Term* p_Int2 = s_Int2.get_first();
    2322
    24   if (p_Int1->get_type() != RF_TYPE(ShortInt) ||
    25       p_Int2->get_type() != RF_TYPE(ShortInt))
    26   {
     23  if (p_Int1->get_type() == type_int32) {
     24    intptr_t i1 = static_cast<Int32*>(p_Int1)->to_int();
     25    if (p_Int2->get_type() == type_int32) {
     26      intptr_t i2 = static_cast<Int32*>(p_Int2)->to_int();
     27      s_Quo = Int32::create_expr(i1 / i2);
     28      s_Rem = Int32::create_expr(i1 % i2);
     29    } else {
     30      RF_LIB_ERROR("Invalid argument");
     31    }
     32  } else {
    2733    RF_LIB_ERROR("Invalid argument");
    2834  }
    29 
    30   intptr_t int1 = ShortInt(*p_Int1);
    31   intptr_t int2 = ShortInt(*p_Int2);
    32 
    33   if (!int2) RF_LIB_ERROR("DIV Divide by zero");
    34  
    35   s_Quo = ShortInt(int1 / int2).to_symbol();
    36   s_Rem = ShortInt(int1 % int2).to_symbol();
    37 
    3835RF_END
     36}
    3937
    4038}
    41 }
  • to-imperative/trunk/library/ShortArithm/gcd.cc

    r438 r1100  
    55
    66#include <rf_core.hh>
    7 #include <rf_int.hh>
    87#include <rf_short_int.hh>
    98
     
    1615namespace ShortArithm
    1716{
    18 RF_FUNC (GCD, (RF_ARG s_Int1, s_Int2), (RF_RES s_Gcd))
    1917
    20   Term* p_Int1 = s_Int1.get_first(); 
    21   Term* p_Int2 = s_Int2.get_first(); 
     18RF_DECL(Rem);
    2219
    23   if (p_Int1->get_type() != RF_TYPE(ShortInt) ||
    24       p_Int2->get_type() != RF_TYPE(ShortInt))
    25   {
     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  Expr e1, e2;
     25  if (p_Int1->get_type() == type_int32) {
     26    intptr_t n = static_cast<Int32*>(p_Int1)->to_int();
     27    if (n < 0) e1 = Int32::create_expr(-n);
     28    else e1 = s_Int1;
     29  } else {
     30    RF_LIB_ERROR("Invalid argument");
     31  }
     32  if (p_Int2->get_type() == type_int32) {
     33    intptr_t n = static_cast<Int32*>(p_Int2)->to_int();
     34    if (n < 0) e2 = Int32::create_expr(-n);
     35    else e2 = s_Int2;
     36  } else {
    2637    RF_LIB_ERROR("Invalid argument");
    2738  }
    2839
    29   intptr_t int1 = ShortInt(*p_Int1);
    30   intptr_t int2 = ShortInt(*p_Int2);
     40  Expr r;
     41  while (true) {
     42    p_Int2 = e2.get_first();
     43    if (p_Int2->get_type() == type_int32) {
     44      if (static_cast<Int32*>(p_Int2)->to_int() == 0) break;
     45    }
     46    RF_CALL(Rem, (e1, e2), (r));
     47    e1 = e2;
     48    e2 = r;
     49  }
    3150
    32   if ((!int2) && (!int1)) 
    33     RF_LIB_ERROR("GCD Zero arguments");
    34 
    35   if (int1 < 0) int1 = -int1;
    36   if (int2 < 0) int2 = -int2; 
    37 
    38   while (int2) 
    39   {
    40     intptr_t r = int1 % int2;
    41     int1 = int2;
    42     int2 = r;
    43   };
    44 
    45   s_Gcd = ShortInt(int1).to_symbol();
     51  s_Gcd = e1;
    4652
    4753RF_END
    4854
    4955}
     56
    5057}
  • to-imperative/trunk/library/ShortArithm/mult.cc

    r438 r1100  
    55
    66#include <rf_core.hh>
    7 #include <rf_int.hh>
    87#include <rf_short_int.hh>
    98
     
    1716{
    1817
    19 RF_FUNC (_a_, (RF_ARG s_Int1, s_Int2), (RF_RES s_Int))
     18RF_FUNC (_a_, (RF_ARG s_Int1, s_Int2), (RF_RES s_Res))
    2019
    21   Term* p_Int1 = s_Int1.get_first(); 
    22   Term* p_Int2 = s_Int2.get_first(); 
     20  Term* p_Int1 = s_Int1.get_first();
     21  Term* p_Int2 = s_Int2.get_first();
    2322
    24   if (p_Int1->get_type() != RF_TYPE(ShortInt) ||
    25       p_Int2->get_type() != RF_TYPE(ShortInt) )
    26   {
     23  if (p_Int1->get_type() == type_int32) {
     24    intptr_t i1 = static_cast<Int32*>(p_Int1)->to_int();
     25    if (p_Int2->get_type() == type_int32) {
     26      intptr_t i2 = static_cast<Int32*>(p_Int2)->to_int();
     27      s_Res = Int32::create_expr(i1 * i2);
     28    } else {
     29      RF_LIB_ERROR("Invalid argument");
     30    }
     31  } else {
    2732    RF_LIB_ERROR("Invalid argument");
    2833  }
    29 
    30   intptr_t int1 = ShortInt(*p_Int1);
    31   intptr_t int2 = ShortInt(*p_Int2);
    32    
    33   if (int1 > 0)
    34   {
    35     if (int2 > 0)
    36     {
    37       if (int1 > INTPTR_MAX / int2)
    38         RF_LIB_ERROR("Size limit exceeded");
    39     }
    40     else
    41     if (int2 < INTPTR_MIN / int1)
    42       RF_LIB_ERROR("Size limit exceeded");
    43   }
    44   else
    45   {
    46   if (int2 > 0)
    47   {
    48     if (int1 < INTPTR_MIN / int2)
    49       RF_LIB_ERROR("Size limit exceeded");
    50   }
    51   else
    52   if (int2 && int1 < INTPTR_MAX / int2)
    53     RF_LIB_ERROR("Size limit exceeded");
    54   }
    55 
    56   s_Int = ShortInt(int1 * int2).to_symbol();
    57 
    5834RF_END
     35}
    5936
    6037}
    61 
  • to-imperative/trunk/library/ShortArithm/rem.cc

    r438 r1100  
    55
    66#include <rf_core.hh>
    7 #include <rf_int.hh>
    87#include <rf_short_int.hh>
    98
     
    1615namespace ShortArithm
    1716{
    18 RF_FUNC (Rem, (RF_ARG s_Int1, s_Int2), (RF_RES s_Rem))
    1917
    20   Term* p_Int1 = s_Int1.get_first(); 
    21   Term* p_Int2 = s_Int2.get_first(); 
     18RF_FUNC (Rem, (RF_ARG s_Int1, s_Int2), (RF_RES s_Res))
    2219
    23   if (p_Int1->get_type() != RF_TYPE(ShortInt) ||
    24       p_Int2->get_type() != RF_TYPE(ShortInt) )
    25   {
     20  Term* p_Int1 = s_Int1.get_first();
     21  Term* p_Int2 = s_Int2.get_first();
     22
     23  if (p_Int1->get_type() == type_int32) {
     24    intptr_t i1 = static_cast<Int32*>(p_Int1)->to_int();
     25    if (p_Int2->get_type() == type_int32) {
     26      intptr_t i2 = static_cast<Int32*>(p_Int2)->to_int();
     27      s_Res = Int32::create_expr(i1 % i2);
     28    } else {
     29      RF_LIB_ERROR("Invalid argument");
     30    }
     31  } else {
    2632    RF_LIB_ERROR("Invalid argument");
    2733  }
    28  
    29   intptr_t int1 = ShortInt(*p_Int1);
    30   intptr_t int2 = ShortInt(*p_Int2);
    31 
    32   if (!int2)
    33     RF_LIB_ERROR("DIV Divide by zero");
    34  
    35   s_Rem = ShortInt(int1 % int2).to_symbol();
    36 
    3734RF_END
     35}
    3836
    3937}
    40 }
  • to-imperative/trunk/library/ShortArithm/sub.cc

    r438 r1100  
    55
    66#include <rf_core.hh>
    7 #include <rf_int.hh>
    87#include <rf_short_int.hh>
    98
     
    1615namespace ShortArithm
    1716{
    18 RF_FUNC (_m_, (RF_ARG s_Int1, s_Int2), (RF_RES s_Int))
    1917
    20   Term* p_Int1 = s_Int1.get_first(); 
    21   Term* p_Int2 = s_Int2.get_first();
     18RF_FUNC (_m_, (RF_ARG s_Int1, s_Int2), (RF_RES s_Res))
    2219
    23   if (p_Int1->get_type() != RF_TYPE(ShortInt)||
    24       p_Int2->get_type() != RF_TYPE(ShortInt) )
    25   {
     20  Term* p_Int1 = s_Int1.get_first();
     21  Term* p_Int2 = s_Int2.get_first();
     22
     23  if (p_Int1->get_type() == type_int32) {
     24    intptr_t i1 = static_cast<Int32*>(p_Int1)->to_int();
     25    if (p_Int2->get_type() == type_int32) {
     26      intptr_t i2 = static_cast<Int32*>(p_Int2)->to_int();
     27      s_Res = Int32::create_expr(i1 - i2);
     28    }
     29    else
     30      RF_LIB_ERROR("Invalid argument");
     31  }
     32  else
    2633    RF_LIB_ERROR("Invalid argument");
    27   }
    28 
    29   intptr_t int1 = ShortInt(*p_Int1);
    30   intptr_t int2 = ShortInt(*p_Int2);
    31 
    32   if (int1 > 0)
    33   {
    34     if (int2 < 0 && int1 > INTPTR_MAX + int2)
    35       RF_LIB_ERROR("Size limit exceeded");
    36   }
    37   else
    38   if (int2 > 0 && int1 < INTPTR_MIN + int2)
    39     RF_LIB_ERROR("Size limit exceeded");
    40 
    41   s_Int = ShortInt(int1 - int2).to_symbol();
    42 
     34 
    4335RF_END
    4436}
     37
    4538}
Note: See TracChangeset for help on using the changeset viewer.