bug-gmp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-gmp] enhancement request


From: bhinkle4
Subject: Re: [Bug-gmp] enhancement request
Date: Tue, 12 Sep 2000 22:06:35 -0400

Kevin,
Thanks for the suggestions. I'll try out both options you mentioned. I
hadn't seen the mpz_array_init before and it looks useful. It's not
exactly what I had in mind but it might be ok.

Let me tell you what I've been using so far. The idea is that you reuse
all the existing mpf arithmetic functions and the only new functions you
need are
1) creation routines (init) that allocate one chunk of memory and return
an mpf_ptr
2) a "lock" routine that sets the pointer _mp_d and effectively "locks"
the object to a given memory location so that the standard mpf arithmetic
functions can be called, and
3) a clear routine to free the memory.

I've also added a "sizeof" routine to get the memory footprint for a
given precision and two "init_raw" routines that don't allocate any
memory at all.

The declarations look like

mpf_ptr mppif_lock _PROTO ((mpf_ptr r));
void    mppif_clear _PROTO ((mpf_ptr));
mpf_ptr mppif_init _PROTO ((void));
mpf_ptr mppif_init2 _PROTO ((unsigned long int));
mpf_ptr mppif_init_set _PROTO ((mpf_srcptr));
mpf_ptr mppif_init_set_d _PROTO ((double));
mpf_ptr mppif_init_set_str _PROTO (( __gmp_const char *, int, int*));
size_t  mppif_sizeof _PROTO ((unsigned long int));
void    mppif_init_raw _PROTO ((mpf_ptr r));
void    mppif_init2_raw _PROTO ((mpf_ptr r, unsigned long int
prec_in_bits));

The "pi" stands for "position independent".
The implementations are just copies of the standard functions with slight
modifications. For example, mppif_lock, mppif_clear and mppif_init look
like

mpf_ptr
mppif_lock(mpf_ptr x)
{
  x->_mp_d = (mp_limb_t*)((unsigned char*)x+sizeof(__mpf_struct));
  return x;
}

void
mppif_clear (mpf_ptr m)
{
  (*_mp_free_func) (m, sizeof(__mpf_struct) + 
                    (m->_mp_prec + 1) * BYTES_PER_MP_LIMB);
}

mpf_ptr
mppif_init (void)
{
  mpf_ptr r;
  mp_size_t prec = __gmp_default_fp_limb_precision;
  r = (mpf_ptr) (*_mp_allocate_func) (sizeof(__mpf_struct) + 
                                       (prec + 1) * BYTES_PER_MP_LIMB);
  r->_mp_prec = prec;
  r->_mp_size = 0;
  r->_mp_exp = 0;
  return r;
}

User code would look like

mpf_ptr x, y, z;
x = mppif_init_set_d(1.0);
y = mppif_init_set_d(2.0);
z = mppif_init();
mpf_add(mppif_lock(z), mppif_lock(x), mppif_lock(y));
mppif_clear(x);
...

Seem reasonable? This method doesn't add very many new functions and it
doesn't involve any fiddling around with the standard mpf functions or
data structures.
-Ben


reply via email to

[Prev in Thread] Current Thread [Next in Thread]