Boost logo

Boost :

From: nbecker_at_[hidden]
Date: 2001-04-02 14:35:12


>>>>> "Jens" == Jens Maurer <Jens.Maurer_at_[hidden]> writes:

    Jens> nbecker_at_[hidden] wrote:
>> In order to simulate the effects of finite-width integers, all that is
>> really needed is:
>>
>> 1) Check during construction and assignment
>>
>> 2) Conversion back to int.
>>
>> This of course assumes the bit width is less than or equal to the
>> natural size of int.

    Jens> (I haven't looked closely at your code, so the following may be
    Jens> way off.)

    Jens> If the bit width is equal to the natural size of "int", how are
    Jens> you going to detect overflow in the constructor? The overflow
    Jens> has happened unnoticed during (e.g.) the add, and the overflow
    Jens> information is gone.

Maybe I misunderstand - I think if width == bits(int), then it's not
possible for there to be any overflow on assignment or construction,
so there is no issue.

    Jens> Plus, even for bit widths smaller than "int", I could imagine
    Jens> that carefully crafted multiplications can result in unnoticed
    Jens> overflow as well.

The purpose of this class is to be very fast, using the underlying int
arithmetic. It assumes that widening to int is fine for intermediate
calculations. I believe this is the approach that is most useful. I
have looked at heavier-weight implementations, which try to do much
more within the class, such as defining all of the arithmetic. My
impression is that this is rarely what is really necessary, and that
this heavy approach is probably much slower.

I use this for very computationally intensive applications, such as
Monte-Carlo simulation, where efficiency is of primary importance.

Of course if your application cannot safely assume that widening to an
int is sufficient, then you need a different approach. Is it possible
to have a generic approach which has the same efficiency for the
common case where an int is sufficient?

I did glance at the bitint code. I see it's based on bitset. In the
libstdc++ that comes with gcc<3 the implementation looks horribly
inefficient. I think the code that comes with gcc3 maybe acceptable,
but I'm not sure. In any case, if you want efficient and portable
code then bitset is out.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk