Subject: Re: [boost] [integer] Type-safe and bounded integers with compile-time checking
From: Ben Robinson (icaretaker_at_[hidden])
Date: 2011-09-05 15:57:34
I am planning on submitting a policy based integer class which I have
already implemented that shares many of the features that you have
requested. I currently call it MetaBoundedInt, and the design philosophy
was to provide both an overflow, and ranged checked integer, which leverages
as much compile time information as possible to maximize performance, as
well as communicate overflow/range errors at compile time when possible.
The handling of overflow/range errors is policy based, and allows a single
policy to assert, throw, saturate, wrap, etc... when the bounds (either
overflow or out-of-range) are exceeded. The binary math operators +,-,*,/,%
are all implemented, and perform the policy based behavior. Also, when
assigning one type of integer to another, overflow detection is implemented
correctly by analyzing the value, to detect if it will fit into the smaller
lhs data type and specified range.
Leveraging the compile time information, certain comparisons are completely
eliminated to maximize performance, such as checking the range if the bounds
are the max/min of the data type, which is already checked during overflow
detection. Also, if the data type min/max of the rhs is greater than or
equal to the min/max of the lhs, no overflow detection is required, and
therefore not performed. There are many more specializations which
eliminate needless computation.
Usage looks something like this:
mbi<throwing_policy, int8_t> var1(0U); // Detects only overflow
mbi<throwing_policy, int8_t, -8, 8> var2(0U); // Detects overflow and range
Now use use value just like any other value, except if you overflow the data
type, or exceed the range, the policy determines the behavior.
As you requested Leif, the throwing_policy can be replaced in a release
build with an ignore_policy, so that these additional checks can be
eliminated for maximum release build performance.
I was going to wait to submit this until after my Singularity submission,
which is currently on the review calendar, but given your interest query, I
am responding now.
Does MetaBoundedInt (mbi) pique anybody's interest?
Ben Robinson, Ph.D.
On Mon, Sep 5, 2011 at 7:57 AM, Mathias Gaunard <
> On 05/09/2011 14:29, Leif Linderstam wrote:
> They produce new ranges, e.g. for the addition A + B the range of the
>> result is [ A_low + B_low, A_high + B_high ] where A_low is the lower
>> bound of A's range, and so on.
> Ok, but that would very quickly lead to very big ranges.
> What happens when that range goes beyond the range supported by the
> underlying int type?
> Unsubscribe & other changes: http://lists.boost.org/**
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk