Changeset 1035


Ignore:
Timestamp:
Jul 18, 2003, 10:43:34 AM (17 years ago)
Author:
pooh
Message:
  • Finished library changes for long arithmetic.
Location:
to-imperative/trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/library/Arithm/Makefile

    r813 r1035  
    1212  rem \
    1313  div_rem \
    14   gcd
     14  gcd \
     15  neg
    1516
    1617SOURCES = $(addsuffix .cc, $(MODULES))
  • to-imperative/trunk/library/Arithm/div_rem.cc

    r591 r1035  
    66#include <rf_core.hh>
    77#include <rf_short_int.hh>
     8#include <rf_integer.ih>
     9#include <inttypes.h>
     10#include <gmp.h>
    811
    912namespace refal
     
    1821RF_FUNC (Div_m_Rem, (RF_ARG s_Int1, s_Int2), (RF_RES s_Quo, s_Rem))
    1922
    20   Term* p_Int1 = s_Int1.get_first(); 
    21   Term* p_Int2 = s_Int2.get_first(); 
     23  Term* p_Int1 = s_Int1.get_first();
     24  Term* p_Int2 = s_Int2.get_first();
    2225
    23     intptr_t int1 = 0;
    24   intptr_t int2 = 0;
    25 
    26    if (p_Int1->get_type() == type_short_int)
    27   {
    28     int1 = ((ShortInt*)p_Int1)->to_int();
    29   }
    30   else
    31   if (p_Int1->get_type() == type_int)
    32   {
     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      s_Quo = ShortInt::create_expr(i1 / i2);
     31      s_Rem = ShortInt::create_expr(i1 % i2);
     32    } else if (p_Int2->get_type() == type_int) {
     33      Integer* i2 = static_cast<Integer*>(p_Int2);
     34      Expr r = Integer::create_expr(0);
     35      Integer* rp = static_cast<Integer*>(r.get_first());
     36      s_Quo = Integer::create_expr(div_rem(i1, *i2, rp));
     37      s_Rem = r;
     38    } else {
     39      RF_LIB_ERROR("Invalid argument");
     40    }
     41  } else if (p_Int1->get_type() == type_int) {
     42    Integer* i1 = static_cast<Integer*>(p_Int1);
     43    Expr r = Integer::create_expr(0);
     44    Integer* rp = static_cast<Integer*>(r.get_first());
     45    if (p_Int2->get_type() == type_short_int) {
     46      intptr_t i2 = static_cast<ShortInt*>(p_Int2)->to_int();
     47      s_Quo = Integer::create_expr(i1->div_rem(i2, rp));
     48      s_Rem = r;
     49    } else if (p_Int2->get_type() == type_int) {
     50      Integer* i2 = static_cast<Integer*>(p_Int2);
     51      s_Quo = Integer::create_expr(i1->div_rem(*i2, rp));
     52      s_Rem = r;
     53    } else {
     54      RF_LIB_ERROR("Invalid argument");
     55    }
     56  } else {
    3357    RF_LIB_ERROR("Invalid argument");
    3458  }
    35   else
    36   {
    37     RF_LIB_ERROR("Invalid argument");
    38   }
    39   if (p_Int2->get_type() == type_short_int)
    40   {
    41     int2 = ((ShortInt*)p_Int2)->to_int();
    42   }
    43   else
    44   if (p_Int2->get_type() == type_int)
    45   {
    46     RF_LIB_ERROR("Invalid argument");
    47   }
    48   else
    49   {
    50     RF_LIB_ERROR("Invalid argument");
    51   }
    52  
    53   if (!int2)
    54     RF_LIB_ERROR("DIV Divide by zero");
    55  
    56   s_Quo = ShortInt::create_expr(int1 / int2);
    57   s_Rem = ShortInt::create_expr(int1 % int2);
    58  
    5959RF_END
    6060}
     61
    6162}
  • to-imperative/trunk/library/Arithm/gcd.cc

    r591 r1035  
    1515namespace Arithm
    1616{
    17 RF_FUNC (GCD, (RF_ARG s_Int1, s_Int2), (RF_RES s_Gcd))
    1817
    19   Term* p_Int1 = s_Int1.get_first(); 
    20   Term* p_Int2 = s_Int2.get_first(); 
     18RF_DECL(Rem);
    2119
    22   intptr_t int1 = 0;
    23   intptr_t int2 = 0;
     20RF_FUNC(GCD, (RF_ARG s_Int1, s_Int2), (RF_RES s_Gcd))
    2421
    25    if (p_Int1->get_type() == type_short_int)
    26   {
    27     int1 = ((ShortInt*)p_Int1)->to_int();
    28   }
    29   else
    30   if (p_Int1->get_type() == type_int)
    31   {
     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 {
    3236    RF_LIB_ERROR("Invalid argument");
    3337  }
    34   else
    35   {
    36     RF_LIB_ERROR("Invalid argument");
    37   }
    38   if (p_Int2->get_type() == type_short_int)
    39   {
    40     int2 = ((ShortInt*)p_Int2)->to_int();
    41   }
    42   else
    43   if (p_Int2->get_type() == type_int)
    44   {
    45     RF_LIB_ERROR("Invalid argument");
    46   }
    47   else
    48   {
     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 {
    4947    RF_LIB_ERROR("Invalid argument");
    5048  }
    5149
    52   if ((!int2) && (!int1)) 
    53     RF_LIB_ERROR("GCD Zero arguments");
    54  
    55   if (int1 < 0) int1 = -int1;
    56   if (int2 < 0) int2 = -int2; 
     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  }
    5762
    58   while (int2) 
    59   {
    60     intptr_t r = int1 % int2;
    61     int1 = int2;
    62     int2 = r;
    63   };
    64 
    65   s_Gcd = ShortInt::create_expr(int1);
     63  s_Gcd = e1;
    6664
    6765RF_END
     66
    6867}
     68
    6969}
  • to-imperative/trunk/runtime/rf_integer.hh

    r1031 r1035  
    4646  inline Integer operator % (intptr_t _n) const ;
    4747  friend inline Integer operator % (intptr_t _n1, Integer const& _n2) ;
     48  inline Integer div_rem (Integer const& _n, Integer* _remp) const ;
     49  inline Integer div_rem (intptr_t _n, Integer* _remp) const ;
     50  friend inline Integer div_rem (
     51    intptr_t _n1, Integer const& _n2, Integer* _remp) ;
     52  inline Integer operator - () const ;
     53  inline int sign () const ;
    4854
    4955  inline operator pxx::WString () const ;
  • to-imperative/trunk/runtime/rf_integer.ih

    r1031 r1035  
    215215{
    216216  return Integer(_n1) % _n2;
     217}
     218
     219inline Integer Integer::div_rem (Integer const& _n, Integer* _remp) const
     220{
     221  Integer res(self);
     222  mpz_t* resp = res.get_mpz_ptr();
     223  if (_remp != null) {
     224    mpz_tdiv_qr(*resp, *(_remp->get_mpz_ptr()), *resp, *(_n.get_mpz_ptr()));
     225  } else {
     226    mpz_tdiv_q(*resp, *resp, *(_n.get_mpz_ptr()));
     227  }
     228  return res;
     229}
     230
     231inline Integer Integer::div_rem (intptr_t _n, Integer* _remp) const
     232{
     233  Integer res(self);
     234  mpz_t* resp = res.get_mpz_ptr();
     235  if (_remp != null) {
     236    if (_n >= 0 ) {
     237      mpz_tdiv_qr_ui(*resp, *(_remp->get_mpz_ptr()), *resp, _n);
     238    } else {
     239      mpz_tdiv_qr_ui(*resp, *(_remp->get_mpz_ptr()), *resp, -_n);
     240      mpz_neg(*resp, *resp);
     241    }
     242  } else {
     243    if (_n >= 0 ) {
     244      mpz_tdiv_r_ui(*resp, *resp, _n);
     245    } else {
     246      mpz_tdiv_r_ui(*resp, *resp, -_n);
     247      mpz_neg(*resp, *resp);
     248    }
     249  }
     250  return res;
     251}
     252
     253inline Integer div_rem (intptr_t _n1, Integer const& _n2, Integer* _remp)
     254{
     255  return Integer(_n1).div_rem(_n2, _remp);
     256}
     257
     258inline Integer Integer::operator - () const
     259{
     260  Integer res(self);
     261  mpz_t* resp = res.get_mpz_ptr();
     262  mpz_neg(*resp, *resp);
     263  return res;
     264}
     265
     266inline int Integer::sign () const
     267{
     268  return mpz_sgn(*(get_mpz_ptr()));
    217269}
    218270
Note: See TracChangeset for help on using the changeset viewer.