Subject: Re: [boost] unsigned long vs unsigned int vs std::size_t (32b platform)
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2009-09-21 08:52:29
Steven Watanabe wrote:
> Christoph Mathys wrote:
> >> Wouldn't a template function be appropriate here?
> > Maybe a template function would be more appropriate,
> > avoiding possibly
> > dangerous conversions by putting a static assert into the default
> > implementation. But I'd still have to provide specializations for
> > uint32_t and unsigned long or uint32_t and unsigned int,
> > depending on the platform.
> > Ideally, I'd like to work only with those fixed width integers if I
> > need to provide an implementation for every integer type, avoiding
> > code noise with ifdef and co.
> Would it make sense to dispatch on the size:
Indeed it would and I was going to suggest the same. I do that in my own endianness swapping functions. By using function templates for swapping to and from network order, I can swap any types that have supported sizes. Don't forget to handle sizeof(T) == 8, if you can assume a byte is eight bits, as a special case that simply returns the output. Also note that swapping 64b types to/from network order uses the same code. That is, write it once and use it for both directions.
Some platforms offer highly tuned macros or intrinsics for various sizes. When dispatching on size, you can safely select the right platform-specific implementation.
When T is not a type expected by the underlying swapping code, you must convert from T to the expected type and back again. When I tested the performance of doing so, I found that the various techniques -- casting, casting among pointers, converting union, memcpy(), etc. -- had varying performance. Their relative performance varied by T, particularly by T's size, and by platform, but only slightly. Rather than getting into highly complicated and convoluted tuning logic using the preprocessor and even worse size dispatching, I just selected the converting union approach because it was the clearest to read.
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP http://www.sig.com
IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk