Allocated Limbs

LingWitt@insightbb.com LingWitt@insightbb.com
Tue, 24 Jun 2003 19:29:52 -0400


--Apple-Mail-2-880780813
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed

This code gets rid of the low zero limbs, but it requires that three 
extra limbs be allocated, otherwise errors occur further in my program. 
Why should this be the case?

unsigned int stripLowZeroLimbs(mpf_ptr f)
{
     unsigned 	sizeNew, sizeOld;
     sizeNew = sizeOld = f->_mp_size;
     mp_ptr	limbs			= f->_mp_d;
     mp_ptr	limb			= 0;

     while (*(limb = limbs++) == 0)
         sizeNew--;

     if ((sizeNew += 3) == sizeOld)
         return 0;

     printit(f);

     f->_mp_size = sizeNew;

     mp_ptr newLimbs = static_cast<mp_ptr>(calloc(sizeNew, 
sizeof(mp_ptr)));
     limbs--;
     for (unsigned int index = 3; index < sizeNew; index++)
         newLimbs[index] = *(limbs++);

     free(f->_mp_d);
     f->_mp_d = newLimbs;

     printit(f);

     return sizeOld - f->_mp_size;
}

On Tuesday, Jun 24, 2003, at 11:48 America/New_York, 
LingWitt@insightbb.com wrote:

> unsigned int stripLowZeroLimbs(mpf_ptr f)
> {
>     unsigned sizeNew, sizeOld;
>     sizeNew = sizeOld = f->_mp_size;
>     mp_ptr	limbs			= f->_mp_d;
>     mp_ptr	limb			= 0;
>
>     while (*(limb = limbs++) == 0 && sizeNew > 1)
>         sizeNew--;
>
>     if (++sizeNew == sizeOld)
>         return 0;
>
>     f->_mp_size = sizeNew;
>
>     mp_ptr newLimbs = static_cast<mp_ptr>(calloc(sizeNew, 
> sizeof(mp_ptr)));
>     limbs--;
>     for (unsigned int index = 1; index < sizeNew; index++)
>         newLimbs[index] = *(limbs++);
>
>     free(f->_mp_d);
>     f->_mp_d = newLimbs;
>
>     return sizeOld - f->_mp_size;
> }
>
> On Monday, Jun 23, 2003, at 23:31 America/New_York, 
> LingWitt@insightbb.com wrote:
>
>>> (If you're not sure what you're doing with this you're probably 
>>> better
>>> off to leave it alone.  What was the aim anyway?  Speed?  Correctness
>>> is not affected by low zeros, or shouldn't be.)
>>
>> I want to minimize the amount of unused memory. If a number needs 
>> more to accurately represent itself, then it should get more. If it 
>> doesn't need so much, it should deallocate it. I am under the 
>> impression that only floats created by strings are over-allocated, so 
>> I figure that shaving off the zero limbs upon initialization will do 
>> the trick.
>>
>> There might be 1 000 000 floats allocated at a max precision of 2000 
>> bits. In this case, there are 65 limbs allocated to each mpf_t. On my 
>> system --like most--each limb is 4 bytes, so that comes out to 
>> 2.6e+08 bytes, or a little under 248 MB, when under 8 MB might do.
>>
>>> No, don't change f->_mp_d, copy the limbs down within that data 
>>> space.
>>
>> I don't know what you mean --delete the whole mp_d array and make a 
>> new one? --I've never worked with such low-level operations. Please 
>> give me a little help so I can learn:
>>
>> Why doesn't this work:
>>
>> unsigned int stripLowZeroLimbs(mpf_ptr f)
>> {
>>     unsigned 	sizeNew, sizeOld;
>>     sizeNew = sizeOld = f->_mp_size;
>>     mp_ptr	limbs			= f->_mp_d;
>>     mp_ptr	limb			= 0;
>>
>>     while (*(limb = limbs++) == 0 && sizeNew > 1)
>>         sizeNew--;
>>
>>     if (++sizeNew == sizeOld)
>>         return 0;
>>
>>     f->_mp_size = sizeNew;
>>
>>     mp_ptr newLimbs = static_cast<mp_ptr>(calloc(sizeNew, 
>> sizeof(mp_ptr)));
>>     sizeNew--;
>>     for (unsigned int indexOld = sizeOld - sizeNew, indexNew = 0; 
>> indexNew < sizeNew; indexOld++, indexNew++)
>>         newLimbs[indexNew] = limbs[indexOld];
>>
>>     free(f->_mp_d);
>>     f->_mp_d = newLimbs;
>>
>>     return sizeOld - f->_mp_size;
>> }
>>
>> On Monday, Jun 23, 2003, at 20:43 America/New_York, Kevin Ryde wrote:
--Apple-Mail-2-880780813
Content-Transfer-Encoding: 7bit
Content-Type: text/enriched;
	charset=US-ASCII

<fixed><color><param>7674,0F0D,504E</param>This code gets rid of the
low zero limbs, but it requires that three extra limbs be allocated,
otherwise errors occur further in my program. Why should this be the
case?


unsigned</color> <color><param>7674,0F0D,504E</param>int</color>
stripLowZeroLimbs(mpf_ptr f)

{

    <color><param>7676,0F0F,5050</param>unsigned</color> 	sizeNew,
sizeOld;

    sizeNew = sizeOld = f->_mp_size;

    mp_ptr	limbs			= f->_mp_d;

    mp_ptr	limb			= <color><param>0000,0000,FFFF</param>0</color>;

    

    <color><param>7676,0F0F,5050</param>while</color> (*(limb =
limbs++) == <color><param>0000,0000,FFFF</param>0</color>)

        sizeNew--;

    

    <color><param>7676,0F0F,5050</param>if</color> ((sizeNew +=
<color><param>0000,0000,FFFF</param>3</color>) == sizeOld)

        <color><param>7676,0F0F,5050</param>return</color>
<color><param>0000,0000,FFFF</param>0</color>;

        

    printit(f);

                    

    f->_mp_size = sizeNew;

    

    mp_ptr newLimbs =
<color><param>7676,0F0F,5050</param>static_cast</color><<mp_ptr>(calloc(sizeNew,
<color><param>7676,0F0F,5050</param>sizeof</color>(mp_ptr)));

    limbs--;

    <color><param>7676,0F0F,5050</param>for</color>
(<color><param>7676,0F0F,5050</param>unsigned</color>
<color><param>7676,0F0F,5050</param>int</color> index =
<color><param>0000,0000,FFFF</param>3</color>; index << sizeNew;
index++)

        newLimbs[index] = *(limbs++);

    

    free(f->_mp_d);

    f->_mp_d = newLimbs;

    

    printit(f);

    

    <color><param>7676,0F0F,5050</param>return</color> sizeOld -
f->_mp_size;

}

</fixed>

On Tuesday, Jun 24, 2003, at 11:48 America/New_York,
LingWitt@insightbb.com wrote:


<excerpt><fixed><color><param>7675,0F0E,504F</param>unsigned</color>
<color><param>7675,0F0E,504F</param>int</color>
stripLowZeroLimbs(mpf_ptr f)

{

    <color><param>7675,0F0E,504F</param>unsigned</color> sizeNew,
sizeOld;

    sizeNew = sizeOld = f->_mp_size;

    mp_ptr	limbs			= f->_mp_d;

    mp_ptr	limb			= <color><param>0000,0000,FFFE</param>0</color>;

    

    <color><param>7675,0F0E,504F</param>while</color> (*(limb =
limbs++) == <color><param>0000,0000,FFFE</param>0</color> && sizeNew >
<color><param>0000,0000,FFFE</param>1</color>)

        sizeNew--;

    

    <color><param>7675,0F0E,504F</param>if</color> (++sizeNew ==
sizeOld)

        <color><param>7675,0F0E,504F</param>return</color>
<color><param>0000,0000,FFFE</param>0</color>;

                    

    f->_mp_size = sizeNew;

    

    mp_ptr newLimbs =
<color><param>7675,0F0E,504F</param>static_cast</color><<mp_ptr>(calloc(sizeNew,
<color><param>7675,0F0E,504F</param>sizeof</color>(mp_ptr)));

    limbs--;

    <color><param>7675,0F0E,504F</param>for</color>
(<color><param>7675,0F0E,504F</param>unsigned</color>
<color><param>7675,0F0E,504F</param>int</color> index =
<color><param>0000,0000,FFFE</param>1</color>; index << sizeNew;
index++)

        newLimbs[index] = *(limbs++);

    

    free(f->_mp_d);

    f->_mp_d = newLimbs;

    

    <color><param>7675,0F0E,504F</param>return</color> sizeOld -
f->_mp_size;

}</fixed>


On Monday, Jun 23, 2003, at 23:31 America/New_York,
LingWitt@insightbb.com wrote:


<excerpt><excerpt>(If you're not sure what you're doing with this
you're probably better

off to leave it alone.  What was the aim anyway?  Speed?  Correctness

is not affected by low zeros, or shouldn't be.)

</excerpt>

I want to minimize the amount of unused memory. If a number needs more
to accurately represent itself, then it should get more. If it doesn't
need so much, it should deallocate it. I am under the impression that
only floats created by strings are over-allocated, so I figure that
shaving off the zero limbs upon initialization will do the trick.


There might be 1 000 000 floats allocated at a max precision of 2000
bits. In this case, there are 65 limbs allocated to each mpf_t. On my
system --like most--each limb is 4 bytes, so that comes out to 2.6e+08
bytes, or a little under 248 MB, when under 8 MB might do.


<excerpt>No, don't change f->_mp_d, copy the limbs down within that
data space.

</excerpt>

I don't know what you mean --delete the whole mp_d array and make a
new one? --I've never worked with such low-level operations. Please
give me a little help so I can learn:


Why doesn't this work:


<fixed><color><param>7672,0F0B,504C</param>unsigned</color> int
stripLowZeroLimbs(mpf_ptr f)</fixed>

<fixed>{

    <color><param>7674,0F0D,504E</param>unsigned</color> 	sizeNew,
sizeOld;

    sizeNew = sizeOld = f->_mp_size;

    mp_ptr	limbs			= f->_mp_d;

    mp_ptr	limb			= <color><param>0000,0000,FFFD</param>0</color>;

    

    <color><param>7674,0F0D,504E</param>while</color> (*(limb =
limbs++) == <color><param>0000,0000,FFFD</param>0</color> && sizeNew >
<color><param>0000,0000,FFFD</param>1</color>)

        sizeNew--;

    

    <color><param>7674,0F0D,504E</param>if</color> (++sizeNew ==
sizeOld)

        <color><param>7674,0F0D,504E</param>return</color>
<color><param>0000,0000,FFFD</param>0</color>;

    

    f->_mp_size = sizeNew;

    

    mp_ptr newLimbs =
<color><param>7674,0F0D,504E</param>static_cast</color><<mp_ptr>(calloc(sizeNew,
<color><param>7674,0F0D,504E</param>sizeof</color>(mp_ptr)));

    sizeNew--;

    <color><param>7674,0F0D,504E</param>for</color>
(<color><param>7674,0F0D,504E</param>unsigned</color>
<color><param>7674,0F0D,504E</param>int</color> indexOld = sizeOld -
sizeNew, indexNew = <color><param>0000,0000,FFFD</param>0</color>;
indexNew << sizeNew; indexOld++, indexNew++)

        newLimbs[indexNew] = limbs[indexOld];

    

    free(f->_mp_d);

    f->_mp_d = newLimbs;

    

    <color><param>7674,0F0D,504E</param>return</color> sizeOld -
f->_mp_size;

}</fixed>


On Monday, Jun 23, 2003, at 20:43 America/New_York, Kevin Ryde wrote:</excerpt></excerpt>
--Apple-Mail-2-880780813--