[Review] of binary_int utility starts

Review of binary_int utility written by Scott Schurr begins today and lasts for 7 days. The utility can be found at http://boost-consulting.com/vault/ file binary_int.zip (8 kB). ===Description=== The class template binary_int allows binary literals to be used at compile time. The template reads a binary literal value comprised of from 1 to 16 comma separated 4-bit units of ones and zeros. Example of use: #include <boost/utility.hpp> unsigned int regValue2 = binary_int<1000,1001,0011,0000>::value; enum { val1 = binary_int<1100,0010>::value, val2 = binary_int<1100,0011>::value }; switch (x) { case binary_int<0110,0001>::value: ... break; case binary_int<0111,0001>::value: ... break; } ===Alternative implementation=== Matt Calabrese had written alternative, using only preprocessor. It can be found at http://www.illegal-immigration.com/Riv/boost/binary_literal.hpp Example of use: BOOST_BINARY_LITERAL( 101 0111 1010 0110 ) (there is no need for ::value, more on http://lists.boost.org/Archives/boost/2005/09/93007.php) ===Open questions in Scott Schurs's library=== * How does it work on 64 bit systems? Could someone try? * Naming: perhaps shorter names could be used (e.g. bits/nibble) * BOOST_WORKAROUND in the code, is it really needed? === Questions for the reviewers=== * Which one of the alternatives fits in Boost better? * What is your evaluation of the implementation(s)? * Do you think the utility should be accepted as a Boost library? /Pavel

I hate to reply to this, but I provided another version a while back as well: http://www.illegal-immigration.com/Riv/boost/binary_literal2.hpp which works just like the above version only allows arbitrary groupings of bits up to 8 per grouping, such as: BOOST_BINARY_LITERAL( 10001110 1001 100 0001 ) This version is not extensively tested on many compilers, however, but should be portable. -- -Matt Calabrese

Pavel Vozenilek wrote: Note a review, but...
* How does it work on 64 bit systems? Could someone try?
Tried on Tru64 5.1, CXX 7.1-006:
cxx -model ansi -std strict_ansi -I boost-HEAD binary_int_test.cpp
cxx: Error: binary_int.hpp, line 51: incomplete type is not allowed binary_nibble_VALID_VALUES_BINARY_0000_THROUGH_1111<false> t; ---------------------------------------------------------------^ cxx: Warning: binary_int.hpp, line 101: trailing comma is nonstandard shl = 4u, ------------------^ cxx: Warning: binary_int.hpp, line 109: trailing comma is nonstandard shl = 0u, ------------------^ cxx: Warning: binary_int.hpp, line 191: trailing comma is nonstandard v0_ = (v1_ << binary_int_arg<b0>::shl) | binary_int_arg<b0>::value, -----------------------------------------------------------------------------^ cxx: Warning: binary_int.hpp, line 171: missing return statement at end of non-void function "binary_int<b15, b14, b13, b12, b11, b10, b9, b8, b7, b6, b5, b4, b3, b2, b1, b0>::operator=" binary_int& operator=(const binary_int& rhs) { }; ---------------------------------------------------^ cxx: Info: 1 error detected in the compilation of "binary_int_test.cpp". All compilers I tried (CXX 6.5-042, CXX 7.1-006, G++ 3.4.3, G++ 4.0.1) generate the same error. When enabling the workaround in binary_int.hpp, all compilers accept the code. Executing the resulting binary doesn't give an error. Markus

"Pavel Vozenilek" <pavel_vozenilek@hotmail.com> wrote in message news:dimha9$utq$1@sea.gmane.org...
Review of binary_int utility written by Scott Schurr
...
The class template binary_int allows binary literals to be used at compile time. The template reads a binary literal value comprised of from 1 to 16 comma separated 4-bit units of ones and zeros.
With just reading this description, why not call it what it is: binary_literal. Whether it's a template or a macro(in Matt's case) I think binary_literal is more descriptive. Jeff Flinn

On 10/14/05, Jeff Flinn <TriumphSprint2000@hotmail.com> wrote:
With just reading this description, why not call it what it is: binary_literal. Whether it's a template or a macro(in Matt's case) I think binary_literal is more descriptive.
That is why I decided to call mine "BOOST_BINARY_LITERAL," however, it was also suggested that BOOST_BITS may be simpler and possibly just as descriptive but with less typing. I don't mind either wayt. Right now I think I prefer that it has "literal" in it, since it's more clear that it is useable much like other integral literals. In fact, the result is a hex or octal literal (depending on which version of my macro you look at). -- -Matt Calabrese

Pavel Vozenilek <pavel_vozenilek <at> hotmail.com> writes:
The class template binary_int allows binary literals to be used at compile time. The template reads a binary literal value comprised of from 1 to 16 comma separated 4-bit units of ones and zeros.
If possible, I would like to vote on Matt Calabrese' solution. It seems to be much more elegant and powerful (different bit groupings sizes), with less typing needed, which would result in better looking source codes. I'm also against of having both solutions once in the future because I don't see how additional Boost overcrowding with almost equal libraries would be any good.

===Open questions in Scott Schurs's library===
* How does it work on 64 bit systems? Could someone try?
Sorry, I'm not that well off yet.
* Naming: perhaps shorter names could be used (e.g. bits/nibble)
The current name is fine.
* BOOST_WORKAROUND in the code, is it really needed?
I don't know. I use GCC 3.4.2 (MinGW) and MSVC 7.1, which are treated as class-A compilers.
=== Questions for the reviewers===
* Which one of the alternatives fits in Boost better?
First, assuming binary_int becomes MPL-friendly, I believe the following code illustrates a more common usage: template <typename IntegralConstant> struct color_argb64 { typedef typename bitand_< IntegralConstant , binary_int<0000,0000,0000,1111> >::type blue; typedef typename bitand_< shift_right<IntegralConstant,int_<16>
, binary_int<0000,0000,0000,1111> >::type green; typedef typename bitand_< shift_right<IntegralConstant,int_<32>
, binary_int<0000,0000,0000,1111> >::type red; typedef typename bitand_< shift_right<IntegralConstant,int_<48>
, binary_int<0000,0000,0000,1111> >::type alpha; }; The corresponding macro usage is: #define ARGB64_BLUE(color_int) \ (color_int & BOOST_BINARY_LITERAL(1111)) #define ARGB64_GREEN(color_int) \ ((color_int >> 16) & BOOST_BINARY_LITERAL(1111)) #define ARGB64_RED(color_int) \ ((color_int >> 32) & BOOST_BINARY_LITERAL(1111)) #define ARGB64_ALPHA(color_int) \ ((color_int >> 48) & BOOST_BINARY_LITERAL(1111)) I see ample room in Boost for both libraries. (At least they can check each other for correctness!) Furthermore, the small code size and memory footprint of the macro implementation does not mitigate the weaknesses that macros have. BOOST_BINARY_LITERAL may be more efficient, but for beginners and intermediate users at least, binary_int will be safer.
* What is your evaluation of the implementation(s)?
As limited as binary_int is right now, it's no match for BOOST_BINARY_LITERAL.
* Do you think the utility should be accepted as a Boost library?
Not yet. It isn't Boostified; the documentation is not in the current format; making it MPL-friendly will take more time than is available during this review period--it certainly won't be as trivial as inheriting from integral_c. However, I see enough interest in this utility that once these issues are resolved, I'll probably vote for it in a second review. Cromwell D. Enage __________________________________ Yahoo! FareChase: Search multiple travel sites in one click. http://farechase.yahoo.com
participants (6)
-
Cromwell Enage
-
Goran Mitrovic
-
Jeff Flinn
-
Markus Schöpflin
-
Matt Calabrese
-
Pavel Vozenilek