Changeset 1010


Ignore:
Timestamp:
Jul 15, 2003, 4:12:14 PM (17 years ago)
Author:
pooh
Message:
  • More work on long arithmetic.
Location:
to-imperative/trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/library/Arithm/mult.cc

    r591 r1010  
    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 (_a_, (RF_ARG s_Int1, s_Int2), (RF_RES s_Int))
    1922
    20   Term* p_Int1 = s_Int1.get_first(); 
    21   Term* p_Int2 = s_Int2.get_first(); 
    22  
    23   intptr_t int1 = 0;
    24   intptr_t int2 = 0;
     23  Term* p_Int1 = s_Int1.get_first();
     24  Term* p_Int2 = s_Int2.get_first();
    2525
    26    if (p_Int1->get_type() == type_short_int)
    27   {
     26  intptr_t int1;
     27  intptr_t int2;
     28  Expr result;
     29
     30  if (p_Int1->get_type() == type_short_int) {
    2831    int1 = ((ShortInt*)p_Int1)->to_int();
    29   }
    30   else
    31   if (p_Int1->get_type() == type_int)
    32   {
     32    if (p_Int2->get_type() == type_short_int) {
     33      int2 = ((ShortInt*)p_Int2)->to_int();
     34      if (imaxabs(int1) < INTPTR_MAX / imaxabs(int2)) {
     35        result = ShortInt::create_expr(int1 * int2);
     36      } else {
     37        result = Integer::create_expr(int1);
     38        Integer* res = static_cast<Integer*>(result.get_first());
     39        mpz_t* resp = res->get_mpz_ptr();
     40        mpz_mul_si(*resp, *resp, int2);
     41      }
     42    } else if (p_Int2->get_type() == type_int) {
     43      result = Integer::create_expr(int1);
     44      Integer* res = static_cast<Integer*>(result.get_first());
     45      mpz_t* resp = res->get_mpz_ptr();
     46      Integer* i2 = static_cast<Integer*>(p_Int2);
     47      mpz_t* i2p = i2->get_mpz_ptr();
     48      mpz_mul(*resp, *resp, *i2p);
     49    } else {
     50      RF_LIB_ERROR("Invalid argument");
     51    }
     52  } else if (p_Int1->get_type() == type_int) {
     53    Integer* i1 = static_cast<Integer*>(p_Int1);
     54    mpz_t* i1p = i1->get_mpz_ptr();
     55    if (p_Int2->get_type() == type_short_int) {
     56      int2 = ((ShortInt*)p_Int2)->to_int();
     57      result = Integer::create_expr(int2);
     58      Integer* res = static_cast<Integer*>(result.get_first());
     59      mpz_t* resp = res->get_mpz_ptr();
     60      mpz_mul(*resp, *resp, *i1p);
     61    } else if (p_Int2->get_type() == type_int) {
     62      result = Integer::create_expr(0);
     63      Integer* res = static_cast<Integer*>(result.get_first());
     64      mpz_t* resp = res->get_mpz_ptr();
     65      Integer* i2 = static_cast<Integer*>(p_Int2);
     66      mpz_t* i2p = i2->get_mpz_ptr();
     67      mpz_mul(*resp, *i1p, *i2p);
     68    } else {
     69      RF_LIB_ERROR("Invalid argument");
     70    }
     71  } else {
    3372    RF_LIB_ERROR("Invalid argument");
    3473  }
    35   else
    36   {
    37     RF_LIB_ERROR("Invalid argument");
    38   }
    39 
    40   if (p_Int2->get_type() == type_short_int)
    41   {
    42     int2 = ((ShortInt*)p_Int2)->to_int();
    43   }
    44   else
    45   if (p_Int2->get_type() == type_int)
    46   {
    47     RF_LIB_ERROR("Invalid argument");
    48   }
    49   else
    50   {
    51     RF_LIB_ERROR("Invalid argument");
    52   }
    53  
    54  
    55   if (int1 > 0)
    56   {
    57     if (int2 > 0)
    58     {
    59       if (int1 > INTPTR_MAX / int2)
    60         RF_LIB_ERROR("Size limit exceeded");
    61     }
    62     else
    63     if (int2 < INTPTR_MIN / int1)
    64       RF_LIB_ERROR("Size limit exceeded");
    65   }
    66   else
    67   {
    68   if (int2 > 0)
    69   {
    70     if (int1 < INTPTR_MIN / int2)
    71       RF_LIB_ERROR("Size limit exceeded");
    72   }
    73   else
    74   if (int2 && int1 < INTPTR_MAX /int2)
    75     RF_LIB_ERROR("Size limit exceeded");
    76   }
    77 
    78   s_Int = ShortInt::create_expr(int1 * int2);
    79 
     74  s_Int = result;
    8075RF_END
    8176}
    82 
     77
     78}
  • to-imperative/trunk/runtime/rf_common.hh

    r703 r1010  
    88#include "pxx_common.hh"
    99#include "pxx_heap_allocator.hh"
     10#include "pxx_chunk_allocator.hh"
     11#include <gmp.h>
    1012
    1113namespace rfrt
     
    2325extern uintptr_t heap_size ;
    2426extern pxx::HeapAllocator& allocator ;
     27extern pxx::ChunkAllocator<mpz_t>& mpz_allocator ;
    2528static void* const stack_start = (void*)0x50000000 ;
    2629static uintptr_t const default_stack_size = uintptr_t(1ULL << 26) ;
  • to-imperative/trunk/runtime/rf_core.cc

    r994 r1010  
    2828#include <stdlib.h>
    2929#include <locale.h>
     30#include <gmp.h>
    3031
    3132namespace pxx {
     
    5556
    5657  pxx::HeapAllocator allocator ;
     58  pxx::ChunkAllocator<mpz_t> *mpz_allocator;
    5759
    5860  Init () :
     
    6769    if (s != null) setlocale(LC_ALL, s);
    6870    std::set_terminate(terminate_handler);
     71    mpz_allocator = new pxx::ChunkAllocator<mpz_t>(allocator);
    6972  }
    7073
     
    8083    printf("- identical:     %"PRIuPTR"\n", identical);
    8184    #if PARANOIA
     85    delete mpz_allocator;
    8286    allocator.memory_dump();
    8387    #endif // PARANOIA
     
    9094
    9195pxx::HeapAllocator& allocator = init.allocator ;
     96pxx::ChunkAllocator<mpz_t>& mpz_allocator = *init.mpz_allocator;
    9297
    9398TypeRegister rftype::Term::ref_reg(&Term::ref_funcs, type_ref) ;
     
    95100TypeRegister rftype::Char::reg(&Char::funcs, type_char) ;
    96101TypeRegister rftype::ShortInt::reg(&ShortInt::funcs, type_short_int) ;
    97 TypeRegister rftype::Integer::reg(&Integer::funcs, type_integer) ;
     102TypeRegister rftype::Integer::reg(&Integer::funcs, type_int) ;
    98103TypeRegister rftype::Word::reg(&Word::funcs, type_word) ;
    99104
  • to-imperative/trunk/runtime/rf_integer.cc

    r996 r1010  
    66using namespace rfrt;
    77
    8 pxx::ChunkAllocator<mpz_t> mpz_allocator(allocator);
     8void Integer::ctor (Integer* _to, Integer const* _from)
     9{
     10  new(_to) Integer(*_from);
     11}
     12
     13void Integer::dtor (Integer* _n)
     14{
     15  mpz_t* p = _n->get_mpz_ptr();
     16  mpz_clear(*p);
     17  mpz_allocator.deallocate(p);
     18}
     19
     20bool Integer::eq (Integer const* _n1, Integer const* _n2)
     21{
     22  return *_n1 == *_n2;
     23}
     24
     25int Integer::compare (Integer const* _n1, Integer const* _n2)
     26{
     27  return _n1->cmp(*_n2);
     28}
     29
     30//
     31// FIXME: Not implemented
     32uint32_t Integer::hash (Integer const* _n)
     33{
     34  (void)_n;
     35  return 0;
     36}
     37
     38pxx::WString Integer::to_string (Integer const* _n)
     39{
     40  return (pxx::WString)(*_n);
     41}
    942
    1043short_type_funcs_t Integer::funcs = {
    11   null, // (void (*)(Term*, Term const*))(Integer::ctor),
    12   null, // Integer::dtor,
    13   null, // Integer::eq,
    14   null, // (int (*)(Term const*, Term const*))(Integer::compare),
    15   null, // (uint32_t (*)(Term const*))(Integer::hash),
    16   null // (pxx::WString (*)(Term const*))(Integer::to_string)
     44  (void (*)(Term*, Term const*))(Integer::ctor),
     45  (void (*)(Term*))(Integer::dtor),
     46  (bool (*)(Term const*, Term const*))(Integer::eq),
     47  (int (*)(Term const*, Term const*))(Integer::compare),
     48  (uint32_t (*)(Term const*))(Integer::hash),
     49  (pxx::WString (*)(Term const*))(Integer::to_string)
    1750};
    1851
  • to-imperative/trunk/runtime/rf_integer.hh

    r988 r1010  
    55#include "rf_expr.hh"
    66#include "rf_term.hh"
    7 #include "pxx_chunk_allocator.hh"
     7#include "pxx_string.hh"
    88#include <gmp.h>
    99
     
    1717{
    1818
    19 private:
    20 
    21   static pxx::ChunkAllocator<mpz_t> mpz_allocator ;
    22 
    2319public:
    2420
    2521  inline Integer (intptr_t _n) ;
     22  inline Integer (Integer const& _n) ;
    2623  inline Integer (pxx::WString& _str) ;
    2724  inline ~Integer () ;
     25  inline Integer& operator = (Integer const& _n) ;
    2826
    29   static Expr create_expr (intptr_t _n) ;
    30   static Expr create_expr (pxx::WString& _str);
     27  inline bool operator == (Integer const& _n) const ;
     28  inline int cmp (Integer const& _n) const ;
     29
     30  inline operator pxx::WString () const ;
     31
     32  static inline Expr create_expr (intptr_t _n) ;
     33  static inline Expr create_expr (pxx::WString& _str) ;
     34  inline mpz_t* get_mpz_ptr () ;
    3135
    3236  inline size_t get_char_len () const ;
    3337
    3438  static void ctor (Integer* _to, Integer const* _from) ;
    35   static int compare (Integer const* _shint1, Integer const* _shint2) ;
     39  static void dtor (Integer* _n) ;
     40  static bool eq (Integer const* _n1, Integer const* _n2) ;
     41  static int compare (Integer const* _n1, Integer const* _n2) ;
    3642  static uint32_t hash (Integer const* _shint) ;
    3743  static pxx::WString to_string (Integer const* _shint) ;
  • to-imperative/trunk/runtime/rf_integer.ih

    r993 r1010  
    66#include "rf_term.ih"
    77#include "pxx_chunk_allocator.ih"
     8#include "pxx_string.ih"
    89
    910namespace rftype
     
    1314
    1415inline Integer::Integer (intptr_t _n) :
    15   Term(type_integer)
     16  Term(type_int)
    1617{
    1718  mpz_t* p = mpz_allocator.allocate();
    1819  ptr_data2 = p;
    19   mpz_init(*p);
    20   mpz_set_si(*p, _n);
     20  mpz_init_set_si(*p, _n);
    2121}
    2222
    2323inline Integer::Integer (pxx::WString& _str) :
    24   Term(type_integer)
     24  Term(type_int)
    2525{
    2626  mpz_t* p = mpz_allocator.allocate();
    2727  ptr_data2 = p;
    28   mpz_init(*p);
    2928  size_t len = wcstombs(null, _str.get_data(), 0);
    3029  char* s = static_cast<char*>(alloca(len + 1));
    3130  wcstombs(s, _str.get_data(), len);
    3231  s[len] = 0;
    33   mpz_set_str(*p, s, 0);
     32  mpz_init_set_str(*p, s, 0);
     33}
     34
     35inline Integer::Integer (Integer const& _n) :
     36  Term(type_int)
     37{
     38  mpz_t* p = mpz_allocator.allocate();
     39  ptr_data2 = p;
     40  mpz_t* q = static_cast<mpz_t*>(_n.ptr_data2);
     41  mpz_init_set(*p, *q);
    3442}
    3543
     
    4149}
    4250
     51inline Integer& Integer::operator = (Integer const& _n)
     52{
     53  mpz_t* p = static_cast<mpz_t*>(ptr_data2);
     54  mpz_clear(*p);
     55  mpz_t* q = static_cast<mpz_t*>(_n.ptr_data2);
     56  mpz_init_set(*p, *q);
     57  return self;
     58}
     59
     60inline bool Integer::operator == (Integer const& _n) const
     61{
     62  mpz_t* p = static_cast<mpz_t*>(ptr_data2);
     63  mpz_t* q = static_cast<mpz_t*>(_n.ptr_data2);
     64  return mpz_cmp(*p, *q) == 0;
     65}
     66
     67inline int Integer::cmp (Integer const& _n) const
     68{
     69  mpz_t* p = static_cast<mpz_t*>(ptr_data2);
     70  mpz_t* q = static_cast<mpz_t*>(_n.ptr_data2);
     71  return mpz_cmp(*p, *q);
     72}
     73
     74inline Integer::operator pxx::WString () const
     75{
     76  mpz_t* p = static_cast<mpz_t*>(ptr_data2);
     77  size_t len = mpz_sizeinbase(*p, 10) + 2;
     78  char* s = static_cast<char*>(alloca(len));
     79  mpz_get_str(s, 10, *p);
     80  len = strlen(s) + 1;
     81  wchar_t* ws = static_cast<wchar_t*>(alloca((len + 1)* sizeof(wchar_t)));
     82  len = mbstowcs(ws, s, len);
     83  ws[len] = 0;
     84  return pxx::WString(ws, len);
     85}
     86
     87inline Expr Integer::create_expr (intptr_t _n)
     88{
     89  Expr e = Term::create_expr(1);
     90  new(e.get_first()) Integer(_n);
     91  return e;
     92}
     93
     94inline mpz_t* Integer::get_mpz_ptr ()
     95{
     96  return static_cast<mpz_t*>(ptr_data2);
     97}
     98
    4399}
    44100
  • to-imperative/trunk/runtime/rf_string.cc

    r887 r1010  
    33#include "rf_types.ih"
    44
    5 namespace rftype 
     5namespace rftype
    66{
    77
     
    1212  WString str = WString(e);
    1313  size_t i = str.get_length();
    14   printf("i = \n", i);
     14  printf("i = %u\n", i);
    1515  for (size_t i = 0; i < str.get_length(); i++) {
    16 
    1716    printf ("str[i] = %c\n", str[i]);
    1817    printf("create_expr\n");
    1918  }
    20   return e; 
     19  return e;
    2120}
    2221
  • to-imperative/trunk/runtime/rf_types.hh

    r988 r1010  
    2222  type_int,
    2323  type_short_int,
    24   type_integer,
    2524  type_user,
    2625  type_object_begin = short_types_count,
  • to-imperative/trunk/samples/samples.mk

    r902 r1010  
    2020LDFLAGS += -L$(TOPDIR)/library -L$(TOPDIR)/runtime
    2121LDFLAGS += -L$(TOPDIR)/libp++ -L$(TOPDIR)/libstdc++-stub
    22 LDLIBS += -lrf -lrfrt -lp++ -lsupc++
     22LDLIBS += -lrf -lrfrt -lp++ -lsupc++ -lgmp
    2323
    2424%.cc: %.rf $(RFPC)
Note: See TracChangeset for help on using the changeset viewer.