|
Boost : |
From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2001-09-21 12:15:51
----- Original Message -----
From: Eric Ford <eford_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, September 20, 2001 4:04 PM
Subject: [boost] optimization for numeric_cast
> Is there a reason numeric cast performs runtime checks when casting to
> the same type? I was thinking of adding something like:
>
> typedef typename ::boost::remove_cv<Source>::type Source_no_cv;
> typedef typename ::boost::remove_cv<Target>::type Target_no_cv;
> if(::boost::is_same<Source_no_cv,Target_no_cv>::value) return arg;
>
> E
>
This and other sort of optimizations are possible.
I particular, you might want to look at the file 'cvt_n.h" that I attached
in message:
http://groups.yahoo.com/group/boost/message/17390
that file contains a numeric conversion template class that I wrote some
years ago when boost::numeric_cast<> was merely a static_cast<>.
cvt_n<> offers optimized conversions, automatic and optimized range checking
and rounding, and supports user defined number types.
You might get the files that I attached in that message and set up a test
bed to see the impact in the optimizations and optimized checks and
roundings.
I'll transcript here a part of cvt_n<> documentation.
// T t = cvt_n<T>(s)
//
// Converts from numeric S to numeric T.
// The following optimizations are performed:
//
// 1) Conversion between equal types are effectively bypassed.
// S v ; S u = cvt_n<S>(v) ; // Expands EXACTLY to: S u = v ;
//
// 2) Runtime Range checking -when applicable- is applied, but is
controlled by a traits class.
// For built-in types, range checking is bypassed if conversion goes to
// a bigger range type:
// int u = cvt_n<int>( (short) 1 ) ; // No range checking.
//
// Mixing signed/unsigned types forces range checking:
//
// int u = cvt_n<int> ( (unsigned int) 1) ; // range checking.
//
// 3) Rounding is applied when converting from a non-integer type to an
// integer type (in other cases rounding is not applied).
// The rounding is performed by a free template function round() which
can
// be specialized.
// Range checking is combined to ensure proper conversion after
rounding.
//
// int u = cvt_n<int>(2.5) ; // rounding and range checking.
// float v = cvt_n<float>(2.5) ; // no explicit rounding.
//
// 4) Supports user defined types if they specialize numeric_limits<> and
// provides applicable constructors and conversion operators.
//
If there is group consensus I can rewrite numeric_cast<> so that it behaves
like cvt_n<>.
Regards,
Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk